CAMEL-8692: Improve the PredicateBuilder with the missing vargs and list or helper method
Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/6c246722 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/6c246722 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/6c246722 Branch: refs/heads/master Commit: 6c2467222ce47d82323b34afb9434bb41eb5a46e Parents: 8bcd609 Author: Christian Mueller <cmuel...@apache.org> Authored: Thu Apr 23 21:40:17 2015 +0200 Committer: Christian Mueller <cmuel...@apache.org> Committed: Thu Apr 23 21:40:17 2015 +0200 ---------------------------------------------------------------------- .../apache/camel/builder/PredicateBuilder.java | 30 ++++++++++++++++++++ .../camel/builder/PredicateBuilderTest.java | 6 ++-- 2 files changed, 33 insertions(+), 3 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/6c246722/camel-core/src/main/java/org/apache/camel/builder/PredicateBuilder.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/builder/PredicateBuilder.java b/camel-core/src/main/java/org/apache/camel/builder/PredicateBuilder.java index ae9e9c4..c2eb5c9 100644 --- a/camel-core/src/main/java/org/apache/camel/builder/PredicateBuilder.java +++ b/camel-core/src/main/java/org/apache/camel/builder/PredicateBuilder.java @@ -105,6 +105,36 @@ public final class PredicateBuilder { } /** + * Concat the given predicates into a single predicate, which matches + * if at least one predicates matches. + * + * @param predicates predicates + * @return a single predicate containing all the predicates + */ + public static Predicate or(List<Predicate> predicates) { + Predicate answer = null; + for (Predicate predicate : predicates) { + if (answer == null) { + answer = predicate; + } else { + answer = or(answer, predicate); + } + } + return answer; + } + + /** + * Concat the given predicates into a single predicate, which matches + * if at least one predicates matches. + * + * @param predicates predicates + * @return a single predicate containing all the predicates + */ + public static Predicate or(Predicate... predicates) { + return or(Arrays.asList(predicates)); + } + + /** * A helper method to return true if any of the predicates matches. */ public static Predicate in(final Predicate... predicates) { http://git-wip-us.apache.org/repos/asf/camel/blob/6c246722/camel-core/src/test/java/org/apache/camel/builder/PredicateBuilderTest.java ---------------------------------------------------------------------- diff --git a/camel-core/src/test/java/org/apache/camel/builder/PredicateBuilderTest.java b/camel-core/src/test/java/org/apache/camel/builder/PredicateBuilderTest.java index 6ada574..b1c0338 100644 --- a/camel-core/src/test/java/org/apache/camel/builder/PredicateBuilderTest.java +++ b/camel-core/src/test/java/org/apache/camel/builder/PredicateBuilderTest.java @@ -30,10 +30,8 @@ import static org.apache.camel.builder.Builder.constant; import static org.apache.camel.builder.PredicateBuilder.in; import static org.apache.camel.builder.PredicateBuilder.not; -/** - * @version - */ public class PredicateBuilderTest extends TestSupport { + protected Exchange exchange = new DefaultExchange(new DefaultCamelContext()); public void testRegexPredicates() throws Exception { @@ -91,9 +89,11 @@ public class PredicateBuilderTest extends TestSupport { // check method signature with varargs assertMatches(PredicateBuilder.in(p1, p2, p3)); + assertMatches(PredicateBuilder.or(p1, p2, p3)); // maybe a list of predicates? assertMatches(PredicateBuilder.in(Arrays.asList(p1, p2, p3))); + assertMatches(PredicateBuilder.or(Arrays.asList(p1, p2, p3))); } public void testCompoundAndOrPredicates() throws Exception {