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 eeddfcfc26e641ae8a1429ec042e4aa26d34e899 Author: Matthieu Baechler <[email protected]> AuthorDate: Thu Nov 14 21:52:09 2019 +0100 [Refactoring] implement Or with Stream API --- .../james/mailetcontainer/impl/matchers/Or.java | 29 ++++++++++++---------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/server/mailet/mailetcontainer-camel/src/main/java/org/apache/james/mailetcontainer/impl/matchers/Or.java b/server/mailet/mailetcontainer-camel/src/main/java/org/apache/james/mailetcontainer/impl/matchers/Or.java index 84ddc10..7aab3a5 100644 --- a/server/mailet/mailetcontainer-camel/src/main/java/org/apache/james/mailetcontainer/impl/matchers/Or.java +++ b/server/mailet/mailetcontainer-camel/src/main/java/org/apache/james/mailetcontainer/impl/matchers/Or.java @@ -20,7 +20,9 @@ package org.apache.james.mailetcontainer.impl.matchers; import java.util.Collection; -import java.util.HashSet; +import java.util.Optional; +import java.util.stream.Collectors; +import java.util.stream.Stream; import javax.mail.MessagingException; @@ -28,6 +30,8 @@ import org.apache.james.core.MailAddress; import org.apache.mailet.Mail; import org.apache.mailet.Matcher; +import com.github.fge.lambdas.Throwing; + /** * This is the Or CompositeMatcher - consider it to be a union of the * results. @@ -37,18 +41,17 @@ import org.apache.mailet.Matcher; public class Or extends GenericCompositeMatcher { @Override - public Collection<MailAddress> match(Mail mail) throws MessagingException { - HashSet<MailAddress> result = new HashSet<>(); - for (Matcher matcher : getMatchers()) { - Collection<MailAddress> matchResult = matcher.match(mail); - if (matchResult != null) { - result.addAll(matchResult); - } - if (result.size() == mail.getRecipients().size()) { - break; - } - } - return result; + public Collection<MailAddress> match(Mail mail) { + return getMatchers().stream() + .flatMap(Throwing.<Matcher, Stream<MailAddress>>function(matcher -> applyMatcher(matcher, mail)) + .sneakyThrow()) + .collect(Collectors.toSet()); + } + + private Stream<MailAddress> applyMatcher(Matcher matcher, Mail mail) throws MessagingException { + return Optional.ofNullable(matcher.match(mail)) + .map(Collection::stream) + .orElse(Stream.of()); } } --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
