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


##########
core/camel-support/src/main/java/org/apache/camel/support/builder/ExpressionBuilder.java:
##########
@@ -1593,59 +1594,109 @@ public static Expression concatExpression(final 
Collection<Expression> expressio
      * @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() {
 
-            private Collection<Object> col;
+        for (Expression expression : expressions) {
+            if(expression instanceof ConstantExpressionAdapter){
+                return concatExpressionOptimized(expressions, description);
+            }
+        }
+
+        return concatExpressionUnoptimized(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 concatExpressionUnoptimized(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) {
+                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 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 
Collection<Expression> expressions, final String description) {
+
+
+        return new ExpressionAdapter() {
+
+            private Collection<Object> col;
+
+            @Override
+            public Object evaluate(Exchange exchange) {
+                StringBuilder buffer = new StringBuilder();
+                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);
                     }
                 }
                 return buffer.toString();
             }
 
             @Override
             public void init(CamelContext context) {
-                boolean constant = false;
                 for (Expression expression : expressions) {
                     expression.init(context);
-                    constant |= expression instanceof 
ConstantExpressionAdapter;
                 }
-                if (constant) {
-                    // okay some of the expressions are constant so we can 
optimize and avoid
-                    // evaluate them but use their constant value as-is 
directly
-                    // this can be common with the simple language where you 
use it for templating
-                    // by mixing string text and simple functions together (or 
via the log EIP)
-                    col = new ArrayList<>(expressions.size());
+
+                if(col == null) {
+                    Collection<Object> preprocessedExpression = new 
ArrayList<>(expressions.size());
                     for (Expression expression : expressions) {
                         if (expression instanceof ConstantExpressionAdapter) {
+                            expression.init(context);

Review Comment:
   My fault. I'm sorry. Fixed. 



-- 
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