3 * Copyright (C) 2009-2022 SonarSource SA
4 * mailto:info 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.ce.task.projectanalysis.component;
22 import com.google.common.collect.ImmutableList;
23 import java.util.ArrayList;
24 import java.util.List;
25 import java.util.Optional;
26 import javax.annotation.CheckForNull;
27 import javax.annotation.Nullable;
29 import static com.google.common.base.Preconditions.checkArgument;
30 import static com.google.common.base.Preconditions.checkState;
31 import static java.util.Arrays.asList;
32 import static java.util.Objects.requireNonNull;
35 * Implementation of {@link Component} to unit test report components.
37 public class ReportComponent implements Component {
39 private static final FileAttributes DEFAULT_FILE_ATTRIBUTES = new FileAttributes(false, null, 1, false);
41 public static final Component DUMB_PROJECT = builder(Type.PROJECT, 1)
42 .setKey("PROJECT_KEY")
43 .setUuid("PROJECT_UUID")
44 .setName("Project Name")
45 .setProjectVersion("1.0-SNAPSHOT")
48 private final Type type;
49 private final Status status;
50 private final String name;
51 private final String shortName;
53 private final String description;
54 private final String key;
55 private final String uuid;
56 private final ProjectAttributes projectAttributes;
57 private final ReportAttributes reportAttributes;
58 private final FileAttributes fileAttributes;
59 private final List<Component> children;
61 private ReportComponent(Builder builder) {
62 this.type = builder.type;
63 this.status = builder.status;
64 this.key = builder.key;
65 this.name = builder.name == null ? String.valueOf(builder.key) : builder.name;
66 this.shortName = builder.shortName == null ? this.name : builder.shortName;
67 this.description = builder.description;
68 this.uuid = builder.uuid;
69 this.projectAttributes = Optional.ofNullable(builder.projectVersion)
70 .map(v -> new ProjectAttributes(v, builder.buildString, builder.scmRevisionId))
72 this.reportAttributes = ReportAttributes.newBuilder(builder.ref)
74 this.fileAttributes = builder.fileAttributes == null ? DEFAULT_FILE_ATTRIBUTES : builder.fileAttributes;
75 this.children = ImmutableList.copyOf(builder.children);
79 public Type getType() {
84 public Status getStatus() {
89 public String getUuid() {
91 throw new UnsupportedOperationException(String.format("Component uuid of ref '%d' has not be fed yet", this.reportAttributes.getRef()));
97 public String getKey() {
99 throw new UnsupportedOperationException(String.format("Component key of ref '%d' has not be fed yet", this.reportAttributes.getRef()));
105 public String getName() {
110 public String getShortName() {
111 return this.shortName;
116 public String getDescription() {
117 return this.description;
121 public List<Component> getChildren() {
126 public ProjectAttributes getProjectAttributes() {
127 checkState(this.type == Type.PROJECT);
128 return this.projectAttributes;
132 public ReportAttributes getReportAttributes() {
133 return this.reportAttributes;
137 public FileAttributes getFileAttributes() {
138 checkState(this.type == Type.FILE, "Only component of type FILE can have a FileAttributes object");
139 return this.fileAttributes;
143 public ProjectViewAttributes getProjectViewAttributes() {
144 throw new IllegalStateException("Only component of type PROJECT_VIEW can have a ProjectViewAttributes object");
148 public SubViewAttributes getSubViewAttributes() {
149 throw new IllegalStateException("Only component of type SUBVIEW have a SubViewAttributes object");
153 public ViewAttributes getViewAttributes() {
154 throw new IllegalStateException("Only component of type VIEW have a ViewAttributes object");
158 public boolean equals(@Nullable Object o) {
162 if (o == null || getClass() != o.getClass()) {
165 ReportComponent that = (ReportComponent) o;
166 return uuid.equals(that.uuid);
170 public int hashCode() {
171 return uuid.hashCode();
175 public String toString() {
176 return "ReportComponent{" +
177 "ref=" + this.reportAttributes.getRef() +
178 ", key='" + key + '\'' +
183 public static Builder builder(Type type, int ref, String key) {
184 return new Builder(type, ref).setKey(key).setUuid("uuid_" + ref).setName("name_" + ref);
187 public static Builder builder(Type type, int ref) {
188 return builder(type, ref, "key_" + ref);
191 public static final class Builder {
192 private final Type type;
193 private final int ref;
194 private Status status;
198 private String shortName;
199 private String projectVersion;
200 private String buildString;
201 private String scmRevisionId;
202 private String description;
203 private FileAttributes fileAttributes;
204 private final List<Component> children = new ArrayList<>();
206 private Builder(Type type, int ref) {
207 checkArgument(type.isReportType(), "Component type must be a report type");
210 if (type == Type.PROJECT) {
211 this.projectVersion = "toBeDefined";
215 public Builder setStatus(Status s) {
216 this.status = requireNonNull(s);
220 public Builder setUuid(String s) {
221 this.uuid = requireNonNull(s);
225 public Builder setName(@Nullable String s) {
230 public Builder setShortName(@Nullable String s) {
235 public Builder setKey(String s) {
236 this.key = requireNonNull(s);
240 public Builder setProjectVersion(@Nullable String s) {
241 checkProjectVersion(s);
242 this.projectVersion = s;
246 public Builder setBuildString(@Nullable String buildString) {
247 checkBuildString(buildString);
248 this.buildString = buildString;
252 public Builder setScmRevisionId(@Nullable String scmRevisionId) {
253 this.scmRevisionId = scmRevisionId;
257 public Builder setFileAttributes(FileAttributes fileAttributes) {
258 checkState(type == Type.FILE, "Only Component of type File can have File attributes");
259 this.fileAttributes = fileAttributes;
263 public Builder setDescription(@Nullable String description) {
264 this.description = description;
268 public Builder addChildren(Component... c) {
269 for (Component component : c) {
270 checkArgument(component.getType().isReportType());
272 this.children.addAll(asList(c));
276 public ReportComponent build() {
277 checkProjectVersion(this.projectVersion);
278 checkBuildString(this.buildString);
279 return new ReportComponent(this);
282 private void checkProjectVersion(@Nullable String s) {
283 checkArgument(type != Type.PROJECT ^ s != null, "Project version must and can only be set on Project");
286 private void checkBuildString(@Nullable String s) {
287 checkArgument(type == Type.PROJECT || s == null, "BuildString can only be set on Project");