Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -125,6 +126,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) {
Expand Down Expand Up @@ -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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
Expand Down