• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 package java.util.stream;
26 
27 import java.util.Comparator;
28 import java.util.Iterator;
29 import java.util.Objects;
30 import java.util.Optional;
31 import java.util.Spliterator;
32 import java.util.Spliterators;
33 import java.util.function.BiConsumer;
34 import java.util.function.BiFunction;
35 import java.util.function.BinaryOperator;
36 import java.util.function.Consumer;
37 import java.util.function.DoubleConsumer;
38 import java.util.function.Function;
39 import java.util.function.IntConsumer;
40 import java.util.function.IntFunction;
41 import java.util.function.LongConsumer;
42 import java.util.function.Predicate;
43 import java.util.function.Supplier;
44 import java.util.function.ToDoubleFunction;
45 import java.util.function.ToIntFunction;
46 import java.util.function.ToLongFunction;
47 
48 /**
49  * Abstract base class for an intermediate pipeline stage or pipeline source
50  * stage implementing whose elements are of type {@code U}.
51  *
52  * @param <P_IN> type of elements in the upstream source
53  * @param <P_OUT> type of elements in produced by this stage
54  *
55  * @since 1.8
56  * @hide Visible for CTS testing only (OpenJDK8 tests).
57  */
58 // Android-changed: Made public for CTS tests only.
59 public abstract class ReferencePipeline<P_IN, P_OUT>
60         extends AbstractPipeline<P_IN, P_OUT, Stream<P_OUT>>
61         implements Stream<P_OUT>  {
62 
63     /**
64      * Constructor for the head of a stream pipeline.
65      *
66      * @param source {@code Supplier<Spliterator>} describing the stream source
67      * @param sourceFlags the source flags for the stream source, described in
68      *        {@link StreamOpFlag}
69      * @param parallel {@code true} if the pipeline is parallel
70      */
ReferencePipeline(Supplier<? extends Spliterator<?>> source, int sourceFlags, boolean parallel)71     ReferencePipeline(Supplier<? extends Spliterator<?>> source,
72                       int sourceFlags, boolean parallel) {
73         super(source, sourceFlags, parallel);
74     }
75 
76     /**
77      * Constructor for the head of a stream pipeline.
78      *
79      * @param source {@code Spliterator} describing the stream source
80      * @param sourceFlags The source flags for the stream source, described in
81      *        {@link StreamOpFlag}
82      * @param parallel {@code true} if the pipeline is parallel
83      */
ReferencePipeline(Spliterator<?> source, int sourceFlags, boolean parallel)84     ReferencePipeline(Spliterator<?> source,
85                       int sourceFlags, boolean parallel) {
86         super(source, sourceFlags, parallel);
87     }
88 
89     /**
90      * Constructor for appending an intermediate operation onto an existing
91      * pipeline.
92      *
93      * @param upstream the upstream element source.
94      */
ReferencePipeline(AbstractPipeline<?, P_IN, ?> upstream, int opFlags)95     ReferencePipeline(AbstractPipeline<?, P_IN, ?> upstream, int opFlags) {
96         super(upstream, opFlags);
97     }
98 
99     // Shape-specific methods
100 
101     @Override
102     // Android-changed: Make public, to match the method it's overriding.
getOutputShape()103     public final StreamShape getOutputShape() {
104         return StreamShape.REFERENCE;
105     }
106 
107     @Override
108     // Android-changed: Make public, to match the method it's overriding.
evaluateToNode(PipelineHelper<P_OUT> helper, Spliterator<P_IN> spliterator, boolean flattenTree, IntFunction<P_OUT[]> generator)109     public final <P_IN> Node<P_OUT> evaluateToNode(PipelineHelper<P_OUT> helper,
110                                         Spliterator<P_IN> spliterator,
111                                         boolean flattenTree,
112                                         IntFunction<P_OUT[]> generator) {
113         return Nodes.collect(helper, spliterator, flattenTree, generator);
114     }
115 
116     @Override
117     // Android-changed: Make public, to match the method it's overriding.
wrap(PipelineHelper<P_OUT> ph, Supplier<Spliterator<P_IN>> supplier, boolean isParallel)118     public final <P_IN> Spliterator<P_OUT> wrap(PipelineHelper<P_OUT> ph,
119                                      Supplier<Spliterator<P_IN>> supplier,
120                                      boolean isParallel) {
121         return new StreamSpliterators.WrappingSpliterator<>(ph, supplier, isParallel);
122     }
123 
124     @Override
125     // Android-changed: Make public, to match the method it's overriding.
lazySpliterator(Supplier<? extends Spliterator<P_OUT>> supplier)126     public final Spliterator<P_OUT> lazySpliterator(Supplier<? extends Spliterator<P_OUT>> supplier) {
127         return new StreamSpliterators.DelegatingSpliterator<>(supplier);
128     }
129 
130     @Override
131     // Android-changed: Make public, to match the method it's overriding.
forEachWithCancel(Spliterator<P_OUT> spliterator, Sink<P_OUT> sink)132     public final boolean forEachWithCancel(Spliterator<P_OUT> spliterator, Sink<P_OUT> sink) {
133         boolean cancelled;
134         do { } while (!(cancelled = sink.cancellationRequested()) && spliterator.tryAdvance(sink));
135         return cancelled;
136     }
137 
138     @Override
139     // Android-changed: Make public, to match the method it's overriding.
makeNodeBuilder(long exactSizeIfKnown, IntFunction<P_OUT[]> generator)140     public final Node.Builder<P_OUT> makeNodeBuilder(long exactSizeIfKnown, IntFunction<P_OUT[]> generator) {
141         return Nodes.builder(exactSizeIfKnown, generator);
142     }
143 
144 
145     // BaseStream
146 
147     @Override
iterator()148     public final Iterator<P_OUT> iterator() {
149         return Spliterators.iterator(spliterator());
150     }
151 
152 
153     // Stream
154 
155     // Stateless intermediate operations from Stream
156 
157     @Override
unordered()158     public Stream<P_OUT> unordered() {
159         if (!isOrdered())
160             return this;
161         return new StatelessOp<P_OUT, P_OUT>(this, StreamShape.REFERENCE, StreamOpFlag.NOT_ORDERED) {
162             @Override
163             // Android-changed: Make public, to match the method it's overriding.
164             public Sink<P_OUT> opWrapSink(int flags, Sink<P_OUT> sink) {
165                 return sink;
166             }
167         };
168     }
169 
170     @Override
171     public final Stream<P_OUT> filter(Predicate<? super P_OUT> predicate) {
172         Objects.requireNonNull(predicate);
173         return new StatelessOp<P_OUT, P_OUT>(this, StreamShape.REFERENCE,
174                                      StreamOpFlag.NOT_SIZED) {
175             @Override
176             // Android-changed: Make public, to match the method it's overriding.
177             public Sink<P_OUT> opWrapSink(int flags, Sink<P_OUT> sink) {
178                 return new Sink.ChainedReference<P_OUT, P_OUT>(sink) {
179                     @Override
180                     public void begin(long size) {
181                         downstream.begin(-1);
182                     }
183 
184                     @Override
185                     public void accept(P_OUT u) {
186                         if (predicate.test(u))
187                             downstream.accept(u);
188                     }
189                 };
190             }
191         };
192     }
193 
194     @Override
195     @SuppressWarnings("unchecked")
196     public final <R> Stream<R> map(Function<? super P_OUT, ? extends R> mapper) {
197         Objects.requireNonNull(mapper);
198         return new StatelessOp<P_OUT, R>(this, StreamShape.REFERENCE,
199                                      StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT) {
200             @Override
201             public Sink<P_OUT> opWrapSink(int flags, Sink<R> sink) {
202                 return new Sink.ChainedReference<P_OUT, R>(sink) {
203                     @Override
204                     public void accept(P_OUT u) {
205                         downstream.accept(mapper.apply(u));
206                     }
207                 };
208             }
209         };
210     }
211 
212     @Override
213     public final IntStream mapToInt(ToIntFunction<? super P_OUT> mapper) {
214         Objects.requireNonNull(mapper);
215         return new IntPipeline.StatelessOp<P_OUT>(this, StreamShape.REFERENCE,
216                                               StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT) {
217             @Override
218             // Android-changed: Make public, to match the method it's overriding.
219             public Sink<P_OUT> opWrapSink(int flags, Sink<Integer> sink) {
220                 return new Sink.ChainedReference<P_OUT, Integer>(sink) {
221                     @Override
222                     public void accept(P_OUT u) {
223                         downstream.accept(mapper.applyAsInt(u));
224                     }
225                 };
226             }
227         };
228     }
229 
230     @Override
231     public final LongStream mapToLong(ToLongFunction<? super P_OUT> mapper) {
232         Objects.requireNonNull(mapper);
233         return new LongPipeline.StatelessOp<P_OUT>(this, StreamShape.REFERENCE,
234                                       StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT) {
235             @Override
236             // Android-changed: Make public, to match the method it's overriding.
237             public Sink<P_OUT> opWrapSink(int flags, Sink<Long> sink) {
238                 return new Sink.ChainedReference<P_OUT, Long>(sink) {
239                     @Override
240                     public void accept(P_OUT u) {
241                         downstream.accept(mapper.applyAsLong(u));
242                     }
243                 };
244             }
245         };
246     }
247 
248     @Override
249     public final DoubleStream mapToDouble(ToDoubleFunction<? super P_OUT> mapper) {
250         Objects.requireNonNull(mapper);
251         return new DoublePipeline.StatelessOp<P_OUT>(this, StreamShape.REFERENCE,
252                                         StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT) {
253             @Override
254             // Android-changed: Make public, to match the method it's overriding.
255             public Sink<P_OUT> opWrapSink(int flags, Sink<Double> sink) {
256                 return new Sink.ChainedReference<P_OUT, Double>(sink) {
257                     @Override
258                     public void accept(P_OUT u) {
259                         downstream.accept(mapper.applyAsDouble(u));
260                     }
261                 };
262             }
263         };
264     }
265 
266     @Override
267     public final <R> Stream<R> flatMap(Function<? super P_OUT, ? extends Stream<? extends R>> mapper) {
268         Objects.requireNonNull(mapper);
269         // We can do better than this, by polling cancellationRequested when stream is infinite
270         return new StatelessOp<P_OUT, R>(this, StreamShape.REFERENCE,
271                                      StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT | StreamOpFlag.NOT_SIZED) {
272             @Override
273             public Sink<P_OUT> opWrapSink(int flags, Sink<R> sink) {
274                 return new Sink.ChainedReference<P_OUT, R>(sink) {
275                     @Override
276                     public void begin(long size) {
277                         downstream.begin(-1);
278                     }
279 
280                     @Override
281                     public void accept(P_OUT u) {
282                         try (Stream<? extends R> result = mapper.apply(u)) {
283                             // We can do better that this too; optimize for depth=0 case and just grab spliterator and forEach it
284                             if (result != null)
285                                 result.sequential().forEach(downstream);
286                         }
287                     }
288                 };
289             }
290         };
291     }
292 
293     @Override
294     public final IntStream flatMapToInt(Function<? super P_OUT, ? extends IntStream> mapper) {
295         Objects.requireNonNull(mapper);
296         // We can do better than this, by polling cancellationRequested when stream is infinite
297         return new IntPipeline.StatelessOp<P_OUT>(this, StreamShape.REFERENCE,
298                                               StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT | StreamOpFlag.NOT_SIZED) {
299             @Override
300             // Android-changed: Make public, to match the method it's overriding.
301             public Sink<P_OUT> opWrapSink(int flags, Sink<Integer> sink) {
302                 return new Sink.ChainedReference<P_OUT, Integer>(sink) {
303                     IntConsumer downstreamAsInt = downstream::accept;
304                     @Override
305                     public void begin(long size) {
306                         downstream.begin(-1);
307                     }
308 
309                     @Override
310                     public void accept(P_OUT u) {
311                         try (IntStream result = mapper.apply(u)) {
312                             // We can do better that this too; optimize for depth=0 case and just grab spliterator and forEach it
313                             if (result != null)
314                                 result.sequential().forEach(downstreamAsInt);
315                         }
316                     }
317                 };
318             }
319         };
320     }
321 
322     @Override
323     public final DoubleStream flatMapToDouble(Function<? super P_OUT, ? extends DoubleStream> mapper) {
324         Objects.requireNonNull(mapper);
325         // We can do better than this, by polling cancellationRequested when stream is infinite
326         return new DoublePipeline.StatelessOp<P_OUT>(this, StreamShape.REFERENCE,
327                                                      StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT | StreamOpFlag.NOT_SIZED) {
328             @Override
329             // Android-changed: Make public, to match the method it's overriding.
330             public Sink<P_OUT> opWrapSink(int flags, Sink<Double> sink) {
331                 return new Sink.ChainedReference<P_OUT, Double>(sink) {
332                     DoubleConsumer downstreamAsDouble = downstream::accept;
333                     @Override
334                     public void begin(long size) {
335                         downstream.begin(-1);
336                     }
337 
338                     @Override
339                     public void accept(P_OUT u) {
340                         try (DoubleStream result = mapper.apply(u)) {
341                             // We can do better that this too; optimize for depth=0 case and just grab spliterator and forEach it
342                             if (result != null)
343                                 result.sequential().forEach(downstreamAsDouble);
344                         }
345                     }
346                 };
347             }
348         };
349     }
350 
351     @Override
352     public final LongStream flatMapToLong(Function<? super P_OUT, ? extends LongStream> mapper) {
353         Objects.requireNonNull(mapper);
354         // We can do better than this, by polling cancellationRequested when stream is infinite
355         return new LongPipeline.StatelessOp<P_OUT>(this, StreamShape.REFERENCE,
356                                                    StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT | StreamOpFlag.NOT_SIZED) {
357             @Override
358             // Android-changed: Make public, to match the method it's overriding.
359             public Sink<P_OUT> opWrapSink(int flags, Sink<Long> sink) {
360                 return new Sink.ChainedReference<P_OUT, Long>(sink) {
361                     LongConsumer downstreamAsLong = downstream::accept;
362                     @Override
363                     public void begin(long size) {
364                         downstream.begin(-1);
365                     }
366 
367                     @Override
368                     public void accept(P_OUT u) {
369                         try (LongStream result = mapper.apply(u)) {
370                             // We can do better that this too; optimize for depth=0 case and just grab spliterator and forEach it
371                             if (result != null)
372                                 result.sequential().forEach(downstreamAsLong);
373                         }
374                     }
375                 };
376             }
377         };
378     }
379 
380     @Override
381     public final Stream<P_OUT> peek(Consumer<? super P_OUT> action) {
382         Objects.requireNonNull(action);
383         return new StatelessOp<P_OUT, P_OUT>(this, StreamShape.REFERENCE,
384                                      0) {
385             @Override
386             // Android-changed: Make public, to match the method it's overriding.
387             public Sink<P_OUT> opWrapSink(int flags, Sink<P_OUT> sink) {
388                 return new Sink.ChainedReference<P_OUT, P_OUT>(sink) {
389                     @Override
390                     public void accept(P_OUT u) {
391                         action.accept(u);
392                         downstream.accept(u);
393                     }
394                 };
395             }
396         };
397     }
398 
399     // Stateful intermediate operations from Stream
400 
401     @Override
402     public final Stream<P_OUT> distinct() {
403         return DistinctOps.makeRef(this);
404     }
405 
406     @Override
407     public final Stream<P_OUT> sorted() {
408         return SortedOps.makeRef(this);
409     }
410 
411     @Override
412     public final Stream<P_OUT> sorted(Comparator<? super P_OUT> comparator) {
413         return SortedOps.makeRef(this, comparator);
414     }
415 
416     @Override
417     public final Stream<P_OUT> limit(long maxSize) {
418         if (maxSize < 0)
419             throw new IllegalArgumentException(Long.toString(maxSize));
420         return SliceOps.makeRef(this, 0, maxSize);
421     }
422 
423     @Override
424     public final Stream<P_OUT> skip(long n) {
425         if (n < 0)
426             throw new IllegalArgumentException(Long.toString(n));
427         if (n == 0)
428             return this;
429         else
430             return SliceOps.makeRef(this, n, -1);
431     }
432 
433     // Terminal operations from Stream
434 
435     @Override
436     public void forEach(Consumer<? super P_OUT> action) {
437         evaluate(ForEachOps.makeRef(action, false));
438     }
439 
440     @Override
441     public void forEachOrdered(Consumer<? super P_OUT> action) {
442         evaluate(ForEachOps.makeRef(action, true));
443     }
444 
445     @Override
446     @SuppressWarnings("unchecked")
447     public final <A> A[] toArray(IntFunction<A[]> generator) {
448         // Since A has no relation to U (not possible to declare that A is an upper bound of U)
449         // there will be no static type checking.
450         // Therefore use a raw type and assume A == U rather than propagating the separation of A and U
451         // throughout the code-base.
452         // The runtime type of U is never checked for equality with the component type of the runtime type of A[].
453         // Runtime checking will be performed when an element is stored in A[], thus if A is not a
454         // super type of U an ArrayStoreException will be thrown.
455         @SuppressWarnings("rawtypes")
456         IntFunction rawGenerator = (IntFunction) generator;
457         // Android-changed: Eclipse compiler requires explicit (Node<A[]>) cast (b/29399275).
458         return (A[]) Nodes.flatten((Node<A[]>) evaluateToArrayNode(rawGenerator), rawGenerator)
459                 .asArray(rawGenerator);
460     }
461 
462     @Override
463     public final Object[] toArray() {
464         return toArray(Object[]::new);
465     }
466 
467     @Override
468     public final boolean anyMatch(Predicate<? super P_OUT> predicate) {
469         return evaluate(MatchOps.makeRef(predicate, MatchOps.MatchKind.ANY));
470     }
471 
472     @Override
473     public final boolean allMatch(Predicate<? super P_OUT> predicate) {
474         return evaluate(MatchOps.makeRef(predicate, MatchOps.MatchKind.ALL));
475     }
476 
477     @Override
478     public final boolean noneMatch(Predicate<? super P_OUT> predicate) {
479         return evaluate(MatchOps.makeRef(predicate, MatchOps.MatchKind.NONE));
480     }
481 
482     @Override
483     public final Optional<P_OUT> findFirst() {
484         return evaluate(FindOps.makeRef(true));
485     }
486 
487     @Override
488     public final Optional<P_OUT> findAny() {
489         return evaluate(FindOps.makeRef(false));
490     }
491 
492     @Override
493     public final P_OUT reduce(final P_OUT identity, final BinaryOperator<P_OUT> accumulator) {
494         return evaluate(ReduceOps.makeRef(identity, accumulator, accumulator));
495     }
496 
497     @Override
498     public final Optional<P_OUT> reduce(BinaryOperator<P_OUT> accumulator) {
499         return evaluate(ReduceOps.makeRef(accumulator));
500     }
501 
502     @Override
503     public final <R> R reduce(R identity, BiFunction<R, ? super P_OUT, R> accumulator, BinaryOperator<R> combiner) {
504         return evaluate(ReduceOps.makeRef(identity, accumulator, combiner));
505     }
506 
507     @Override
508     @SuppressWarnings("unchecked")
509     public final <R, A> R collect(Collector<? super P_OUT, A, R> collector) {
510         A container;
511         if (isParallel()
512                 && (collector.characteristics().contains(Collector.Characteristics.CONCURRENT))
513                 && (!isOrdered() || collector.characteristics().contains(Collector.Characteristics.UNORDERED))) {
514             container = collector.supplier().get();
515             BiConsumer<A, ? super P_OUT> accumulator = collector.accumulator();
516             forEach(u -> accumulator.accept(container, u));
517         }
518         else {
519             container = evaluate(ReduceOps.makeRef(collector));
520         }
521         return collector.characteristics().contains(Collector.Characteristics.IDENTITY_FINISH)
522                ? (R) container
523                : collector.finisher().apply(container);
524     }
525 
526     @Override
527     public final <R> R collect(Supplier<R> supplier,
528                                BiConsumer<R, ? super P_OUT> accumulator,
529                                BiConsumer<R, R> combiner) {
530         return evaluate(ReduceOps.makeRef(supplier, accumulator, combiner));
531     }
532 
533     @Override
534     public final Optional<P_OUT> max(Comparator<? super P_OUT> comparator) {
535         return reduce(BinaryOperator.maxBy(comparator));
536     }
537 
538     @Override
539     public final Optional<P_OUT> min(Comparator<? super P_OUT> comparator) {
540         return reduce(BinaryOperator.minBy(comparator));
541 
542     }
543 
544     @Override
545     public final long count() {
546         return mapToLong(e -> 1L).sum();
547     }
548 
549 
550     //
551 
552     /**
553      * Source stage of a ReferencePipeline.
554      *
555      * @param <E_IN> type of elements in the upstream source
556      * @param <E_OUT> type of elements in produced by this stage
557      * @since 1.8
558      * @hide Visible for CTS testing only (OpenJDK8 tests).
559      */
560     // Android-changed: Made public for CTS tests only.
561     public static class Head<E_IN, E_OUT> extends ReferencePipeline<E_IN, E_OUT> {
562         /**
563          * Constructor for the source stage of a Stream.
564          *
565          * @param source {@code Supplier<Spliterator>} describing the stream
566          *               source
567          * @param sourceFlags the source flags for the stream source, described
568          *                    in {@link StreamOpFlag}
569          */
570         // Android-changed: Made public for CTS tests only.
571         public Head(Supplier<? extends Spliterator<?>> source,
572              int sourceFlags, boolean parallel) {
573             super(source, sourceFlags, parallel);
574         }
575 
576         /**
577          * Constructor for the source stage of a Stream.
578          *
579          * @param source {@code Spliterator} describing the stream source
580          * @param sourceFlags the source flags for the stream source, described
581          *                    in {@link StreamOpFlag}
582          */
583         // Android-changed: Made public for CTS tests only.
584         public Head(Spliterator<?> source,
585              int sourceFlags, boolean parallel) {
586             super(source, sourceFlags, parallel);
587         }
588 
589         @Override
590         // Android-changed: Make public, to match the method it's overriding.
591         public final boolean opIsStateful() {
592             throw new UnsupportedOperationException();
593         }
594 
595         @Override
596         // Android-changed: Make public, to match the method it's overriding.
597         public final Sink<E_IN> opWrapSink(int flags, Sink<E_OUT> sink) {
598             throw new UnsupportedOperationException();
599         }
600 
601         // Optimized sequential terminal operations for the head of the pipeline
602 
603         @Override
604         public void forEach(Consumer<? super E_OUT> action) {
605             if (!isParallel()) {
606                 sourceStageSpliterator().forEachRemaining(action);
607             }
608             else {
609                 super.forEach(action);
610             }
611         }
612 
613         @Override
614         public void forEachOrdered(Consumer<? super E_OUT> action) {
615             if (!isParallel()) {
616                 sourceStageSpliterator().forEachRemaining(action);
617             }
618             else {
619                 super.forEachOrdered(action);
620             }
621         }
622     }
623 
624     /**
625      * Base class for a stateless intermediate stage of a Stream.
626      *
627      * @param <E_IN> type of elements in the upstream source
628      * @param <E_OUT> type of elements in produced by this stage
629      * @since 1.8
630      * @hide Visible for CTS testing only (OpenJDK8 tests).
631      */
632     // Android-changed: Made public for CTS tests only.
633     public abstract static class StatelessOp<E_IN, E_OUT>
634             extends ReferencePipeline<E_IN, E_OUT> {
635         /**
636          * Construct a new Stream by appending a stateless intermediate
637          * operation to an existing stream.
638          *
639          * @param upstream The upstream pipeline stage
640          * @param inputShape The stream shape for the upstream pipeline stage
641          * @param opFlags Operation flags for the new stage
642          */
643         // Android-changed: Made public for CTS tests only.
644         public StatelessOp(AbstractPipeline<?, E_IN, ?> upstream,
645                     StreamShape inputShape,
646                     int opFlags) {
647             super(upstream, opFlags);
648             assert upstream.getOutputShape() == inputShape;
649         }
650 
651         @Override
652         // Android-changed: Make public, to match the method it's overriding.
653         public final boolean opIsStateful() {
654             return false;
655         }
656     }
657 
658     /**
659      * Base class for a stateful intermediate stage of a Stream.
660      *
661      * @param <E_IN> type of elements in the upstream source
662      * @param <E_OUT> type of elements in produced by this stage
663      * @since 1.8
664      * @hide Visible for CTS testing only (OpenJDK8 tests).
665      */
666     // Android-changed: Made public for CTS tests only.
667     public abstract static class StatefulOp<E_IN, E_OUT>
668             extends ReferencePipeline<E_IN, E_OUT> {
669         /**
670          * Construct a new Stream by appending a stateful intermediate operation
671          * to an existing stream.
672          * @param upstream The upstream pipeline stage
673          * @param inputShape The stream shape for the upstream pipeline stage
674          * @param opFlags Operation flags for the new stage
675          */
676         // Android-changed: Made public for CTS tests only.
677         public StatefulOp(AbstractPipeline<?, E_IN, ?> upstream,
678                    StreamShape inputShape,
679                    int opFlags) {
680             super(upstream, opFlags);
681             assert upstream.getOutputShape() == inputShape;
682         }
683 
684         @Override
685         // Android-changed: Make public, to match the method it's overriding.
686         public final boolean opIsStateful() {
687             return true;
688         }
689 
690         @Override
691         // Android-changed: Make public, to match the method it's overriding.
692         public abstract <P_IN> Node<E_OUT> opEvaluateParallel(PipelineHelper<E_OUT> helper,
693                                                        Spliterator<P_IN> spliterator,
694                                                        IntFunction<E_OUT[]> generator);
695     }
696 }
697