camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject svn commit: r1441730 - in /camel/trunk/camel-core/src/main/java/org/apache/camel: builder/ExpressionBuilder.java language/simple/ast/SimpleFunctionExpression.java
Date Sat, 02 Feb 2013 10:55:59 GMT
Author: davsclaus
Date: Sat Feb  2 10:55:58 2013
New Revision: 1441730

URL: http://svn.apache.org/viewvc?rev=1441730&view=rev
Log:
CAMEL-6016: Added type function to simple language.

Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/language/simple/ast/SimpleFunctionExpression.java

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java?rev=1441730&r1=1441729&r2=1441730&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java Sat
Feb  2 10:55:58 2013
@@ -26,6 +26,7 @@ import java.util.Date;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Scanner;
+import java.util.concurrent.atomic.AtomicReference;
 import java.util.regex.Pattern;
 
 import org.apache.camel.CamelContext;
@@ -725,6 +726,33 @@ public final class ExpressionBuilder {
     }
 
     /**
+     * Returns an expression that caches the evaluation of another expression
+     * and returns the cached value, to avoid re-evaluating the expression.
+     *
+     * @param expression  the target expression to cache
+     * @return the cached value
+     */
+    public static Expression cacheExpression(final Expression expression) {
+        return new ExpressionAdapter() {
+            private final AtomicReference<Object> cache = new AtomicReference<Object>();
+
+            public Object evaluate(Exchange exchange) {
+                Object answer = cache.get();
+                if (answer == null) {
+                    answer = expression.evaluate(exchange, Object.class);
+                    cache.set(answer);
+                }
+                return answer;
+            }
+
+            @Override
+            public String toString() {
+                return expression.toString();
+            }
+        };
+    }
+
+    /**
      * Returns the expression for the exchanges inbound message body
      */
     public static Expression bodyExpression() {

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/language/simple/ast/SimpleFunctionExpression.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/language/simple/ast/SimpleFunctionExpression.java?rev=1441730&r1=1441729&r2=1441730&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/language/simple/ast/SimpleFunctionExpression.java
(original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/language/simple/ast/SimpleFunctionExpression.java
Sat Feb  2 10:55:58 2013
@@ -178,7 +178,9 @@ public class SimpleFunctionExpression ex
         // const: prefix
         remainder = ifStartsWithReturnRemainder("type:", function);
         if (remainder != null) {
-            return ExpressionBuilder.typeExpression(remainder);
+            Expression exp = ExpressionBuilder.typeExpression(remainder);
+            // we want to cache this expression so we wont re-evaluate it as the type/constant
wont change
+            return ExpressionBuilder.cacheExpression(exp);
         }
 
         if (strict) {



Mime
View raw message