• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 package java.util.stream;
26 
27 import java.util.Spliterator;
28 import java.util.function.IntFunction;
29 
30 /**
31  * Helper class for executing <a href="package-summary.html#StreamOps">
32  * stream pipelines</a>, capturing all of the information about a stream
33  * pipeline (output shape, intermediate operations, stream flags, parallelism,
34  * etc) in one place.
35  *
36  * <p>
37  * A {@code PipelineHelper} describes the initial segment of a stream pipeline,
38  * including its source, intermediate operations, and may additionally
39  * incorporate information about the terminal (or stateful) operation which
40  * follows the last intermediate operation described by this
41  * {@code PipelineHelper}. The {@code PipelineHelper} is passed to the
42  * {@link TerminalOp#evaluateParallel(PipelineHelper, java.util.Spliterator)},
43  * {@link TerminalOp#evaluateSequential(PipelineHelper, java.util.Spliterator)},
44  * and {@link AbstractPipeline#opEvaluateParallel(PipelineHelper, java.util.Spliterator,
45  * java.util.function.IntFunction)}, methods, which can use the
46  * {@code PipelineHelper} to access information about the pipeline such as
47  * head shape, stream flags, and size, and use the helper methods
48  * such as {@link #wrapAndCopyInto(Sink, Spliterator)},
49  * {@link #copyInto(Sink, Spliterator)}, and {@link #wrapSink(Sink)} to execute
50  * pipeline operations.
51  *
52  * @param <P_OUT> type of output elements from the pipeline
53  * @since 1.8
54  * @hide Visible for CTS testing only (OpenJDK8 tests).
55  */
56 // Android-changed: Made public for CTS tests only.
57 public abstract class PipelineHelper<P_OUT> {
58 
59     /**
60      * Gets the stream shape for the source of the pipeline segment.
61      *
62      * @return the stream shape for the source of the pipeline segment.
63      */
getSourceShape()64     abstract StreamShape getSourceShape();
65 
66     /**
67      * Gets the combined stream and operation flags for the output of the described
68      * pipeline.  This will incorporate stream flags from the stream source, all
69      * the intermediate operations and the terminal operation.
70      *
71      * @return the combined stream and operation flags
72      * @see StreamOpFlag
73      */
74     // Android-changed: Made public for CTS tests only.
getStreamAndOpFlags()75     public abstract int getStreamAndOpFlags();
76 
77     /**
78      * Returns the exact output size of the portion of the output resulting from
79      * applying the pipeline stages described by this {@code PipelineHelper} to
80      * the the portion of the input described by the provided
81      * {@code Spliterator}, if known.  If not known or known infinite, will
82      * return {@code -1}.
83      *
84      * @apiNote
85      * The exact output size is known if the {@code Spliterator} has the
86      * {@code SIZED} characteristic, and the operation flags
87      * {@link StreamOpFlag#SIZED} is known on the combined stream and operation
88      * flags.
89      *
90      * @param spliterator the spliterator describing the relevant portion of the
91      *        source data
92      * @return the exact size if known, or -1 if infinite or unknown
93      */
exactOutputSizeIfKnown(Spliterator<P_IN> spliterator)94     abstract<P_IN> long exactOutputSizeIfKnown(Spliterator<P_IN> spliterator);
95 
96     /**
97      * Applies the pipeline stages described by this {@code PipelineHelper} to
98      * the provided {@code Spliterator} and send the results to the provided
99      * {@code Sink}.
100      *
101      * @implSpec
102      * The implementation behaves as if:
103      * <pre>{@code
104      *     intoWrapped(wrapSink(sink), spliterator);
105      * }</pre>
106      *
107      * @param sink the {@code Sink} to receive the results
108      * @param spliterator the spliterator describing the source input to process
109      */
wrapAndCopyInto(S sink, Spliterator<P_IN> spliterator)110     abstract<P_IN, S extends Sink<P_OUT>> S wrapAndCopyInto(S sink, Spliterator<P_IN> spliterator);
111 
112     /**
113      * Pushes elements obtained from the {@code Spliterator} into the provided
114      * {@code Sink}.  If the stream pipeline is known to have short-circuiting
115      * stages in it (see {@link StreamOpFlag#SHORT_CIRCUIT}), the
116      * {@link Sink#cancellationRequested()} is checked after each
117      * element, stopping if cancellation is requested.
118      *
119      * @implSpec
120      * This method conforms to the {@code Sink} protocol of calling
121      * {@code Sink.begin} before pushing elements, via {@code Sink.accept}, and
122      * calling {@code Sink.end} after all elements have been pushed.
123      *
124      * @param wrappedSink the destination {@code Sink}
125      * @param spliterator the source {@code Spliterator}
126      */
copyInto(Sink<P_IN> wrappedSink, Spliterator<P_IN> spliterator)127     abstract<P_IN> void copyInto(Sink<P_IN> wrappedSink, Spliterator<P_IN> spliterator);
128 
129     /**
130      * Pushes elements obtained from the {@code Spliterator} into the provided
131      * {@code Sink}, checking {@link Sink#cancellationRequested()} after each
132      * element, and stopping if cancellation is requested.
133      *
134      * @implSpec
135      * This method conforms to the {@code Sink} protocol of calling
136      * {@code Sink.begin} before pushing elements, via {@code Sink.accept}, and
137      * calling {@code Sink.end} after all elements have been pushed or if
138      * cancellation is requested.
139      *
140      * @param wrappedSink the destination {@code Sink}
141      * @param spliterator the source {@code Spliterator}
142      */
copyIntoWithCancel(Sink<P_IN> wrappedSink, Spliterator<P_IN> spliterator)143     abstract <P_IN> void copyIntoWithCancel(Sink<P_IN> wrappedSink, Spliterator<P_IN> spliterator);
144 
145     /**
146      * Takes a {@code Sink} that accepts elements of the output type of the
147      * {@code PipelineHelper}, and wrap it with a {@code Sink} that accepts
148      * elements of the input type and implements all the intermediate operations
149      * described by this {@code PipelineHelper}, delivering the result into the
150      * provided {@code Sink}.
151      *
152      * @param sink the {@code Sink} to receive the results
153      * @return a {@code Sink} that implements the pipeline stages and sends
154      *         results to the provided {@code Sink}
155      */
156     // Android-changed: Made public for CTS tests only.
wrapSink(Sink<P_OUT> sink)157     public abstract<P_IN> Sink<P_IN> wrapSink(Sink<P_OUT> sink);
158 
159     /**
160      *
161      * @param spliterator
162      * @param <P_IN>
163      * @return
164      */
wrapSpliterator(Spliterator<P_IN> spliterator)165     abstract<P_IN> Spliterator<P_OUT> wrapSpliterator(Spliterator<P_IN> spliterator);
166 
167     /**
168      * Constructs a @{link Node.Builder} compatible with the output shape of
169      * this {@code PipelineHelper}.
170      *
171      * @param exactSizeIfKnown if >=0 then a builder will be created that has a
172      *        fixed capacity of exactly sizeIfKnown elements; if < 0 then the
173      *        builder has variable capacity.  A fixed capacity builder will fail
174      *        if an element is added after the builder has reached capacity.
175      * @param generator a factory function for array instances
176      * @return a {@code Node.Builder} compatible with the output shape of this
177      *         {@code PipelineHelper}
178      */
makeNodeBuilder(long exactSizeIfKnown, IntFunction<P_OUT[]> generator)179     abstract Node.Builder<P_OUT> makeNodeBuilder(long exactSizeIfKnown,
180                                                  IntFunction<P_OUT[]> generator);
181 
182     /**
183      * Collects all output elements resulting from applying the pipeline stages
184      * to the source {@code Spliterator} into a {@code Node}.
185      *
186      * @implNote
187      * If the pipeline has no intermediate operations and the source is backed
188      * by a {@code Node} then that {@code Node} will be returned (or flattened
189      * and then returned). This reduces copying for a pipeline consisting of a
190      * stateful operation followed by a terminal operation that returns an
191      * array, such as:
192      * <pre>{@code
193      *     stream.sorted().toArray();
194      * }</pre>
195      *
196      * @param spliterator the source {@code Spliterator}
197      * @param flatten if true and the pipeline is a parallel pipeline then the
198      *        {@code Node} returned will contain no children, otherwise the
199      *        {@code Node} may represent the root in a tree that reflects the
200      *        shape of the computation tree.
201      * @param generator a factory function for array instances
202      * @return the {@code Node} containing all output elements
203      */
204     // Android-changed: Made public for CTS tests only.
evaluate(Spliterator<P_IN> spliterator, boolean flatten, IntFunction<P_OUT[]> generator)205     public abstract<P_IN> Node<P_OUT> evaluate(Spliterator<P_IN> spliterator,
206                                         boolean flatten,
207                                         IntFunction<P_OUT[]> generator);
208 }
209