Permalink
Browse files

CAMEL-6993. Use varargs in PredicateBuilder

  • Loading branch information...
hzbarcea committed Jan 7, 2014
1 parent 1cc2316 commit bfd4204d4dd4de87593a8c7682ea761dd708c83d
@@ -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");
@@ -126,6 +127,13 @@ public String toString() {
};
}
+ /**
+ * A helper method to return true if any of the predicates matches.
+ */
+ public static Predicate in(List<Predicate> 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<Predicate> 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.
*
@@ -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));

0 comments on commit bfd4204

Please sign in to comment.