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