MAILBOX-307 Factorise code for EnumSet copies in MailboxACL
Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/f5a57cb0 Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/f5a57cb0 Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/f5a57cb0 Branch: refs/heads/master Commit: f5a57cb0d4385ac5674bdb78f4ac64570632dfc6 Parents: 6bf7a9f Author: benwa <btell...@linagora.com> Authored: Wed Sep 27 10:41:29 2017 +0700 Committer: Matthieu Baechler <matth...@apache.org> Committed: Fri Sep 29 09:20:40 2017 +0200 ---------------------------------------------------------------------- .../apache/james/mailbox/model/MailboxACL.java | 33 ++++++++++---------- 1 file changed, 16 insertions(+), 17 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/f5a57cb0/mailbox/api/src/main/java/org/apache/james/mailbox/model/MailboxACL.java ---------------------------------------------------------------------- diff --git a/mailbox/api/src/main/java/org/apache/james/mailbox/model/MailboxACL.java b/mailbox/api/src/main/java/org/apache/james/mailbox/model/MailboxACL.java index cfbcff0..64180fb 100644 --- a/mailbox/api/src/main/java/org/apache/james/mailbox/model/MailboxACL.java +++ b/mailbox/api/src/main/java/org/apache/james/mailbox/model/MailboxACL.java @@ -21,6 +21,7 @@ package org.apache.james.mailbox.model; import java.util.AbstractMap; import java.util.Arrays; +import java.util.Collection; import java.util.EnumSet; import java.util.Iterator; import java.util.List; @@ -51,6 +52,13 @@ import com.google.common.collect.ImmutableMap; */ public class MailboxACL { + private static EnumSet<Right> copyOf(Collection<Right> collection) { + if (collection.isEmpty()) { + return EnumSet.noneOf(Right.class); + } + return EnumSet.copyOf(collection); + } + /** * SETACL command mode. */ @@ -176,7 +184,7 @@ public class MailboxACL { private final EnumSet<Right> value; private Rfc4314Rights(EnumSet<Right> rights) { - this.value = EnumSet.copyOf(rights); + this.value = copyOf(rights); } private Rfc4314Rights() { @@ -184,7 +192,7 @@ public class MailboxACL { } public Rfc4314Rights(Right... rights) { - this(EnumSet.copyOf(Arrays.asList(rights))); + this(copyOf(Arrays.asList(rights))); } public Rfc4314Rights(Right right) throws UnsupportedRightException { @@ -193,26 +201,17 @@ public class MailboxACL { /* Used for json serialization (probably a bad idea) */ public Rfc4314Rights(int serializedRights) { - List<Right> rights = Right.allRights.stream() + this(copyOf(Right.allRights + .stream() .filter(right -> ((serializedRights >> right.ordinal()) & 1) != 0) - .collect(Collectors.toList()); - if (rights.isEmpty()) { - this.value = EnumSet.noneOf(Right.class); - } else { - this.value = EnumSet.copyOf(rights); - } + .collect(Collectors.toList()))); } public Rfc4314Rights(String serializedRfc4314Rights) throws UnsupportedRightException { - List<Right> rights = serializedRfc4314Rights.chars() + this.value = copyOf(serializedRfc4314Rights.chars() .mapToObj(i -> (char) i) .flatMap(Throwing.function(this::convert).sneakyThrow()) - .collect(Collectors.toList()); - if (rights.isEmpty()) { - this.value = EnumSet.noneOf(Right.class); - } else { - this.value = EnumSet.copyOf(rights); - } + .collect(Collectors.toList())); } private Stream<Right> convert(char flag) throws UnsupportedRightException { @@ -292,7 +291,7 @@ public class MailboxACL { * @throws UnsupportedRightException */ public Rfc4314Rights except(Rfc4314Rights toRemove) throws UnsupportedRightException { - EnumSet<Right> copy = EnumSet.copyOf(value); + EnumSet<Right> copy = copyOf(value); copy.removeAll(convertRightsToList(toRemove)); return new Rfc4314Rights(copy); } --------------------------------------------------------------------- To unsubscribe, e-mail: server-dev-unsubscr...@james.apache.org For additional commands, e-mail: server-dev-h...@james.apache.org