camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject [1/2] git commit: CAMEL-6639: Added statis methods to simple language for predicate vs expression.
Date Thu, 15 Aug 2013 11:22:54 GMT
Updated Branches:
  refs/heads/master 2368ed213 -> 903327e8f


CAMEL-6639: Added statis methods to simple language for predicate vs expression.


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

Branch: refs/heads/master
Commit: 903327e8f0d3c0c0f1eba24ab3f19f8f91dbe652
Parents: 90c30f8
Author: Claus Ibsen <davsclaus@apache.org>
Authored: Thu Aug 15 12:15:09 2013 +0200
Committer: Claus Ibsen <davsclaus@apache.org>
Committed: Thu Aug 15 13:22:42 2013 +0200

----------------------------------------------------------------------
 .../camel/language/simple/SimpleLanguage.java   | 38 +++++++++++++++-
 .../util/PredicateToExpressionAdapter.java      | 48 ++++++++++++++++++++
 .../camel/language/simple/SimpleTest.java       | 26 +++++++++++
 3 files changed, 110 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/903327e8/camel-core/src/main/java/org/apache/camel/language/simple/SimpleLanguage.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/language/simple/SimpleLanguage.java
b/camel-core/src/main/java/org/apache/camel/language/simple/SimpleLanguage.java
index ff0a9e2..76db93a 100644
--- a/camel-core/src/main/java/org/apache/camel/language/simple/SimpleLanguage.java
+++ b/camel-core/src/main/java/org/apache/camel/language/simple/SimpleLanguage.java
@@ -21,6 +21,7 @@ import org.apache.camel.Predicate;
 import org.apache.camel.builder.ExpressionBuilder;
 import org.apache.camel.support.LanguageSupport;
 import org.apache.camel.util.ObjectHelper;
+import org.apache.camel.util.PredicateToExpressionAdapter;
 
 /**
  * A <a href="http://camel.apache.org/simple.html">simple language</a>
@@ -158,14 +159,47 @@ public class SimpleLanguage extends LanguageSupport {
         return answer;
     }
 
+    /**
+     * Creates a new {@link Expression}.
+     * <p/>
+     * <b>Important:</b> If you need to use a predicate (function to return true|false)
then use
+     * {@link #predicate(String)} instead.
+     */
     public static Expression simple(String expression) {
-        return SIMPLE.createExpression(expression);
+        return expression(expression);
     }
 
