jdk/src/share/classes/java/util/stream/ReferencePipeline.java
author psandoz
Thu, 16 Jan 2014 18:20:31 +0100
changeset 22289 bb9c71b84919
parent 20866 36155ee613ef
permissions -rw-r--r--
8029452: Fork/Join task ForEachOps.ForEachOrderedTask clarifications and minor improvements Reviewed-by: mduigou, briangoetz
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.Iterator;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    29
import java.util.Objects;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    30
import java.util.Optional;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    31
import java.util.Spliterator;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    32
import java.util.Spliterators;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    33
import java.util.function.BiConsumer;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    34
import java.util.function.BiFunction;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    35
import java.util.function.BinaryOperator;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    36
import java.util.function.Consumer;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    37
import java.util.function.DoubleConsumer;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    38
import java.util.function.Function;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    39
import java.util.function.IntConsumer;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    40
import java.util.function.IntFunction;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    41
import java.util.function.LongConsumer;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    42
import java.util.function.Predicate;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    43
import java.util.function.Supplier;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    44
import java.util.function.ToDoubleFunction;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    45
import java.util.function.ToIntFunction;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    46
import java.util.function.ToLongFunction;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    47
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    48
/**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    49
 * Abstract base class for an intermediate pipeline stage or pipeline source
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    50
 * stage implementing whose elements are of type {@code U}.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    51
 *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    52
 * @param <P_IN> type of elements in the upstream source
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    53
 * @param <P_OUT> type of elements in produced by this stage
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    54
 *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    55
 * @since 1.8
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    56
 */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    57
