Make NotifyConfig an AutoValue
Change-Id: Ieacab9a89aecf65972efa5bf2ed216da7b3e42ac
diff --git a/java/com/google/gerrit/server/git/NotifyConfig.java b/java/com/google/gerrit/server/git/NotifyConfig.java
index 429f15a..1a1bbb6 100644
--- a/java/com/google/gerrit/server/git/NotifyConfig.java
+++ b/java/com/google/gerrit/server/git/NotifyConfig.java
@@ -14,113 +14,101 @@
package com.google.gerrit.server.git;
-import com.google.common.base.MoreObjects;
+import com.google.auto.value.AutoValue;
import com.google.common.base.Strings;
+import com.google.common.collect.ImmutableSet;
import com.google.gerrit.common.data.GroupReference;
import com.google.gerrit.mail.Address;
import com.google.gerrit.server.account.ProjectWatches.NotifyType;
import java.util.EnumSet;
-import java.util.HashSet;
import java.util.Set;
+import org.eclipse.jgit.annotations.Nullable;
-public class NotifyConfig implements Comparable<NotifyConfig> {
+@AutoValue
+public abstract class NotifyConfig implements Comparable<NotifyConfig> {
public enum Header {
TO,
CC,
BCC
}
- private String name;
- private EnumSet<NotifyType> types = EnumSet.of(NotifyType.ALL);
- private String filter;
+ @Nullable
+ public abstract String getName();
- private Header header;
- private Set<GroupReference> groups = new HashSet<>();
- private Set<Address> addresses = new HashSet<>();
+ public abstract ImmutableSet<NotifyType> getNotify();
- public String getName() {
- return name;
- }
+ @Nullable
+ public abstract String getFilter();
- public void setName(String name) {
- this.name = name;
- }
+ @Nullable
+ public abstract Header getHeader();
+
+ public abstract ImmutableSet<GroupReference> getGroups();
+
+ public abstract ImmutableSet<Address> getAddresses();
public boolean isNotify(NotifyType type) {
- return types.contains(type) || types.contains(NotifyType.ALL);
+ return getNotify().contains(type) || getNotify().contains(NotifyType.ALL);
}
- public Set<NotifyType> getNotify() {
- return types;
+ public static Builder builder() {
+ return new AutoValue_NotifyConfig.Builder()
+ .setNotify(ImmutableSet.copyOf(EnumSet.of(NotifyType.ALL)));
}
- public void setTypes(Set<NotifyType> newTypes) {
- types = EnumSet.copyOf(newTypes);
- }
+ @AutoValue.Builder
+ public abstract static class Builder {
+ public abstract Builder setName(String name);
- public String getFilter() {
- return filter;
- }
+ public abstract Builder setNotify(Set<NotifyType> newTypes);
- public void setFilter(String filter) {
- if ("*".equals(filter)) {
- this.filter = null;
- } else {
- this.filter = Strings.emptyToNull(filter);
+ public abstract Builder setFilter(@Nullable String filter);
+
+ public abstract Builder setHeader(Header hdr);
+
+ public Builder addGroup(GroupReference group) {
+ groupsBuilder().add(group);
+ return this;
+ }
+
+ public Builder addAddress(Address address) {
+ addressesBuilder().add(address);
+ return this;
+ }
+
+ protected abstract ImmutableSet.Builder<GroupReference> groupsBuilder();
+
+ protected abstract ImmutableSet.Builder<Address> addressesBuilder();
+
+ protected abstract NotifyConfig autoBuild();
+
+ protected abstract String getFilter();
+
+ public NotifyConfig build() {
+ if ("*".equals(getFilter())) {
+ setFilter(null);
+ } else {
+ setFilter(Strings.emptyToNull(getFilter()));
+ }
+ return autoBuild();
}
}
- public Header getHeader() {
- return header;
- }
-
- public void setHeader(Header hdr) {
- header = hdr;
- }
-
- public Set<GroupReference> getGroups() {
- return groups;
- }
-
- public Set<Address> getAddresses() {
- return addresses;
- }
-
- public void addEmail(GroupReference group) {
- groups.add(group);
- }
-
- public void addEmail(Address address) {
- addresses.add(address);
+ @Override
+ public final int compareTo(NotifyConfig o) {
+ return getName().compareTo(o.getName());
}
@Override
- public int compareTo(NotifyConfig o) {
- return name.compareTo(o.name);
+ public final int hashCode() {
+ return getName().hashCode();
}
@Override
- public int hashCode() {
- return name.hashCode();
- }
-
- @Override
- public boolean equals(Object obj) {
+ public final boolean equals(Object obj) {
if (obj instanceof NotifyConfig) {
return compareTo((NotifyConfig) obj) == 0;
}
return false;
}
-
- @Override
- public String toString() {
- return MoreObjects.toStringHelper(this)
- .add("name", name)
- .add("addresses", addresses)
- .add("groups", groups)
- .add("header", header)
- .add("types", types)
- .add("filter", filter)
- .toString();
- }
}
diff --git a/java/com/google/gerrit/server/project/ProjectConfig.java b/java/com/google/gerrit/server/project/ProjectConfig.java
index 3d4b024..35257ef 100644
--- a/java/com/google/gerrit/server/project/ProjectConfig.java
+++ b/java/com/google/gerrit/server/project/ProjectConfig.java
@@ -741,13 +741,13 @@
private void loadNotifySections(Config rc) {
notifySections = new HashMap<>();
for (String sectionName : rc.getSubsections(NOTIFY)) {
- NotifyConfig n = new NotifyConfig();
+ NotifyConfig.Builder n = NotifyConfig.builder();
n.setName(sectionName);
n.setFilter(rc.getString(NOTIFY, sectionName, KEY_FILTER));
EnumSet<NotifyType> types = EnumSet.noneOf(NotifyType.class);
types.addAll(ConfigUtil.getEnumList(rc, NOTIFY, sectionName, KEY_TYPE, NotifyType.ALL));
- n.setTypes(types);
+ n.setNotify(types);
n.setHeader(rc.getEnum(NOTIFY, sectionName, KEY_HEADER, NotifyConfig.Header.BCC));
for (String dst : rc.getStringList(NOTIFY, sectionName, KEY_EMAIL)) {
@@ -758,7 +758,7 @@
ref = groupList.resolve(GroupReference.create(groupName));
}
if (ref.getUUID() != null) {
- n.addEmail(ref);
+ n.addGroup(ref);
} else {
error(
ValidationError.create(
@@ -769,7 +769,7 @@
error(ValidationError.create(PROJECT_CONFIG, dst + " not supported"));
} else {
try {
- n.addEmail(Address.parse(dst));
+ n.addAddress(Address.parse(dst));
} catch (IllegalArgumentException err) {
error(
ValidationError.create(
@@ -778,7 +778,7 @@
}
}
}
- notifySections.put(sectionName, n);
+ notifySections.put(sectionName, n.build());
}
}
diff --git a/javatests/com/google/gerrit/acceptance/server/project/ProjectWatchIT.java b/javatests/com/google/gerrit/acceptance/server/project/ProjectWatchIT.java
index 7a80cbd..fcdea16 100644
--- a/javatests/com/google/gerrit/acceptance/server/project/ProjectWatchIT.java
+++ b/javatests/com/google/gerrit/acceptance/server/project/ProjectWatchIT.java
@@ -50,15 +50,15 @@
@Test
public void newPatchSetsNotifyConfig() throws Exception {
Address addr = Address.create("Watcher", "watcher@example.com");
- NotifyConfig nc = new NotifyConfig();
- nc.addEmail(addr);
+ NotifyConfig.Builder nc = NotifyConfig.builder();
+ nc.addAddress(addr);
nc.setName("new-patch-set");
nc.setHeader(NotifyConfig.Header.CC);
- nc.setTypes(EnumSet.of(NotifyType.NEW_PATCHSETS));
+ nc.setNotify(EnumSet.of(NotifyType.NEW_PATCHSETS));
nc.setFilter("message:sekret");
try (ProjectConfigUpdate u = updateProject(project)) {
- u.getConfig().putNotifyConfig("watch", nc);
+ u.getConfig().putNotifyConfig("watch", nc.build());
u.save();
}
@@ -91,14 +91,14 @@
@Test
public void noNotificationForPrivateChangesForWatchersInNotifyConfig() throws Exception {
Address addr = Address.create("Watcher", "watcher@example.com");
- NotifyConfig nc = new NotifyConfig();
- nc.addEmail(addr);
+ NotifyConfig.Builder nc = NotifyConfig.builder();
+ nc.addAddress(addr);
nc.setName("team");
nc.setHeader(NotifyConfig.Header.TO);
- nc.setTypes(EnumSet.of(NotifyType.NEW_CHANGES, NotifyType.ALL_COMMENTS));
+ nc.setNotify(EnumSet.of(NotifyType.NEW_CHANGES, NotifyType.ALL_COMMENTS));
try (ProjectConfigUpdate u = updateProject(project)) {
- u.getConfig().putNotifyConfig("team", nc);
+ u.getConfig().putNotifyConfig("team", nc.build());
u.save();
}
@@ -123,14 +123,14 @@
public void noNotificationForChangeThatIsTurnedPrivateForWatchersInNotifyConfig()
throws Exception {
Address addr = Address.create("Watcher", "watcher@example.com");
- NotifyConfig nc = new NotifyConfig();
- nc.addEmail(addr);
+ NotifyConfig.Builder nc = NotifyConfig.builder();
+ nc.addAddress(addr);
nc.setName("team");
nc.setHeader(NotifyConfig.Header.TO);
- nc.setTypes(EnumSet.of(NotifyType.NEW_PATCHSETS));
+ nc.setNotify(EnumSet.of(NotifyType.NEW_PATCHSETS));
try (ProjectConfigUpdate u = updateProject(project)) {
- u.getConfig().putNotifyConfig("team", nc);
+ u.getConfig().putNotifyConfig("team", nc.build());
u.save();
}
@@ -152,14 +152,14 @@
@Test
public void noNotificationForWipChangesForWatchersInNotifyConfig() throws Exception {
Address addr = Address.create("Watcher", "watcher@example.com");
- NotifyConfig nc = new NotifyConfig();
- nc.addEmail(addr);
+ NotifyConfig.Builder nc = NotifyConfig.builder();
+ nc.addAddress(addr);
nc.setName("team");
nc.setHeader(NotifyConfig.Header.TO);
- nc.setTypes(EnumSet.of(NotifyType.NEW_CHANGES, NotifyType.ALL_COMMENTS));
+ nc.setNotify(EnumSet.of(NotifyType.NEW_CHANGES, NotifyType.ALL_COMMENTS));
try (ProjectConfigUpdate u = updateProject(project)) {
- u.getConfig().putNotifyConfig("team", nc);
+ u.getConfig().putNotifyConfig("team", nc.build());
u.save();
}
@@ -183,14 +183,14 @@
@Test
public void noNotificationForChangeThatIsTurnedWipForWatchersInNotifyConfig() throws Exception {
Address addr = Address.create("Watcher", "watcher@example.com");
- NotifyConfig nc = new NotifyConfig();
- nc.addEmail(addr);
+ NotifyConfig.Builder nc = NotifyConfig.builder();
+ nc.addAddress(addr);
nc.setName("team");
nc.setHeader(NotifyConfig.Header.TO);
- nc.setTypes(EnumSet.of(NotifyType.NEW_PATCHSETS));
+ nc.setNotify(EnumSet.of(NotifyType.NEW_PATCHSETS));
try (ProjectConfigUpdate u = updateProject(project)) {
- u.getConfig().putNotifyConfig("team", nc);
+ u.getConfig().putNotifyConfig("team", nc.build());
u.save();
}