You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by rc...@apache.org on 2019/12/02 02:26:56 UTC

[james-project] 04/05: [Refactoring] implement Xor with reduce

This is an automated email from the ASF dual-hosted git repository.

rcordier pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/james-project.git

commit 2cad541df37926701edbf91231e00b2708d5d9f1
Author: Matthieu Baechler <ma...@apache.org>
AuthorDate: Thu Nov 14 21:42:48 2019 +0100

    [Refactoring] implement Xor with reduce
---
 .../james/mailetcontainer/impl/matchers/Xor.java   | 35 +++++++++++-----------
 1 file changed, 17 insertions(+), 18 deletions(-)

diff --git a/server/mailet/mailetcontainer-camel/src/main/java/org/apache/james/mailetcontainer/impl/matchers/Xor.java b/server/mailet/mailetcontainer-camel/src/main/java/org/apache/james/mailetcontainer/impl/matchers/Xor.java
index c7c0fa9..d2606a7 100644
--- a/server/mailet/mailetcontainer-camel/src/main/java/org/apache/james/mailetcontainer/impl/matchers/Xor.java
+++ b/server/mailet/mailetcontainer-camel/src/main/java/org/apache/james/mailetcontainer/impl/matchers/Xor.java
@@ -19,9 +19,9 @@
 
 package org.apache.james.mailetcontainer.impl.matchers;
 
-import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Optional;
+import java.util.Set;
 
 import javax.mail.MessagingException;
 
@@ -29,6 +29,7 @@ import org.apache.james.core.MailAddress;
 import org.apache.mailet.Mail;
 import org.apache.mailet.Matcher;
 
+import com.github.fge.lambdas.Throwing;
 import com.google.common.collect.ImmutableSet;
 import com.google.common.collect.Sets;
 
@@ -42,29 +43,27 @@ import com.google.common.collect.Sets;
 public class Xor extends GenericCompositeMatcher {
 
     @Override
-    public Collection<MailAddress> match(Mail mail) throws MessagingException {
-        Collection<MailAddress> finalResult = null;
-        boolean first = true;
-        for (Matcher matcher: getMatchers()) {
-            Collection<MailAddress> matchedAddresses = Optional.ofNullable(matcher.match(mail)).orElse(new ArrayList<>());
+    public Collection<MailAddress> match(Mail mail) {
+        Set<MailAddress> recipients = getMatchers().stream()
+            .map(Throwing.<Matcher, Set<MailAddress>>function(matcher -> applyMatcher(mail, matcher)).sneakyThrow())
+            .reduce(ImmutableSet.of(), this::performXor);
 
-            if (first) {
-                finalResult = matchedAddresses;
-                first = false;
-            } else {
-                finalResult = performXor(finalResult, matchedAddresses);
-            }
+        if (recipients.isEmpty()) {
+            return null;
         }
-        return finalResult;
+        return recipients;
     }
 
-    private Collection<MailAddress> performXor(Collection<MailAddress> collection1, Collection<MailAddress> collection2) {
-        ImmutableSet<MailAddress> set1 = ImmutableSet.copyOf(collection1);
-        ImmutableSet<MailAddress> set2 = ImmutableSet.copyOf(collection2);
+    private Set<MailAddress> applyMatcher(Mail mail, Matcher matcher) throws MessagingException {
+        return Optional.ofNullable(matcher.match(mail))
+            .map(ImmutableSet::copyOf)
+            .orElse(ImmutableSet.of());
+    }
+
+    private Set<MailAddress> performXor(Set<MailAddress> set1, Set<MailAddress> set2) {
         return Sets.difference(
             Sets.union(set1, set2),
-            Sets.intersection(set1, set2))
-            .immutableCopy();
+            Sets.intersection(set1, set2));
     }
 
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org