This is an automated email from the ASF dual-hosted git repository.

orpiske pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/main by this push:
     new 24d1d50295f CAMEL-20931: cleanup duplicated code
24d1d50295f is described below

commit 24d1d50295f5a02c973d7df9b0f62abdc0289206
Author: Otavio Rodolfo Piske <angusyo...@gmail.com>
AuthorDate: Mon Jul 1 10:36:39 2024 +0200

    CAMEL-20931: cleanup duplicated code
---
 .../camel/language/csimple/CSimpleHelper.java      | 26 +++++---------------
 .../camel/support/builder/ExpressionBuilder.java   | 20 +++-------------
 .../java/org/apache/camel/util/StringHelper.java   | 28 ++++++++++++++++++++++
 3 files changed, 37 insertions(+), 37 deletions(-)

diff --git 
a/core/camel-core-languages/src/main/java/org/apache/camel/language/csimple/CSimpleHelper.java
 
b/core/camel-core-languages/src/main/java/org/apache/camel/language/csimple/CSimpleHelper.java
index a540d1eabd6..45dad64ea19 100644
--- 
a/core/camel-core-languages/src/main/java/org/apache/camel/language/csimple/CSimpleHelper.java
+++ 
b/core/camel-core-languages/src/main/java/org/apache/camel/language/csimple/CSimpleHelper.java
@@ -52,6 +52,8 @@ import org.apache.camel.util.StringHelper;
 import org.apache.camel.util.json.Jsoner;
 import org.apache.camel.util.xml.pretty.XmlPrettyPrinter;
 
+import static org.apache.camel.util.StringHelper.between;
+
 /**
  * A set of helper as static imports for the Camel compiled simple language.
  */
@@ -128,7 +130,7 @@ public final class CSimpleHelper {
         List<String> keys = OgnlHelper.splitOgnl(key);
         for (String k : keys) {
             if (k.startsWith("[") && k.endsWith("]")) {
-                k = StringHelper.between(k, "[", "]");
+                k = between(k, "[", "]");
             }
             obj = doObjectAsIndex(context, obj, k);
         }
@@ -154,7 +156,7 @@ public final class CSimpleHelper {
         List<String> keys = OgnlHelper.splitOgnl(key);
         for (String k : keys) {
             if (k.startsWith("[") && k.endsWith("]")) {
-                k = StringHelper.between(k, "[", "]");
+                k = between(k, "[", "]");
             }
             obj = doObjectAsIndex(exchange.getContext(), obj, k);
         }
@@ -180,7 +182,7 @@ public final class CSimpleHelper {
         List<String> keys = OgnlHelper.splitOgnl(key);
         for (String k : keys) {
             if (k.startsWith("[") && k.endsWith("]")) {
-                k = StringHelper.between(k, "[", "]");
+                k = between(k, "[", "]");
             }
             obj = doObjectAsIndex(exchange.getContext(), obj, k);
         }
@@ -497,23 +499,7 @@ public final class CSimpleHelper {
         if (text == null) {
             return null;
         }
-        int len = text.length();
-        if (head > 0) {
-            if (head <= len) {
-                text = text.substring(head);
-            } else {
-                text = "";
-            }
-            len = text.length();
-        }
-        if (tail > 0) {
-            if (tail <= len) {
-                text = text.substring(0, len - tail);
-            } else {
-                text = "";
-            }
-        }
-        return text;
+        return between(text, head, tail);
     }
 
     public static int random(Exchange exchange, Object min, Object max) {
diff --git 
a/core/camel-support/src/main/java/org/apache/camel/support/builder/ExpressionBuilder.java
 
b/core/camel-support/src/main/java/org/apache/camel/support/builder/ExpressionBuilder.java
index 19a0fdfe4d5..ac696463421 100644
--- 
a/core/camel-support/src/main/java/org/apache/camel/support/builder/ExpressionBuilder.java
+++ 
b/core/camel-support/src/main/java/org/apache/camel/support/builder/ExpressionBuilder.java
@@ -61,6 +61,8 @@ import org.apache.camel.util.StringHelper;
 import org.apache.camel.util.json.Jsoner;
 import org.apache.camel.util.xml.pretty.XmlPrettyPrinter;
 
+import static org.apache.camel.util.StringHelper.between;
+
 /**
  * A helper class for working with <a 
href="http://camel.apache.org/expression.html";>expressions</a>.
  */
@@ -1854,23 +1856,7 @@ public class ExpressionBuilder {
                 if (text == null) {
                     return null;
                 }
-                int len = text.length();
-                if (head > 0) {
-                    if (head <= len) {
-                        text = text.substring(head);
-                    } else {
-                        text = "";
-                    }
-                    len = text.length();
-                }
-                if (tail > 0) {
-                    if (tail <= len) {
-                        text = text.substring(0, len - tail);
-                    } else {
-                        text = "";
-                    }
-                }
-                return text;
+                return between(text, head, tail);
             }
 
             @Override
diff --git 
a/core/camel-util/src/main/java/org/apache/camel/util/StringHelper.java 
b/core/camel-util/src/main/java/org/apache/camel/util/StringHelper.java
index 73a164a58e1..0af0754f7ae 100644
--- a/core/camel-util/src/main/java/org/apache/camel/util/StringHelper.java
+++ b/core/camel-util/src/main/java/org/apache/camel/util/StringHelper.java
@@ -831,6 +831,34 @@ public final class StringHelper {
         }
     }
 
+    /**
+     * Returns the substring between the given head and tail
+     *
+     * @param  text the text
+     * @param  head the head of the substring
+     * @param  tail the tail of the substring
+     * @return      the substring between the given head and tail
+     */
+    public static String between(String text, int head, int tail) {
+        int len = text.length();
+        if (head > 0) {
+            if (head <= len) {
+                text = text.substring(head);
+            } else {
+                text = "";
+            }
+            len = text.length();
+        }
+        if (tail > 0) {
+            if (tail <= len) {
+                text = text.substring(0, len - tail);
+            } else {
+                text = "";
+            }
+        }
+        return text;
+    }
+
     /**
      * Returns the string between the most outer pair of tokens
      * <p/>

Reply via email to