common: Add a new StreamUtils class
authorAlexandre Montplaisir <alexmonthy@efficios.com>
Tue, 15 Mar 2016 23:46:45 +0000 (19:46 -0400)
committerAlexandre Montplaisir <alexmonthy@efficios.com>
Wed, 30 Mar 2016 00:27:45 +0000 (20:27 -0400)
We often use Iterable in our APIs, due its minimal behavior
exposition. Unfortunately, there is no Iterable.stream() method,
the official way is done using StreamSupport and the corresponding
spliterator.

Since this is a very commonly used pattern, we can introduce a
small utility method that will make it less verbose.

At the same time, we can move the StreamFlattener into the new
utils class.

Change-Id: Ia4fed4f8a5da56f08ac133eb10efae970004e511
Signed-off-by: Alexandre Montplaisir <alexmonthy@efficios.com>
Reviewed-on: https://git.eclipse.org/r/68563
Reviewed-by: Hudson CI
Reviewed-by: Marc-Andre Laperle <marc-andre.laperle@ericsson.com>
Tested-by: Marc-Andre Laperle <marc-andre.laperle@ericsson.com>
common/org.eclipse.tracecompass.common.core.tests/src/org/eclipse/tracecompass/common/core/tests/StreamFlattenerTest.java [new file with mode: 0644]
common/org.eclipse.tracecompass.common.core.tests/src/org/eclipse/tracecompass/common/core/tests/collect/StreamFlattenerTest.java [deleted file]
common/org.eclipse.tracecompass.common.core/src/org/eclipse/tracecompass/common/core/StreamUtils.java [new file with mode: 0644]
common/org.eclipse.tracecompass.common.core/src/org/eclipse/tracecompass/common/core/collect/StreamFlattener.java [deleted file]

