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