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 3dc5df265820d..37f3af6a8c1da 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 @@ -86,7 +86,8 @@ public String toString() { } /** - * A helper method to combine multiple predicates by a logical OR + * A helper method to combine two predicates by a logical OR. + * If you want to combine multiple predicates see {@link #in(Predicate...)} */ public static Predicate or(final Predicate left, final Predicate right) { notNull(left, "left"); @@ -125,6 +126,13 @@ public String toString() { } }; } + + /** + * A helper method to return true if any of the predicates matches. + */ + public static Predicate in(List predicates) { + return in(predicates.toArray(new Predicate[0])); + } public static Predicate isEqualTo(final Expression left, final Expression right) { return new BinaryPredicateSupport(left, right) { @@ -442,6 +450,17 @@ public static Predicate and(List predicates) { return answer; } + /** + * Concat the given predicates into a single predicate, which only matches + * if all the predicates matches. + * + * @param predicates predicates + * @return a single predicate containing all the predicates + */ + public static Predicate and(Predicate... predicates) { + return and(Arrays.asList(predicates)); + } + /** * A constant predicate. * 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 1b244f113a18a..6ada574ce6d49 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 @@ -16,12 +16,16 @@ */ package org.apache.camel.builder; + +import java.util.Arrays; + import org.apache.camel.Exchange; import org.apache.camel.Message; import org.apache.camel.Predicate; import org.apache.camel.TestSupport; import org.apache.camel.impl.DefaultCamelContext; import org.apache.camel.impl.DefaultExchange; + import static org.apache.camel.builder.Builder.constant; import static org.apache.camel.builder.PredicateBuilder.in; import static org.apache.camel.builder.PredicateBuilder.not; @@ -67,6 +71,31 @@ public void testCompoundAndPredicates() throws Exception { assertMatches(and); } + public void testCompoundAndPredicatesVarargs() throws Exception { + Predicate p1 = header("name").isEqualTo(constant("James")); + Predicate p2 = header("size").isGreaterThanOrEqualTo(constant(10)); + Predicate p3 = header("location").contains(constant("London")); + Predicate and = PredicateBuilder.and(p1, p2, p3); + + assertMatches(and); + } + + public void testOrSignatures() throws Exception { + Predicate p1 = header("name").isEqualTo(constant("does-not-apply")); + Predicate p2 = header("size").isGreaterThanOrEqualTo(constant(10)); + Predicate p3 = header("location").contains(constant("does-not-apply")); + + // check method signature with two parameters + assertMatches(PredicateBuilder.or(p1, p2)); + assertMatches(PredicateBuilder.or(p2, p3)); + + // check method signature with varargs + assertMatches(PredicateBuilder.in(p1, p2, p3)); + + // maybe a list of predicates? + assertMatches(PredicateBuilder.in(Arrays.asList(p1, p2, p3))); + } + public void testCompoundAndOrPredicates() throws Exception { Predicate p1 = header("name").isEqualTo(constant("Hiram")); Predicate p2 = header("size").isGreaterThan(constant(100));