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.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 key;
45 private final String uuid;
48 private final String description;
49 private final List<Component> children;
51 private final ProjectAttributes projectAttributes;
52 private final ReportAttributes reportAttributes;
54 private final FileAttributes fileAttributes;
56 private ComponentImpl(Builder builder) {
57 this.type = builder.type;
58 this.status = builder.status;
59 this.key = builder.key;
60 this.name = builder.name;
61 this.shortName = MoreObjects.firstNonNull(builder.shortName, builder.name).intern();
62 this.description = builder.description;
63 this.uuid = builder.uuid;
64 this.projectAttributes = builder.projectAttributes;
65 this.reportAttributes = builder.reportAttributes;
66 this.fileAttributes = builder.fileAttributes;
67 this.children = ImmutableList.copyOf(builder.children);
71 public Type getType() {
76 public Status getStatus() {
81 public String getUuid() {
86 public String getKey() {
91 public String getName() {
96 public String getOldName() {
97 return this.getFileAttributes().getOldName();
101 public String getShortName() {
102 return this.shortName;
107 public String getDescription() {
108 return this.description;
112 public List<Component> getChildren() {
117 public ProjectAttributes getProjectAttributes() {
118 checkState(this.type == Type.PROJECT, "Only component of type PROJECT have a ProjectAttributes object");
119 return this.projectAttributes;
123 public ReportAttributes getReportAttributes() {
124 return this.reportAttributes;
128 public FileAttributes getFileAttributes() {
129 checkState(this.type == Type.FILE, "Only component of type FILE have a FileAttributes object");
130 return this.fileAttributes;
134 public ProjectViewAttributes getProjectViewAttributes() {
135 throw new IllegalStateException("Only component of type PROJECT_VIEW have a ProjectViewAttributes object");
139 public SubViewAttributes getSubViewAttributes() {
140 throw new IllegalStateException("Only component of type SUBVIEW have a SubViewAttributes object");
144 public ViewAttributes getViewAttributes() {
145 throw new IllegalStateException("Only component of type VIEW have a ViewAttributes object");
148 public static Builder builder(Type type) {
149 return new Builder(type);
152 public static final class Builder {
154 private static final String KEY_CANNOT_BE_NULL = "Key can't be null";
155 private static final String UUID_CANNOT_BE_NULL = "uuid can't be null";
156 private static final String REPORT_ATTRIBUTES_CANNOT_BE_NULL = "reportAttributes can't be null";
157 private static final String NAME_CANNOT_BE_NULL = "name can't be null";
158 private static final String STATUS_CANNOT_BE_NULL = "status can't be null";
160 private final Type type;
161 private Status status;
162 private ProjectAttributes projectAttributes;
163 private ReportAttributes reportAttributes;
167 private String oldName;
168 private String shortName;
169 private String description;
170 private FileAttributes fileAttributes;
171 private final List<Component> children = new ArrayList<>();
173 private Builder(Type type) {
174 this.type = requireNonNull(type, "type can't be null");
177 public Builder setUuid(String s) {
178 this.uuid = requireNonNull(s, UUID_CANNOT_BE_NULL);
183 public String getUuid() {
187 public Builder setStatus(Status status) {
188 this.status = requireNonNull(status, STATUS_CANNOT_BE_NULL);
192 public Builder setKey(String key) {
193 this.key = requireNonNull(key, KEY_CANNOT_BE_NULL);
197 public Builder setName(String name) {
198 this.name = abbreviate(requireNonNull(name, NAME_CANNOT_BE_NULL), MAX_COMPONENT_NAME_LENGTH);
202 public Builder setShortName(String shortName) {
203 this.shortName = abbreviate(requireNonNull(shortName, NAME_CANNOT_BE_NULL), MAX_COMPONENT_NAME_LENGTH);
207 public Builder setDescription(@Nullable String description) {
208 this.description = abbreviate(trimToNull(description), MAX_COMPONENT_DESCRIPTION_LENGTH);
212 public Builder setProjectAttributes(ProjectAttributes projectAttributes) {
213 checkProjectAttributes(projectAttributes);
214 this.projectAttributes = projectAttributes;
218 public Builder setReportAttributes(ReportAttributes reportAttributes) {
219 this.reportAttributes = requireNonNull(reportAttributes, REPORT_ATTRIBUTES_CANNOT_BE_NULL);
223 public Builder setFileAttributes(@Nullable FileAttributes fileAttributes) {
224 this.fileAttributes = fileAttributes;
228 public Builder addChildren(List<Component> components) {
229 for (Component component : components) {
230 checkArgument(component.getType().isReportType());
232 this.children.addAll(components);
236 public ComponentImpl build() {
237 requireNonNull(reportAttributes, REPORT_ATTRIBUTES_CANNOT_BE_NULL);
238 requireNonNull(uuid, UUID_CANNOT_BE_NULL);
239 requireNonNull(key, KEY_CANNOT_BE_NULL);
240 requireNonNull(name, NAME_CANNOT_BE_NULL);
241 requireNonNull(status, STATUS_CANNOT_BE_NULL);
242 checkProjectAttributes(this.projectAttributes);
243 return new ComponentImpl(this);
246 private void checkProjectAttributes(@Nullable ProjectAttributes projectAttributes) {
247 checkArgument(type != Type.PROJECT ^ projectAttributes != null, "ProjectAttributes must and can only be set for type PROJECT");
252 public String toString() {
253 return "ComponentImpl{" +
255 ", status=" + status +
256 ", name='" + name + '\'' +
257 ", key='" + key + '\'' +
258 ", uuid='" + uuid + '\'' +
259 ", description='" + description + '\'' +
260 ", children=" + children +
261 ", projectAttributes=" + projectAttributes +
262 ", reportAttributes=" + reportAttributes +
263 ", fileAttributes=" + fileAttributes +
268 public boolean equals(@Nullable Object o) {
272 if (o == null || getClass() != o.getClass()) {
275 ComponentImpl component = (ComponentImpl) o;
276 return uuid.equals(component.uuid);
280 public int hashCode() {
281 return uuid.hashCode();