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.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 String name;
39 private final String key;
40 private final String uuid;
43 private final String description;
44 private final List<Component> children;
46 private final ReportAttributes reportAttributes;
48 private final FileAttributes fileAttributes;
50 private ComponentImpl(Builder builder) {
51 this.type = builder.type;
52 this.key = builder.key;
53 this.name = builder.name;
54 this.description = builder.description;
55 this.uuid = builder.uuid;
56 this.reportAttributes = builder.reportAttributes;
57 this.fileAttributes = builder.fileAttributes;
58 this.children = ImmutableList.copyOf(builder.children);
62 public Type getType() {
67 public String getUuid() {
72 public String getKey() {
77 public String getName() {
83 public String getDescription() {
84 return this.description;
88 public List<Component> getChildren() {
93 public ReportAttributes getReportAttributes() {
94 return this.reportAttributes;
98 public FileAttributes getFileAttributes() {
99 checkState(this.type == Type.FILE, "Only component of type FILE have a FileAttributes object");
100 return this.fileAttributes;
104 public ProjectViewAttributes getProjectViewAttributes() {
105 throw new IllegalStateException("Only component of type PROJECT_VIEW have a FileAttributes object");
108 public static Builder builder(Type type) {
109 return new Builder(type);
112 public static final class Builder {
114 private static final String KEY_CANNOT_BE_NULL = "key can't be null";
115 private static final String UUID_CANNOT_BE_NULL = "uuid can't be null";
116 private static final String REPORT_ATTRIBUTES_CANNOT_BE_NULL = "reportAttributes can't be null";
117 private static final String NAME_CANNOT_BE_NULL = "name can't be null";
119 private final Type type;
120 private ReportAttributes reportAttributes;
124 private String description;
125 private FileAttributes fileAttributes;
126 private final List<Component> children = new ArrayList<>();
128 private Builder(Type type){
129 this.type = requireNonNull(type, "type can't be null");
132 public Builder setReportAttributes(ReportAttributes reportAttributes) {
133 this.reportAttributes = requireNonNull(reportAttributes, REPORT_ATTRIBUTES_CANNOT_BE_NULL);
137 public Builder setUuid(String s) {
138 this.uuid = requireNonNull(s, UUID_CANNOT_BE_NULL);
142 public Builder setKey(String s) {
143 this.key = requireNonNull(s, KEY_CANNOT_BE_NULL);
147 public Builder setName(String name) {
148 this.name = requireNonNull(name, NAME_CANNOT_BE_NULL);
152 public Builder setDescription(@Nullable String description) {
153 this.description = trimToNull(description);
157 public Builder setFileAttributes(@Nullable FileAttributes fileAttributes) {
158 this.fileAttributes = fileAttributes;
162 public Builder addChildren(Component... c) {
163 for (Component component : c) {
164 checkArgument(component.getType().isReportType());
166 this.children.addAll(asList(c));
170 public ComponentImpl build() {
171 requireNonNull(reportAttributes, REPORT_ATTRIBUTES_CANNOT_BE_NULL);
172 requireNonNull(uuid, UUID_CANNOT_BE_NULL);
173 requireNonNull(key, KEY_CANNOT_BE_NULL);
174 requireNonNull(name, NAME_CANNOT_BE_NULL);
175 return new ComponentImpl(this);
180 public String toString() {
181 return "ComponentImpl{" +
182 "key='" + key + '\'' +
184 ", uuid='" + uuid + '\'' +
185 ", name='" + name + '\'' +
186 ", description='" + description + '\'' +
187 ", fileAttributes=" + fileAttributes +
188 ", reportAttributes=" + reportAttributes +
193 public boolean equals(@Nullable Object o) {
197 if (o == null || getClass() != o.getClass()) {
200 ComponentImpl component = (ComponentImpl) o;
201 return uuid.equals(component.uuid);
205 public int hashCode() {
206 return uuid.hashCode();