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