james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From btell...@apache.org
Subject [05/17] james-project git commit: MAILET-124 Simplify AND composite matcher
Date Thu, 15 Sep 2016 10:08:46 GMT
MAILET-124 Simplify AND composite matcher


Project: http://git-wip-us.apache.org/repos/asf/james-project/repo
Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/12df8f9c
Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/12df8f9c
Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/12df8f9c

Branch: refs/heads/master
Commit: 12df8f9cc3f12d604dc7e82b1058f8649075536b
Parents: 28445fb
Author: Benoit Tellier <btellier@linagora.com>
Authored: Thu Sep 1 15:21:50 2016 +0700
Committer: Benoit Tellier <btellier@linagora.com>
Committed: Thu Sep 15 11:51:19 2016 +0200

----------------------------------------------------------------------
 .../mailetcontainer/impl/matchers/And.java      | 87 +++++++-------------
 1 file changed, 32 insertions(+), 55 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/12df8f9c/server/mailet/mailetcontainer-camel/src/main/java/org/apache/james/mailetcontainer/impl/matchers/And.java
----------------------------------------------------------------------
diff --git a/server/mailet/mailetcontainer-camel/src/main/java/org/apache/james/mailetcontainer/impl/matchers/And.java
b/server/mailet/mailetcontainer-camel/src/main/java/org/apache/james/mailetcontainer/impl/matchers/And.java
index 931f01c..9738495 100644
--- a/server/mailet/mailetcontainer-camel/src/main/java/org/apache/james/mailetcontainer/impl/matchers/And.java
+++ b/server/mailet/mailetcontainer-camel/src/main/java/org/apache/james/mailetcontainer/impl/matchers/And.java
@@ -20,14 +20,18 @@ package org.apache.james.mailetcontainer.impl.matchers;
  ****************************************************************/
 
 import java.util.Collection;
-import java.util.Iterator;
-import java.util.ArrayList;
+import java.util.List;
+import java.util.Set;
 
 import org.apache.mailet.MailAddress;
 import org.apache.mailet.Mail;
 import javax.mail.MessagingException;
 import org.apache.mailet.Matcher;
 
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableSet;
+import com.google.common.collect.Sets;
+
 /**
  * This matcher performs And conjunction between the two recipients
  */
@@ -41,64 +45,37 @@ public class And extends GenericCompositeMatcher {
      * @return Collection of Recipient from the And composition results of the
      *         child Matchers.
      */
-    public Collection<MailAddress> match(Mail mail) throws MessagingException {
-        Collection<MailAddress> finalResult = null;
-        Matcher matcher;
-        boolean first = true;
-        for (Iterator<Matcher> matcherIter = iterator(); matcherIter.hasNext();) {
-            matcher = (matcherIter.next());
-            Collection<MailAddress> result = matcher.match(mail);
-
-            if (result == null) {
-                // short-circuit
-                // log("Matching with " +
-                // matcher.getMatcherConfig().getMatcherName() +
-                // " result.size()=0");
-                return new ArrayList<MailAddress>(0);
-            }
-            if (result.size() == 0) {
-                return result;
-            }
+    public Collection<MailAddress> match(final Mail mail) throws MessagingException
{
+        List<Set<MailAddress>> individualMatchedResults = performMatchOnMatchers(mail);
+        return computeIntersection(individualMatchedResults);
+    }
 
-            // log("Matching with " +
-            // matcher.getMatcherConfig().getMatcherName() +
-            // " result.size()="+result.size());
+    private Collection<MailAddress> computeIntersection(List<Set<MailAddress>>
individualMatchedResults) {
+        if (individualMatchedResults.size() == 0) {
+            return null;
+        }
+        if (individualMatchedResults.size() == 1) {
+            return individualMatchedResults.get(0);
+        }
+        Set<MailAddress> temporaryResult = ImmutableSet.copyOf(individualMatchedResults.get(0));
+        List<Set<MailAddress>> followingResults = individualMatchedResults.subList(1,
individualMatchedResults.size());
+        for(Set<MailAddress> matchedAddresses: followingResults) {
+            temporaryResult = Sets.intersection(temporaryResult, matchedAddresses);
+        }
+        return temporaryResult;
+    }
 
-            if (first) {
-                finalResult = result;
-                first = false;
+    private List<Set<MailAddress>> performMatchOnMatchers(Mail mail) throws MessagingException
{
+        ImmutableList.Builder<Set<MailAddress>> builder = ImmutableList.builder();
+        for (Matcher matcher : getMatchers()) {
+            Collection<MailAddress> matchedMailAddress = matcher.match(mail);
+            if (matchedMailAddress != null && !matchedMailAddress.isEmpty()) {
+                builder.add(ImmutableSet.copyOf(matchedMailAddress));
             } else {
-                // Check if we need to And ...
-                // if the finalResult and the subsequent result are the same
-                // collection, then it contains the same recipients
-                // so we can short-circuit building the AND of the two
-                if (finalResult != result) {
-                    // the two results are different collections, so we AND
-                    // them
-                    // Ensure that the finalResult only contains recipients
-                    // in the result collection
-                    Collection<MailAddress> newResult = new ArrayList<MailAddress>();
-                    MailAddress recipient;
-                    for (Object aFinalResult : finalResult) {
-                        recipient = (MailAddress) aFinalResult;
-                        // log("recipient="+recipient.toString());
-                        if (result.contains(recipient)) {
-                            newResult.add(recipient);
-                        }
-                    }
-                    recipient = null;
-                    // basically the finalResult gets replaced with a
-                    // smaller result
-                    // otherwise finalResult would have been equal to result
-                    // (in all cases)
-                    finalResult = newResult;
-                }
+                return ImmutableList.of();
             }
-            result = null;
         }
-        matcher = null;
-        // log("answer is "+finalResult.toString());
-        return finalResult;
+        return builder.build();
     }
 
 }


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


Mime
View raw message