changeset 7531:e343f811b2b4

Remove OpUtils and move evaluateSequential to a default method on PipelineHelper.
author psandoz
date Mon, 25 Feb 2013 13:27:17 +0100
parents 12dac52b2230
children 3e50294c68ea
files src/share/classes/java/util/stream/OpUtils.java src/share/classes/java/util/stream/PipelineHelper.java src/share/classes/java/util/stream/SliceOp.java src/share/classes/java/util/stream/StatefulOp.java
diffstat 4 files changed, 38 insertions(+), 66 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/java/util/stream/OpUtils.java	Mon Feb 25 12:21:08 2013 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,64 +0,0 @@
-/*
- * 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 java.util.stream;
-
-/**
- * Utility methods useful in the implementation of stream operations ({@link IntermediateOp}, {@link StatefulOp},
- * {@link TerminalOp}).
- * @since 1.8
- */
-class OpUtils {
-    private OpUtils() {
-        throw new IllegalStateException("no instances");
-    }
-
-    /**
-     * Given a {@link PipelineHelper} describing a stream source and a sequence of intermediate stream operations,
-     * compute the contents of the stream, sequentially, and collect the results into a {@code Node}.  The order
-     * of output elements will respect the encounter order of the source stream, and all computation will happen
-     * in the invoking thread.
-     *
-     * @param op An {@code IntermediateOp} representing the final operation to be implemented, typically
-     *           a {@code StatefulOp}
-     * @param helper A {@code PipelineHelper} describing the stream source and operations
-     * @param <P_IN> The input type of the stream pipeline
-     * @param <P_OUT> The output type of the stream pipeline
-     * @return A {@code Node} containing the output of the stream pipeline
-     */
-    static<P_IN, P_OUT> Node<P_OUT> evaluateSequential(IntermediateOp<P_OUT, P_OUT> op,
-                                                       PipelineHelper<P_IN, P_OUT> helper) {
-        long sizeIfKnown = StreamOpFlag.SIZED.isPreserved(op.getOpFlags())
-                           ? helper.exactOutputSizeIfKnown(helper.sourceSpliterator())
-                           : -1;
-        final Node.Builder<P_OUT> nb = helper.makeNodeBuilder(sizeIfKnown);
-        Sink<P_OUT> opSink = op.wrapSink(helper.getStreamAndOpFlags(), nb);
-
-        if (!StreamOpFlag.SHORT_CIRCUIT.isKnown(op.getOpFlags()))
-            helper.into(opSink, helper.sourceSpliterator());
-        else
-            helper.intoWrappedWithCancel(helper.wrapSink(opSink), helper.sourceSpliterator());
-        return nb.build();
-    }
-}
--- a/src/share/classes/java/util/stream/PipelineHelper.java	Mon Feb 25 12:21:08 2013 +0100
+++ b/src/share/classes/java/util/stream/PipelineHelper.java	Mon Feb 25 13:27:17 2013 +0100
@@ -203,4 +203,40 @@
      * @return a factory for arrays of the output type of this pipeline.
      */
     IntFunction<P_OUT[]> arrayGenerator();
+
+    /**
+     * Collect all output elements resulting from the applying the pipeline stages, plus an additional final stage
+     * that is an intermediate operation, to the source {@code Spliterator} into a {code Node}.
+     * The order of output elements will respect the encounter order of the source stream, and all computation will
+     * happen in the invoking thread.
+     * <p>
+     * Implementations of {@link StatefulOp#evaluateParallel(PipelineHelper)} can defer to this method if
+     * a sequential implementation is acceptable.
+     *
+     * @implSpec
+     * If the intermediate operation injects {@link StreamOpFlag#SHORT_CIRCUIT} then this implementation must
+     * stop collecting output elements when the sink returned from {@link IntermediateOp#wrapSink(int, Sink)}
+     * reports it is cancelled.
+     *
+     * If the intermediate operation preserves or injects {@link StreamOpFlag#SIZED} and the output size of the
+     * pipeline is known then this implementation may apply size optimizations since the output size is known.
+     *
+     * @param op An {@code IntermediateOp} representing the final stage in the pipeline, typically
+     *           a {@code StatefulOp}
+     * @return A {@code Node} containing the output of the stream pipeline
+     */
+    default Node<P_OUT> evaluateSequential(IntermediateOp<P_OUT, P_OUT> op) {
+        long sizeIfKnown = StreamOpFlag.SIZED.isPreserved(op.getOpFlags())
+                           ? exactOutputSizeIfKnown(sourceSpliterator())
+                           : -1;
+        final Node.Builder<P_OUT> nb = makeNodeBuilder(sizeIfKnown);
+        Sink<P_OUT> opSink = op.wrapSink(getStreamAndOpFlags(), nb);
+
+        if (!StreamOpFlag.SHORT_CIRCUIT.isKnown(op.getOpFlags()))
+            into(opSink, sourceSpliterator());
+        else
+            intoWrappedWithCancel(wrapSink(opSink), sourceSpliterator());
+        return nb.build();
+    }
+
 }
--- a/src/share/classes/java/util/stream/SliceOp.java	Mon Feb 25 12:21:08 2013 +0100
+++ b/src/share/classes/java/util/stream/SliceOp.java	Mon Feb 25 13:27:17 2013 +0100
@@ -272,7 +272,7 @@
         @Override
         protected final Node<T> doLeaf() {
             if (isRoot()) {
-                return OpUtils.evaluateSequential(op, helper);
+                return helper.evaluateSequential(op);
             }
             else {
                 Node<T> node = helper.into(helper.makeNodeBuilder(-1),
--- a/src/share/classes/java/util/stream/StatefulOp.java	Mon Feb 25 12:21:08 2013 +0100
+++ b/src/share/classes/java/util/stream/StatefulOp.java	Mon Feb 25 13:27:17 2013 +0100
@@ -37,7 +37,7 @@
      *
      * An implementation of this method must be provided, but it is acceptable if the implementation is sequential.
      * A generic sequential implementation is available as
-     * {@link OpUtils#evaluateSequential(IntermediateOp, PipelineHelper)}.
+     * {@link PipelineHelper#evaluateSequential(IntermediateOp)}.
      */
     <P_IN> Node<E> evaluateParallel(PipelineHelper<P_IN, E> helper);
 }