3 * Copyright (C) 2009-2017 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.server.computation.task.projectanalysis.component;
22 import com.google.common.collect.ImmutableList;
23 import java.util.ArrayList;
24 import java.util.List;
25 import javax.annotation.CheckForNull;
26 import javax.annotation.Nullable;
27 import javax.annotation.concurrent.Immutable;
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;
33 import static org.apache.commons.lang.StringUtils.trimToNull;
36 public class ComponentImpl implements Component {
37 private final Type type;
38 private final Status status;
39 private final String name;
40 private final String key;
41 private final String uuid;
44 private final String description;
45 private final List<Component> children;
47 private final ReportAttributes reportAttributes;
49 private final FileAttributes fileAttributes;
51 private ComponentImpl(Builder builder) {
52 this.type = builder.type;
53 this.status = builder.status;
54 this.key = builder.key;
55 this.name = builder.name;
56 this.description = builder.description;
57 this.uuid = builder.uuid;
58 this.reportAttributes = builder.reportAttributes;
59 this.fileAttributes = builder.fileAttributes;
60 this.children = ImmutableList.copyOf(builder.children);
64 public Type getType() {
69 public Status getStatus() {
74 public String getUuid() {
79 public String getKey() {
84 public String getName() {
90 public String getDescription() {
91 return this.description;
95 public List<Component> getChildren() {
100 public ReportAttributes getReportAttributes() {
101 return this.reportAttributes;
105 public FileAttributes getFileAttributes() {
106 checkState(this.type == Type.FILE, "Only component of type FILE have a FileAttributes object");
107 return this.fileAttributes;
111 public ProjectViewAttributes getProjectViewAttributes() {
112 throw new IllegalStateException("Only component of type PROJECT_VIEW have a ProjectViewAttributes object");
116 public SubViewAttributes getSubViewAttributes() {
117 throw new IllegalStateException("Only component of type SUBVIEW have a SubViewAttributes object");
121 public ViewAttributes getViewAttributes() {
122 throw new IllegalStateException("Only component of type VIEW have a ViewAttributes object");
125 public static Builder builder(Type type) {
126 return new Builder(type);
129 public static final class Builder {
131 private static final String KEY_CANNOT_BE_NULL = "key can't be null";
132 private static final String UUID_CANNOT_BE_NULL = "uuid can't be null";
133 private static final String REPORT_ATTRIBUTES_CANNOT_BE_NULL = "reportAttributes can't be null";
134 private static final String NAME_CANNOT_BE_NULL = "name can't be null";
135 private static final String STATUS_CANNOT_BE_NULL = "status can't be null";
137 private final Type type;
138 private Status status;
139 private ReportAttributes reportAttributes;
143 private String description;
144 private FileAttributes fileAttributes;
145 private final List<Component> children = new ArrayList<>();
147 private Builder(Type type) {
148 this.type = requireNonNull(type, "type can't be null");
151 public Builder setReportAttributes(ReportAttributes reportAttributes) {
152 this.reportAttributes = requireNonNull(reportAttributes, REPORT_ATTRIBUTES_CANNOT_BE_NULL);
156 public Builder setUuid(String s) {
157 this.uuid = requireNonNull(s, UUID_CANNOT_BE_NULL);
162 public String getUuid() {
166 public Builder setStatus(Status status) {
167 this.status = requireNonNull(status, STATUS_CANNOT_BE_NULL);
171 public Builder setKey(String s) {
172 this.key = requireNonNull(s, KEY_CANNOT_BE_NULL);
176 public Builder setName(String name) {
177 this.name = requireNonNull(name, NAME_CANNOT_BE_NULL);
181 public Builder setDescription(@Nullable String description) {
182 this.description = trimToNull(description);
186 public Builder setFileAttributes(@Nullable FileAttributes fileAttributes) {
187 this.fileAttributes = fileAttributes;
191 public Builder addChildren(Component... c) {
192 for (Component component : c) {
193 checkArgument(component.getType().isReportType());
195 this.children.addAll(asList(c));
199 public ComponentImpl build() {
200 requireNonNull(reportAttributes, REPORT_ATTRIBUTES_CANNOT_BE_NULL);
201 requireNonNull(uuid, UUID_CANNOT_BE_NULL);
202 requireNonNull(key, KEY_CANNOT_BE_NULL);
203 requireNonNull(name, NAME_CANNOT_BE_NULL);
204 requireNonNull(status, STATUS_CANNOT_BE_NULL);
205 return new ComponentImpl(this);
210 public String toString() {
211 return "ComponentImpl{" +
212 "key='" + key + '\'' +
214 ", uuid='" + uuid + '\'' +
215 ", name='" + name + '\'' +
216 ", description='" + description + '\'' +
217 ", fileAttributes=" + fileAttributes +
218 ", reportAttributes=" + reportAttributes +
223 public boolean equals(@Nullable Object o) {
227 if (o == null || getClass() != o.getClass()) {
230 ComponentImpl component = (ComponentImpl) o;
231 return uuid.equals(component.uuid);
235 public int hashCode() {
236 return uuid.hashCode();