diff --git a/common/org.eclipse.tracecompass.common.core.tests/src/org/eclipse/tracecompass/common/core/tests/StreamFlattenerTest.java b/common/org.eclipse.tracecompass.common.core.tests/src/org/eclipse/tracecompass/common/core/tests/StreamFlattenerTest.java
new file mode 100644 (file)
index 0000000..ef85675
--- /dev/null
@@ -0,0 +1,82 @@
+/*******************************************************************************
+ * Copyright (c) 2015 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 java.util.Arrays;
+import java.util.List;
+import java.util.stream.Collectors;
+
+import org.eclipse.tracecompass.common.core.StreamUtils.StreamFlattener;
+import org.junit.Test;
+
+/**
+ * Test for {@link StreamFlattener}.
+ *
+ * @author Alexandre Montplaisir
+ */
+public class StreamFlattenerTest {
+
+    /**
+     * Test flattening a tree.
+     *
+     * Each node has a String value, and they will be organized as:
+     *
+     * <pre>
+     *     A
+     *    / \
+     *   B   C
+     *  /|   |\
+     * D E   F G
+     * </pre>
+     *
+     * In-order, depth-first visiting should give the following sequence:<br>
+     * A -> B -> D -> E -> C -> F -> G
+     */
+    @Test
+    public void testFlattenTree() {
+        /* Prepare the tree */
+        TreeNode nodeD = new TreeNode(new TreeNode[0], "D");
+        TreeNode nodeE = new TreeNode(new TreeNode[0], "E");
+        TreeNode nodeF = new TreeNode(new TreeNode[0], "F");
+        TreeNode nodeG = new TreeNode(new TreeNode[0], "G");
+        TreeNode nodeB = new TreeNode(new TreeNode[] {nodeD, nodeE}, "B");
+        TreeNode nodeC = new TreeNode(new TreeNode[] {nodeF, nodeG}, "C");
+        TreeNode nodeA = new TreeNode(new TreeNode[] {nodeB, nodeC}, "A");
+
+        /* Run the test */
+        StreamFlattener<TreeNode> sf = new StreamFlattener<>(node -> Arrays.stream(node.getChildren()));
+
+        List<String> expected = Arrays.asList("A", "B", "D", "E", "C", "F", "G");
+        List<String> results = sf.flatten(nodeA).map(TreeNode::getValue).collect(Collectors.toList());
+
+        assertEquals(expected, results);
+    }
+
+    private static class TreeNode {
+
+        private final TreeNode[] children;
+        private final String value;
+
+        public TreeNode(TreeNode[] children, String value) {
+            this.children = children;
+            this.value = value;
+        }
+
+        public TreeNode[] getChildren() {
+            return children;
+        }
+
+        public String getValue() {
+            return value;
+        }
+    }
+}
diff --git a/common/org.eclipse.tracecompass.common.core.tests/src/org/eclipse/tracecompass/common/core/tests/collect/StreamFlattenerTest.java b/common/org.eclipse.tracecompass.common.core.tests/src/org/eclipse/tracecompass/common/core/tests/collect/StreamFlattenerTest.java
deleted file mode 100644 (file)
index b2949fb..0000000
+++ /dev/null
@@ -1,82 +0,0 @@
-/*******************************************************************************
- * Copyright (c) 2015 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.collect;
-
-import static org.junit.Assert.assertEquals;
-
-import java.util.Arrays;
-import java.util.List;
-import java.util.stream.Collectors;
-
-import org.eclipse.tracecompass.common.core.collect.StreamFlattener;
-import org.junit.Test;
-
-/**
- * Test for {@link StreamFlattener}.
- *
- * @author Alexandre Montplaisir
- */
-public class StreamFlattenerTest {
-
-    /**
-     * Test flattening a tree.
-     *
-     * Each node has a String value, and they will be organized as:
-     *
-     * <pre>
-     *     A
-     *    / \
-     *   B   C
-     *  /|   |\
-     * D E   F G
-     * </pre>
-     *
-     * In-order, depth-first visiting should give the following sequence:<br>
-     * A -> B -> D -> E -> C -> F -> G
-     */
-    @Test
-    public void testFlattenTree() {
-        /* Prepare the tree */
-        TreeNode nodeD = new TreeNode(new TreeNode[0], "D");
-        TreeNode nodeE = new TreeNode(new TreeNode[0], "E");
-        TreeNode nodeF = new TreeNode(new TreeNode[0], "F");
-        TreeNode nodeG = new TreeNode(new TreeNode[0], "G");
-        TreeNode nodeB = new TreeNode(new TreeNode[] {nodeD, nodeE}, "B");
-        TreeNode nodeC = new TreeNode(new TreeNode[] {nodeF, nodeG}, "C");
-        TreeNode nodeA = new TreeNode(new TreeNode[] {nodeB, nodeC}, "A");
-
-        /* Run the test */
-        StreamFlattener<TreeNode> sf = new StreamFlattener<>(node -> Arrays.stream(node.getChildren()));
-
-        List<String> expected = Arrays.asList("A", "B", "D", "E", "C", "F", "G");
-        List<String> results = sf.flatten(nodeA).map(TreeNode::getValue).collect(Collectors.toList());
-
-        assertEquals(expected, results);
-    }
-
-    private static class TreeNode {
-
-        private final TreeNode[] children;
-        private final String value;
-
-        public TreeNode(TreeNode[] children, String value) {
-            this.children = children;
-            this.value = value;
-        }
-
-        public TreeNode[] getChildren() {
-            return children;
-        }
-
-        public String getValue() {
-            return value;
-        }
-    }
-}
diff --git a/common/org.eclipse.tracecompass.common.core/src/org/eclipse/tracecompass/common/core/StreamUtils.java b/common/org.eclipse.tracecompass.common.core/src/org/eclipse/tracecompass/common/core/StreamUtils.java
new file mode 100644 (file)
index 0000000..f9f3f1d
--- /dev/null
@@ -0,0 +1,77 @@
+/*******************************************************************************
+ * 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;
+
+import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
+
+import java.util.function.Function;
+import java.util.stream.Stream;
+import java.util.stream.StreamSupport;
+
+/**
+ * Common utilities for {@link Stream}.
+ *
+ * @author Alexandre Montplaisir
+ * @since 2.0
+ */
+public final class StreamUtils {
+
+    private StreamUtils() {}
+
+    /**
+     * Get a sequential {@link Stream} from an {@link Iterable}.
+     *
+     * @param iterable
+     *            Any iterable
+     * @return The stream on the elements of the iterable
+     */
+    public static <T> Stream<T> getStream(Iterable<T> iterable) {
+        return StreamSupport.stream(iterable.spliterator(), false);
+    }
+
+    /**
+     * Generic utility class to "flatten" a data structure using the
+     * {@link Stream} API.
+     *
+     * @param <T>
+     *            The type of container, or "node" in the tree
+     */
+    public static class StreamFlattener<T> {
+
+        private final Function<T, Stream<T>> fGetChildrenFunction;
+
+        /**
+         * Constructor
+         *
+         * @param getChildrenFunction
+         *            The function to use to get each element's children. Should
+         *            return a {@link Stream} of those children.
+         */
+        public StreamFlattener(Function<T, Stream<T>> getChildrenFunction) {
+            fGetChildrenFunction = getChildrenFunction;
+        }
+
+        /**
+         * Do an in-order flattening of the data structure, starting at the given
+         * element (or node).
+         *
+         * @param element
+         *            The tree node or similar from which to start
+         * @return A unified Stream of all the children that were found,
+         *         recursively.
+         */
+        public Stream<T> flatten(T element) {
+            Stream<T> ret = Stream.concat(
+                    Stream.of(element),
+                    fGetChildrenFunction.apply(element).flatMap(this::flatten));
+            return checkNotNull(ret);
+        }
+    }
+}
diff --git a/common/org.eclipse.tracecompass.common.core/src/org/eclipse/tracecompass/common/core/collect/StreamFlattener.java b/common/org.eclipse.tracecompass.common.core/src/org/eclipse/tracecompass/common/core/collect/StreamFlattener.java
deleted file mode 100644 (file)
index 5daaa96..0000000
+++ /dev/null
@@ -1,57 +0,0 @@
-/*******************************************************************************
- * Copyright (c) 2015 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.collect;
-
-import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
-
-import java.util.function.Function;
-import java.util.stream.Stream;
-
-/**
- * Generic utility class to "flatten" a data structure using the {@link Stream}
- * API.
- *
- * @author Alexandre Montplaisir
- *
- * @param <T>
- *            The type of container, or "node" in the tree
- * @since 2.0
- */
-public class StreamFlattener<T> {
-
-    private final Function<T, Stream<T>> fGetChildrenFunction;
-
-    /**
-     * Constructor
-     *
-     * @param getChildrenFunction
-     *            The function to use to get each element's children. Should
-     *            return a {@link Stream} of those children.
-     */
-    public StreamFlattener(Function<T, Stream<T>> getChildrenFunction) {
-        fGetChildrenFunction = getChildrenFunction;
-    }
-
-    /**
-     * Do an in-order flattening of the data structure, starting at the given
-     * element (or node).
-     *
-     * @param element
-     *            The tree node or similar from which to start
-     * @return A unified Stream of all the children that were found,
-     *         recursively.
-     */
-    public Stream<T> flatten(T element) {
-        Stream<T> ret = Stream.concat(
-                Stream.of(element),
-                fGetChildrenFunction.apply(element).flatMap(this::flatten));
-        return checkNotNull(ret);
-    }
-}
This page took 0.02947 seconds and 5 git commands to generate.