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.

IssuesChangesNotificationBuilder.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2020 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.server.issue.notification;
  21. import com.google.common.collect.ImmutableSet;
  22. import java.util.Objects;
  23. import java.util.Optional;
  24. import java.util.Set;
  25. import javax.annotation.CheckForNull;
  26. import javax.annotation.Nullable;
  27. import javax.annotation.concurrent.Immutable;
  28. import org.sonar.api.rule.RuleKey;
  29. import org.sonar.api.rules.RuleType;
  30. import static com.google.common.base.Preconditions.checkArgument;
  31. import static java.util.Objects.requireNonNull;
  32. import static java.util.Optional.ofNullable;
  33. @Immutable
  34. public class IssuesChangesNotificationBuilder {
  35. private static final String KEY_CANT_BE_NULL_MESSAGE = "key can't be null";
  36. private final Set<ChangedIssue> issues;
  37. private final Change change;
  38. public IssuesChangesNotificationBuilder(Set<ChangedIssue> issues, Change change) {
  39. checkArgument(!issues.isEmpty(), "issues can't be empty");
  40. this.issues = ImmutableSet.copyOf(issues);
  41. this.change = requireNonNull(change, "change can't be null");
  42. }
  43. public Set<ChangedIssue> getIssues() {
  44. return issues;
  45. }
  46. public Change getChange() {
  47. return change;
  48. }
  49. @Immutable
  50. public static final class ChangedIssue {
  51. private final String key;
  52. private final String newStatus;
  53. @CheckForNull
  54. private final String newResolution;
  55. @CheckForNull
  56. private final User assignee;
  57. private final Rule rule;
  58. private final Project project;
  59. public ChangedIssue(Builder builder) {
  60. this.key = requireNonNull(builder.key, KEY_CANT_BE_NULL_MESSAGE);
  61. this.newStatus = requireNonNull(builder.newStatus, "newStatus can't be null");
  62. this.newResolution = builder.newResolution;
  63. this.assignee = builder.assignee;
  64. this.rule = requireNonNull(builder.rule, "rule can't be null");
  65. this.project = requireNonNull(builder.project, "project can't be null");
  66. }
  67. public String getKey() {
  68. return key;
  69. }
  70. public String getNewStatus() {
  71. return newStatus;
  72. }
  73. public Optional<String> getNewResolution() {
  74. return ofNullable(newResolution);
  75. }
  76. public Optional<User> getAssignee() {
  77. return ofNullable(assignee);
  78. }
  79. public Rule getRule() {
  80. return rule;
  81. }
  82. public Project getProject() {
  83. return project;
  84. }
  85. public static class Builder {
  86. private final String key;
  87. private String newStatus;
  88. @CheckForNull
  89. private String newResolution;
  90. @CheckForNull
  91. private User assignee;
  92. private Rule rule;
  93. private Project project;
  94. public Builder(String key) {
  95. this.key = key;
  96. }
  97. public Builder setNewStatus(String newStatus) {
  98. this.newStatus = newStatus;
  99. return this;
  100. }
  101. public Builder setNewResolution(@Nullable String newResolution) {
  102. this.newResolution = newResolution;
  103. return this;
  104. }
  105. public Builder setAssignee(@Nullable User assignee) {
  106. this.assignee = assignee;
  107. return this;
  108. }
  109. public Builder setRule(Rule rule) {
  110. this.rule = rule;
  111. return this;
  112. }
  113. public Builder setProject(Project project) {
  114. this.project = project;
  115. return this;
  116. }
  117. public ChangedIssue build() {
  118. return new ChangedIssue(this);
  119. }
  120. }
  121. @Override
  122. public boolean equals(Object o) {
  123. if (this == o) {
  124. return true;
  125. }
  126. if (o == null || getClass() != o.getClass()) {
  127. return false;
  128. }
  129. ChangedIssue that = (ChangedIssue) o;
  130. return key.equals(that.key) &&
  131. newStatus.equals(that.newStatus) &&
  132. Objects.equals(newResolution, that.newResolution) &&
  133. Objects.equals(assignee, that.assignee) &&
  134. rule.equals(that.rule) &&
  135. project.equals(that.project);
  136. }
  137. @Override
  138. public int hashCode() {
  139. return Objects.hash(key, newStatus, newResolution, assignee, rule, project);
  140. }
  141. @Override
  142. public String toString() {
  143. return "ChangedIssue{" +
  144. "key='" + key + '\'' +
  145. ", newStatus='" + newStatus + '\'' +
  146. ", newResolution='" + newResolution + '\'' +
  147. ", assignee=" + assignee +
  148. ", rule=" + rule +
  149. ", project=" + project +
  150. '}';
  151. }
  152. }
  153. public static final class User {
  154. private final String uuid;
  155. private final String login;
  156. @CheckForNull
  157. private final String name;
  158. public User(String uuid, String login, @Nullable String name) {
  159. this.uuid = requireNonNull(uuid, "uuid can't be null");
  160. this.login = requireNonNull(login, "login can't be null");
  161. this.name = name;
  162. }
  163. public String getUuid() {
  164. return uuid;
  165. }
  166. public String getLogin() {
  167. return login;
  168. }
  169. public Optional<String> getName() {
  170. return ofNullable(name);
  171. }
  172. @Override
  173. public String toString() {
  174. return "User{" +
  175. "uuid='" + uuid + '\'' +
  176. ", login='" + login + '\'' +
  177. ", name='" + name + '\'' +
  178. '}';
  179. }
  180. @Override
  181. public boolean equals(Object o) {
  182. if (this == o) {
  183. return true;
  184. }
  185. if (o == null || getClass() != o.getClass()) {
  186. return false;
  187. }
  188. User user = (User) o;
  189. return uuid.equals(user.uuid) &&
  190. login.equals(user.login) &&
  191. Objects.equals(name, user.name);
  192. }
  193. @Override
  194. public int hashCode() {
  195. return Objects.hash(uuid, login, name);
  196. }
  197. }
  198. @Immutable
  199. public static final class Rule {
  200. private final RuleKey key;
  201. private final RuleType ruleType;
  202. private final String name;
  203. public Rule(RuleKey key, @Nullable String ruleType, String name) {
  204. this(key, ruleType != null ? RuleType.valueOf(ruleType) : null, name);
  205. }
  206. public Rule(RuleKey key, @Nullable RuleType ruleType, String name) {
  207. this.key = requireNonNull(key, KEY_CANT_BE_NULL_MESSAGE);
  208. this.ruleType = ruleType;
  209. this.name = requireNonNull(name, "name can't be null");
  210. }
  211. public RuleKey getKey() {
  212. return key;
  213. }
  214. @CheckForNull
  215. public RuleType getRuleType() {
  216. return ruleType;
  217. }
  218. public String getName() {
  219. return name;
  220. }
  221. @Override
  222. public boolean equals(Object o) {
  223. if (this == o) {
  224. return true;
  225. }
  226. if (o == null || getClass() != o.getClass()) {
  227. return false;
  228. }
  229. Rule that = (Rule) o;
  230. return key.equals(that.key) && ruleType.equals(that.ruleType) && name.equals(that.name);
  231. }
  232. @Override
  233. public int hashCode() {
  234. return Objects.hash(key, ruleType, name);
  235. }
  236. @Override
  237. public String toString() {
  238. return "Rule{" +
  239. "key=" + key +
  240. ", type=" + ruleType +
  241. ", name='" + name + '\'' +
  242. '}';
  243. }
  244. }
  245. @Immutable
  246. public static final class Project {
  247. private final String uuid;
  248. private final String key;
  249. private final String projectName;
  250. @Nullable
  251. private final String branchName;
  252. public Project(Builder builder) {
  253. this.uuid = requireNonNull(builder.uuid, "uuid can't be null");
  254. this.key = requireNonNull(builder.key, KEY_CANT_BE_NULL_MESSAGE);
  255. this.projectName = requireNonNull(builder.projectName, "projectName can't be null");
  256. this.branchName = builder.branchName;
  257. }
  258. public String getUuid() {
  259. return uuid;
  260. }
  261. public String getKey() {
  262. return key;
  263. }
  264. public String getProjectName() {
  265. return projectName;
  266. }
  267. public Optional<String> getBranchName() {
  268. return ofNullable(branchName);
  269. }
  270. @Override
  271. public boolean equals(Object o) {
  272. if (this == o) {
  273. return true;
  274. }
  275. if (o == null || getClass() != o.getClass()) {
  276. return false;
  277. }
  278. Project project = (Project) o;
  279. return uuid.equals(project.uuid) &&
  280. key.equals(project.key) &&
  281. projectName.equals(project.projectName) &&
  282. Objects.equals(branchName, project.branchName);
  283. }
  284. @Override
  285. public int hashCode() {
  286. return Objects.hash(uuid, key, projectName, branchName);
  287. }
  288. @Override
  289. public String toString() {
  290. return "Project{" +
  291. "uuid='" + uuid + '\'' +
  292. ", key='" + key + '\'' +
  293. ", projectName='" + projectName + '\'' +
  294. ", branchName='" + branchName + '\'' +
  295. '}';
  296. }
  297. public static class Builder {
  298. private final String uuid;
  299. private String key;
  300. private String projectName;
  301. @CheckForNull
  302. private String branchName;
  303. public Builder(String uuid) {
  304. this.uuid = uuid;
  305. }
  306. public Builder setKey(String key) {
  307. this.key = key;
  308. return this;
  309. }
  310. public Builder setProjectName(String projectName) {
  311. this.projectName = projectName;
  312. return this;
  313. }
  314. public Builder setBranchName(@Nullable String branchName) {
  315. this.branchName = branchName;
  316. return this;
  317. }
  318. public Project build() {
  319. return new Project(this);
  320. }
  321. }
  322. }
  323. public abstract static class Change {
  324. protected final long date;
  325. private Change(long date) {
  326. this.date = date;
  327. }
  328. public long getDate() {
  329. return date;
  330. }
  331. public abstract boolean isAuthorLogin(String login);
  332. }
  333. @Immutable
  334. public static final class AnalysisChange extends Change {
  335. public AnalysisChange(long date) {
  336. super(date);
  337. }
  338. @Override
  339. public boolean isAuthorLogin(String login) {
  340. return false;
  341. }
  342. @Override
  343. public boolean equals(Object o) {
  344. if (this == o) {
  345. return true;
  346. }
  347. if (o == null || getClass() != o.getClass()) {
  348. return false;
  349. }
  350. Change change = (Change) o;
  351. return date == change.date;
  352. }
  353. @Override
  354. public int hashCode() {
  355. return Objects.hash(date);
  356. }
  357. @Override
  358. public String toString() {
  359. return "AnalysisChange{" + date + '}';
  360. }
  361. }
  362. @Immutable
  363. public static final class UserChange extends Change {
  364. private final User user;
  365. public UserChange(long date, User user) {
  366. super(date);
  367. this.user = requireNonNull(user, "user can't be null");
  368. }
  369. public User getUser() {
  370. return user;
  371. }
  372. @Override
  373. public boolean isAuthorLogin(String login) {
  374. return this.user.login.equals(login);
  375. }
  376. @Override
  377. public boolean equals(Object o) {
  378. if (this == o) {
  379. return true;
  380. }
  381. if (o == null || getClass() != o.getClass()) {
  382. return false;
  383. }
  384. UserChange that = (UserChange) o;
  385. return date == that.date && user.equals(that.user);
  386. }
  387. @Override
  388. public int hashCode() {
  389. return Objects.hash(user, date);
  390. }
  391. @Override
  392. public String toString() {
  393. return "UserChange{" +
  394. "date=" + date +
  395. ", user=" + user +
  396. '}';
  397. }
  398. }
  399. }