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 bt...@apache.org on 2016/09/15 10:08:45 UTC
[04/17] james-project git commit: MAILET-124 Simplify XOR composite
matcher
MAILET-124 Simplify XOR 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/28445fb9
Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/28445fb9
Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/28445fb9
Branch: refs/heads/master
Commit: 28445fb928ea684c2539a2bc06e176d707e3dc79
Parents: beb9cd6
Author: Benoit Tellier <bt...@linagora.com>
Authored: Thu Sep 1 15:21:07 2016 +0700
Committer: Benoit Tellier <bt...@linagora.com>
Committed: Thu Sep 15 11:51:15 2016 +0200
----------------------------------------------------------------------
.../mailetcontainer/impl/matchers/Xor.java | 59 ++++++--------------
1 file changed, 18 insertions(+), 41 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/james-project/blob/28445fb9/server/mailet/mailetcontainer-camel/src/main/java/org/apache/james/mailetcontainer/impl/matchers/Xor.java
----------------------------------------------------------------------
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 849e7ab..c7da294 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
@@ -20,7 +20,6 @@
package org.apache.james.mailetcontainer.impl.matchers;
import java.util.Collection;
-import java.util.Iterator;
import java.util.ArrayList;
import org.apache.mailet.MailAddress;
@@ -28,6 +27,11 @@ import org.apache.mailet.Mail;
import javax.mail.MessagingException;
import org.apache.mailet.Matcher;
+import com.google.common.base.Optional;
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableSet;
+import com.google.common.collect.Sets;
+
public class Xor extends GenericCompositeMatcher {
/**
@@ -40,54 +44,27 @@ public class Xor extends GenericCompositeMatcher {
*/
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) {
- result = new ArrayList<MailAddress>(0);
- }
- // log("Matching with " +
- // matcher.getMatcherConfig().getMatcherName() +
- // " result="+result.toString() );
+ for (Matcher matcher: getMatchers()) {
+ Collection<MailAddress> matchedAddresses = Optional.fromNullable(matcher.match(mail)).or(new ArrayList<MailAddress>());
if (first) {
- finalResult = result;
+ finalResult = matchedAddresses;
first = false;
} else {
- // Check if we need to Xor ...
- // if the finalResult and the subsequent result are the same
- // collection, then it contains the same recipients
- // so we can short-circuit building the XOR and return an empty
- // set
- if (finalResult == result) {
- // the XOR of the same collection is empty
- finalResult.clear();
- // log("same collection - so clear");
- } else {
- // the two results are different collections, so we XOR them
- // Ensure that the finalResult does not contain recipients
- // in the result collection
- MailAddress recipient;
- for (Object aResult : result) {
- recipient = (MailAddress) (aResult);
- if (!finalResult.contains(recipient)) {
- finalResult.add(recipient);
- } else {
- finalResult.remove(recipient);
- }
- }
- recipient = null;
- // log("xor recipients into new finalResult="+finalResult);
- }
- // basically the finalResult gets replaced with a smaller result
- // otherwise finalResult would have been equal to result (in all
- // cases)
+ finalResult = performXor(finalResult, matchedAddresses);
}
- result = null;
}
return finalResult;
}
+ private Collection<MailAddress> performXor(Collection<MailAddress> collection1, Collection<MailAddress> collection2) {
+ ImmutableSet<MailAddress> set1 = ImmutableSet.copyOf(collection1);
+ ImmutableSet<MailAddress> set2 = ImmutableSet.copyOf(collection2);
+ return Sets.difference(
+ Sets.union(set1, set2),
+ Sets.intersection(set1, set2))
+ .immutableCopy();
+ }
+
}
---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org