JAMES-2366 Mapping ordering is in fact a policy
Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/f8c43adc Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/f8c43adc Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/f8c43adc Branch: refs/heads/master Commit: f8c43adc6dc916ab25c4f64a7c22b69ae1942b6b Parents: 9fe1d26 Author: Matthieu Baechler <matth...@apache.org> Authored: Tue Apr 17 14:37:04 2018 +0200 Committer: Matthieu Baechler <matth...@apache.org> Committed: Tue Apr 24 14:45:26 2018 +0200 ---------------------------------------------------------------------- .../java/org/apache/james/rrt/lib/Mapping.java | 20 ++++------ .../org/apache/james/rrt/lib/MappingsImpl.java | 39 ++++++++++++++++++-- 2 files changed, 42 insertions(+), 17 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/f8c43adc/server/data/data-api/src/main/java/org/apache/james/rrt/lib/Mapping.java ---------------------------------------------------------------------- diff --git a/server/data/data-api/src/main/java/org/apache/james/rrt/lib/Mapping.java b/server/data/data-api/src/main/java/org/apache/james/rrt/lib/Mapping.java index 39be80d..d85a6d2 100644 --- a/server/data/data-api/src/main/java/org/apache/james/rrt/lib/Mapping.java +++ b/server/data/data-api/src/main/java/org/apache/james/rrt/lib/Mapping.java @@ -53,19 +53,17 @@ public interface Mapping { Optional<MailAddress> asMailAddress(); enum Type { - Regex("regex:", 4), - Domain("domain:", 1), - Error("error:", 4), - Forward("forward:", 3), - Group("group:", 2), - Address("", 4); + Regex("regex:"), + Domain("domain:"), + Error("error:"), + Forward("forward:"), + Group("group:"), + Address(""); private final String asPrefix; - private final int order; - Type(String asPrefix, Integer order) { + Type(String asPrefix) { this.asPrefix = asPrefix; - this.order = order; } public String asPrefix() { @@ -84,10 +82,6 @@ public interface Mapping { || mapping.startsWith(Forward.asPrefix()) || mapping.startsWith(Group.asPrefix()); } - - public int getOrder() { - return order; - } } Type getType(); http://git-wip-us.apache.org/repos/asf/james-project/blob/f8c43adc/server/data/data-library/src/main/java/org/apache/james/rrt/lib/MappingsImpl.java ---------------------------------------------------------------------- diff --git a/server/data/data-library/src/main/java/org/apache/james/rrt/lib/MappingsImpl.java b/server/data/data-library/src/main/java/org/apache/james/rrt/lib/MappingsImpl.java index ada8ac0..3b56c9e 100644 --- a/server/data/data-library/src/main/java/org/apache/james/rrt/lib/MappingsImpl.java +++ b/server/data/data-library/src/main/java/org/apache/james/rrt/lib/MappingsImpl.java @@ -44,9 +44,40 @@ public class MappingsImpl implements Mappings, Serializable { private static final long serialVersionUID = 1L; - public static final Comparator<Mapping> MAPPING_COMPARATOR = Comparator - .<Mapping, Integer>comparing(mapping -> mapping.getType().getOrder()) - .thenComparing(Mapping::asString); + private static class DefaultMappingOrderingPolicy { + + private static final Comparator<Mapping> MAPPING_COMPARATOR = Comparator + .<Mapping, Integer>comparing(DefaultMappingOrderingPolicy::typeOrder) + .thenComparing(Mapping::asString); + + private static int typeOrder(Mapping mapping) { + return typeOrder(mapping.getType()); + } + + private static int typeOrder(Mapping.Type type) { + switch (type) { + case Domain: + return 1; + case Group: + return 2; + case Forward: + return 3; + case Regex: + return 4; + case Error: + return 4; + case Address: + return 4; + } + throw new IllegalArgumentException("missing enum handling"); + } + + public Comparator<Mapping> comparator() { + return MAPPING_COMPARATOR; + } + } + + public static MappingsImpl empty() { return builder().build(); @@ -121,7 +152,7 @@ public class MappingsImpl implements Mappings, Serializable { public MappingsImpl build() { return new MappingsImpl(mappings.build() .stream() - .sorted(MAPPING_COMPARATOR) + .sorted(new DefaultMappingOrderingPolicy().comparator()) .collect(Guavate.toImmutableList())); } --------------------------------------------------------------------- To unsubscribe, e-mail: server-dev-unsubscr...@james.apache.org For additional commands, e-mail: server-dev-h...@james.apache.org