changeset 6372:0cf151046f3e

Add Stream.unordered() that clears the encounter order flag such that any downstream stateful operations evaluated in parallel may choose to apply algorithms that do not preserve encounter order. Such algorithms might be more efficient than the order preserving algorithms.
author psandoz
date Mon, 05 Nov 2012 21:22:22 +0100
parents 685d2eb9aa05
children 197bf7b0507d
files src/share/classes/java/util/streams/Stream.java src/share/classes/java/util/streams/ValuePipeline.java test-ng/tests/org/openjdk/tests/java/util/streams/ops/UnorderedStreamTest.java
diffstat 3 files changed, 70 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/java/util/streams/Stream.java	Mon Nov 05 18:03:52 2012 +0100
+++ b/src/share/classes/java/util/streams/Stream.java	Mon Nov 05 21:22:22 2012 +0100
@@ -113,8 +113,19 @@
 
     Optional<T> findAny();
 
+    /**
+     * Convert this stream, if a parallel stream, to a sequential stream.
+     *
+     * @return a sequential stream.
+     */
     Stream<T> sequential();
 
+    /**
+     * Convert this stream to a stream that has no encounter order.
+     *
+     * @return a stream whose output elements have no encounter order.
+     */
+    Stream<T> unordered();
 
     /**
      * An aggregate that supports an {@code addAll(Stream)} operation.
--- a/src/share/classes/java/util/streams/ValuePipeline.java	Mon Nov 05 18:03:52 2012 +0100
+++ b/src/share/classes/java/util/streams/ValuePipeline.java	Mon Nov 05 21:22:22 2012 +0100
@@ -163,6 +163,11 @@
     }
 
     @Override
+    public Stream<U> unordered() {
+        return chainValue(new FlagDeclaringOp<U>(StreamOpFlags.NOT_ORDERED));
+    }
+
+    @Override
     public U reduce(final U seed, final BinaryOperator<U> op) {
         return pipeline(new FoldOp<>(seed, op, op));
     }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test-ng/tests/org/openjdk/tests/java/util/streams/ops/UnorderedStreamTest.java	Mon Nov 05 21:22:22 2012 +0100
@@ -0,0 +1,54 @@
+/*
+ * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package org.openjdk.tests.java.util.streams.ops;
+
+import org.testng.annotations.Test;
+
+import java.util.Arrays;
+import java.util.EnumSet;
+import java.util.streams.*;
+
+@Test
+public class UnorderedStreamTest {
+
+    public void testUnordered() {
+        testUnordered(Arrays.asList(1, 2, 3).stream());
+        testUnordered(Arrays.asList(1, 2, 3).parallel());
+    }
+
+    <T> void testUnordered(Stream<T> s) {
+        s = s.unordered();
+
+        @SuppressWarnings("unchecked")
+        Stream<Integer> st = new ValuePipeline<Integer, Integer>(
+                (AbstractPipeline<Integer, Integer>)s,
+                new FlagOpTest.TestFlagExpectedOp<>(0,
+                                                    EnumSet.noneOf(StreamOpFlags.class),
+                                                    EnumSet.noneOf(StreamOpFlags.class),
+                                                    EnumSet.of(StreamOpFlags.ORDERED)));
+        st.toArray();
+    }
+
+}