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 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,
50 @Nullable ProjectViewAttributes projectViewAttributes, @Nullable SubViewAttributes subViewAttributes, @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;
73 private final String key;
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 setName(@Nullable String name) {
97 public Builder setDescription(String description) {
98 this.description = description;
102 public Builder setChildren(List<Component> children) {
103 this.children = children;
107 public Builder setProjectViewAttributes(@Nullable ProjectViewAttributes projectViewAttributes) {
108 this.projectViewAttributes = projectViewAttributes;
112 public Builder setSubViewAttributes(@Nullable SubViewAttributes subViewAttributes) {
113 this.subViewAttributes = subViewAttributes;
117 public Builder setViewAttributes(@Nullable ViewAttributes viewAttributes) {
118 this.viewAttributes = viewAttributes;
122 public Builder addChildren(Component... c) {
123 for (Component viewsComponent : c) {
124 checkArgument(viewsComponent.getType().isViewsType());
126 this.children.addAll(asList(c));
130 public ViewsComponent build() {
131 return new ViewsComponent(type, key, uuid, name, description, children, projectViewAttributes, subViewAttributes, viewAttributes);
136 public Type getType() {
141 public Status getStatus() {
142 return Status.UNAVAILABLE;
146 public String getUuid() {
151 public String getKey() {
156 public String getName() {
157 checkState(this.name != null, "No name has been set");
163 public String getDescription() {
164 return this.description;
168 public List<Component> getChildren() {
173 public ReportAttributes getReportAttributes() {
174 throw new IllegalStateException("A component of type " + type + " does not have report attributes");
178 public FileAttributes getFileAttributes() {
179 throw new IllegalStateException("A component of type " + type + " does not have file attributes");
183 public ProjectViewAttributes getProjectViewAttributes() {
184 checkState(this.type != Type.PROJECT_VIEW || this.projectViewAttributes != null, "A ProjectViewAttribute object should have been set");
185 return this.projectViewAttributes;
189 public SubViewAttributes getSubViewAttributes() {
190 checkState(this.type != Type.SUBVIEW || this.subViewAttributes != null, "A SubViewAttributes object should have been set");
191 return this.subViewAttributes;
195 public ViewAttributes getViewAttributes() {
196 checkState(this.type != Type.VIEW || this.viewAttributes != null, "A ViewAttributes object should have been set");
197 return viewAttributes;
201 public String toString() {
202 return "ViewsComponent{" +
204 ", key='" + key + '\'' +
205 ", uuid='" + uuid + '\'' +
206 ", name='" + name + '\'' +
207 ", children=" + children +
208 ", projectViewAttributes=" + projectViewAttributes +
209 ", subViewAttributes=" + subViewAttributes +
210 ", viewAttributes=" + viewAttributes +
215 public boolean equals(@Nullable Object o) {
219 if (o == null || getClass() != o.getClass()) {
222 ViewsComponent that = (ViewsComponent) o;
223 return key.equals(that.key);
227 public int hashCode() {
228 return Objects.hash(key);