3 * Copyright (C) 2009-2023 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.issue.notification;
22 import com.google.common.collect.ImmutableSet;
23 import java.util.Objects;
24 import java.util.Optional;
26 import javax.annotation.CheckForNull;
27 import javax.annotation.Nullable;
28 import javax.annotation.concurrent.Immutable;
29 import org.sonar.api.rule.RuleKey;
30 import org.sonar.api.rules.RuleType;
31 import org.sonar.core.issue.status.IssueStatus;
33 import static com.google.common.base.Preconditions.checkArgument;
34 import static java.util.Objects.requireNonNull;
35 import static java.util.Optional.ofNullable;
38 public class IssuesChangesNotificationBuilder {
40 private static final String KEY_CANT_BE_NULL_MESSAGE = "key can't be null";
41 private final Set<ChangedIssue> issues;
42 private final Change change;
44 public IssuesChangesNotificationBuilder(Set<ChangedIssue> issues, Change change) {
45 checkArgument(!issues.isEmpty(), "issues can't be empty");
47 this.issues = ImmutableSet.copyOf(issues);
48 this.change = requireNonNull(change, "change can't be null");
51 public Set<ChangedIssue> getIssues() {
55 public Change getChange() {
60 public static final class ChangedIssue {
61 private final String key;
62 private final String newStatus;
63 private final IssueStatus newIssueStatus;
65 private final User assignee;
66 private final Rule rule;
67 private final Project project;
69 public ChangedIssue(Builder builder) {
70 this.key = requireNonNull(builder.key, KEY_CANT_BE_NULL_MESSAGE);
71 this.newStatus = requireNonNull(builder.newStatus, "newStatus can't be null");
72 this.newIssueStatus = builder.newIssueStatus;
73 this.assignee = builder.assignee;
74 this.rule = requireNonNull(builder.rule, "rule can't be null");
75 this.project = requireNonNull(builder.project, "project can't be null");
78 public String getKey() {
82 public String getNewStatus() {
86 public Optional<IssueStatus> getNewIssueStatus() {
87 return ofNullable(newIssueStatus);
90 public Optional<User> getAssignee() {
91 return ofNullable(assignee);
94 public Rule getRule() {
98 public Project getProject() {
102 public static class Builder {
103 private final String key;
105 private IssueStatus newIssueStatus;
106 private String newStatus;
108 private User assignee;
110 private Project project;
112 public Builder(String key) {
116 public Builder setNewStatus(String newStatus) {
117 this.newStatus = newStatus;
121 public Builder setAssignee(@Nullable User assignee) {
122 this.assignee = assignee;
126 public Builder setNewIssueStatus(@Nullable IssueStatus newIssueStatus) {
127 this.newIssueStatus = newIssueStatus;
131 public Builder setRule(Rule rule) {
136 public Builder setProject(Project project) {
137 this.project = project;
141 public ChangedIssue build() {
142 return new ChangedIssue(this);
147 public boolean equals(Object o) {
151 if (o == null || getClass() != o.getClass()) {
154 ChangedIssue that = (ChangedIssue) o;
155 return key.equals(that.key) &&
156 newStatus.equals(that.newStatus) &&
157 newIssueStatus == that.newIssueStatus &&
158 Objects.equals(assignee, that.assignee) &&
159 rule.equals(that.rule) &&
160 project.equals(that.project);
164 public int hashCode() {
165 return Objects.hash(key, newStatus, newIssueStatus, assignee, rule, project);
169 public String toString() {
170 return "ChangedIssue{" +
171 "key='" + key + '\'' +
172 ", newStatus='" + newStatus + '\'' +
173 ", newIssueStatus='" + newIssueStatus + '\'' +
174 ", assignee=" + assignee +
176 ", project=" + project +
181 public static final class User {
182 private final String uuid;
183 private final String login;
185 private final String name;
187 public User(String uuid, String login, @Nullable String name) {
188 this.uuid = requireNonNull(uuid, "uuid can't be null");
189 this.login = requireNonNull(login, "login can't be null");
193 public String getUuid() {
197 public String getLogin() {
201 public Optional<String> getName() {
202 return ofNullable(name);
206 public String toString() {
208 "uuid='" + uuid + '\'' +
209 ", login='" + login + '\'' +
210 ", name='" + name + '\'' +
215 public boolean equals(Object o) {
219 if (o == null || getClass() != o.getClass()) {
222 User user = (User) o;
223 return uuid.equals(user.uuid) &&
224 login.equals(user.login) &&
225 Objects.equals(name, user.name);
229 public int hashCode() {
230 return Objects.hash(uuid, login, name);
235 public static final class Rule {
236 private final RuleKey key;
237 private final RuleType ruleType;
238 private final String name;
240 public Rule(RuleKey key, @Nullable String ruleType, String name) {
241 this(key, ruleType != null ? RuleType.valueOf(ruleType) : null, name);
244 public Rule(RuleKey key, @Nullable RuleType ruleType, String name) {
245 this.key = requireNonNull(key, KEY_CANT_BE_NULL_MESSAGE);
246 this.ruleType = ruleType;
247 this.name = requireNonNull(name, "name can't be null");
250 public RuleKey getKey() {
255 public RuleType getRuleType() {
259 public String getName() {
264 public boolean equals(Object o) {
268 if (o == null || getClass() != o.getClass()) {
271 Rule that = (Rule) o;
272 return key.equals(that.key) && ruleType == that.ruleType && name.equals(that.name);
276 public int hashCode() {
277 return Objects.hash(key, ruleType, name);
281 public String toString() {
284 ", type=" + ruleType +
285 ", name='" + name + '\'' +
291 public static final class Project {
292 private final String uuid;
293 private final String key;
294 private final String projectName;
296 private final String branchName;
298 public Project(Builder builder) {
299 this.uuid = requireNonNull(builder.uuid, "uuid can't be null");
300 this.key = requireNonNull(builder.key, KEY_CANT_BE_NULL_MESSAGE);
301 this.projectName = requireNonNull(builder.projectName, "projectName can't be null");
302 this.branchName = builder.branchName;
305 public String getUuid() {
309 public String getKey() {
313 public String getProjectName() {
317 public Optional<String> getBranchName() {
318 return ofNullable(branchName);
322 public boolean equals(Object o) {
326 if (o == null || getClass() != o.getClass()) {
329 Project project = (Project) o;
330 return uuid.equals(project.uuid) &&
331 key.equals(project.key) &&
332 projectName.equals(project.projectName) &&
333 Objects.equals(branchName, project.branchName);
337 public int hashCode() {
338 return Objects.hash(uuid, key, projectName, branchName);
342 public String toString() {
344 "uuid='" + uuid + '\'' +
345 ", key='" + key + '\'' +
346 ", projectName='" + projectName + '\'' +
347 ", branchName='" + branchName + '\'' +
351 public static class Builder {
352 private final String uuid;
354 private String projectName;
356 private String branchName;
358 public Builder(String uuid) {
362 public Builder setKey(String key) {
367 public Builder setProjectName(String projectName) {
368 this.projectName = projectName;
372 public Builder setBranchName(@Nullable String branchName) {
373 this.branchName = branchName;
377 public Project build() {
378 return new Project(this);
383 public abstract static class Change {
384 protected final long date;
386 private Change(long date) {
390 public long getDate() {
394 public abstract boolean isAuthorLogin(String login);
398 public static final class AnalysisChange extends Change {
399 public AnalysisChange(long date) {
404 public boolean isAuthorLogin(String login) {
409 public boolean equals(Object o) {
413 if (o == null || getClass() != o.getClass()) {
416 Change change = (Change) o;
417 return date == change.date;
421 public int hashCode() {
422 return Objects.hash(date);
426 public String toString() {
427 return "AnalysisChange{" + date + '}';
432 public static final class UserChange extends Change {
433 private final User user;
435 public UserChange(long date, User user) {
437 this.user = requireNonNull(user, "user can't be null");
440 public User getUser() {
445 public boolean isAuthorLogin(String login) {
446 return this.user.login.equals(login);
450 public boolean equals(Object o) {
454 if (o == null || getClass() != o.getClass()) {
457 UserChange that = (UserChange) o;
458 return date == that.date && user.equals(that.user);
462 public int hashCode() {
463 return Objects.hash(user, date);
467 public String toString() {
468 return "UserChange{" +