+    /**
+     * Creates a new {@link Expression} (or {@link Predicate}
+     * if the resultType is a <tt>Boolean</tt>, or <tt>boolean</tt>
type).
+     */
     public static Expression simple(String expression, Class<?> resultType) {
         SimpleLanguage answer = new SimpleLanguage();
         answer.setResultType(resultType);
-        return answer.createExpression(expression);
+        if (resultType == Boolean.class || resultType == boolean.class) {
+            // if its a boolean as result then its a predicate
+            Predicate predicate = answer.createPredicate(expression);
+            return PredicateToExpressionAdapter.toExpression(predicate);
+        } else {
+            return answer.createExpression(expression);
+        }
+    }
+
+    /**
+     * Creates a new {@link Expression}.
+     * <p/>
+     * <b>Important:</b> If you need to use a predicate (function to return true|false)
then use
+     * {@link #predicate(String)} instead.
+     */
+    public static Expression expression(String expression) {
+        return SIMPLE.createExpression(expression);
+    }
+
+    /**
+     * Creates a new {@link Predicate}.
+     */
+    public static Predicate predicate(String predicate) {
+        return SIMPLE.createPredicate(predicate);
     }
 
     /**

http://git-wip-us.apache.org/repos/asf/camel/blob/903327e8/camel-core/src/main/java/org/apache/camel/util/PredicateToExpressionAdapter.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/util/PredicateToExpressionAdapter.java
b/camel-core/src/main/java/org/apache/camel/util/PredicateToExpressionAdapter.java
new file mode 100644
index 0000000..8f76abf
--- /dev/null
+++ b/camel-core/src/main/java/org/apache/camel/util/PredicateToExpressionAdapter.java
@@ -0,0 +1,48 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.util;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.Expression;
+import org.apache.camel.Predicate;
+
+public final class PredicateToExpressionAdapter implements Expression {
+    private final Predicate predicate;
+
+    public PredicateToExpressionAdapter(Predicate predicate) {
+        this.predicate = predicate;
+    }
+
+    @Override
+    public <T> T evaluate(Exchange exchange, Class<T> type) {
+        boolean matches = predicate.matches(exchange);
+        return exchange.getContext().getTypeConverter().convertTo(type, exchange, matches);
+    }
+
+    @Override
+    public String toString() {
+        return predicate.toString();
+    }
+
+    /**
+     * Converts the given predicate into an {@link org.apache.camel.Expression}
+     */
+    public static Expression toExpression(final Predicate predicate) {
+        return new PredicateToExpressionAdapter(predicate);
+    }
+    
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/camel/blob/903327e8/camel-core/src/test/java/org/apache/camel/language/simple/SimpleTest.java
----------------------------------------------------------------------
diff --git a/camel-core/src/test/java/org/apache/camel/language/simple/SimpleTest.java b/camel-core/src/test/java/org/apache/camel/language/simple/SimpleTest.java
index e66de80..7eca1b5 100644
--- a/camel-core/src/test/java/org/apache/camel/language/simple/SimpleTest.java
+++ b/camel-core/src/test/java/org/apache/camel/language/simple/SimpleTest.java
@@ -32,6 +32,7 @@ import org.apache.camel.Expression;
 import org.apache.camel.ExpressionIllegalSyntaxException;
 import org.apache.camel.InvalidPayloadException;
 import org.apache.camel.LanguageTestSupport;
+import org.apache.camel.Predicate;
 import org.apache.camel.component.bean.MethodNotFoundException;
 import org.apache.camel.impl.JndiRegistry;
 import org.apache.camel.language.bean.RuntimeBeanExpressionException;
@@ -51,6 +52,31 @@ public class SimpleTest extends LanguageTestSupport {
         return jndi;
     }
 
+    public void testSimpleExpressionOrPredicate() throws Exception {
+        Predicate predicate = SimpleLanguage.predicate("${header.bar} == 123");
+        assertTrue(predicate.matches(exchange));
+
+        predicate = SimpleLanguage.predicate("${header.bar} == 124");
+        assertFalse(predicate.matches(exchange));
+
+        Expression expression = SimpleLanguage.expression("${body}");
+        assertEquals("<hello id='m123'>world!</hello>", expression.evaluate(exchange,
String.class));
+
+        expression = SimpleLanguage.simple("${body}");
+        assertEquals("<hello id='m123'>world!</hello>", expression.evaluate(exchange,
String.class));
+        expression = SimpleLanguage.simple("${body}", String.class);
+        assertEquals("<hello id='m123'>world!</hello>", expression.evaluate(exchange,
String.class));
+
+        expression = SimpleLanguage.simple("${header.bar} == 123", boolean.class);
+        assertEquals(Boolean.TRUE, expression.evaluate(exchange, Object.class));
+        expression = SimpleLanguage.simple("${header.bar} == 124", boolean.class);
+        assertEquals(Boolean.FALSE, expression.evaluate(exchange, Object.class));
+        expression = SimpleLanguage.simple("${header.bar} == 123", Boolean.class);
+        assertEquals(Boolean.TRUE, expression.evaluate(exchange, Object.class));
+        expression = SimpleLanguage.simple("${header.bar} == 124", Boolean.class);
+        assertEquals(Boolean.FALSE, expression.evaluate(exchange, Object.class));
+    }
+
     public void testResultType() throws Exception {
         assertEquals(123, SimpleLanguage.simple("${header.bar}", int.class).evaluate(exchange,
Object.class));
         assertEquals("123", SimpleLanguage.simple("${header.bar}", String.class).evaluate(exchange,
Object.class));


Mime
View raw message