author | psandoz |
Mon, 10 Jun 2013 12:26:20 +0200 | |
changeset 18155 | 889970e5b728 |
parent 18154 | 5ede18269905 |
child 18171 | 2725a30c1a02 |
permissions | -rw-r--r-- |
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.IntSummaryStatistics; |
|
28 |
import java.util.Objects; |
|
29 |
import java.util.OptionalDouble; |
|
30 |
import java.util.OptionalInt; |
|
31 |
import java.util.PrimitiveIterator; |
|
32 |
import java.util.Spliterator; |
|
33 |
import java.util.Spliterators; |
|
34 |
import java.util.function.BiConsumer; |
|
35 |
import java.util.function.BinaryOperator; |
|
36 |
import java.util.function.IntBinaryOperator; |
|
37 |
import java.util.function.IntConsumer; |
|
38 |
import java.util.function.IntFunction; |
|
39 |
import java.util.function.IntPredicate; |
|
40 |
import java.util.function.IntToDoubleFunction; |
|
41 |
import java.util.function.IntToLongFunction; |
|
42 |
import java.util.function.IntUnaryOperator; |
|
43 |
import java.util.function.ObjIntConsumer; |
|
44 |
import java.util.function.Supplier; |
|
45 |
||
46 |
/** |
|
47 |
* Abstract base class for an intermediate pipeline stage or pipeline source |
|
48 |
* stage implementing whose elements are of type {@code int}. |
|
49 |
* |
|
50 |
* @param <E_IN> type of elements in the upstream source |
|
51 |
* @since 1.8 |
|
52 |
*/ |
|
53 |
abstract class IntPipeline<E_IN> |
|
54 |
extends AbstractPipeline<E_IN, Integer, IntStream> |
|
55 |
implements IntStream { |
|
56 |
||
57 |
/** |
|
58 |
* Constructor for the head of a stream pipeline. |
|
59 |
* |
|
60 |
* @param source {@code Supplier<Spliterator>} describing the stream source |
|
61 |
* @param sourceFlags The source flags for the stream source, described in |
|
62 |
* {@link StreamOpFlag} |
|
63 |
* @param parallel {@code true} if the pipeline is parallel |
|
64 |
*/ |
|
65 |
IntPipeline(Supplier<? extends Spliterator<Integer>> source, |
|
66 |
int sourceFlags, boolean parallel) { |
|
67 |
super(source, sourceFlags, parallel); |
|
68 |
} |
|
69 |
||
70 |
/** |
|
71 |
* Constructor for the head of a stream pipeline. |
|
72 |
* |
|
73 |
* @param source {@code Spliterator} describing the stream source |
|
74 |
* @param sourceFlags The source flags for the stream source, described in |
|
75 |
* {@link StreamOpFlag} |
|
76 |
* @param parallel {@code true} if the pipeline is parallel |
|
77 |
*/ |
|
78 |
IntPipeline(Spliterator<Integer> source, |
|
79 |
int sourceFlags, boolean parallel) { |
|
80 |
super(source, sourceFlags, parallel); |
|
81 |
} |
|
82 |
||
83 |
/** |
|
84 |
* Constructor for appending an intermediate operation onto an existing |
|
85 |
* pipeline. |
|
86 |
* |
|
87 |
* @param upstream the upstream element source |
|
88 |
* @param opFlags the operation flags for the new operation |
|
89 |
*/ |
|
90 |
IntPipeline(AbstractPipeline<?, E_IN, ?> upstream, int opFlags) { |
|
91 |
super(upstream, opFlags); |
|
92 |
} |
|
93 |
||
94 |
/** |
|
95 |
* Adapt a {@code Sink<Integer> to an {@code IntConsumer}, ideally simply |
|
96 |
* by casting. |
|
97 |
*/ |
|
98 |
private static IntConsumer adapt(Sink<Integer> sink) { |
|
99 |
if (sink instanceof IntConsumer) { |
|
100 |
return (IntConsumer) sink; |
|
101 |
} |
|
102 |
else { |
|
103 |
if (Tripwire.ENABLED) |
|
104 |
Tripwire.trip(AbstractPipeline.class, |
|
105 |
"using IntStream.adapt(Sink<Integer> s)"); |
|
106 |
return sink::accept; |
|
107 |
} |
|
108 |
} |
|
109 |
||
110 |
/** |
|
111 |
* Adapt a {@code Spliterator<Integer>} to a {@code Spliterator.OfInt}. |
|
112 |
* |
|
113 |
* @implNote |
|
114 |
* The implementation attempts to cast to a Spliterator.OfInt, and throws an |
|
115 |
* exception if this cast is not possible. |
|
116 |
*/ |
|
117 |
private static Spliterator.OfInt adapt(Spliterator<Integer> s) { |
|
118 |
if (s instanceof Spliterator.OfInt) { |
|
119 |
return (Spliterator.OfInt) s; |
|
120 |
} |
|
121 |
else { |
|
122 |
if (Tripwire.ENABLED) |
|
123 |
Tripwire.trip(AbstractPipeline.class, |
|
124 |
"using IntStream.adapt(Spliterator<Integer> s)"); |
|
125 |
throw new UnsupportedOperationException("IntStream.adapt(Spliterator<Integer> s)"); |
|
126 |
} |
|
127 |
} |
|
128 |
||
129 |
||
130 |
// Shape-specific methods |
|
131 |
||
132 |
@Override |
|
133 |
final StreamShape getOutputShape() { |
|
134 |
return StreamShape.INT_VALUE; |
|
135 |
} |
|
136 |
||
137 |
@Override |
|
138 |
final <P_IN> Node<Integer> evaluateToNode(PipelineHelper<Integer> helper, |
|
139 |
Spliterator<P_IN> spliterator, |
|
140 |
boolean flattenTree, |
|
141 |
IntFunction<Integer[]> generator) { |
|
142 |
return Nodes.collectInt(helper, spliterator, flattenTree); |
|
143 |
} |
|
144 |
||
145 |
@Override |
|
146 |
final <P_IN> Spliterator<Integer> wrap(PipelineHelper<Integer> ph, |
|
147 |
Supplier<Spliterator<P_IN>> supplier, |
|
148 |
boolean isParallel) { |
|
149 |
return new StreamSpliterators.IntWrappingSpliterator<>(ph, supplier, isParallel); |
|
150 |
} |
|
151 |
||
152 |
@Override |
|
153 |
final Spliterator.OfInt lazySpliterator(Supplier<? extends Spliterator<Integer>> supplier) { |
|
154 |
return new StreamSpliterators.DelegatingSpliterator.OfInt((Supplier<Spliterator.OfInt>) supplier); |
|
155 |
} |
|
156 |
||
157 |
@Override |
|
158 |
final void forEachWithCancel(Spliterator<Integer> spliterator, Sink<Integer> sink) { |
|
159 |
Spliterator.OfInt spl = adapt(spliterator); |
|
160 |
IntConsumer adaptedSink = adapt(sink); |
|
161 |
do { } while (!sink.cancellationRequested() && spl.tryAdvance(adaptedSink)); |
|
162 |
} |
|
163 |
||
164 |
@Override |
|
165 |
final Node.Builder<Integer> makeNodeBuilder(long exactSizeIfKnown, |
|
166 |
IntFunction<Integer[]> generator) { |
|
167 |
return Nodes.intBuilder(exactSizeIfKnown); |
|
168 |
} |
|
169 |
||
170 |
||
171 |
// IntStream |
|
172 |
||
173 |
@Override |
|
174 |
public final PrimitiveIterator.OfInt iterator() { |
|
18155
889970e5b728
8015792: Rename Spliterators.spliteratorFromIterator to Spliterators.iterator
psandoz
parents:
18154
diff
changeset
|
175 |
return Spliterators.iterator(spliterator()); |
17182 | 176 |
} |
177 |
||
178 |
@Override |
|
179 |
public final Spliterator.OfInt spliterator() { |
|
180 |
return adapt(super.spliterator()); |
|
181 |
} |
|
182 |
||
183 |
// Stateless intermediate ops from IntStream |
|
184 |
||
185 |
@Override |
|
18154
5ede18269905
8015798: Rename IntStream.longs/doubles and LongStream.doubles to asXxxStream
psandoz
parents:
17182
diff
changeset
|
186 |
public final LongStream asLongStream() { |
17182 | 187 |
return new LongPipeline.StatelessOp<Integer>(this, StreamShape.INT_VALUE, |
188 |
StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT) { |
|
189 |
@Override |
|
190 |
Sink<Integer> opWrapSink(int flags, Sink<Long> sink) { |
|
191 |
return new Sink.ChainedInt(sink) { |
|
192 |
@Override |
|
193 |
public void accept(int t) { |
|
194 |
downstream.accept((long) t); |
|
195 |
} |
|
196 |
}; |
|
197 |
} |
|
198 |
}; |
|
199 |
} |
|
200 |
||
201 |
@Override |
|
18154
5ede18269905
8015798: Rename IntStream.longs/doubles and LongStream.doubles to asXxxStream
psandoz
parents:
17182
diff
changeset
|
202 |
public final DoubleStream asDoubleStream() { |
17182 | 203 |
return new DoublePipeline.StatelessOp<Integer>(this, StreamShape.INT_VALUE, |
204 |
StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT) { |
|
205 |
@Override |
|
206 |
Sink<Integer> opWrapSink(int flags, Sink<Double> sink) { |
|
207 |
return new Sink.ChainedInt(sink) { |
|
208 |
@Override |
|
209 |
public void accept(int t) { |
|
210 |
downstream.accept((double) t); |
|
211 |
} |
|
212 |
}; |
|
213 |
} |
|
214 |
}; |
|
215 |
} |
|
216 |
||
217 |
@Override |
|
218 |
public final Stream<Integer> boxed() { |
|
219 |
return mapToObj(Integer::valueOf); |
|
220 |
} |
|
221 |
||
222 |
@Override |
|
223 |
public final IntStream map(IntUnaryOperator mapper) { |
|
224 |
Objects.requireNonNull(mapper); |
|
225 |
return new StatelessOp<Integer>(this, StreamShape.INT_VALUE, |
|
226 |
StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT) { |
|
227 |
@Override |
|
228 |
Sink<Integer> opWrapSink(int flags, Sink<Integer> sink) { |
|
229 |
return new Sink.ChainedInt(sink) { |
|
230 |
@Override |
|
231 |
public void accept(int t) { |
|
232 |
downstream.accept(mapper.applyAsInt(t)); |
|
233 |
} |
|
234 |
}; |
|
235 |
} |
|
236 |
}; |
|
237 |
} |
|
238 |
||
239 |
@Override |
|
240 |
public final <U> Stream<U> mapToObj(IntFunction<? extends U> mapper) { |
|
241 |
Objects.requireNonNull(mapper); |
|
242 |
return new ReferencePipeline.StatelessOp<Integer, U>(this, StreamShape.INT_VALUE, |
|
243 |
StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT) { |
|
244 |
@Override |
|
245 |
Sink<Integer> opWrapSink(int flags, Sink<U> sink) { |
|
246 |
return new Sink.ChainedInt(sink) { |
|
247 |
@Override |
|
248 |
public void accept(int t) { |
|
249 |
downstream.accept(mapper.apply(t)); |
|
250 |
} |
|
251 |
}; |
|
252 |
} |
|
253 |
}; |
|
254 |
} |
|
255 |
||
256 |
@Override |
|
257 |
public final LongStream mapToLong(IntToLongFunction mapper) { |
|
258 |
Objects.requireNonNull(mapper); |
|
259 |
return new LongPipeline.StatelessOp<Integer>(this, StreamShape.INT_VALUE, |
|
260 |
StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT) { |
|
261 |
@Override |
|
262 |
Sink<Integer> opWrapSink(int flags, Sink<Long> sink) { |
|
263 |
return new Sink.ChainedInt(sink) { |
|
264 |
@Override |
|
265 |
public void accept(int t) { |
|
266 |
downstream.accept(mapper.applyAsLong(t)); |
|
267 |
} |
|
268 |
}; |
|
269 |
} |
|
270 |
}; |
|
271 |
} |
|
272 |
||
273 |
@Override |
|
274 |
public final DoubleStream mapToDouble(IntToDoubleFunction mapper) { |
|
275 |
Objects.requireNonNull(mapper); |
|
276 |
return new DoublePipeline.StatelessOp<Integer>(this, StreamShape.INT_VALUE, |
|
277 |
StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT) { |
|
278 |
@Override |
|
279 |
Sink<Integer> opWrapSink(int flags, Sink<Double> sink) { |
|
280 |
return new Sink.ChainedInt(sink) { |
|
281 |
@Override |
|
282 |
public void accept(int t) { |
|
283 |
downstream.accept(mapper.applyAsDouble(t)); |
|
284 |
} |
|
285 |
}; |
|
286 |
} |
|
287 |
}; |
|
288 |
} |
|
289 |
||
290 |
@Override |
|
291 |
public final IntStream flatMap(IntFunction<? extends IntStream> mapper) { |
|
292 |
return new StatelessOp<Integer>(this, StreamShape.INT_VALUE, |
|
293 |
StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT | StreamOpFlag.NOT_SIZED) { |
|
294 |
@Override |
|
295 |
Sink<Integer> opWrapSink(int flags, Sink<Integer> sink) { |
|
296 |
return new Sink.ChainedInt(sink) { |
|
297 |
public void accept(int t) { |
|
298 |
// We can do better that this too; optimize for depth=0 case and just grab spliterator and forEach it |
|
299 |
IntStream result = mapper.apply(t); |
|
300 |
if (result != null) |
|
301 |
result.sequential().forEach(i -> downstream.accept(i)); |
|
302 |
} |
|
303 |
}; |
|
304 |
} |
|
305 |
}; |
|
306 |
} |
|
307 |
||
308 |
@Override |
|
309 |
public IntStream unordered() { |
|
310 |
if (!isOrdered()) |
|
311 |
return this; |
|
312 |
return new StatelessOp<Integer>(this, StreamShape.INT_VALUE, StreamOpFlag.NOT_ORDERED) { |
|
313 |
@Override |
|
314 |
Sink<Integer> opWrapSink(int flags, Sink<Integer> sink) { |
|
315 |
return sink; |
|
316 |
} |
|
317 |
}; |
|
318 |
} |
|
319 |
||
320 |
@Override |
|
321 |
public final IntStream filter(IntPredicate predicate) { |
|
322 |
Objects.requireNonNull(predicate); |
|
323 |
return new StatelessOp<Integer>(this, StreamShape.INT_VALUE, |
|
324 |
StreamOpFlag.NOT_SIZED) { |
|
325 |
@Override |
|
326 |
Sink<Integer> opWrapSink(int flags, Sink<Integer> sink) { |
|
327 |
return new Sink.ChainedInt(sink) { |
|
328 |
@Override |
|
329 |
public void accept(int t) { |
|
330 |
if (predicate.test(t)) |
|
331 |
downstream.accept(t); |
|
332 |
} |
|
333 |
}; |
|
334 |
} |
|
335 |
}; |
|
336 |
} |
|
337 |
||
338 |
@Override |
|
339 |
public final IntStream peek(IntConsumer consumer) { |
|
340 |
Objects.requireNonNull(consumer); |
|
341 |
return new StatelessOp<Integer>(this, StreamShape.INT_VALUE, |
|
342 |
0) { |
|
343 |
@Override |
|
344 |
Sink<Integer> opWrapSink(int flags, Sink<Integer> sink) { |
|
345 |
return new Sink.ChainedInt(sink) { |
|
346 |
@Override |
|
347 |
public void accept(int t) { |
|
348 |
consumer.accept(t); |
|
349 |
downstream.accept(t); |
|
350 |
} |
|
351 |
}; |
|
352 |
} |
|
353 |
}; |
|
354 |
} |
|
355 |
||
356 |
// Stateful intermediate ops from IntStream |
|
357 |
||
358 |
private IntStream slice(long skip, long limit) { |
|
359 |
return SliceOps.makeInt(this, skip, limit); |
|
360 |
} |
|
361 |
||
362 |
@Override |
|
363 |
public final IntStream limit(long maxSize) { |
|
364 |
if (maxSize < 0) |
|
365 |
throw new IllegalArgumentException(Long.toString(maxSize)); |
|
366 |
return slice(0, maxSize); |
|
367 |
} |
|
368 |
||
369 |
@Override |
|
370 |
public final IntStream substream(long startingOffset) { |
|
371 |
if (startingOffset < 0) |
|
372 |
throw new IllegalArgumentException(Long.toString(startingOffset)); |
|
373 |
if (startingOffset == 0) |
|
374 |
return this; |
|
375 |
else |
|
376 |
return slice(startingOffset, -1); |
|
377 |
} |
|
378 |
||
379 |
@Override |
|
380 |
public final IntStream substream(long startingOffset, long endingOffset) { |
|
381 |
if (startingOffset < 0 || endingOffset < startingOffset) |
|
382 |
throw new IllegalArgumentException(String.format("substream(%d, %d)", startingOffset, endingOffset)); |
|
383 |
return slice(startingOffset, endingOffset - startingOffset); |
|
384 |
} |
|
385 |
||
386 |
@Override |
|
387 |
public final IntStream sorted() { |
|
388 |
return SortedOps.makeInt(this); |
|
389 |
} |
|
390 |
||
391 |
@Override |
|
392 |
public final IntStream distinct() { |
|
393 |
// While functional and quick to implement, this approach is not very efficient. |
|
394 |
// An efficient version requires an int-specific map/set implementation. |
|
395 |
return boxed().distinct().mapToInt(i -> i); |
|
396 |
} |
|
397 |
||
398 |
// Terminal ops from IntStream |
|
399 |
||
400 |
@Override |
|
401 |
public void forEach(IntConsumer action) { |
|
402 |
evaluate(ForEachOps.makeInt(action, false)); |
|
403 |
} |
|
404 |
||
405 |
@Override |
|
406 |
public void forEachOrdered(IntConsumer action) { |
|
407 |
evaluate(ForEachOps.makeInt(action, true)); |
|
408 |
} |
|
409 |
||
410 |
@Override |
|
411 |
public final int sum() { |
|
412 |
return reduce(0, Integer::sum); |
|
413 |
} |
|
414 |
||
415 |
@Override |
|
416 |
public final OptionalInt min() { |
|
417 |
return reduce(Math::min); |
|
418 |
} |
|
419 |
||
420 |
@Override |
|
421 |
public final OptionalInt max() { |
|
422 |
return reduce(Math::max); |
|
423 |
} |
|
424 |
||
425 |
@Override |
|
426 |
public final long count() { |
|
18154
5ede18269905
8015798: Rename IntStream.longs/doubles and LongStream.doubles to asXxxStream
psandoz
parents:
17182
diff
changeset
|
427 |
return asLongStream().map(e -> 1L).sum(); |
17182 | 428 |
} |
429 |
||
430 |
@Override |
|
431 |
public final OptionalDouble average() { |
|
432 |
long[] avg = collect(() -> new long[2], |
|
433 |
(ll, i) -> { |
|
434 |
ll[0]++; |
|
435 |
ll[1] += i; |
|
436 |
}, |
|
437 |
(ll, rr) -> { |
|
438 |
ll[0] += rr[0]; |
|
439 |
ll[1] += rr[1]; |
|
440 |
}); |
|
441 |
return avg[0] > 0 |
|
442 |
? OptionalDouble.of((double) avg[1] / avg[0]) |
|
443 |
: OptionalDouble.empty(); |
|
444 |
} |
|
445 |
||
446 |
@Override |
|
447 |
public final IntSummaryStatistics summaryStatistics() { |
|
448 |
return collect(IntSummaryStatistics::new, IntSummaryStatistics::accept, |
|
449 |
IntSummaryStatistics::combine); |
|
450 |
} |
|
451 |
||
452 |
@Override |
|
453 |
public final int reduce(int identity, IntBinaryOperator op) { |
|
454 |
return evaluate(ReduceOps.makeInt(identity, op)); |
|
455 |
} |
|
456 |
||
457 |
@Override |
|
458 |
public final OptionalInt reduce(IntBinaryOperator op) { |
|
459 |
return evaluate(ReduceOps.makeInt(op)); |
|
460 |
} |
|
461 |
||
462 |
@Override |
|
463 |
public final <R> R collect(Supplier<R> resultFactory, |
|
464 |
ObjIntConsumer<R> accumulator, |
|
465 |
BiConsumer<R, R> combiner) { |
|
466 |
BinaryOperator<R> operator = (left, right) -> { |
|
467 |
combiner.accept(left, right); |
|
468 |
return left; |
|
469 |
}; |
|
470 |
return evaluate(ReduceOps.makeInt(resultFactory, accumulator, operator)); |
|
471 |
} |
|
472 |
||
473 |
@Override |
|
474 |
public final boolean anyMatch(IntPredicate predicate) { |
|
475 |
return evaluate(MatchOps.makeInt(predicate, MatchOps.MatchKind.ANY)); |
|
476 |
} |
|
477 |
||
478 |
@Override |
|
479 |
public final boolean allMatch(IntPredicate predicate) { |
|
480 |
return evaluate(MatchOps.makeInt(predicate, MatchOps.MatchKind.ALL)); |
|
481 |
} |
|
482 |
||
483 |
@Override |
|
484 |
public final boolean noneMatch(IntPredicate predicate) { |
|
485 |
return evaluate(MatchOps.makeInt(predicate, MatchOps.MatchKind.NONE)); |
|
486 |
} |
|
487 |
||
488 |
@Override |
|
489 |
public final OptionalInt findFirst() { |
|
490 |
return evaluate(FindOps.makeInt(true)); |
|
491 |
} |
|
492 |
||
493 |
@Override |
|
494 |
public final OptionalInt findAny() { |
|
495 |
return evaluate(FindOps.makeInt(false)); |
|
496 |
} |
|
497 |
||
498 |
@Override |
|
499 |
public final int[] toArray() { |
|
500 |
return Nodes.flattenInt((Node.OfInt) evaluateToArrayNode(Integer[]::new)) |
|
501 |
.asIntArray(); |
|
502 |
} |
|
503 |
||
504 |
// |
|
505 |
||
506 |
/** |
|
507 |
* Source stage of an IntStream. |
|
508 |
* |
|
509 |
* @param <E_IN> type of elements in the upstream source |
|
510 |
* @since 1.8 |
|
511 |
*/ |
|
512 |
static class Head<E_IN> extends IntPipeline<E_IN> { |
|
513 |
/** |
|
514 |
* Constructor for the source stage of an IntStream. |
|
515 |
* |
|
516 |
* @param source {@code Supplier<Spliterator>} describing the stream |
|
517 |
* source |
|
518 |
* @param sourceFlags the source flags for the stream source, described |
|
519 |
* in {@link StreamOpFlag} |
|
520 |
* @param parallel {@code true} if the pipeline is parallel |
|
521 |
*/ |
|
522 |
Head(Supplier<? extends Spliterator<Integer>> source, |
|
523 |
int sourceFlags, boolean parallel) { |
|
524 |
super(source, sourceFlags, parallel); |
|
525 |
} |
|
526 |
||
527 |
/** |
|
528 |
* Constructor for the source stage of an IntStream. |
|
529 |
* |
|
530 |
* @param source {@code Spliterator} describing the stream source |
|
531 |
* @param sourceFlags the source flags for the stream source, described |
|
532 |
* in {@link StreamOpFlag} |
|
533 |
* @param parallel {@code true} if the pipeline is parallel |
|
534 |
*/ |
|
535 |
Head(Spliterator<Integer> source, |
|
536 |
int sourceFlags, boolean parallel) { |
|
537 |
super(source, sourceFlags, parallel); |
|
538 |
} |
|
539 |
||
540 |
@Override |
|
541 |
final boolean opIsStateful() { |
|
542 |
throw new UnsupportedOperationException(); |
|
543 |
} |
|
544 |
||
545 |
@Override |
|
546 |
final Sink<E_IN> opWrapSink(int flags, Sink<Integer> sink) { |
|
547 |
throw new UnsupportedOperationException(); |
|
548 |
} |
|
549 |
||
550 |
// Optimized sequential terminal operations for the head of the pipeline |
|
551 |
||
552 |
@Override |
|
553 |
public void forEach(IntConsumer action) { |
|
554 |
if (!isParallel()) { |
|
555 |
adapt(sourceStageSpliterator()).forEachRemaining(action); |
|
556 |
} |
|
557 |
else { |
|
558 |
super.forEach(action); |
|
559 |
} |
|
560 |
} |
|
561 |
||
562 |
@Override |
|
563 |
public void forEachOrdered(IntConsumer action) { |
|
564 |
if (!isParallel()) { |
|
565 |
adapt(sourceStageSpliterator()).forEachRemaining(action); |
|
566 |
} |
|
567 |
else { |
|
568 |
super.forEachOrdered(action); |
|
569 |
} |
|
570 |
} |
|
571 |
} |
|
572 |
||
573 |
/** |
|
574 |
* Base class for a stateless intermediate stage of an IntStream |
|
575 |
* |
|
576 |
* @param <E_IN> type of elements in the upstream source |
|
577 |
* @since 1.8 |
|
578 |
*/ |
|
579 |
abstract static class StatelessOp<E_IN> extends IntPipeline<E_IN> { |
|
580 |
/** |
|
581 |
* Construct a new IntStream by appending a stateless intermediate |
|
582 |
* operation to an existing stream. |
|
583 |
* @param upstream The upstream pipeline stage |
|
584 |
* @param inputShape The stream shape for the upstream pipeline stage |
|
585 |
* @param opFlags Operation flags for the new stage |
|
586 |
*/ |
|
587 |
StatelessOp(AbstractPipeline<?, E_IN, ?> upstream, |
|
588 |
StreamShape inputShape, |
|
589 |
int opFlags) { |
|
590 |
super(upstream, opFlags); |
|
591 |
assert upstream.getOutputShape() == inputShape; |
|
592 |
} |
|
593 |
||
594 |
@Override |
|
595 |
final boolean opIsStateful() { |
|
596 |
return false; |
|
597 |
} |
|
598 |
} |
|
599 |
||
600 |
/** |
|
601 |
* Base class for a stateful intermediate stage of an IntStream. |
|
602 |
* |
|
603 |
* @param <E_IN> type of elements in the upstream source |
|
604 |
* @since 1.8 |
|
605 |
*/ |
|
606 |
abstract static class StatefulOp<E_IN> extends IntPipeline<E_IN> { |
|
607 |
/** |
|
608 |
* Construct a new IntStream by appending a stateful intermediate |
|
609 |
* operation to an existing stream. |
|
610 |
* @param upstream The upstream pipeline stage |
|
611 |
* @param inputShape The stream shape for the upstream pipeline stage |
|
612 |
* @param opFlags Operation flags for the new stage |
|
613 |
*/ |
|
614 |
StatefulOp(AbstractPipeline<?, E_IN, ?> upstream, |
|
615 |
StreamShape inputShape, |
|
616 |
int opFlags) { |
|
617 |
super(upstream, opFlags); |
|
618 |
assert upstream.getOutputShape() == inputShape; |
|
619 |
} |
|
620 |
||
621 |
@Override |
|
622 |
final boolean opIsStateful() { |
|
623 |
return true; |
|
624 |
} |
|
625 |
||
626 |
@Override |
|
627 |
abstract <P_IN> Node<Integer> opEvaluateParallel(PipelineHelper<Integer> helper, |
|
628 |
Spliterator<P_IN> spliterator, |
|
629 |
IntFunction<Integer[]> generator); |
|
630 |
} |
|
631 |
} |