This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/camel.git
commit b0e60bef4f3b89cc568e85fd6dba554be30a2900 Author: Claus Ibsen <[email protected]> AuthorDate: Sun Aug 4 11:36:50 2019 +0200 CAMEL-13774: Add Java8 Stream to Iterator as type converter. --- .../java/org/apache/camel/converter/ObjectConverter.java | 1 + .../java/org/apache/camel/converter/ObjectConverterTest.java | 10 ++++++++++ .../test/java/org/apache/camel/processor/SimpleMockTest.java | 2 ++ .../src/main/java/org/apache/camel/support/ObjectHelper.java | 12 ++++++++---- 4 files changed, 21 insertions(+), 4 deletions(-) diff --git a/core/camel-base/src/main/java/org/apache/camel/converter/ObjectConverter.java b/core/camel-base/src/main/java/org/apache/camel/converter/ObjectConverter.java index 6945f49..f546448 100644 --- a/core/camel-base/src/main/java/org/apache/camel/converter/ObjectConverter.java +++ b/core/camel-base/src/main/java/org/apache/camel/converter/ObjectConverter.java @@ -18,6 +18,7 @@ package org.apache.camel.converter; import java.math.BigInteger; import java.util.Iterator; +import java.util.stream.Stream; import org.apache.camel.Converter; import org.apache.camel.Exchange; diff --git a/core/camel-core/src/test/java/org/apache/camel/converter/ObjectConverterTest.java b/core/camel-core/src/test/java/org/apache/camel/converter/ObjectConverterTest.java index b533f31..f6dcfe1 100644 --- a/core/camel-core/src/test/java/org/apache/camel/converter/ObjectConverterTest.java +++ b/core/camel-core/src/test/java/org/apache/camel/converter/ObjectConverterTest.java @@ -19,6 +19,7 @@ package org.apache.camel.converter; import java.math.BigInteger; import java.util.Date; import java.util.Iterator; +import java.util.stream.Stream; import org.junit.Assert; import org.junit.Test; @@ -33,6 +34,15 @@ public class ObjectConverterTest extends Assert { assertEquals(false, it.hasNext()); } + @Test + public void testStreamIterator() { + Iterator<?> it = ObjectConverter.iterator(Stream.of("Claus", "Jonathan", "Andrea")); + assertEquals("Claus", it.next()); + assertEquals("Jonathan", it.next()); + assertEquals("Andrea", it.next()); + assertEquals(false, it.hasNext()); + } + @SuppressWarnings("unchecked") @Test public void testIterable() { diff --git a/core/camel-core/src/test/java/org/apache/camel/processor/SimpleMockTest.java b/core/camel-core/src/test/java/org/apache/camel/processor/SimpleMockTest.java index 015a692..b8bd72d 100644 --- a/core/camel-core/src/test/java/org/apache/camel/processor/SimpleMockTest.java +++ b/core/camel-core/src/test/java/org/apache/camel/processor/SimpleMockTest.java @@ -16,6 +16,8 @@ */ package org.apache.camel.processor; +import java.util.stream.Stream; + import org.apache.camel.ContextTestSupport; import org.apache.camel.builder.RouteBuilder; import org.apache.camel.component.mock.MockEndpoint; 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 195b1c7..19496e0 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 @@ -25,6 +25,7 @@ import java.util.Collections; import java.util.Iterator; import java.util.NoSuchElementException; import java.util.concurrent.Callable; +import java.util.stream.Stream; import org.w3c.dom.Node; import org.w3c.dom.NodeList; @@ -382,7 +383,7 @@ public final class ObjectHelper { } /** - * Creates an iterator over the value if the value is a collection, an + * Creates an iterator over the value if the value is a {@link Stream}, collection, an * Object[], a String with values separated by comma, * or a primitive type array; otherwise to simplify the caller's code, * we just create a singleton collection iterator over a single value @@ -398,7 +399,7 @@ public final class ObjectHelper { } /** - * Creates an iterator over the value if the value is a collection, an + * Creates an iterator over the value if the value is a {@link Stream}, collection, an * Object[], a String with values separated by the given delimiter, * or a primitive type array; otherwise to simplify the caller's * code, we just create a singleton collection iterator over a single value @@ -414,7 +415,7 @@ public final class ObjectHelper { } /** - * Creates an iterator over the value if the value is a collection, an + * Creates an iterator over the value if the value is a {@link Stream}, collection, an * Object[], a String with values separated by the given delimiter, * or a primitive type array; otherwise to simplify the caller's * code, we just create a singleton collection iterator over a single value @@ -434,7 +435,7 @@ public final class ObjectHelper { } /** - * Creates an iterator over the value if the value is a collection, an + * Creates an iterator over the value if the value is a {@link Stream}, collection, an * Object[], a String with values separated by the given delimiter, * or a primitive type array; otherwise to simplify the caller's * code, we just create a singleton collection iterator over a single value @@ -452,6 +453,9 @@ public final class ObjectHelper { */ public static Iterator<?> createIterator(Object value, String delimiter, boolean allowEmptyValues, boolean pattern) { + if (value instanceof Stream) { + return ((Stream) value).iterator(); + } return createIterable(value, delimiter, allowEmptyValues, pattern).iterator(); }
