You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ComponentImpl.java 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2024 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  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.
  10. *
  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.
  15. *
  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.
  19. */
  20. package org.sonar.ce.task.projectanalysis.component;
  21. import com.google.common.base.MoreObjects;
  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;
  28. import static com.google.common.base.Preconditions.checkArgument;
  29. import static com.google.common.base.Preconditions.checkState;
  30. import static java.util.Objects.requireNonNull;
  31. import static org.apache.commons.lang3.StringUtils.abbreviate;
  32. import static org.apache.commons.lang3.StringUtils.trimToNull;
  33. import static org.sonar.db.component.ComponentValidator.MAX_COMPONENT_DESCRIPTION_LENGTH;
  34. import static org.sonar.db.component.ComponentValidator.MAX_COMPONENT_NAME_LENGTH;
  35. @Immutable
  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 shortName;
  41. private final String key;
  42. private final String uuid;
  43. @CheckForNull
  44. private final String description;
  45. private final List<Component> children;
  46. @CheckForNull
  47. private final ProjectAttributes projectAttributes;
  48. private final ReportAttributes reportAttributes;
  49. @CheckForNull
  50. 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.shortName = MoreObjects.firstNonNull(builder.shortName, builder.name).intern();
  57. this.description = builder.description;
  58. this.uuid = builder.uuid;
  59. this.projectAttributes = builder.projectAttributes;
  60. this.reportAttributes = builder.reportAttributes;
  61. this.fileAttributes = builder.fileAttributes;
  62. this.children = ImmutableList.copyOf(builder.children);
  63. }
  64. @Override
  65. public Type getType() {
  66. return type;
  67. }
  68. @Override
  69. public Status getStatus() {
  70. return status;
  71. }
  72. @Override
  73. public String getUuid() {
  74. return uuid;
  75. }
  76. @Override
  77. public String getKey() {
  78. return key;
  79. }
  80. @Override
  81. public String getName() {
  82. return this.name;
  83. }
  84. @Override
  85. public String getShortName() {
  86. return this.shortName;
  87. }
  88. @Override
  89. @CheckForNull
  90. public String getDescription() {
  91. return this.description;
  92. }
  93. @Override
  94. public List<Component> getChildren() {
  95. return children;
  96. }
  97. @Override
  98. public ProjectAttributes getProjectAttributes() {
  99. checkState(this.type == Type.PROJECT, "Only component of type PROJECT have a ProjectAttributes object");
  100. return this.projectAttributes;
  101. }
  102. @Override
  103. public ReportAttributes getReportAttributes() {
  104. return this.reportAttributes;
  105. }
  106. @Override
  107. public FileAttributes getFileAttributes() {
  108. checkState(this.type == Type.FILE, "Only component of type FILE have a FileAttributes object");
  109. return this.fileAttributes;
  110. }
  111. @Override
  112. public ProjectViewAttributes getProjectViewAttributes() {
  113. throw new IllegalStateException("Only component of type PROJECT_VIEW have a ProjectViewAttributes object");
  114. }
  115. @Override
  116. public SubViewAttributes getSubViewAttributes() {
  117. throw new IllegalStateException("Only component of type SUBVIEW have a SubViewAttributes object");
  118. }
  119. @Override
  120. public ViewAttributes getViewAttributes() {
  121. throw new IllegalStateException("Only component of type VIEW have a ViewAttributes object");
  122. }
  123. public static Builder builder(Type type) {
  124. return new Builder(type);
  125. }
  126. public static final class Builder {
  127. private static final String KEY_CANNOT_BE_NULL = "Key can't be null";
  128. private static final String UUID_CANNOT_BE_NULL = "uuid can't be null";
  129. private static final String REPORT_ATTRIBUTES_CANNOT_BE_NULL = "reportAttributes can't be null";
  130. private static final String NAME_CANNOT_BE_NULL = "name can't be null";
  131. private static final String STATUS_CANNOT_BE_NULL = "status can't be null";
  132. private final Type type;
  133. private Status status;
  134. private ProjectAttributes projectAttributes;
  135. private ReportAttributes reportAttributes;
  136. private String uuid;
  137. private String key;
  138. private String name;
  139. private String shortName;
  140. private String description;
  141. private FileAttributes fileAttributes;
  142. private final List<Component> children = new ArrayList<>();
  143. private Builder(Type type) {
  144. this.type = requireNonNull(type, "type can't be null");
  145. }
  146. public Builder setUuid(String s) {
  147. this.uuid = requireNonNull(s, UUID_CANNOT_BE_NULL);
  148. return this;
  149. }
  150. @CheckForNull
  151. public String getUuid() {
  152. return uuid;
  153. }
  154. public Builder setStatus(Status status) {
  155. this.status = requireNonNull(status, STATUS_CANNOT_BE_NULL);
  156. return this;
  157. }
  158. public Builder setKey(String key) {
  159. this.key = requireNonNull(key, KEY_CANNOT_BE_NULL);
  160. return this;
  161. }
  162. public Builder setName(String name) {
  163. this.name = abbreviate(requireNonNull(name, NAME_CANNOT_BE_NULL), MAX_COMPONENT_NAME_LENGTH);
  164. return this;
  165. }
  166. public Builder setShortName(String shortName) {
  167. this.shortName = abbreviate(requireNonNull(shortName, NAME_CANNOT_BE_NULL), MAX_COMPONENT_NAME_LENGTH);
  168. return this;
  169. }
  170. public Builder setDescription(@Nullable String description) {
  171. this.description = abbreviate(trimToNull(description), MAX_COMPONENT_DESCRIPTION_LENGTH);
  172. return this;
  173. }
  174. public Builder setProjectAttributes(ProjectAttributes projectAttributes) {
  175. checkProjectAttributes(projectAttributes);
  176. this.projectAttributes = projectAttributes;
  177. return this;
  178. }
  179. public Builder setReportAttributes(ReportAttributes reportAttributes) {
  180. this.reportAttributes = requireNonNull(reportAttributes, REPORT_ATTRIBUTES_CANNOT_BE_NULL);
  181. return this;
  182. }
  183. public Builder setFileAttributes(@Nullable FileAttributes fileAttributes) {
  184. this.fileAttributes = fileAttributes;
  185. return this;
  186. }
  187. public Builder addChildren(List<Component> components) {
  188. for (Component component : components) {
  189. checkArgument(component.getType().isReportType());
  190. }
  191. this.children.addAll(components);
  192. return this;
  193. }
  194. public ComponentImpl build() {
  195. requireNonNull(reportAttributes, REPORT_ATTRIBUTES_CANNOT_BE_NULL);
  196. requireNonNull(uuid, UUID_CANNOT_BE_NULL);
  197. requireNonNull(key, KEY_CANNOT_BE_NULL);
  198. requireNonNull(name, NAME_CANNOT_BE_NULL);
  199. requireNonNull(status, STATUS_CANNOT_BE_NULL);
  200. checkProjectAttributes(this.projectAttributes);
  201. return new ComponentImpl(this);
  202. }
  203. private void checkProjectAttributes(@Nullable ProjectAttributes projectAttributes) {
  204. checkArgument(type != Type.PROJECT ^ projectAttributes != null, "ProjectAttributes must and can only be set for type PROJECT");
  205. }
  206. }
  207. @Override
  208. public String toString() {
  209. return "ComponentImpl{" +
  210. "type=" + type +
  211. ", status=" + status +
  212. ", name='" + name + '\'' +
  213. ", key='" + key + '\'' +
  214. ", uuid='" + uuid + '\'' +
  215. ", description='" + description + '\'' +
  216. ", children=" + children +
  217. ", projectAttributes=" + projectAttributes +
  218. ", reportAttributes=" + reportAttributes +
  219. ", fileAttributes=" + fileAttributes +
  220. '}';
  221. }
  222. @Override
  223. public boolean equals(@Nullable Object o) {
  224. if (this == o) {
  225. return true;
  226. }
  227. if (o == null || getClass() != o.getClass()) {
  228. return false;
  229. }
  230. ComponentImpl component = (ComponentImpl) o;
  231. return uuid.equals(component.uuid);
  232. }
  233. @Override
  234. public int hashCode() {
  235. return uuid.hashCode();
  236. }
  237. }