/* * 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; import java.util.Spliterator; import java.util.function.IntFunction; /** * Helper class for executing stream pipelines, capturing * all of the information about a stream pipeline (source, output shape, stream flags, parallelism, etc) * in one place. * * @apiNote * A stream pipeline consists of a source, zero or more intermediate operations, and a terminal operation. * Execution of the stream pipeline begins when the terminal operation is executed. * A {@code PipelineHelper} describes the portion of a stream pipeline including its source, some or all of its * intermediate operations, and certain information about the terminal (or stateful) operation which follows * the last intermediate operation described by this {@code PipelineHelper}.The * {@code PipelineHelper} is passed to the {@link TerminalOp#evaluateParallel(PipelineHelper)}, * {@link TerminalOp#evaluateSequential(PipelineHelper)}, and {@link StatefulOp#evaluateParallel(PipelineHelper)}, * methods, which can use the {@code PipelineHelper} to access the source {@code Spliterator} for the pipeline, * information about the pipeline such as input shape, output shape, stream flags, and size, and use the helper * methods such as {@link #into(Sink, Spliterator)}, {@link #intoWrapped(Sink, Spliterator)}, * and {@link #wrapSink(Sink)} to execute pipeline operations. * * @param Type of input elements to the pipeline * @param Type of output elements from the pipeline * @since 1.8 */ interface PipelineHelper { /** * Get the {@code StreamShape} describing the input shape of the pipeline * @return The input shape of the pipeline */ StreamShape getInputShape(); /** * Get the {@code StreamShape} describing the output shape of the pipeline * @return The output shape of the pipeline */ StreamShape getOutputShape(); /** * Get the combined stream and operation flags for the output of the pipeline. This will incorporate * stream flags from the stream source, all the intermediate operations and the terminal operation. * * @return the combined stream and operation flags for the output of the pipeline * @see StreamOpFlag */ int getStreamAndOpFlags(); /** * Get the operation flags for the terminal operation. * * @return the operation flags for the terminal operation. * @see StreamOpFlag */ // @@@ Specifying this concisely is somewhat complicated since since the actual terminal operation flags // are masked by StreamOpFlag.UPSTREAM_TERMINAL_OP_MASK as the flags propagate upstream through parallel // pipeline chunks int getTerminalOpFlags(); /** * Returns whether this pipeline is parallel or sequential * * @return true if the pipeline is a parallel pipeline, otherwise false */ boolean isParallel(); /** * Get the {@code Spliterator} for the source of the pipeline. This {@code Spliterator} reflects * only the source elements, not the actions of any of the intermediate stages. * * @return the source spliterator */ Spliterator sourceSpliterator(); /** * Returns the exact output size of the portion of the output resulting from applying the pipeline stages * described by this {@code PipelineHelper} to the the portion of the input described by the provided * {@code Spliterator}, if known. If not known or known infinite, will return {@code -1}. * * @apiNote * The exact output size is known if the {@code Spliterator} has the {@code SIZED} characteristic, * and the operation flags {@link StreamOpFlag#SIZED} is known on the combined stream and operation * flags. * * @param spliterator the spliterator describing the relevant portion of the source data * @return the exact size if known, or -1 if infinite or unknown */ long exactOutputSizeIfKnown(Spliterator spliterator); /** * Apply the pipeline stages described by this {@code PipelineHelper} to the provided {@code Spliterator} and * send the results to the provided {@code Sink}. * * @implSpec * The implementation behaves as if: *
     *     intoWrapped(wrapSink(sink), spliterator);
     * 
* * @param sink the {@code Sink} to receive the results * @param spliterator the spliterator describing the portion of the source input to process */ > S into(S sink, Spliterator spliterator); /** * Push elements obtained from the {@code Spliterator} into the provided {@code Sink}. If the stream * pipeline is known to have short-circuiting stages in it (see {@link StreamOpFlag#SHORT_CIRCUIT}), then the * elements are delivered as per {@link #intoWrappedWithCancel(Sink, Spliterator)}. * * @implSpec * This method conforms to the {@code Sink} protocol of calling {@code Sink.begin} before pushing * elements, via {@code Sink.accept}, and calling {@code Sink.end} after all elements have * been pushed. * * @param wrappedSink the destination {@code Sink} * @param spliterator the source {@code Spliterator} */ void intoWrapped(Sink wrappedSink, Spliterator spliterator); /** * Push elements obtained from the {@code Spliterator} into the provided {@code Sink}, checking * {@link Sink#cancellationRequested()} after each element, and stopping if cancellation is requested. * * @implSpec * This method conforms to the {@code Sink} protocol of calling {@code Sink.begin} before pushing * elements, via {@code Sink.accept}, and calling {@code Sink.end} after all elements have * been pushed or if cancellation is requested. * * @param wrappedSink the destination {@code Sink} * @param spliterator the source {@code Spliterator} */ void intoWrappedWithCancel(Sink wrappedSink, Spliterator spliterator); /** * Take a {@code Sink} that accepts elements of the output type of the {@code PipelineHelper}, and wrap it with * a {@code Sink} that accepts elements of the input type and implements all the intermediate operations described * by this {@code PipelineHelper}, delivering the result into the provided {@code Sink}. * * @param sink the {@code Sink} to receive the results * @return a {@code Sink} that implements the pipeline stages and sends results to the provided {@code Sink} */ Sink wrapSink(Sink sink); /** * Construct a @{link Node.Builder} compatible with the output shape of this {@code PipelineHelper} * * @param exactSizeIfKnown if >=0 then a builder will be created that has a fixed capacity of exactly * sizeIfKnown elements; if < 0 then the builder has variable capacity. * A fixed capacity builder will fail if an element is added and the builder has reached * capacity. * @return A {@code Node.Builder} compatible with the output shape of this {@code PipelineHelper} */ Node.Builder makeNodeBuilder(long exactSizeIfKnown); /** * Collect all output elements resulting from applying the pipeline stages to the source {@code Spliterator} * into a {@code Node}. * * @implSpec * If the pipeline has no intermediate operations and the source is backed by a {@code Node} then * that {@code Node} will be returned or flattened and then returned. This reduces copying for a pipeline * consisting of a stateful operation followed by a terminal operation that returns an array, such as: *
{@code
     *     stream.sorted().toArray();
     * }
* * @param flatten if true and the pipeline is a parallel pipeline then the {@code Node} returned * will contain no children, otherwise the {@code Node} may represent the root in a * tree that reflects the shape of the computation tree. * @return the {@code Node} containing all output elements */ Node collectOutput(boolean flatten); /** * Get an array factory associated with the output type of this pipeline. * * @return a factory for arrays of the output type of this pipeline. */ IntFunction arrayGenerator(); }