essobedo commented on code in PR #9332:
URL: https://github.com/apache/camel/pull/9332#discussion_r1109946937


##########
core/camel-support/src/main/java/org/apache/camel/support/builder/ExpressionBuilder.java:
##########
@@ -1577,74 +1578,122 @@ public String toString() {
      * Returns an expression which returns the string concatenation value of 
the various
      * expressions
      *
+     * @param context the Camel context
      * @param expressions the expression to be concatenated dynamically
      * @return an expression which when evaluated will return the concatenated 
values
      */
-    public static Expression concatExpression(final Collection<Expression> 
expressions) {
-        return concatExpression(expressions, null);
+    public static Expression concatExpression(final CamelContext context, 
final Collection<Expression> expressions) {
+        return concatExpression(context, expressions, null);
     }
 
     /**
      * Returns an expression which returns the string concatenation value of 
the various
      * expressions
      *
+     * @param context the Camel context
      * @param expressions the expression to be concatenated dynamically
      * @param description the text description of the expression
      * @return an expression which when evaluated will return the concatenated 
values
      */
-    public static Expression concatExpression(final Collection<Expression> 
expressions, final String description) {
-        return new ExpressionAdapter() {
+    public static Expression concatExpression(final CamelContext context, 
final Collection<Expression> expressions, final String description) {
+
+        for (Expression expression : expressions) {
+            if(expression instanceof ConstantExpressionAdapter){
+                return concatExpressionOptimized(context, expressions, 
description);
+            }
+        }
 
-            private Collection<Object> col;
+        return concatExpression(expressions,description);
+    }
+
+    /**
+     * Returns an expression which returns the string concatenation value of 
the various
+     * expressions
+     *
+     * @param expressions the expression to be concatenated dynamically
+     * @param description the text description of the expression
+     * @return an expression which when evaluated will return the concatenated 
values
+     */
+    private static Expression concatExpression(final Collection<Expression> 
expressions, final String description) {
+        return new ExpressionAdapter() {
 
             @Override
             public Object evaluate(Exchange exchange) {
                 StringBuilder buffer = new StringBuilder();
-                if (col != null) {
-                    // optimize for constant expressions so we can do this a 
bit faster
-                    for (Object obj : col) {
-                        if (obj instanceof Expression) {
-                            Expression expression = (Expression) obj;
-                            String text = expression.evaluate(exchange, 
String.class);
-                            if (text != null) {
-                                buffer.append(text);
-                            }
-                        } else {
-                            buffer.append((String) obj);
-                        }
+                for (Expression expression : expressions) {
+                    String text = expression.evaluate(exchange, String.class);
+                    if (text != null) {
+                        buffer.append(text);
                     }
+                }
+                return buffer.toString();
+            }
+
+            @Override
+            public void init(CamelContext context) {
+                boolean constant = false;
+                for (Expression expression : expressions) {
+                    expression.init(context);
+                }
+            }
+
+            @Override
+            public String toString() {
+                if (description != null) {
+                    return description;
                 } else {
-                    for (Expression expression : expressions) {
+                    return "concat(" + expressions + ")";
+                }
+            }
+        };
+    }
+
+    /**
+     * Returns an optimized expression which returns the string concatenation 
value of the various.
+     * expressions
+     *
+     * @param context the Camel context
+     * @param expressions the expression to be concatenated dynamically
+     * @param description the text description of the expression
+     * @return an expression which when evaluated will return the concatenated 
values
+     */
+    private static Expression concatExpressionOptimized(final CamelContext 
context, final Collection<Expression> expressions, final String description) {
+        Collection<Object> preprocessedExpression = new 
ArrayList<>(expressions.size());
+        for (Expression expression : expressions) {
+            if (expression instanceof ConstantExpressionAdapter) {
+                expression.init(context);

Review Comment:
   > About the lock, the problem is the `evaluate` method being called 
concurrently with the `init`calling. Thus, I think the lock should be put at 
the `evaluate` method as well. However, we came back to the penalty we said 
before.
   
   To be thread-safe, we have to consider the state changes and the visibility 
of those changes. Here only the `init` method affects the state and the `init` 
method is always called before `evaluate` so applying a double-check locking 
approach in the `init` method is good enough as it ensures that the state is 
changed only once at the very first call and that all threads thanks to a 
volatile read or synchronized block will always have the right state before 
calling `evaluate`. I hope it is clear enough.
   
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscr...@camel.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to