3 * Copyright (C) 2009-2019 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.base.MoreObjects;
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;
30 import static com.google.common.base.Preconditions.checkArgument;
31 import static com.google.common.base.Preconditions.checkState;
32 import static java.util.Objects.requireNonNull;
33 import static org.apache.commons.lang.StringUtils.abbreviate;
34 import static org.apache.commons.lang.StringUtils.trimToNull;
35 import static org.sonar.db.component.ComponentValidator.MAX_COMPONENT_DESCRIPTION_LENGTH;
36 import static org.sonar.db.component.ComponentValidator.MAX_COMPONENT_NAME_LENGTH;
39 public class ComponentImpl implements Component {
40 private final Type type;
41 private final Status status;
42 private final String name;
43 private final String shortName;
44 private final String dbKey;
45 private final String key;
46 private final String uuid;
49 private final String description;
50 private final List<Component> children;
52 private final ProjectAttributes projectAttributes;
53 private final ReportAttributes reportAttributes;
55 private final FileAttributes fileAttributes;
57 private ComponentImpl(Builder builder) {
58 this.type = builder.type;
59 this.status = builder.status;
60 this.dbKey = builder.dbKey;
61 this.key = MoreObjects.firstNonNull(builder.key, builder.dbKey);
62 this.name = builder.name;
63 this.shortName = MoreObjects.firstNonNull(builder.shortName, builder.name);
64 this.description = builder.description;
65 this.uuid = builder.uuid;
66 this.projectAttributes = builder.projectAttributes;
67 this.reportAttributes = builder.reportAttributes;
68 this.fileAttributes = builder.fileAttributes;
69 this.children = ImmutableList.copyOf(builder.children);
73 public Type getType() {
78 public Status getStatus() {
83 public String getUuid() {
88 public String getDbKey() {
93 public String getKey() {
98 public String getName() {
103 public String getShortName() {
104 return this.shortName;
109 public String getDescription() {
110 return this.description;
114 public List<Component> getChildren() {
119 public ProjectAttributes getProjectAttributes() {
120 checkState(this.type == Type.PROJECT, "Only component of type PROJECT have a ProjectAttributes object");
121 return this.projectAttributes;
125 public ReportAttributes getReportAttributes() {
126 return this.reportAttributes;
130 public FileAttributes getFileAttributes() {
131 checkState(this.type == Type.FILE, "Only component of type FILE have a FileAttributes object");
132 return this.fileAttributes;
136 public ProjectViewAttributes getProjectViewAttributes() {
137 throw new IllegalStateException("Only component of type PROJECT_VIEW have a ProjectViewAttributes object");
141 public SubViewAttributes getSubViewAttributes() {
142 throw new IllegalStateException("Only component of type SUBVIEW have a SubViewAttributes object");
146 public ViewAttributes getViewAttributes() {
147 throw new IllegalStateException("Only component of type VIEW have a ViewAttributes object");
150 public static Builder builder(Type type) {
151 return new Builder(type);
154 public static final class Builder {
156 private static final String DB_KEY_CANNOT_BE_NULL = "DB key can't be null";
157 private static final String KEY_CANNOT_BE_NULL = "Key can't be null";
158 private static final String UUID_CANNOT_BE_NULL = "uuid can't be null";
159 private static final String REPORT_ATTRIBUTES_CANNOT_BE_NULL = "reportAttributes can't be null";
160 private static final String NAME_CANNOT_BE_NULL = "name can't be null";
161 private static final String STATUS_CANNOT_BE_NULL = "status can't be null";
163 private final Type type;
164 private Status status;
165 private ProjectAttributes projectAttributes;
166 private ReportAttributes reportAttributes;
168 private String dbKey;
171 private String shortName;
172 private String description;
173 private FileAttributes fileAttributes;
174 private final List<Component> children = new ArrayList<>();
176 private Builder(Type type) {
177 this.type = requireNonNull(type, "type can't be null");
180 public Builder setUuid(String s) {
181 this.uuid = requireNonNull(s, UUID_CANNOT_BE_NULL);
186 public String getUuid() {
190 public Builder setStatus(Status status) {
191 this.status = requireNonNull(status, STATUS_CANNOT_BE_NULL);
195 public Builder setDbKey(String s) {
196 this.dbKey = requireNonNull(s, DB_KEY_CANNOT_BE_NULL);
200 public Builder setKey(String key) {
201 this.key = requireNonNull(key, KEY_CANNOT_BE_NULL);
205 public Builder setName(String name) {
206 this.name = abbreviate(requireNonNull(name, NAME_CANNOT_BE_NULL), MAX_COMPONENT_NAME_LENGTH);
210 public Builder setShortName(String shortName) {
211 this.shortName = abbreviate(requireNonNull(shortName, NAME_CANNOT_BE_NULL), MAX_COMPONENT_NAME_LENGTH);
215 public Builder setDescription(@Nullable String description) {
216 this.description = abbreviate(trimToNull(description), MAX_COMPONENT_DESCRIPTION_LENGTH);
220 public Builder setProjectAttributes(ProjectAttributes projectAttributes) {
221 checkProjectAttributes(projectAttributes);
222 this.projectAttributes = projectAttributes;
226 public Builder setReportAttributes(ReportAttributes reportAttributes) {
227 this.reportAttributes = requireNonNull(reportAttributes, REPORT_ATTRIBUTES_CANNOT_BE_NULL);
231 public Builder setFileAttributes(@Nullable FileAttributes fileAttributes) {
232 this.fileAttributes = fileAttributes;
236 public Builder addChildren(List<Component> components) {
237 for (Component component : components) {
238 checkArgument(component.getType().isReportType());
240 this.children.addAll(components);
244 public ComponentImpl build() {
245 requireNonNull(reportAttributes, REPORT_ATTRIBUTES_CANNOT_BE_NULL);
246 requireNonNull(uuid, UUID_CANNOT_BE_NULL);
247 requireNonNull(dbKey, DB_KEY_CANNOT_BE_NULL);
248 requireNonNull(name, NAME_CANNOT_BE_NULL);
249 requireNonNull(status, STATUS_CANNOT_BE_NULL);
250 checkProjectAttributes(this.projectAttributes);
251 return new ComponentImpl(this);
254 private void checkProjectAttributes(@Nullable ProjectAttributes projectAttributes) {
255 checkArgument(type != Type.PROJECT ^ projectAttributes != null, "ProjectAttributes must and can only be set for type PROJECT");
260 public String toString() {
261 return "ComponentImpl{" +
263 ", status=" + status +
264 ", name='" + name + '\'' +
265 ", dbKey='" + dbKey + '\'' +
266 ", key='" + key + '\'' +
267 ", uuid='" + uuid + '\'' +
268 ", description='" + description + '\'' +
269 ", children=" + children +
270 ", projectAttributes=" + projectAttributes +
271 ", reportAttributes=" + reportAttributes +
272 ", fileAttributes=" + fileAttributes +
277 public boolean equals(@Nullable Object o) {
281 if (o == null || getClass() != o.getClass()) {
284 ComponentImpl component = (ComponentImpl) o;
285 return uuid.equals(component.uuid);
289 public int hashCode() {
290 return uuid.hashCode();