17182
|
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.Objects;
|
|
28 |
import java.util.Spliterator;
|
|
29 |
import java.util.function.IntFunction;
|
|
30 |
import java.util.function.Supplier;
|
|
31 |
|
|
32 |
/**
|
|
33 |
* Abstract base class for "pipeline" classes, which are the core
|
|
34 |
* implementations of the Stream interface and its primitive specializations.
|
|
35 |
* Manages construction and evaluation of stream pipelines.
|
|
36 |
*
|
|
37 |
* <p>An {@code AbstractPipeline} represents an initial portion of a stream
|
|
38 |
* pipeline, encapsulating a stream source and zero or more intermediate
|
|
39 |
* operations. The individual {@code AbstractPipeline} objects are often
|
|
40 |
* referred to as <em>stages</em>, where each stage describes either the stream
|
|
41 |
* source or an intermediate operation.
|
|
42 |
*
|
|
43 |
* <p>A concrete intermediate stage is generally built from an
|
|
44 |
* {@code AbstractPipeline}, a shape-specific pipeline class which extends it
|
|
45 |
* (e.g., {@code IntPipeline}) which is also abstract, and an operation-specific
|
|
46 |
* concrete class which extends that. {@code AbstractPipeline} contains most of
|
|
47 |
* the mechanics of evaluating the pipeline, and implements methods that will be
|
|
48 |
* used by the operation; the shape-specific classes add helper methods for
|
|
49 |
* dealing with collection of results into the appropriate shape-specific
|
|
50 |
* containers.
|
|
51 |
*
|
|
52 |
* <p>After chaining a new intermediate operation, or executing a terminal
|
|
53 |
* operation, the stream is considered to be consumed, and no more intermediate
|
|
54 |
* or terminal operations are permitted on this stream instance.
|
|
55 |
*
|
|
56 |
* <p>{@code AbstractPipeline} implements a number of methods that are
|
|
57 |
* specified in {@link BaseStream}, though it does not implement
|
|
58 |
* {@code BaseStream} directly. Subclasses of {@code AbstractPipeline}
|
|
59 |
* will generally implement {@code BaseStream}.
|
|
60 |
*
|
|
61 |
* @implNote
|
|
62 |
* <p>For sequential streams, and parallel streams without
|
|
63 |
* <a href="package-summary.html#StreamOps">stateful intermediate
|
|
64 |
* operations</a>, parallel streams, pipeline evaluation is done in a single
|
|
65 |
* pass that "jams" all the operations together. For parallel streams with
|
|
66 |
* stateful operations, execution is divided into segments, where each
|
|
67 |
* stateful operations marks the end of a segment, and each segment is
|
|
68 |
* evaluated separately and the result used as the input to the next
|
|
69 |
* segment. In all cases, the source data is not consumed until a terminal
|
|
70 |
* operation begins.
|
|
71 |
*
|
|
72 |
* @param <E_IN> type of input elements
|
|
73 |
* @param <E_OUT> type of output elements
|
|
74 |
* @param <S> type of the subclass implementing {@code BaseStream}
|
|
75 |
* @since 1.8
|
|
76 |
*/
|
|
77 |
abstract class AbstractPipeline<E_IN, E_OUT, S extends BaseStream<E_OUT, S>>
|
|
78 |
extends PipelineHelper<E_OUT> {
|
|
79 |
/**
|
|
80 |
* Backlink to the head of the pipeline chain (self if this is the source
|
|
81 |
* stage).
|
|
82 |
*/
|
|
83 |
private final AbstractPipeline sourceStage;
|
|
84 |
|
|
85 |
/**
|
|
86 |
* The "upstream" pipeline, or null if this is the source stage.
|
|
87 |
*/
|
|
88 |
private final AbstractPipeline previousStage;
|
|
89 |
|
|
90 |
/**
|
|
91 |
* The operation flags for the intermediate operation represented by this
|
|
92 |
* pipeline object.
|
|
93 |
*/
|
|
94 |
protected final int sourceOrOpFlags;
|
|
95 |
|
|
96 |
/**
|
|
97 |
* The next stage in the pipeline, or null if this is the last stage.
|
|
98 |
* Effectively final at the point of linking to the next pipeline.
|
|
99 |
*/
|
|
100 |
private AbstractPipeline nextStage;
|
|
101 |
|
|
102 |
/**
|
|
103 |
* The number of intermediate operations between this pipeline object
|
|
104 |
* and the stream source if sequential, or the previous stateful if parallel.
|
|
105 |
* Valid at the point of pipeline preparation for evaluation.
|
|
106 |
*/
|
|
107 |
private int depth;
|
|
108 |
|
|
109 |
/**
|
|
110 |
* The combined source and operation flags for the source and all operations
|
|
111 |
* up to and including the operation represented by this pipeline object.
|
|
112 |
* Valid at the point of pipeline preparation for evaluation.
|
|
113 |
*/
|
|
114 |
private int combinedFlags;
|
|
115 |
|
|
116 |
/**
|
|
117 |
* The source spliterator. Only valid for the head pipeline.
|
|
118 |
* Before the pipeline is consumed if non-null then {@code sourceSupplier}
|
|
119 |
* must be null. After the pipeline is consumed if non-null then is set to
|
|
120 |
* null.
|
|
121 |
*/
|
|
122 |
private Spliterator<?> sourceSpliterator;
|
|
123 |
|
|
124 |
/**
|
|
125 |
* The source supplier. Only valid for the head pipeline. Before the
|
|
126 |
* pipeline is consumed if non-null then {@code sourceSpliterator} must be
|
|
127 |
* null. After the pipeline is consumed if non-null then is set to null.
|
|
128 |
*/
|
|
129 |
private Supplier<? extends Spliterator<?>> sourceSupplier;
|
|
130 |
|
|
131 |
/**
|
|
132 |
* True if this pipeline has been linked or consumed
|
|
133 |
*/
|
|
134 |
private boolean linkedOrConsumed;
|
|
135 |
|
|
136 |
/**
|
|
137 |
* True if there are any stateful ops in the pipeline; only valid for the
|
|
138 |
* source stage.
|
|
139 |
*/
|
|
140 |
private boolean sourceAnyStateful;
|
|
141 |
|
|
142 |
/**
|
|
143 |
* True if pipeline is parallel, otherwise the pipeline is sequential; only
|
|
144 |
* valid for the source stage.
|
|
145 |
*/
|
|
146 |
private boolean parallel;
|
|
147 |
|
|
148 |
/**
|
|
149 |
* Constructor for the head of a stream pipeline.
|
|
150 |
*
|
|
151 |
* @param source {@code Supplier<Spliterator>} describing the stream source
|
|
152 |
* @param sourceFlags The source flags for the stream source, described in
|
|
153 |
* {@link StreamOpFlag}
|
|
154 |
* @param parallel True if the pipeline is parallel
|
|
155 |
*/
|
|
156 |
AbstractPipeline(Supplier<? extends Spliterator<?>> source,
|
|
157 |
int sourceFlags, boolean parallel) {
|
|
158 |
this.previousStage = null;
|
|
159 |
this.sourceSupplier = source;
|
|
160 |
this.sourceStage = this;
|
|
161 |
this.sourceOrOpFlags = sourceFlags & StreamOpFlag.STREAM_MASK;
|
|
162 |
// The following is an optimization of:
|
|
163 |
// StreamOpFlag.combineOpFlags(sourceOrOpFlags, StreamOpFlag.INITIAL_OPS_VALUE);
|
|
164 |
this.combinedFlags = (~(sourceOrOpFlags << 1)) & StreamOpFlag.INITIAL_OPS_VALUE;
|
|
165 |
this.depth = 0;
|
|
166 |
this.parallel = parallel;
|
|
167 |
}
|
|
168 |
|
|
169 |
/**
|
|
170 |
* Constructor for the head of a stream pipeline.
|
|
171 |
*
|
|
172 |
* @param source {@code Spliterator} describing the stream source
|
|
173 |
* @param sourceFlags the source flags for the stream source, described in
|
|
174 |
* {@link StreamOpFlag}
|
|
175 |
* @param parallel {@code true} if the pipeline is parallel
|
|
176 |
*/
|
|
177 |
AbstractPipeline(Spliterator<?> source,
|
|
178 |
int sourceFlags, boolean parallel) {
|
|
179 |
this.previousStage = null;
|
|
180 |
this.sourceSpliterator = source;
|
|
181 |
this.sourceStage = this;
|
|
182 |
this.sourceOrOpFlags = sourceFlags & StreamOpFlag.STREAM_MASK;
|
|
183 |
// The following is an optimization of:
|
|
184 |
// StreamOpFlag.combineOpFlags(sourceOrOpFlags, StreamOpFlag.INITIAL_OPS_VALUE);
|
|
185 |
this.combinedFlags = (~(sourceOrOpFlags << 1)) & StreamOpFlag.INITIAL_OPS_VALUE;
|
|
186 |
this.depth = 0;
|
|
187 |
this.parallel = parallel;
|
|
188 |
}
|
|
189 |
|
|
190 |
/**
|
|
191 |
* Constructor for appending an intermediate operation stage onto an
|
|
192 |
* existing pipeline.
|
|
193 |
*
|
|
194 |
* @param previousStage the upstream pipeline stage
|
|
195 |
* @param opFlags the operation flags for the new stage, described in
|
|
196 |
* {@link StreamOpFlag}
|
|
197 |
*/
|
|
198 |
AbstractPipeline(AbstractPipeline<?, E_IN, ?> previousStage, int opFlags) {
|
|
199 |
if (previousStage.linkedOrConsumed)
|
|
200 |
throw new IllegalStateException("stream has already been operated upon");
|
|
201 |
previousStage.linkedOrConsumed = true;
|
|
202 |
previousStage.nextStage = this;
|
|
203 |
|
|
204 |
this.previousStage = previousStage;
|
|
205 |
this.sourceOrOpFlags = opFlags & StreamOpFlag.OP_MASK;
|
|
206 |
this.combinedFlags = StreamOpFlag.combineOpFlags(opFlags, previousStage.combinedFlags);
|
|
207 |
this.sourceStage = previousStage.sourceStage;
|
|
208 |
if (opIsStateful())
|
|
209 |
sourceStage.sourceAnyStateful = true;
|
|
210 |
this.depth = previousStage.depth + 1;
|
|
211 |
}
|
|
212 |
|
|
213 |
|
|
214 |
// Terminal evaluation methods
|
|
215 |
|
|
216 |
/**
|
|
217 |
* Evaluate the pipeline with a terminal operation to produce a result.
|
|
218 |
*
|
|
219 |
* @param <R> the type of result
|
|
220 |
* @param terminalOp the terminal operation to be applied to the pipeline.
|
|
221 |
* @return the result
|
|
222 |
*/
|
|
223 |
final <R> R evaluate(TerminalOp<E_OUT, R> terminalOp) {
|
|
224 |
assert getOutputShape() == terminalOp.inputShape();
|
|
225 |
if (linkedOrConsumed)
|
|
226 |
throw new IllegalStateException("stream has already been operated upon");
|
|
227 |
linkedOrConsumed = true;
|
|
228 |
|
|
229 |
return isParallel()
|
|
230 |
? (R) terminalOp.evaluateParallel(this, sourceSpliterator(terminalOp.getOpFlags()))
|
|
231 |
: (R) terminalOp.evaluateSequential(this, sourceSpliterator(terminalOp.getOpFlags()));
|
|
232 |
}
|
|
233 |
|
|
234 |
/**
|
|
235 |
* Collect the elements output from the pipeline stage.
|
|
236 |
*
|
|
237 |
* @param generator the array generator to be used to create array instances
|
|
238 |
* @return a flat array-backed Node that holds the collected output elements
|
|
239 |
*/
|
|
240 |
final Node<E_OUT> evaluateToArrayNode(IntFunction<E_OUT[]> generator) {
|
|
241 |
if (linkedOrConsumed)
|
|
242 |
throw new IllegalStateException("stream has already been operated upon");
|
|
243 |
linkedOrConsumed = true;
|
|
244 |
|
|
245 |
// If the last intermediate operation is stateful then
|
|
246 |
// evaluate directly to avoid an extra collection step
|
|
247 |
if (isParallel() && previousStage != null && opIsStateful()) {
|
|
248 |
return opEvaluateParallel(previousStage, previousStage.sourceSpliterator(0), generator);
|
|
249 |
}
|
|
250 |
else {
|
|
251 |
return evaluate(sourceSpliterator(0), true, generator);
|
|
252 |
}
|
|
253 |
}
|
|
254 |
|
|
255 |
/**
|
|
256 |
* Gets the source stage spliterator if this pipeline stage is the source
|
|
257 |
* stage. The pipeline is consumed after this method is called and
|
|
258 |
* returns successfully.
|
|
259 |
*
|
|
260 |
* @return the source stage spliterator
|
|
261 |
* @throws IllegalStateException if this pipeline stage is not the source
|
|
262 |
* stage.
|
|
263 |
*/
|
|
264 |
final Spliterator<E_OUT> sourceStageSpliterator() {
|
|
265 |
if (this != sourceStage)
|
|
266 |
throw new IllegalStateException();
|
|
267 |
|
|
268 |
if (linkedOrConsumed)
|
|
269 |
throw new IllegalStateException("stream has already been operated upon");
|
|
270 |
linkedOrConsumed = true;
|
|
271 |
|
|
272 |
if (sourceStage.sourceSpliterator != null) {
|
|
273 |
Spliterator<E_OUT> s = sourceStage.sourceSpliterator;
|
|
274 |
sourceStage.sourceSpliterator = null;
|
|
275 |
return s;
|
|
276 |
}
|
|
277 |
else if (sourceStage.sourceSupplier != null) {
|
|
278 |
Spliterator<E_OUT> s = (Spliterator<E_OUT>) sourceStage.sourceSupplier.get();
|
|
279 |
sourceStage.sourceSupplier = null;
|
|
280 |
return s;
|
|
281 |
}
|
|
282 |
else {
|
|
283 |
throw new IllegalStateException("source already consumed");
|
|
284 |
}
|
|
285 |
}
|
|
286 |
|
|
287 |
// BaseStream
|
|
288 |
|
|
289 |
/**
|
|
290 |
* Implements {@link BaseStream#sequential()}
|
|
291 |
*/
|
|
292 |
public final S sequential() {
|
|
293 |
sourceStage.parallel = false;
|
|
294 |
return (S) this;
|
|
295 |
}
|
|
296 |
|
|
297 |
/**
|
|
298 |
* Implements {@link BaseStream#parallel()}
|
|
299 |
*/
|
|
300 |
public final S parallel() {
|
|
301 |
sourceStage.parallel = true;
|
|
302 |
return (S) this;
|
|
303 |
}
|
|
304 |
|
|
305 |
// Primitive specialization use co-variant overrides, hence is not final
|
|
306 |
/**
|
|
307 |
* Implements {@link BaseStream#spliterator()}
|
|
308 |
*/
|
|
309 |
public Spliterator<E_OUT> spliterator() {
|
|
310 |
if (linkedOrConsumed)
|
|
311 |
throw new IllegalStateException("stream has already been operated upon");
|
|
312 |
linkedOrConsumed = true;
|
|
313 |
|
|
314 |
if (this == sourceStage) {
|
|
315 |
if (sourceStage.sourceSpliterator != null) {
|
|
316 |
Spliterator<E_OUT> s = sourceStage.sourceSpliterator;
|
|
317 |
sourceStage.sourceSpliterator = null;
|
|
318 |
return s;
|
|
319 |
}
|
|
320 |
else if (sourceStage.sourceSupplier != null) {
|
|
321 |
Supplier<Spliterator<E_OUT>> s = sourceStage.sourceSupplier;
|
|
322 |
sourceStage.sourceSupplier = null;
|
|
323 |
return lazySpliterator(s);
|
|
324 |
}
|
|
325 |
else {
|
|
326 |
throw new IllegalStateException("source already consumed");
|
|
327 |
}
|
|
328 |
}
|
|
329 |
else {
|
|
330 |
return wrap(this, () -> sourceSpliterator(0), isParallel());
|
|
331 |
}
|
|
332 |
}
|
|
333 |
|
|
334 |
/**
|
|
335 |
* Implements {@link BaseStream#isParallel()}
|
|
336 |
*/
|
|
337 |
public final boolean isParallel() {
|
|
338 |
return sourceStage.parallel;
|
|
339 |
}
|
|
340 |
|
|
341 |
|
|
342 |
/**
|
|
343 |
* Returns the composition of stream flags of the stream source and all
|
|
344 |
* intermediate operations.
|
|
345 |
*
|
|
346 |
* @return the composition of stream flags of the stream source and all
|
|
347 |
* intermediate operations
|
|
348 |
* @see StreamOpFlag
|
|
349 |
*/
|
|
350 |
final int getStreamFlags() {
|
|
351 |
return StreamOpFlag.toStreamFlags(combinedFlags);
|
|
352 |
}
|
|
353 |
|
|
354 |
/**
|
|
355 |
* Prepare the pipeline for a parallel execution. As the pipeline is built,
|
|
356 |
* the flags and depth indicators are set up for a sequential execution.
|
|
357 |
* If the execution is parallel, and there are any stateful operations, then
|
|
358 |
* some of these need to be adjusted, as well as adjusting for flags from
|
|
359 |
* the terminal operation (such as back-propagating UNORDERED).
|
|
360 |
* Need not be called for a sequential execution.
|
|
361 |
*
|
|
362 |
* @param terminalFlags Operation flags for the terminal operation
|
|
363 |
*/
|
|
364 |
private void parallelPrepare(int terminalFlags) {
|
|
365 |
AbstractPipeline backPropagationHead = sourceStage;
|
|
366 |
if (sourceStage.sourceAnyStateful) {
|
|
367 |
int depth = 1;
|
|
368 |
for (AbstractPipeline u = sourceStage, p = sourceStage.nextStage;
|
|
369 |
p != null;
|
|
370 |
u = p, p = p.nextStage) {
|
|
371 |
int thisOpFlags = p.sourceOrOpFlags;
|
|
372 |
if (p.opIsStateful()) {
|
|
373 |
// If the stateful operation is a short-circuit operation
|
|
374 |
// then move the back propagation head forwards
|
|
375 |
// NOTE: there are no size-injecting ops
|
|
376 |
if (StreamOpFlag.SHORT_CIRCUIT.isKnown(thisOpFlags)) {
|
|
377 |
backPropagationHead = p;
|
|
378 |
}
|
|
379 |
|
|
380 |
depth = 0;
|
|
381 |
// The following injects size, it is equivalent to:
|
|
382 |
// StreamOpFlag.combineOpFlags(StreamOpFlag.IS_SIZED, p.combinedFlags);
|
|
383 |
thisOpFlags = (thisOpFlags & ~StreamOpFlag.NOT_SIZED) | StreamOpFlag.IS_SIZED;
|
|
384 |
}
|
|
385 |
p.depth = depth++;
|
|
386 |
p.combinedFlags = StreamOpFlag.combineOpFlags(thisOpFlags, u.combinedFlags);
|
|
387 |
}
|
|
388 |
}
|
|
389 |
|
|
390 |
// Apply the upstream terminal flags
|
|
391 |
if (terminalFlags != 0) {
|
|
392 |
int upstreamTerminalFlags = terminalFlags & StreamOpFlag.UPSTREAM_TERMINAL_OP_MASK;
|
|
393 |
for (AbstractPipeline p = backPropagationHead; p.nextStage != null; p = p.nextStage) {
|
|
394 |
p.combinedFlags = StreamOpFlag.combineOpFlags(upstreamTerminalFlags, p.combinedFlags);
|
|
395 |
}
|
|
396 |
|
|
397 |
combinedFlags = StreamOpFlag.combineOpFlags(terminalFlags, combinedFlags);
|
|
398 |
}
|
|
399 |
}
|
|
400 |
|
|
401 |
/**
|
|
402 |
* Get the source spliterator for this pipeline stage. For a sequential or
|
|
403 |
* stateless parallel pipeline, this is the source spliterator. For a
|
|
404 |
* stateful parallel pipeline, this is a spliterator describing the results
|
|
405 |
* of all computations up to and including the most recent stateful
|
|
406 |
* operation.
|
|
407 |
*/
|
|
408 |
private Spliterator<?> sourceSpliterator(int terminalFlags) {
|
|
409 |
// Get the source spliterator of the pipeline
|
|
410 |
Spliterator<?> spliterator = null;
|
|
411 |
if (sourceStage.sourceSpliterator != null) {
|
|
412 |
spliterator = sourceStage.sourceSpliterator;
|
|
413 |
sourceStage.sourceSpliterator = null;
|
|
414 |
}
|
|
415 |
else if (sourceStage.sourceSupplier != null) {
|
|
416 |
spliterator = (Spliterator<?>) sourceStage.sourceSupplier.get();
|
|
417 |
sourceStage.sourceSupplier = null;
|
|
418 |
}
|
|
419 |
else {
|
|
420 |
throw new IllegalStateException("source already consumed");
|
|
421 |
}
|
|
422 |
|
|
423 |
if (isParallel()) {
|
|
424 |
// @@@ Merge parallelPrepare with the loop below and use the
|
|
425 |
// spliterator characteristics to determine if SIZED
|
|
426 |
// should be injected
|
|
427 |
parallelPrepare(terminalFlags);
|
|
428 |
|
|
429 |
// Adapt the source spliterator, evaluating each stateful op
|
|
430 |
// in the pipeline up to and including this pipeline stage
|
|
431 |
for (AbstractPipeline u = sourceStage, p = sourceStage.nextStage, e = this;
|
|
432 |
u != e;
|
|
433 |
u = p, p = p.nextStage) {
|
|
434 |
|
|
435 |
if (p.opIsStateful()) {
|
|
436 |
spliterator = p.opEvaluateParallelLazy(u, spliterator);
|
|
437 |
}
|
|
438 |
}
|
|
439 |
}
|
|
440 |
else if (terminalFlags != 0) {
|
|
441 |
combinedFlags = StreamOpFlag.combineOpFlags(terminalFlags, combinedFlags);
|
|
442 |
}
|
|
443 |
|
|
444 |
return spliterator;
|
|
445 |
}
|
|
446 |
|
|
447 |
|
|
448 |
// PipelineHelper
|
|
449 |
|
|
450 |
@Override
|
|
451 |
final <P_IN> long exactOutputSizeIfKnown(Spliterator<P_IN> spliterator) {
|
|
452 |
return StreamOpFlag.SIZED.isKnown(getStreamAndOpFlags()) ? spliterator.getExactSizeIfKnown() : -1;
|
|
453 |
}
|
|
454 |
|
|
455 |
@Override
|
|
456 |
final <P_IN, S extends Sink<E_OUT>> S wrapAndCopyInto(S sink, Spliterator<P_IN> spliterator) {
|
|
457 |
copyInto(wrapSink(Objects.requireNonNull(sink)), spliterator);
|
|
458 |
return sink;
|
|
459 |
}
|
|
460 |
|
|
461 |
@Override
|
|
462 |
final <P_IN> void copyInto(Sink<P_IN> wrappedSink, Spliterator<P_IN> spliterator) {
|
|
463 |
Objects.requireNonNull(wrappedSink);
|
|
464 |
|
|
465 |
if (!StreamOpFlag.SHORT_CIRCUIT.isKnown(getStreamAndOpFlags())) {
|
|
466 |
wrappedSink.begin(spliterator.getExactSizeIfKnown());
|
|
467 |
spliterator.forEachRemaining(wrappedSink);
|
|
468 |
wrappedSink.end();
|
|
469 |
}
|
|
470 |
else {
|
|
471 |
copyIntoWithCancel(wrappedSink, spliterator);
|
|
472 |
}
|
|
473 |
}
|
|
474 |
|
|
475 |
@Override
|
|
476 |
final <P_IN> void copyIntoWithCancel(Sink<P_IN> wrappedSink, Spliterator<P_IN> spliterator) {
|
|
477 |
AbstractPipeline p = AbstractPipeline.this;
|
|
478 |
while (p.depth > 0) {
|
|
479 |
p = p.previousStage;
|
|
480 |
}
|
|
481 |
wrappedSink.begin(spliterator.getExactSizeIfKnown());
|
|
482 |
p.forEachWithCancel(spliterator, wrappedSink);
|
|
483 |
wrappedSink.end();
|
|
484 |
}
|
|
485 |
|
|
486 |
@Override
|
|
487 |
final int getStreamAndOpFlags() {
|
|
488 |
return combinedFlags;
|
|
489 |
}
|
|
490 |
|
|
491 |
final boolean isOrdered() {
|
|
492 |
return StreamOpFlag.ORDERED.isKnown(combinedFlags);
|
|
493 |
}
|
|
494 |
|
|
495 |
@Override
|
|
496 |
final <P_IN> Sink<P_IN> wrapSink(Sink<E_OUT> sink) {
|
|
497 |
Objects.requireNonNull(sink);
|
|
498 |
|
|
499 |
for (AbstractPipeline p=AbstractPipeline.this; p.depth > 0; p=p.previousStage) {
|
|
500 |
sink = p.opWrapSink(p.previousStage.combinedFlags, sink);
|
|
501 |
}
|
|
502 |
return (Sink<P_IN>) sink;
|
|
503 |
}
|
|
504 |
|
|
505 |
@Override
|
|
506 |
@SuppressWarnings("unchecked")
|
|
507 |
final <P_IN> Node<E_OUT> evaluate(Spliterator<P_IN> spliterator,
|
|
508 |
boolean flatten,
|
|
509 |
IntFunction<E_OUT[]> generator) {
|
|
510 |
if (isParallel()) {
|
|
511 |
// @@@ Optimize if op of this pipeline stage is a stateful op
|
|
512 |
return evaluateToNode(this, spliterator, flatten, generator);
|
|
513 |
}
|
|
514 |
else {
|
|
515 |
Node.Builder<E_OUT> nb = makeNodeBuilder(
|
|
516 |
exactOutputSizeIfKnown(spliterator), generator);
|
|
517 |
return wrapAndCopyInto(nb, spliterator).build();
|
|
518 |
}
|
|
519 |
}
|
|
520 |
|
|
521 |
|
|
522 |
// Shape-specific abstract methods, implemented by XxxPipeline classes
|
|
523 |
|
|
524 |
/**
|
|
525 |
* Get the output shape of the pipeline. If the pipeline is the head,
|
|
526 |
* then it's output shape corresponds to the shape of the source.
|
|
527 |
* Otherwise, it's output shape corresponds to the output shape of the
|
|
528 |
* associated operation.
|
|
529 |
*
|
|
530 |
* @return the output shape
|
|
531 |
*/
|
|
532 |
abstract StreamShape getOutputShape();
|
|
533 |
|
|
534 |
/**
|
|
535 |
* Collect elements output from a pipeline into a Node that holds elements
|
|
536 |
* of this shape.
|
|
537 |
*
|
|
538 |
* @param helper the pipeline helper describing the pipeline stages
|
|
539 |
* @param spliterator the source spliterator
|
|
540 |
* @param flattenTree true if the returned node should be flattened
|
|
541 |
* @param generator the array generator
|
|
542 |
* @return a Node holding the output of the pipeline
|
|
543 |
*/
|
|
544 |
abstract <P_IN> Node<E_OUT> evaluateToNode(PipelineHelper<E_OUT> helper,
|
|
545 |
Spliterator<P_IN> spliterator,
|
|
546 |
boolean flattenTree,
|
|
547 |
IntFunction<E_OUT[]> generator);
|
|
548 |
|
|
549 |
/**
|
|
550 |
* Create a spliterator that wraps a source spliterator, compatible with
|
|
551 |
* this stream shape, and operations associated with a {@link
|
|
552 |
* PipelineHelper}.
|
|
553 |
*
|
|
554 |
* @param ph the pipeline helper describing the pipeline stages
|
|
555 |
* @param supplier the supplier of a spliterator
|
|
556 |
* @return a wrapping spliterator compatible with this shape
|
|
557 |
*/
|
|
558 |
abstract <P_IN> Spliterator<E_OUT> wrap(PipelineHelper<E_OUT> ph,
|
|
559 |
Supplier<Spliterator<P_IN>> supplier,
|
|
560 |
boolean isParallel);
|
|
561 |
|
|
562 |
/**
|
|
563 |
* Create a lazy spliterator that wraps and obtains the supplied the
|
|
564 |
* spliterator when a method is invoked on the lazy spliterator.
|
|
565 |
* @param supplier the supplier of a spliterator
|
|
566 |
*/
|
|
567 |
abstract Spliterator<E_OUT> lazySpliterator(Supplier<? extends Spliterator<E_OUT>> supplier);
|
|
568 |
|
|
569 |
/**
|
|
570 |
* Traverse the elements of a spliterator compatible with this stream shape,
|
|
571 |
* pushing those elements into a sink. If the sink requests cancellation,
|
|
572 |
* no further elements will be pulled or pushed.
|
|
573 |
*
|
|
574 |
* @param spliterator the spliterator to pull elements from
|
|
575 |
* @param sink the sink to push elements to
|
|
576 |
*/
|
|
577 |
abstract void forEachWithCancel(Spliterator<E_OUT> spliterator, Sink<E_OUT> sink);
|
|
578 |
|
|
579 |
/**
|
|
580 |
* Make a node builder compatible with this stream shape.
|
|
581 |
*
|
|
582 |
* @param exactSizeIfKnown if {@literal >=0}, then a node builder will be created that
|
|
583 |
* has a fixed capacity of at most sizeIfKnown elements. If {@literal < 0},
|
|
584 |
* then the node builder has an unfixed capacity. A fixed capacity node
|
|
585 |
* builder will throw exceptions if an element is added after builder has
|
|
586 |
* reached capacity, or is built before the builder has reached capacity.
|
|
587 |
* @param generator the array generator to be used to create instances of a
|
|
588 |
* T[] array. For implementations supporting primitive nodes, this parameter
|
|
589 |
* may be ignored.
|
|
590 |
* @return a node builder
|
|
591 |
*/
|
|
592 |
abstract Node.Builder<E_OUT> makeNodeBuilder(long exactSizeIfKnown,
|
|
593 |
IntFunction<E_OUT[]> generator);
|
|
594 |
|
|
595 |
|
|
596 |
// Op-specific abstract methods, implemented by the operation class
|
|
597 |
|
|
598 |
/**
|
|
599 |
* Returns whether this operation is stateful or not. If it is stateful,
|
|
600 |
* then the method
|
|
601 |
* {@link #opEvaluateParallel(PipelineHelper, java.util.Spliterator, java.util.function.IntFunction)}
|
|
602 |
* must be overridden.
|
|
603 |
*
|
|
604 |
* @return {@code true} if this operation is stateful
|
|
605 |
*/
|
|
606 |
abstract boolean opIsStateful();
|
|
607 |
|
|
608 |
/**
|
|
609 |
* Accepts a {@code Sink} which will receive the results of this operation,
|
|
610 |
* and return a {@code Sink} which accepts elements of the input type of
|
|
611 |
* this operation and which performs the operation, passing the results to
|
|
612 |
* the provided {@code Sink}.
|
|
613 |
*
|
|
614 |
* @apiNote
|
|
615 |
* The implementation may use the {@code flags} parameter to optimize the
|
|
616 |
* sink wrapping. For example, if the input is already {@code DISTINCT},
|
|
617 |
* the implementation for the {@code Stream#distinct()} method could just
|
|
618 |
* return the sink it was passed.
|
|
619 |
*
|
|
620 |
* @param flags The combined stream and operation flags up to, but not
|
|
621 |
* including, this operation
|
|
622 |
* @param sink sink to which elements should be sent after processing
|
|
623 |
* @return a sink which accepts elements, perform the operation upon
|
|
624 |
* each element, and passes the results (if any) to the provided
|
|
625 |
* {@code Sink}.
|
|
626 |
*/
|
|
627 |
abstract Sink<E_IN> opWrapSink(int flags, Sink<E_OUT> sink);
|
|
628 |
|
|
629 |
/**
|
|
630 |
* Performs a parallel evaluation of the operation using the specified
|
|
631 |
* {@code PipelineHelper} which describes the upstream intermediate
|
|
632 |
* operations. Only called on stateful operations. If {@link
|
|
633 |
* #opIsStateful()} returns true then implementations must override the
|
|
634 |
* default implementation.
|
|
635 |
*
|
|
636 |
* @implSpec The default implementation always throw
|
|
637 |
* {@code UnsupportedOperationException}.
|
|
638 |
*
|
|
639 |
* @param helper the pipeline helper describing the pipeline stages
|
|
640 |
* @param spliterator the source {@code Spliterator}
|
|
641 |
* @param generator the array generator
|
|
642 |
* @return a {@code Node} describing the result of the evaluation
|
|
643 |
*/
|
|
644 |
<P_IN> Node<E_OUT> opEvaluateParallel(PipelineHelper<E_OUT> helper,
|
|
645 |
Spliterator<P_IN> spliterator,
|
|
646 |
IntFunction<E_OUT[]> generator) {
|
|
647 |
throw new UnsupportedOperationException("Parallel evaluation is not supported");
|
|
648 |
}
|
|
649 |
|
|
650 |
/**
|
|
651 |
* Returns a {@code Spliterator} describing a parallel evaluation of the
|
|
652 |
* operation, using the specified {@code PipelineHelper} which describes the
|
|
653 |
* upstream intermediate operations. Only called on stateful operations.
|
|
654 |
* It is not necessary (though acceptable) to do a full computation of the
|
|
655 |
* result here; it is preferable, if possible, to describe the result via a
|
|
656 |
* lazily evaluated spliterator.
|
|
657 |
*
|
|
658 |
* @implSpec The default implementation behaves as if:
|
|
659 |
* <pre>{@code
|
|
660 |
* return evaluateParallel(helper, i -> (E_OUT[]) new
|
|
661 |
* Object[i]).spliterator();
|
|
662 |
* }</pre>
|
|
663 |
* and is suitable for implementations that cannot do better than a full
|
|
664 |
* synchronous evaluation.
|
|
665 |
*
|
|
666 |
* @param helper the pipeline helper
|
|
667 |
* @param spliterator the source {@code Spliterator}
|
|
668 |
* @return a {@code Spliterator} describing the result of the evaluation
|
|
669 |
*/
|
|
670 |
<P_IN> Spliterator<E_OUT> opEvaluateParallelLazy(PipelineHelper<E_OUT> helper,
|
|
671 |
Spliterator<P_IN> spliterator) {
|
|
672 |
return opEvaluateParallel(helper, spliterator, i -> (E_OUT[]) new Object[i]).spliterator();
|
|
673 |
}
|
|
674 |
}
|