common: Add StreamUtils method to wrap an Iterator into a Stream
authorAlexandre Montplaisir <alexmonthy@efficios.com>
Tue, 29 Nov 2016 21:55:46 +0000 (16:55 -0500)
committerAlexandre Montplaisir <alexmonthy@efficios.com>
Fri, 2 Dec 2016 19:52:36 +0000 (14:52 -0500)
Change-Id: Ib3027589bdfe4b328596dabdd572e77ca434dd68
Signed-off-by: Alexandre Montplaisir <alexmonthy@efficios.com>
Reviewed-on: https://git.eclipse.org/r/85990
Reviewed-by: Hudson CI
Reviewed-by: Matthew Khouzam <matthew.khouzam@ericsson.com>
common/org.eclipse.tracecompass.common.core.tests/src/org/eclipse/tracecompass/common/core/tests/StreamUtilsTest.java [new file with mode: 0644]
common/org.eclipse.tracecompass.common.core/META-INF/MANIFEST.MF
common/org.eclipse.tracecompass.common.core/src/org/eclipse/tracecompass/common/core/StreamUtils.java

diff --git a/common/org.eclipse.tracecompass.common.core.tests/src/org/eclipse/tracecompass/common/core/tests/StreamUtilsTest.java b/common/org.eclipse.tracecompass.common.core.tests/src/org/eclipse/tracecompass/common/core/tests/StreamUtilsTest.java
new file mode 100644 (file)
index 0000000..ef07316
--- /dev/null
@@ -0,0 +1,59 @@
+/*******************************************************************************
+ * Copyright (c) 2016 EfficiOS Inc., Alexandre Montplaisir
+ *
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *******************************************************************************/
+
+package org.eclipse.tracecompass.common.core.tests;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import java.util.Iterator;
+import java.util.List;
+
+import org.eclipse.tracecompass.common.core.StreamUtils;
+import org.junit.Test;
+
+import com.google.common.collect.ImmutableList;
+
+/**
+ * Tests for {@link StreamUtils} methods.
+ *
+ * @author Alexandre Montplaisir
+ */
+public class StreamUtilsTest {
+
+    /**
+     * Test {@link StreamUtils#getStream(Iterator)}.
+     */
+    @Test
+    public void testGetStreamIterator() {
+        List<Integer> list = ImmutableList.of(1, 2, 3, 4, 5);
+
+        /* Test short-circuiting terminal operation */
+        Iterator<Integer> iter = list.iterator();
+        boolean test = StreamUtils.getStream(iter)
+                .anyMatch(value -> value == 3);
+        assertTrue(test);
+
+        /*
+         * Test that the short-circuiting operation stopped its iteration where
+         * it needed to.
+         */
+        assertTrue(iter.hasNext());
+        assertEquals(4, iter.next().intValue());
+
+        /* Test fully-consuming terminal operation */
+        iter = list.iterator();
+        int sum = StreamUtils.getStream(iter)
+                .mapToInt(Integer::intValue)
+                .sum();
+        assertEquals(15, sum);
+        assertFalse(iter.hasNext());
+    }
+}
index 4136117979c3a91f2270b481a3850e1c060fbb49..eca20a17c83fa05108c98caf9aa12f219e17407e 100644 (file)
@@ -2,7 +2,7 @@ Manifest-Version: 1.0
 Bundle-ManifestVersion: 2
 Bundle-Name: %Bundle-Name
 Bundle-Vendor: %Bundle-Vendor
-Bundle-Version: 2.1.0.qualifier
+Bundle-Version: 2.2.0.qualifier
 Bundle-Localization: plugin
 Bundle-SymbolicName: org.eclipse.tracecompass.common.core;singleton:=true
 Bundle-Activator: org.eclipse.tracecompass.internal.common.core.Activator
index f9f3f1d31eecd969ecd72b17c370894e9df8601f..aa8b7a31cf03f78947f721b35c701f3db5fe6afb 100644 (file)
@@ -11,6 +11,9 @@ package org.eclipse.tracecompass.common.core;
 
 import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
 
+import java.util.Iterator;
+import java.util.Spliterator;
+import java.util.Spliterators;
 import java.util.function.Function;
 import java.util.stream.Stream;
 import java.util.stream.StreamSupport;
@@ -36,6 +39,23 @@ public final class StreamUtils {
         return StreamSupport.stream(iterable.spliterator(), false);
     }
 
+    /**
+     * Get a {@link Stream} from a generic {@link Iterator}.
+     *
+     * Depending on the type of terminal operation used on the stream, the
+     * iterator may or may not have some elements remaining. Be wary if you
+     * re-use the same iterator afterwards.
+     *
+     * @param iterator
+     *            The iterator to wrap
+     * @return A stream containing the iterator's elements
+     * @since 2.2
+     */
+    public static <T> Stream<T> getStream(Iterator<T> iterator) {
+        Spliterator<T> spliterator = Spliterators.spliteratorUnknownSize(iterator, 0);
+        return StreamSupport.stream(spliterator, false);
+    }
+
     /**
      * Generic utility class to "flatten" a data structure using the
      * {@link Stream} API.
This page took 0.026306 seconds and 5 git commands to generate.