You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by or...@apache.org on 2023/10/27 15:17:27 UTC

(camel) 02/02: (chores) camel-core: break large methods

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

commit 9b59f350f6bc6718145529426e8e2d2e5e15bc45
Author: Otavio Rodolfo Piske <an...@gmail.com>
AuthorDate: Fri Oct 27 16:50:13 2023 +0200

    (chores) camel-core: break large methods
    
    This should help obtain better information when profiling the code
---
 .../camel/support/DefaultHeaderFilterStrategy.java |  77 +++++---
 .../org/apache/camel/support/ObjectHelper.java     | 216 ++++++++++++---------
 2 files changed, 168 insertions(+), 125 deletions(-)

diff --git a/core/camel-support/src/main/java/org/apache/camel/support/DefaultHeaderFilterStrategy.java b/core/camel-support/src/main/java/org/apache/camel/support/DefaultHeaderFilterStrategy.java
index eb091d31ad2..7a99e29ab16 100644
--- a/core/camel-support/src/main/java/org/apache/camel/support/DefaultHeaderFilterStrategy.java
+++ b/core/camel-support/src/main/java/org/apache/camel/support/DefaultHeaderFilterStrategy.java
@@ -309,47 +309,68 @@ public class DefaultHeaderFilterStrategy implements HeaderFilterStrategy {
         }
 
         if (startsWith != null) {
-            for (String s : startsWith) {
-                boolean match = headerName.startsWith(s);
-                if (match) {
-                    return filterOnMatch;
-                }
+            if (tryHeaderMatch(headerName, startsWith)) {
+                return filterOnMatch;
             }
         }
 
         if (pattern != null) {
-            // optimize if its the default pattern as we know the pattern is to check for keys starting with Camel
-            if (pattern == CAMEL_FILTER_PATTERN) {
-                boolean match = headerName.startsWith("Camel") || headerName.startsWith("camel")
-                        || headerName.startsWith("org.apache.camel.");
-                if (match) {
-                    return filterOnMatch;
-                }
-            } else if (pattern.matcher(headerName).matches()) {
+            if (tryPattern(headerName, pattern)) {
                 return filterOnMatch;
             }
         }
 
         if (filter != null) {
-            if (isCaseInsensitive()) {
-                for (String filterString : filter) {
-                    if (filterString.equalsIgnoreCase(headerName)) {
-                        return filterOnMatch;
-                    }
-                }
-            } else if (isLowerCase()) {
-                String lower = headerName.toLowerCase(Locale.ENGLISH);
-                if (filter.contains(lower)) {
-                    return filterOnMatch;
-                }
-            } else {
-                if (filter.contains(headerName)) {
-                    return filterOnMatch;
-                }
+            if (evalFilterMatch(headerName, filter)) {
+                return filterOnMatch;
             }
         }
 
         return extendedFilter(direction, headerName, headerValue, exchange);
     }
 
+    private boolean tryPattern(String headerName, Pattern pattern) {
+        // optimize if its the default pattern as we know the pattern is to check for keys starting with Camel
+        if (pattern == CAMEL_FILTER_PATTERN) {
+            boolean match = headerName.startsWith("Camel") || headerName.startsWith("camel")
+                    || headerName.startsWith("org.apache.camel.");
+            if (match) {
+                return true;
+            }
+        } else if (pattern.matcher(headerName).matches()) {
+            return true;
+        }
+        return false;
+    }
+
+    private boolean tryHeaderMatch(String headerName, String[] startsWith) {
+        for (String s : startsWith) {
+            boolean match = headerName.startsWith(s);
+            if (match) {
+                return true;
+            }
+        }
+        return false;
+    }
+
+    private boolean evalFilterMatch(String headerName, Set<String> filter) {
+        if (isCaseInsensitive()) {
+            for (String filterString : filter) {
+                if (filterString.equalsIgnoreCase(headerName)) {
+                    return true;
+                }
+            }
+        } else if (isLowerCase()) {
+            String lower = headerName.toLowerCase(Locale.ENGLISH);
+            if (filter.contains(lower)) {
+                return true;
+            }
+        } else {
+            if (filter.contains(headerName)) {
+                return true;
+            }
+        }
+        return false;
+    }
+
 }
diff --git a/core/camel-support/src/main/java/org/apache/camel/support/ObjectHelper.java b/core/camel-support/src/main/java/org/apache/camel/support/ObjectHelper.java
index 8d18cd513d8..33526c2c926 100644
--- a/core/camel-support/src/main/java/org/apache/camel/support/ObjectHelper.java
+++ b/core/camel-support/src/main/java/org/apache/camel/support/ObjectHelper.java
@@ -782,6 +782,8 @@ public final class ObjectHelper {
 
         if (value == null) {
             return Collections.emptyList();
+        } else if (value instanceof String) {
+            return createStringIterator((String) value, delimiter, allowEmptyValues, pattern);
         } else if (value instanceof Iterator) {
             final Iterator<Object> iterator = (Iterator<Object>) value;
             return (Iterable<Object>) () -> iterator;
@@ -791,116 +793,136 @@ public final class ObjectHelper {
             Map<?, ?> map = (Map<?, ?>) value;
             return map.entrySet();
         } else if (value.getClass().isArray()) {
-            if (org.apache.camel.util.ObjectHelper.isPrimitiveArrayType(value.getClass())) {
-                final Object array = value;
-                return (Iterable<Object>) () -> new Iterator<>() {
-                    private int idx;
+            return createArrayIterator(value);
+        } else if (value instanceof NodeList) {
+            // lets iterate through DOM results after performing XPaths
+            final NodeList nodeList = (NodeList) value;
+            return (Iterable<Node>) () -> createNodeListIterator(nodeList);
+        } else {
+            return Collections.singletonList(value);
+        }
+    }
 
-                    public boolean hasNext() {
-                        return idx < Array.getLength(array);
-                    }
+    private static Iterable<Object> createArrayIterator(Object value) {
+        if (org.apache.camel.util.ObjectHelper.isPrimitiveArrayType(value.getClass())) {
+            final Object array = value;
+            return () -> createPrimitiveArrayIterator(array);
+        } else {
+            return Arrays.asList((Object[]) value);
+        }
+    }
 
-                    public Object next() {
-                        if (!hasNext()) {
-                            throw new NoSuchElementException(
-                                    "no more element available for '" + array + "' at the index " + idx);
-                        }
+    private static Iterable<?> createStringIterator(String value, String delimiter, boolean allowEmptyValues, boolean pattern) {
+        final String s = value;
 
-                        return Array.get(array, idx++);
-                    }
+        // this code is optimized to only use a Scanner if needed, eg there is a delimiter
 
-                    @Override
-                    public void remove() {
-                        throw new UnsupportedOperationException();
-                    }
-                };
-            } else {
-                return Arrays.asList((Object[]) value);
+        if (delimiter != null && (pattern || s.contains(delimiter))) {
+            // if its the default delimiter and the value has parenthesis
+            if (DEFAULT_DELIMITER.equals(delimiter)) {
+                return createDelimitedStringIterator(s);
+            } else if (pattern) {
+                return (Iterable<String>) () -> new StringIteratorForPattern(s, delimiter);
             }
-        } else if (value instanceof NodeList) {
-            // lets iterate through DOM results after performing XPaths
-            final NodeList nodeList = (NodeList) value;
-            return (Iterable<Node>) () -> new Iterator<>() {
-                private int idx;
+            return (Iterable<String>) () -> new StringIterator(s, delimiter);
+        } else {
+            return (Iterable<Object>) () -> createPlainIterator(allowEmptyValues, s);
+        }
+    }
 
-                public boolean hasNext() {
-                    return idx < nodeList.getLength();
-                }
+    private static Iterable<String> createDelimitedStringIterator(String s) {
+        if (s.indexOf('(') != -1 && s.indexOf(')') != -1) {
+            // we use the default delimiter which is a comma, then cater for bean expressions with OGNL
+            // which may have balanced parentheses pairs as well.
+            // if the value contains parentheses we need to balance those, to avoid iterating
+            // in the middle of parentheses pair, so use this regular expression (a bit hard to read)
+            // the regexp will split by comma, but honor parentheses pair that may include commas
+            // as well, eg if value = "bean=foo?method=killer(a,b),bean=bar?method=great(a,b)"
+            // then the regexp will split that into two:
+            // -> bean=foo?method=killer(a,b)
+            // -> bean=bar?method=great(a,b)
+            // http://stackoverflow.com/questions/1516090/splitting-a-title-into-separate-parts
+            return (Iterable<String>) () -> new Scanner(s, PARENTHESIS_PATTERN);
+        } else {
+            // optimized split string on default delimiter
+            int count = StringHelper.countChar(s, DEFAULT_DELIMITER_CHAR) + 1;
+            return (Iterable<String>) () -> StringHelper.splitOnCharacterAsIterator(s, DEFAULT_DELIMITER_CHAR,
+                    count);
+        }
+    }
 
-                public Node next() {
-                    if (!hasNext()) {
-                        throw new NoSuchElementException(
-                                "no more element available for '" + nodeList + "' at the index " + idx);
-                    }
+    private static Iterator<Object> createPlainIterator(boolean allowEmptyValues, String s) {
+        // use a plain iterator that returns the value as is as there are only a single value
+        return new Iterator<>() {
+            private int idx;
 
-                    return nodeList.item(idx++);
+            public boolean hasNext() {
+                return idx == 0 && (allowEmptyValues || org.apache.camel.util.ObjectHelper.isNotEmpty(s));
+            }
+
+            public Object next() {
+                if (!hasNext()) {
+                    throw new NoSuchElementException(
+                            "no more element available for '" + s + "' at the index " + idx);
                 }
 
-                @Override
-                public void remove() {
-                    throw new UnsupportedOperationException();
+                idx++;
+                return s;
+            }
+
+            @Override
+            public void remove() {
+                throw new UnsupportedOperationException();
+            }
+        };
+    }
+
+    private static Iterator<Node> createNodeListIterator(NodeList nodeList) {
+        return new Iterator<>() {
+            private int idx;
+
+            public boolean hasNext() {
+                return idx < nodeList.getLength();
+            }
+
+            public Node next() {
+                if (!hasNext()) {
+                    throw new NoSuchElementException(
+                            "no more element available for '" + nodeList + "' at the index " + idx);
                 }
-            };
-        } else if (value instanceof String) {
-            final String s = (String) value;
-
-            // this code is optimized to only use a Scanner if needed, eg there is a delimiter
-
-            if (delimiter != null && (pattern || s.contains(delimiter))) {
-                // if its the default delimiter and the value has parenthesis
-                if (DEFAULT_DELIMITER.equals(delimiter)) {
-                    if (s.indexOf('(') != -1 && s.indexOf(')') != -1) {
-                        // we use the default delimiter which is a comma, then cater for bean expressions with OGNL
-                        // which may have balanced parentheses pairs as well.
-                        // if the value contains parentheses we need to balance those, to avoid iterating
-                        // in the middle of parentheses pair, so use this regular expression (a bit hard to read)
-                        // the regexp will split by comma, but honor parentheses pair that may include commas
-                        // as well, eg if value = "bean=foo?method=killer(a,b),bean=bar?method=great(a,b)"
-                        // then the regexp will split that into two:
-                        // -> bean=foo?method=killer(a,b)
-                        // -> bean=bar?method=great(a,b)
-                        // http://stackoverflow.com/questions/1516090/splitting-a-title-into-separate-parts
-                        return (Iterable<String>) () -> new Scanner(s, PARENTHESIS_PATTERN);
-                    } else {
-                        // optimized split string on default delimiter
-                        int count = StringHelper.countChar(s, DEFAULT_DELIMITER_CHAR) + 1;
-                        return (Iterable<String>) () -> StringHelper.splitOnCharacterAsIterator(s, DEFAULT_DELIMITER_CHAR,
-                                count);
-                    }
-                } else if (pattern) {
-                    return (Iterable<String>) () -> new StringIteratorForPattern(s, delimiter);
+
+                return nodeList.item(idx++);
+            }
+
+            @Override
+            public void remove() {
+                throw new UnsupportedOperationException();
+            }
+        };
+    }
+
+    private static Iterator<Object> createPrimitiveArrayIterator(Object array) {
+        return new Iterator<>() {
+            private int idx;
+
+            public boolean hasNext() {
+                return idx < Array.getLength(array);
+            }
+
+            public Object next() {
+                if (!hasNext()) {
+                    throw new NoSuchElementException(
+                            "no more element available for '" + array + "' at the index " + idx);
                 }
-                return (Iterable<String>) () -> new StringIterator(s, delimiter);
-            } else {
-                return (Iterable<Object>) () -> {
-                    // use a plain iterator that returns the value as is as there are only a single value
-                    return new Iterator<>() {
-                        private int idx;
-
-                        public boolean hasNext() {
-                            return idx == 0 && (allowEmptyValues || org.apache.camel.util.ObjectHelper.isNotEmpty(s));
-                        }
-
-                        public Object next() {
-                            if (!hasNext()) {
-                                throw new NoSuchElementException(
-                                        "no more element available for '" + s + "' at the index " + idx);
-                            }
-
-                            idx++;
-                            return s;
-                        }
-
-                        @Override
-                        public void remove() {
-                            throw new UnsupportedOperationException();
-                        }
-                    };
-                };
+
+                return Array.get(array, idx++);
             }
-        } else {
-            return Collections.singletonList(value);
-        }
+
+            @Override
+            public void remove() {
+                throw new UnsupportedOperationException();
+            }
+        };
     }
 
     /**