abstract class ReferencePipeline<P_IN, P_OUT>
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    58
        extends AbstractPipeline<P_IN, P_OUT, Stream<P_OUT>>
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    59
        implements Stream<P_OUT>  {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    60
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    61
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    62
     * Constructor for the head of a stream pipeline.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    63
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    64
     * @param source {@code Supplier<Spliterator>} describing the stream source
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    65
     * @param sourceFlags the source flags for the stream source, described in
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    66
     *        {@link StreamOpFlag}
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    67
     * @param parallel {@code true} if the pipeline is parallel
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    68
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    69
    ReferencePipeline(Supplier<? extends Spliterator<?>> source,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    70
                      int sourceFlags, boolean parallel) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    71
        super(source, sourceFlags, parallel);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    72
    }
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
     * Constructor for the head of a stream pipeline.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    76
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    77
     * @param source {@code Spliterator} describing the stream source
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    78
     * @param sourceFlags The source flags for the stream source, described in
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    79
     *        {@link StreamOpFlag}
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    80
     * @param parallel {@code true} if the pipeline is parallel
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    81
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    82
    ReferencePipeline(Spliterator<?> source,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    83
                      int sourceFlags, boolean parallel) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    84
        super(source, sourceFlags, parallel);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    85
    }
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
     * Constructor for appending an intermediate operation onto an existing
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    89
     * pipeline.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    90
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    91
     * @param upstream the upstream element source.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    92
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    93
    ReferencePipeline(AbstractPipeline<?, P_IN, ?> upstream, int opFlags) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    94
        super(upstream, opFlags);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    95
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    96
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    97
    // Shape-specific methods
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    98
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    99
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   100
    final StreamShape getOutputShape() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   101
        return StreamShape.REFERENCE;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   102
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   103
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   104
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   105
    final <P_IN> Node<P_OUT> evaluateToNode(PipelineHelper<P_OUT> helper,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   106
                                        Spliterator<P_IN> spliterator,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   107
                                        boolean flattenTree,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   108
                                        IntFunction<P_OUT[]> generator) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   109
        return Nodes.collect(helper, spliterator, flattenTree, generator);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   110
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   111
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   112
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   113
    final <P_IN> Spliterator<P_OUT> wrap(PipelineHelper<P_OUT> ph,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   114
                                     Supplier<Spliterator<P_IN>> supplier,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   115
                                     boolean isParallel) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   116
        return new StreamSpliterators.WrappingSpliterator<>(ph, supplier, isParallel);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   117
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   118
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   119
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   120
    final Spliterator<P_OUT> lazySpliterator(Supplier<? extends Spliterator<P_OUT>> supplier) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   121
        return new StreamSpliterators.DelegatingSpliterator<>(supplier);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   122
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   123
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   124
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   125
    final void forEachWithCancel(Spliterator<P_OUT> spliterator, Sink<P_OUT> sink) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   126
        do { } while (!sink.cancellationRequested() && spliterator.tryAdvance(sink));
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   127
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   128
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   129
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   130
    final Node.Builder<P_OUT> makeNodeBuilder(long exactSizeIfKnown, IntFunction<P_OUT[]> generator) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   131
        return Nodes.builder(exactSizeIfKnown, generator);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   132
    }
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
    // BaseStream
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   136
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   137
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   138
    public final Iterator<P_OUT> iterator() {
18155
889970e5b728 8015792: Rename Spliterators.spliteratorFromIterator to Spliterators.iterator
psandoz
parents: 17182
diff changeset
   139
        return Spliterators.iterator(spliterator());
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   140
    }
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
    // Stream
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   144
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   145
    // Stateless intermediate operations from Stream
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   146
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   147
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   148
    public Stream<P_OUT> unordered() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   149
        if (!isOrdered())
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   150
            return this;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   151
        return new StatelessOp<P_OUT, P_OUT>(this, StreamShape.REFERENCE, StreamOpFlag.NOT_ORDERED) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   152
            @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   153
            Sink<P_OUT> opWrapSink(int flags, Sink<P_OUT> sink) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   154
                return sink;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   155
            }
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
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   160
    public final Stream<P_OUT> filter(Predicate<? super P_OUT> predicate) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   161
        Objects.requireNonNull(predicate);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   162
        return new StatelessOp<P_OUT, P_OUT>(this, StreamShape.REFERENCE,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   163
                                     StreamOpFlag.NOT_SIZED) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   164
            @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   165
            Sink<P_OUT> opWrapSink(int flags, Sink<P_OUT> sink) {
19593
ce0cd954351c 8023681: Fix raw type warning caused by Sink
henryjen
parents: 19220
diff changeset
   166
                return new Sink.ChainedReference<P_OUT, P_OUT>(sink) {
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   167
                    @Override
18528
9b02581cf079 8016324: filter/flatMap pipeline sinks should pass size information to downstream sink
psandoz
parents: 18155
diff changeset
   168
                    public void begin(long size) {
9b02581cf079 8016324: filter/flatMap pipeline sinks should pass size information to downstream sink
psandoz
parents: 18155
diff changeset
   169
                        downstream.begin(-1);
9b02581cf079 8016324: filter/flatMap pipeline sinks should pass size information to downstream sink
psandoz
parents: 18155
diff changeset
   170
                    }
9b02581cf079 8016324: filter/flatMap pipeline sinks should pass size information to downstream sink
psandoz
parents: 18155
diff changeset
   171
9b02581cf079 8016324: filter/flatMap pipeline sinks should pass size information to downstream sink
psandoz
parents: 18155
diff changeset
   172
                    @Override
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   173
                    public void accept(P_OUT u) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   174
                        if (predicate.test(u))
19593
ce0cd954351c 8023681: Fix raw type warning caused by Sink
henryjen
parents: 19220
diff changeset
   175
                            downstream.accept(u);
17182
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
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   179
        };
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   180
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   181
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   182
    @Override
19220
d3d40ccb544e 8022476: cleanup some raw types and unchecked warnings in java.util.stream
mduigou
parents: 19214
diff changeset
   183
    @SuppressWarnings("unchecked")
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   184
    public final <R> Stream<R> map(Function<? super P_OUT, ? extends R> mapper) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   185
        Objects.requireNonNull(mapper);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   186
        return new StatelessOp<P_OUT, R>(this, StreamShape.REFERENCE,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   187
                                     StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   188
            @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   189
            Sink<P_OUT> opWrapSink(int flags, Sink<R> sink) {
19593
ce0cd954351c 8023681: Fix raw type warning caused by Sink
henryjen
parents: 19220
diff changeset
   190
                return new Sink.ChainedReference<P_OUT, R>(sink) {
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   191
                    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   192
                    public void accept(P_OUT u) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   193
                        downstream.accept(mapper.apply(u));
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   194
                    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   195
                };
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   196
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   197
        };
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   198
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   199
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   200
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   201
    public final IntStream mapToInt(ToIntFunction<? super P_OUT> mapper) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   202
        Objects.requireNonNull(mapper);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   203
        return new IntPipeline.StatelessOp<P_OUT>(this, StreamShape.REFERENCE,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   204
                                              StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   205
            @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   206
            Sink<P_OUT> opWrapSink(int flags, Sink<Integer> sink) {
19593
ce0cd954351c 8023681: Fix raw type warning caused by Sink
henryjen
parents: 19220
diff changeset
   207
                return new Sink.ChainedReference<P_OUT, Integer>(sink) {
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   208
                    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   209
                    public void accept(P_OUT u) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   210
                        downstream.accept(mapper.applyAsInt(u));
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   211
                    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   212
                };
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   213
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   214
        };
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   215
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   216
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   217
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   218
    public final LongStream mapToLong(ToLongFunction<? super P_OUT> mapper) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   219
        Objects.requireNonNull(mapper);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   220
        return new LongPipeline.StatelessOp<P_OUT>(this, StreamShape.REFERENCE,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   221
                                      StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   222
            @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   223
            Sink<P_OUT> opWrapSink(int flags, Sink<Long> sink) {
19593
ce0cd954351c 8023681: Fix raw type warning caused by Sink
henryjen
parents: 19220
diff changeset
   224
                return new Sink.ChainedReference<P_OUT, Long>(sink) {
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   225
                    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   226
                    public void accept(P_OUT u) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   227
                        downstream.accept(mapper.applyAsLong(u));
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   228
                    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   229
                };
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   230
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   231
        };
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   232
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   233
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   234
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   235
    public final DoubleStream mapToDouble(ToDoubleFunction<? super P_OUT> mapper) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   236
        Objects.requireNonNull(mapper);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   237
        return new DoublePipeline.StatelessOp<P_OUT>(this, StreamShape.REFERENCE,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   238
                                        StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   239
            @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   240
            Sink<P_OUT> opWrapSink(int flags, Sink<Double> sink) {
19593
ce0cd954351c 8023681: Fix raw type warning caused by Sink
henryjen
parents: 19220
diff changeset
   241
                return new Sink.ChainedReference<P_OUT, Double>(sink) {
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   242
                    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   243
                    public void accept(P_OUT u) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   244
                        downstream.accept(mapper.applyAsDouble(u));
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   245
                    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   246
                };
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   247
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   248
        };
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   249
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   250
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   251
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   252
    public final <R> Stream<R> flatMap(Function<? super P_OUT, ? extends Stream<? extends R>> mapper) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   253
        Objects.requireNonNull(mapper);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   254
        // We can do better than this, by polling cancellationRequested when stream is infinite
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   255
        return new StatelessOp<P_OUT, R>(this, StreamShape.REFERENCE,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   256
                                     StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT | StreamOpFlag.NOT_SIZED) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   257
            @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   258
            Sink<P_OUT> opWrapSink(int flags, Sink<R> sink) {
19593
ce0cd954351c 8023681: Fix raw type warning caused by Sink
henryjen
parents: 19220
diff changeset
   259
                return new Sink.ChainedReference<P_OUT, R>(sink) {
18528
9b02581cf079 8016324: filter/flatMap pipeline sinks should pass size information to downstream sink
psandoz
parents: 18155
diff changeset
   260
                    @Override
9b02581cf079 8016324: filter/flatMap pipeline sinks should pass size information to downstream sink
psandoz
parents: 18155
diff changeset
   261
                    public void begin(long size) {
9b02581cf079 8016324: filter/flatMap pipeline sinks should pass size information to downstream sink
psandoz
parents: 18155
diff changeset
   262
                        downstream.begin(-1);
9b02581cf079 8016324: filter/flatMap pipeline sinks should pass size information to downstream sink
psandoz
parents: 18155
diff changeset
   263
                    }
9b02581cf079 8016324: filter/flatMap pipeline sinks should pass size information to downstream sink
psandoz
parents: 18155
diff changeset
   264
9b02581cf079 8016324: filter/flatMap pipeline sinks should pass size information to downstream sink
psandoz
parents: 18155
diff changeset
   265
                    @Override
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   266
                    public void accept(P_OUT u) {
19800
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19799
diff changeset
   267
                        try (Stream<? extends R> result = mapper.apply(u)) {
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19799
diff changeset
   268
                            // We can do better that this too; optimize for depth=0 case and just grab spliterator and forEach it
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19799
diff changeset
   269
                            if (result != null)
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19799
diff changeset
   270
                                result.sequential().forEach(downstream);
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19799
diff changeset
   271
                        }
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   272
                    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   273
                };
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   274
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   275
        };
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   276
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   277
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   278
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   279
    public final IntStream flatMapToInt(Function<? super P_OUT, ? extends IntStream> mapper) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   280
        Objects.requireNonNull(mapper);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   281
        // We can do better than this, by polling cancellationRequested when stream is infinite
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   282
        return new IntPipeline.StatelessOp<P_OUT>(this, StreamShape.REFERENCE,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   283
                                              StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT | StreamOpFlag.NOT_SIZED) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   284
            @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   285
            Sink<P_OUT> opWrapSink(int flags, Sink<Integer> sink) {
19593
ce0cd954351c 8023681: Fix raw type warning caused by Sink
henryjen
parents: 19220
diff changeset
   286
                return new Sink.ChainedReference<P_OUT, Integer>(sink) {
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   287
                    IntConsumer downstreamAsInt = downstream::accept;
18528
9b02581cf079 8016324: filter/flatMap pipeline sinks should pass size information to downstream sink
psandoz
parents: 18155
diff changeset
   288
                    @Override
9b02581cf079 8016324: filter/flatMap pipeline sinks should pass size information to downstream sink
psandoz
parents: 18155
diff changeset
   289
                    public void begin(long size) {
9b02581cf079 8016324: filter/flatMap pipeline sinks should pass size information to downstream sink
psandoz
parents: 18155
diff changeset
   290
                        downstream.begin(-1);
9b02581cf079 8016324: filter/flatMap pipeline sinks should pass size information to downstream sink
psandoz
parents: 18155
diff changeset
   291
                    }
9b02581cf079 8016324: filter/flatMap pipeline sinks should pass size information to downstream sink
psandoz
parents: 18155
diff changeset
   292
9b02581cf079 8016324: filter/flatMap pipeline sinks should pass size information to downstream sink
psandoz
parents: 18155
diff changeset
   293
                    @Override
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   294
                    public void accept(P_OUT u) {
19800
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19799
diff changeset
   295
                        try (IntStream result = mapper.apply(u)) {
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19799
diff changeset
   296
                            // We can do better that this too; optimize for depth=0 case and just grab spliterator and forEach it
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19799
diff changeset
   297
                            if (result != null)
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19799
diff changeset
   298
                                result.sequential().forEach(downstreamAsInt);
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19799
diff changeset
   299
                        }
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   300
                    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   301
                };
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
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   307
    public final DoubleStream flatMapToDouble(Function<? super P_OUT, ? extends DoubleStream> mapper) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   308
        Objects.requireNonNull(mapper);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   309
        // We can do better than this, by polling cancellationRequested when stream is infinite
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   310
        return new DoublePipeline.StatelessOp<P_OUT>(this, StreamShape.REFERENCE,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   311
                                                     StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT | StreamOpFlag.NOT_SIZED) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   312
            @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   313
            Sink<P_OUT> opWrapSink(int flags, Sink<Double> sink) {
19593
ce0cd954351c 8023681: Fix raw type warning caused by Sink
henryjen
parents: 19220
diff changeset
   314
                return new Sink.ChainedReference<P_OUT, Double>(sink) {
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   315
                    DoubleConsumer downstreamAsDouble = downstream::accept;
18528
9b02581cf079 8016324: filter/flatMap pipeline sinks should pass size information to downstream sink
psandoz
parents: 18155
diff changeset
   316
                    @Override
9b02581cf079 8016324: filter/flatMap pipeline sinks should pass size information to downstream sink
psandoz
parents: 18155
diff changeset
   317
                    public void begin(long size) {
9b02581cf079 8016324: filter/flatMap pipeline sinks should pass size information to downstream sink
psandoz
parents: 18155
diff changeset
   318
                        downstream.begin(-1);
9b02581cf079 8016324: filter/flatMap pipeline sinks should pass size information to downstream sink
psandoz
parents: 18155
diff changeset
   319
                    }
9b02581cf079 8016324: filter/flatMap pipeline sinks should pass size information to downstream sink
psandoz
parents: 18155
diff changeset
   320
9b02581cf079 8016324: filter/flatMap pipeline sinks should pass size information to downstream sink
psandoz
parents: 18155
diff changeset
   321
                    @Override
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   322
                    public void accept(P_OUT u) {
19800
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19799
diff changeset
   323
                        try (DoubleStream result = mapper.apply(u)) {
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19799
diff changeset
   324
                            // We can do better that this too; optimize for depth=0 case and just grab spliterator and forEach it
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19799
diff changeset
   325
                            if (result != null)
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19799
diff changeset
   326
                                result.sequential().forEach(downstreamAsDouble);
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19799
diff changeset
   327
                        }
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   328
                    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   329
                };
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   330
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   331
        };
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   332
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   333
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   334
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   335
    public final LongStream flatMapToLong(Function<? super P_OUT, ? extends LongStream> mapper) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   336
        Objects.requireNonNull(mapper);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   337
        // We can do better than this, by polling cancellationRequested when stream is infinite
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   338
        return new LongPipeline.StatelessOp<P_OUT>(this, StreamShape.REFERENCE,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   339
                                                   StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT | StreamOpFlag.NOT_SIZED) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   340
            @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   341
            Sink<P_OUT> opWrapSink(int flags, Sink<Long> sink) {
19593
ce0cd954351c 8023681: Fix raw type warning caused by Sink
henryjen
parents: 19220
diff changeset
   342
                return new Sink.ChainedReference<P_OUT, Long>(sink) {
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   343
                    LongConsumer downstreamAsLong = downstream::accept;
18528
9b02581cf079 8016324: filter/flatMap pipeline sinks should pass size information to downstream sink
psandoz
parents: 18155
diff changeset
   344
                    @Override
9b02581cf079 8016324: filter/flatMap pipeline sinks should pass size information to downstream sink
psandoz
parents: 18155
diff changeset
   345
                    public void begin(long size) {
9b02581cf079 8016324: filter/flatMap pipeline sinks should pass size information to downstream sink
psandoz
parents: 18155
diff changeset
   346
                        downstream.begin(-1);
9b02581cf079 8016324: filter/flatMap pipeline sinks should pass size information to downstream sink
psandoz
parents: 18155
diff changeset
   347
                    }
9b02581cf079 8016324: filter/flatMap pipeline sinks should pass size information to downstream sink
psandoz
parents: 18155
diff changeset
   348
9b02581cf079 8016324: filter/flatMap pipeline sinks should pass size information to downstream sink
psandoz
parents: 18155
diff changeset
   349
                    @Override
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   350
                    public void accept(P_OUT u) {
19800
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19799
diff changeset
   351
                        try (LongStream result = mapper.apply(u)) {
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19799
diff changeset
   352
                            // We can do better that this too; optimize for depth=0 case and just grab spliterator and forEach it
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19799
diff changeset
   353
                            if (result != null)
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19799
diff changeset
   354
                                result.sequential().forEach(downstreamAsLong);
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19799
diff changeset
   355
                        }
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   356
                    }
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
        };
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   360
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   361
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   362
    @Override
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   363
    public final Stream<P_OUT> peek(Consumer<? super P_OUT> action) {
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   364
        Objects.requireNonNull(action);
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   365
        return new StatelessOp<P_OUT, P_OUT>(this, StreamShape.REFERENCE,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   366
                                     0) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   367
            @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   368
            Sink<P_OUT> opWrapSink(int flags, Sink<P_OUT> sink) {
19593
ce0cd954351c 8023681: Fix raw type warning caused by Sink
henryjen
parents: 19220
diff changeset
   369
                return new Sink.ChainedReference<P_OUT, P_OUT>(sink) {
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   370
                    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   371
                    public void accept(P_OUT u) {
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   372
                        action.accept(u);
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   373
                        downstream.accept(u);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   374
                    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   375
                };
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   376
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   377
        };
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   378
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   379
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   380
    // Stateful intermediate operations from Stream
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   381
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   382
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   383
    public final Stream<P_OUT> distinct() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   384
        return DistinctOps.makeRef(this);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   385
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   386
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   387
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   388
    public final Stream<P_OUT> sorted() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   389
        return SortedOps.makeRef(this);
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
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   393
    public final Stream<P_OUT> sorted(Comparator<? super P_OUT> comparator) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   394
        return SortedOps.makeRef(this, comparator);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   395
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   396
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   397
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   398
    public final Stream<P_OUT> limit(long maxSize) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   399
        if (maxSize < 0)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   400
            throw new IllegalArgumentException(Long.toString(maxSize));
20866
36155ee613ef 8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents: 19859
diff changeset
   401
        return SliceOps.makeRef(this, 0, maxSize);
17182
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
20866
36155ee613ef 8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents: 19859
diff changeset
   405
    public final Stream<P_OUT> skip(long n) {
36155ee613ef 8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents: 19859
diff changeset
   406
        if (n < 0)
36155ee613ef 8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents: 19859
diff changeset
   407
            throw new IllegalArgumentException(Long.toString(n));
36155ee613ef 8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents: 19859
diff changeset
   408
        if (n == 0)
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   409
            return this;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   410
        else
20866
36155ee613ef 8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents: 19859
diff changeset
   411
            return SliceOps.makeRef(this, n, -1);
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   412
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   413
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   414
    // Terminal operations from Stream
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   415
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   416
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   417
    public void forEach(Consumer<? super P_OUT> action) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   418
        evaluate(ForEachOps.makeRef(action, false));
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   419
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   420
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   421
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   422
    public void forEachOrdered(Consumer<? super P_OUT> action) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   423
        evaluate(ForEachOps.makeRef(action, true));
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   424
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   425
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   426
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   427
    @SuppressWarnings("unchecked")
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   428
    public final <A> A[] toArray(IntFunction<A[]> generator) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   429
        // 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
   430
        // there will be no static type checking.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   431
        // 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
   432
        // throughout the code-base.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   433
        // 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
   434
        // 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
   435
        // super type of U an ArrayStoreException will be thrown.
19220
d3d40ccb544e 8022476: cleanup some raw types and unchecked warnings in java.util.stream
mduigou
parents: 19214
diff changeset
   436
        @SuppressWarnings("rawtypes")
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   437
        IntFunction rawGenerator = (IntFunction) generator;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   438
        return (A[]) Nodes.flatten(evaluateToArrayNode(rawGenerator), rawGenerator)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   439
                              .asArray(rawGenerator);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   440
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   441
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   442
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   443
    public final Object[] toArray() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   444
        return toArray(Object[]::new);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   445
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   446
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   447
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   448
    public final boolean anyMatch(Predicate<? super P_OUT> predicate) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   449
        return evaluate(MatchOps.makeRef(predicate, MatchOps.MatchKind.ANY));
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   450
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   451
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   452
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   453
    public final boolean allMatch(Predicate<? super P_OUT> predicate) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   454
        return evaluate(MatchOps.makeRef(predicate, MatchOps.MatchKind.ALL));
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   455
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   456
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   457
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   458
    public final boolean noneMatch(Predicate<? super P_OUT> predicate) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   459
        return evaluate(MatchOps.makeRef(predicate, MatchOps.MatchKind.NONE));
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   460
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   461
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   462
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   463
    public final Optional<P_OUT> findFirst() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   464
        return evaluate(FindOps.makeRef(true));
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   465
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   466
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   467
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   468
    public final Optional<P_OUT> findAny() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   469
        return evaluate(FindOps.makeRef(false));
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   470
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   471
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   472
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   473
    public final P_OUT reduce(final P_OUT identity, final BinaryOperator<P_OUT> accumulator) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   474
        return evaluate(ReduceOps.makeRef(identity, accumulator, accumulator));
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 Optional<P_OUT> reduce(BinaryOperator<P_OUT> accumulator) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   479
        return evaluate(ReduceOps.makeRef(accumulator));
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   480
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   481
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   482
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   483
    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
   484
        return evaluate(ReduceOps.makeRef(identity, accumulator, combiner));
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   485
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   486
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   487
    @Override
19593
ce0cd954351c 8023681: Fix raw type warning caused by Sink
henryjen
parents: 19220
diff changeset
   488
    @SuppressWarnings("unchecked")
19799
efa9ff09b024 8024178: Difference in Stream.collect(Collector) methods located in jdk8 and jdk8-lambda repos
henryjen
parents: 19593
diff changeset
   489
    public final <R, A> R collect(Collector<? super P_OUT, A, R> collector) {
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   490
        A container;
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   491
        if (isParallel()
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   492
                && (collector.characteristics().contains(Collector.Characteristics.CONCURRENT))
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   493
                && (!isOrdered() || collector.characteristics().contains(Collector.Characteristics.UNORDERED))) {
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   494
            container = collector.supplier().get();
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   495
            BiConsumer<A, ? super P_OUT> accumulator = collector.accumulator();
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   496
            forEach(u -> accumulator.accept(container, u));
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   497
        }
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   498
        else {
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   499
            container = evaluate(ReduceOps.makeRef(collector));
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   500
        }
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   501
        return collector.characteristics().contains(Collector.Characteristics.IDENTITY_FINISH)
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   502
               ? (R) container
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   503
               : collector.finisher().apply(container);
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   504
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   505
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   506
    @Override
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   507
    public final <R> R collect(Supplier<R> supplier,
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   508
                               BiConsumer<R, ? super P_OUT> accumulator,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   509
                               BiConsumer<R, R> combiner) {
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   510
        return evaluate(ReduceOps.makeRef(supplier, accumulator, combiner));
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   511
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   512
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   513
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   514
    public final Optional<P_OUT> max(Comparator<? super P_OUT> comparator) {
18571
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18528
diff changeset
   515
        return reduce(BinaryOperator.maxBy(comparator));
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   516
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   517
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   518
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   519
    public final Optional<P_OUT> min(Comparator<? super P_OUT> comparator) {
18571
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18528
diff changeset
   520
        return reduce(BinaryOperator.minBy(comparator));
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   521
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
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   525
    public final long count() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   526
        return mapToLong(e -> 1L).sum();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   527
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   528
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   529
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   530
    //
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   531
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   532
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   533
     * Source stage of a ReferencePipeline.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   534
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   535
     * @param <E_IN> type of elements in the upstream source
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   536
     * @param <E_OUT> type of elements in produced by this stage
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   537
     * @since 1.8
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   538
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   539
    static class Head<E_IN, E_OUT> extends ReferencePipeline<E_IN, E_OUT> {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   540
        /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   541
         * Constructor for the source stage of a Stream.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   542
         *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   543
         * @param source {@code Supplier<Spliterator>} describing the stream
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   544
         *               source
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   545
         * @param sourceFlags the source flags for the stream source, described
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   546
         *                    in {@link StreamOpFlag}
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   547
         */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   548
        Head(Supplier<? extends Spliterator<?>> source,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   549
             int sourceFlags, boolean parallel) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   550
            super(source, sourceFlags, parallel);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   551
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   552
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   553
        /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   554
         * Constructor for the source stage of a Stream.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   555
         *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   556
         * @param source {@code Spliterator} describing the stream source
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   557
         * @param sourceFlags the source flags for the stream source, described
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   558
         *                    in {@link StreamOpFlag}
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   559
         */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   560
        Head(Spliterator<?> source,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   561
             int sourceFlags, boolean parallel) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   562
            super(source, sourceFlags, parallel);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   563
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   564
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   565
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   566
        final boolean opIsStateful() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   567
            throw new UnsupportedOperationException();
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
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   571
        final Sink<E_IN> opWrapSink(int flags, Sink<E_OUT> sink) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   572
            throw new UnsupportedOperationException();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   573
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   574
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   575
        // Optimized sequential terminal operations for the head of the pipeline
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   576
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   577
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   578
        public void forEach(Consumer<? super E_OUT> action) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   579
            if (!isParallel()) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   580
                sourceStageSpliterator().forEachRemaining(action);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   581
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   582
            else {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   583
                super.forEach(action);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   584
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   585
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   586
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   587
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   588
        public void forEachOrdered(Consumer<? super E_OUT> action) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   589
            if (!isParallel()) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   590
                sourceStageSpliterator().forEachRemaining(action);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   591
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   592
            else {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   593
                super.forEachOrdered(action);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   594
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   595
        }
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
     * Base class for a stateless intermediate stage of a Stream.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   600
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   601
     * @param <E_IN> type of elements in the upstream source
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   602
     * @param <E_OUT> type of elements in produced by this stage
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   603
     * @since 1.8
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   604
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   605
    abstract static class StatelessOp<E_IN, E_OUT>
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   606
            extends ReferencePipeline<E_IN, E_OUT> {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   607
        /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   608
         * Construct a new Stream by appending a stateless intermediate
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   609
         * operation to an existing stream.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   610
         *
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
        StatelessOp(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 false;
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
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   628
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   629
     * Base class for a stateful intermediate stage of a Stream.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   630
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   631
     * @param <E_IN> type of elements in the upstream source
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   632
     * @param <E_OUT> type of elements in produced by this stage
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   633
     * @since 1.8
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   634
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   635
    abstract static class StatefulOp<E_IN, E_OUT>
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   636
            extends ReferencePipeline<E_IN, E_OUT> {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   637
        /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   638
         * Construct a new Stream by appending a stateful intermediate operation
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   639
         * to an existing stream.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   640
         * @param upstream The upstream pipeline stage
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   641
         * @param inputShape The stream shape for the upstream pipeline stage
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   642
         * @param opFlags Operation flags for the new stage
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   643
         */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   644
        StatefulOp(AbstractPipeline<?, E_IN, ?> upstream,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   645
                   StreamShape inputShape,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   646
                   int opFlags) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   647
            super(upstream, opFlags);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   648
            assert upstream.getOutputShape() == inputShape;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   649
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   650
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   651
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   652
        final boolean opIsStateful() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   653
            return true;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   654
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   655
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   656
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   657
        abstract <P_IN> Node<E_OUT> opEvaluateParallel(PipelineHelper<E_OUT> helper,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   658
                                                       Spliterator<P_IN> spliterator,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   659
                                                       IntFunction<E_OUT[]> generator);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   660
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   661
}