3 * Copyright (C) 2009-2016 SonarSource SA
4 * mailto:contact AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.server.computation.task.projectanalysis.component;
22 import com.google.common.annotations.VisibleForTesting;
23 import com.google.common.collect.ImmutableList;
24 import java.util.ArrayList;
25 import java.util.List;
26 import javax.annotation.CheckForNull;
27 import javax.annotation.Nullable;
28 import javax.annotation.concurrent.Immutable;
29 import org.sonar.scanner.protocol.output.ScannerReport;
30 import org.sonar.scanner.protocol.output.ScannerReport.Component.ComponentType;
32 import static com.google.common.base.Preconditions.checkArgument;
33 import static com.google.common.base.Preconditions.checkNotNull;
34 import static com.google.common.base.Preconditions.checkState;
35 import static java.util.Arrays.asList;
36 import static org.apache.commons.lang.StringUtils.trimToNull;
39 public class ComponentImpl implements Component {
40 private final Type type;
41 private final String name;
42 private final String key;
43 private final String uuid;
46 private final String description;
47 private final List<Component> children;
49 private final ReportAttributes reportAttributes;
51 private final FileAttributes fileAttributes;
53 private ComponentImpl(Builder builder) {
54 this.type = builder.type;
55 this.key = builder.key;
56 this.name = builder.name == null ? String.valueOf(builder.key) : builder.name;
57 this.description = builder.description;
58 this.uuid = builder.uuid;
59 this.reportAttributes = builder.reportAttributes;
60 this.fileAttributes = builder.fileAttributes;
61 this.children = ImmutableList.copyOf(builder.children);
65 public Type getType() {
70 public String getUuid() {
75 public String getKey() {
80 public String getName() {
86 public String getDescription() {
87 return this.description;
91 public List<Component> getChildren() {
96 public ReportAttributes getReportAttributes() {
97 return this.reportAttributes;
101 public FileAttributes getFileAttributes() {
102 checkState(this.type == Type.FILE, "Only component of type FILE have a FileAttributes object");
103 return this.fileAttributes;
107 public ProjectViewAttributes getProjectViewAttributes() {
108 throw new IllegalStateException("Only component of type PROJECT_VIEW have a FileAttributes object");
111 public static Builder builder(ScannerReport.Component component) {
112 return new Builder(component);
115 public static final class Builder {
117 private final Type type;
118 private final ReportAttributes reportAttributes;
122 private String description;
123 private FileAttributes fileAttributes;
124 private final List<Component> children = new ArrayList<>();
126 private Builder(ScannerReport.Component component) {
127 checkNotNull(component);
128 this.type = convertType(component.getType());
129 this.name = component.getName();
130 this.description = trimToNull(component.getDescription());
131 this.reportAttributes = createBatchAttributes(component);
132 this.fileAttributes = createFileAttributes(component);
135 public Builder setUuid(String s) {
136 this.uuid = checkNotNull(s);
140 public Builder setKey(String s) {
141 this.key = checkNotNull(s);
145 public Builder addChildren(Component... c) {
146 for (Component component : c) {
147 checkArgument(component.getType().isReportType());
149 this.children.addAll(asList(c));
153 public ComponentImpl build() {
156 return new ComponentImpl(this);
159 private static ReportAttributes createBatchAttributes(ScannerReport.Component component) {
160 return ReportAttributes.newBuilder(component.getRef())
161 .setVersion(trimToNull(component.getVersion()))
162 .setPath(trimToNull(component.getPath()))
167 private static FileAttributes createFileAttributes(ScannerReport.Component component) {
168 if (component.getType() != ComponentType.FILE) {
172 return new FileAttributes(
173 component.getIsTest(),
174 trimToNull(component.getLanguage()));
178 static Type convertType(ComponentType type) {
185 return Type.DIRECTORY;
189 throw new IllegalArgumentException("Unsupported ComponentType value " + type);
195 public String toString() {
196 return "ComponentImpl{" +
197 "key='" + key + '\'' +
199 ", uuid='" + uuid + '\'' +
200 ", name='" + name + '\'' +
201 ", description='" + description + '\'' +
202 ", fileAttributes=" + fileAttributes +
203 ", reportAttributes=" + reportAttributes +
208 public boolean equals(@Nullable Object o) {
212 if (o == null || getClass() != o.getClass()) {
215 ComponentImpl component = (ComponentImpl) o;
216 return uuid.equals(component.uuid);
220 public int hashCode() {
221 return uuid.hashCode();