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.collect.ImmutableList;
23 import java.util.ArrayList;
24 import java.util.List;
25 import java.util.Objects;
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 views components.
37 public class ViewsComponent implements Component {
38 private final Type type;
39 private final String key;
40 private final String uuid;
41 private final String name;
42 private final String description;
43 private final List<Component> children;
44 private final ProjectViewAttributes projectViewAttributes;
45 private final SubViewAttributes subViewAttributes;
46 private final ViewAttributes viewAttributes;
48 private ViewsComponent(Type type, String key, @Nullable String uuid, @Nullable String name, @Nullable String description,
49 List<Component> children, @Nullable ProjectViewAttributes projectViewAttributes, @Nullable SubViewAttributes subViewAttributes,
50 @Nullable ViewAttributes viewAttributes) {
51 checkArgument(type.isViewsType(), "Component type must be a Views type");
53 this.key = requireNonNull(key);
56 this.description = description;
57 this.children = ImmutableList.copyOf(children);
58 this.projectViewAttributes = projectViewAttributes;
59 this.subViewAttributes = subViewAttributes;
60 this.viewAttributes = viewAttributes;
63 public static Builder builder(Type type, String key) {
64 return new Builder(type, key);
67 public static Builder builder(Type type, int key) {
68 return new Builder(type, String.valueOf(key));
71 public static final class Builder {
72 private final Type type;
76 private String description;
77 private List<Component> children = new ArrayList<>();
78 private ProjectViewAttributes projectViewAttributes;
79 private SubViewAttributes subViewAttributes;
80 private ViewAttributes viewAttributes;
82 private Builder(Type type, String key) {
87 public Builder setUuid(@Nullable String uuid) {
92 public Builder setKey(String key) {
97 public Builder setName(@Nullable String name) {
102 public Builder setDescription(String description) {
103 this.description = description;
107 public Builder setChildren(List<Component> children) {
108 this.children = children;
112 public Builder setProjectViewAttributes(@Nullable ProjectViewAttributes projectViewAttributes) {
113 this.projectViewAttributes = projectViewAttributes;
117 public Builder setSubViewAttributes(@Nullable SubViewAttributes subViewAttributes) {
118 this.subViewAttributes = subViewAttributes;
122 public Builder setViewAttributes(@Nullable ViewAttributes viewAttributes) {
123 this.viewAttributes = viewAttributes;
127 public Builder addChildren(Component... c) {
128 for (Component viewsComponent : c) {
129 checkArgument(viewsComponent.getType().isViewsType());
131 this.children.addAll(asList(c));
135 public ViewsComponent build() {
136 return new ViewsComponent(type, key, uuid, name, description, children, projectViewAttributes, subViewAttributes, viewAttributes);
141 public Type getType() {
146 public Status getStatus() {
147 return Status.UNAVAILABLE;
151 public String getUuid() {
156 public String getDbKey() {
161 * Views has no branch feature, the public key is the same as the key
164 public String getKey() {
169 public String getName() {
170 checkState(this.name != null, "No name has been set");
175 public String getShortName() {
181 public String getDescription() {
182 return this.description;
186 public List<Component> getChildren() {
191 public ProjectAttributes getProjectAttributes() {
192 throw new IllegalStateException("A component of type " + type + " does not have project attributes");
196 public ReportAttributes getReportAttributes() {
197 throw new IllegalStateException("A component of type " + type + " does not have report attributes");
201 public FileAttributes getFileAttributes() {
202 throw new IllegalStateException("A component of type " + type + " does not have file attributes");
206 public ProjectViewAttributes getProjectViewAttributes() {
207 checkState(this.type != Type.PROJECT_VIEW || this.projectViewAttributes != null, "A ProjectViewAttribute object should have been set");
208 return this.projectViewAttributes;
212 public SubViewAttributes getSubViewAttributes() {
213 checkState(this.type != Type.SUBVIEW || this.subViewAttributes != null, "A SubViewAttributes object should have been set");
214 return this.subViewAttributes;
218 public ViewAttributes getViewAttributes() {
219 checkState(this.type != Type.VIEW || this.viewAttributes != null, "A ViewAttributes object should have been set");
220 return viewAttributes;
224 public String toString() {
225 return "ViewsComponent{" +
227 ", key='" + key + '\'' +
228 ", uuid='" + uuid + '\'' +
229 ", name='" + name + '\'' +
230 ", children=" + children +
231 ", projectViewAttributes=" + projectViewAttributes +
232 ", subViewAttributes=" + subViewAttributes +
233 ", viewAttributes=" + viewAttributes +
238 public boolean equals(@Nullable Object o) {
242 if (o == null || getClass() != o.getClass()) {
245 ViewsComponent that = (ViewsComponent) o;
246 return key.equals(that.key);
250 public int hashCode() {
251 return Objects.hash(key);