camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From hadr...@apache.org
Subject git commit: CAMEL-6993. Use varargs in PredicateBuilder
Date Tue, 07 Jan 2014 18:35:19 GMT
Updated Branches:
  refs/heads/camel-2.12.x ab71264d5 -> f69fcbf6e


CAMEL-6993. Use varargs in PredicateBuilder


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/f69fcbf6
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/f69fcbf6
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/f69fcbf6

Branch: refs/heads/camel-2.12.x
Commit: f69fcbf6e01b91817cbab492582f4b35444e3fcb
Parents: ab71264
Author: Hadrian Zbarcea <hzbarcea@gmail.com>
Authored: Tue Jan 7 13:35:02 2014 -0500
Committer: Hadrian Zbarcea <hzbarcea@gmail.com>
Committed: Tue Jan 7 13:35:02 2014 -0500

----------------------------------------------------------------------
 .../apache/camel/builder/PredicateBuilder.java  | 21 +++++++++++++-
 .../camel/builder/PredicateBuilderTest.java     | 29 ++++++++++++++++++++
 2 files changed, 49 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/f69fcbf6/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 3dc5df2..ae9e9c4 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 final class PredicateBuilder {
     }
 
     /**
-     * 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 final class PredicateBuilder {
         };
     }
 
+    /**
+     * 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) {
 
@@ -443,6 +451,17 @@ public final class PredicateBuilder {
     }
 
     /**
+     * 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.
      *
      * @param answer the constant matches

http://git-wip-us.apache.org/repos/asf/camel/blob/f69fcbf6/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 1b244f1..6ada574 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 class PredicateBuilderTest extends TestSupport {
         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));


Mime
View raw message