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


##########
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:
   Hi @essobedo, Looking at the `ConstantExpression`, I think that calling the 
`init` so early is not a problem. Waiting for the @oscerd to confirm it for us. 
   
   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.



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to