camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lburgazz...@apache.org
Subject [3/5] camel git commit: CAMEL-10101: [api-component-framework] splitResult should not convert collections to array
Date Thu, 21 Jul 2016 10:01:33 GMT
CAMEL-10101: [api-component-framework] splitResult should not convert collections to array


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/6a7338bd
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/6a7338bd
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/6a7338bd

Branch: refs/heads/master
Commit: 6a7338bd006df5fed13783197570a1c5b47e4738
Parents: 47e392f
Author: lburgazzoli <lburgazzoli@gmail.com>
Authored: Wed Jun 29 17:10:31 2016 +0200
Committer: lburgazzoli <lburgazzoli@gmail.com>
Committed: Thu Jul 21 11:47:40 2016 +0200

----------------------------------------------------------------------
 .../camel/util/component/ApiConsumerHelper.java | 40 ++++++++++++++++----
 .../camel/util/component/ResultInterceptor.java |  2 +-
 2 files changed, 33 insertions(+), 9 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/6a7338bd/camel-core/src/main/java/org/apache/camel/util/component/ApiConsumerHelper.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/util/component/ApiConsumerHelper.java
b/camel-core/src/main/java/org/apache/camel/util/component/ApiConsumerHelper.java
index 7bcfb37..658ce0c 100644
--- a/camel-core/src/main/java/org/apache/camel/util/component/ApiConsumerHelper.java
+++ b/camel-core/src/main/java/org/apache/camel/util/component/ApiConsumerHelper.java
@@ -91,15 +91,39 @@ public final class ApiConsumerHelper {
         // process result according to type
         if (result != null && splitResult) {
             // try to split the result
-            final Object resultArray = consumer.splitResult(result);
-
-            if (resultArray != result && resultArray.getClass().isArray()) {
-                // create an exchange for every element
-                final int length = Array.getLength(resultArray);
-                for (int i = 0; i < length; i++) {
-                    processResult(consumer, result, Array.get(resultArray, i));
+            final Object results = consumer.splitResult(result);
+
+            if (results != null) {
+                if (results instanceof List) {
+                    // Optimized for lists
+                    final List<?> list = (List<?>)results;
+                    final int size = list.size();
+
+                    // access elements by position rather than with iterator to
+                    // reduce garbage
+                    for (int i = 0; i < size; i++) {
+                        processResult(consumer, result, list.get(i));
+                    }
+
+                    return size;
+                } else if (results instanceof Iterable) {
+                    // Optimized for iterable
+                    int size = 0;
+                    for (Object singleResult : (Iterable<?>)results) {
+                        processResult(consumer, result, singleResult);
+                        size++;
+                    }
+
+                    return size;
+                } else if (results.getClass().isArray()) {
+                    // Optimized for array
+                    final int size = Array.getLength(results);
+                    for (int i = 0; i < size; i++) {
+                        processResult(consumer, result, Array.get(results, i));
+                    }
+
+                    return size;
                 }
-                return length;
             }
         }
 

http://git-wip-us.apache.org/repos/asf/camel/blob/6a7338bd/camel-core/src/main/java/org/apache/camel/util/component/ResultInterceptor.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/util/component/ResultInterceptor.java
b/camel-core/src/main/java/org/apache/camel/util/component/ResultInterceptor.java
index 716ee44..3592328 100644
--- a/camel-core/src/main/java/org/apache/camel/util/component/ResultInterceptor.java
+++ b/camel-core/src/main/java/org/apache/camel/util/component/ResultInterceptor.java
@@ -26,7 +26,7 @@ public interface ResultInterceptor {
     /**
      * Split a complex result into result elements.
      * @param result API method invocation result
-     * @return either the same result if it cannot be split, or an array object with split
results
+     * @return either the same result if it cannot be split, an array or collection object
with split results
      */
     Object splitResult(Object result);
 


Mime
View raw message