src/java.base/share/classes/java/util/stream/AbstractPipeline.java
author jboes
Thu, 03 Oct 2019 18:59:56 +0100
changeset 58459 e25b317d0350
parent 47216 71c04702a3d5
permissions -rw-r--r--
8231161: Wrong return type in code sample in Collector API documentation Summary: Correct declaration of container from R to A and add compilation test Reviewed-by: smarks, lancea
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
     1
/*
36224
6a0a5bdfe79f 8147505: BaseStream.onClose() should not allow registering new handlers after stream is consumed
tvaleev
parents: 31644
diff changeset
     2
 * Copyright (c) 2012, 2016, Oracle and/or its affiliates. All rights reserved.
17182
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.Objects;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    28
import java.util.Spliterator;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    29
import java.util.function.IntFunction;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    30
import java.util.function.Supplier;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    31
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    32
/**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    33
 * Abstract base class for "pipeline" classes, which are the core
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    34
 * implementations of the Stream interface and its primitive specializations.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    35
 * Manages construction and evaluation of stream pipelines.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    36
 *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    37
 * <p>An {@code AbstractPipeline} represents an initial portion of a stream
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    38
 * pipeline, encapsulating a stream source and zero or more intermediate
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    39
 * operations.  The individual {@code AbstractPipeline} objects are often
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    40
 * referred to as <em>stages</em>, where each stage describes either the stream
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    41
 * source or an intermediate operation.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    42
 *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    43
 * <p>A concrete intermediate stage is generally built from an
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    44
 * {@code AbstractPipeline}, a shape-specific pipeline class which extends it
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    45
 * (e.g., {@code IntPipeline}) which is also abstract, and an operation-specific
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    46
 * concrete class which extends that.  {@code AbstractPipeline} contains most of
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    47
 * the mechanics of evaluating the pipeline, and implements methods that will be
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    48
 * used by the operation; the shape-specific classes add helper methods for
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    49
 * dealing with collection of results into the appropriate shape-specific
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    50
 * containers.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    51
 *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    52
 * <p>After chaining a new intermediate operation, or executing a terminal
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    53
 * operation, the stream is considered to be consumed, and no more intermediate
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    54
 * or terminal operations are permitted on this stream instance.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    55
 *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    56
 * @implNote
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    57
 * <p>For sequential streams, and parallel streams without
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    58
 * <a href="package-summary.html#StreamOps">stateful intermediate
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    59
 * operations</a>, parallel streams, pipeline evaluation is done in a single
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    60
 * pass that "jams" all the operations together.  For parallel streams with
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    61
 * stateful operations, execution is divided into segments, where each
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    62
 * stateful operations marks the end of a segment, and each segment is
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    63
 * evaluated separately and the result used as the input to the next
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    64
 * segment.  In all cases, the source data is not consumed until a terminal
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    65
 * operation begins.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    66
 *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    67
 * @param <E_IN>  type of input elements
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    68
 * @param <E_OUT> type of output elements
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    69
 * @param <S> type of the subclass implementing {@code BaseStream}
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    70
 * @since 1.8
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    71
 */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    72
abstract class AbstractPipeline<E_IN, E_OUT, S extends BaseStream<E_OUT, S>>
18789
b518cd4045bc 8019551: Make BaseStream public
psandoz
parents: 18572
diff changeset
    73
        extends PipelineHelper<E_OUT> implements BaseStream<E_OUT, S> {
19800
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19220
diff changeset
    74
    private static final String MSG_STREAM_LINKED = "stream has already been operated upon or closed";
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19220
diff changeset
    75
    private static final String MSG_CONSUMED = "source already consumed or closed";
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19220
diff changeset
    76
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    77
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    78
     * Backlink to the head of the pipeline chain (self if this is the source
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    79
     * stage).
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    80
     */
19220
d3d40ccb544e 8022476: cleanup some raw types and unchecked warnings in java.util.stream
mduigou
parents: 18789
diff changeset
    81
    @SuppressWarnings("rawtypes")
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    82
    private final AbstractPipeline sourceStage;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    83
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    84
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    85
     * The "upstream" pipeline, or null if this is the source stage.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    86
     */
19220
d3d40ccb544e 8022476: cleanup some raw types and unchecked warnings in java.util.stream
mduigou
parents: 18789
diff changeset
    87
    @SuppressWarnings("rawtypes")
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    88
    private final AbstractPipeline previousStage;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    89
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    90
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    91
     * The operation flags for the intermediate operation represented by this
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    92
     * pipeline object.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    93
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    94
    protected final int sourceOrOpFlags;
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
     * The next stage in the pipeline, or null if this is the last stage.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    98
     * Effectively final at the point of linking to the next pipeline.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    99
     */
19220
d3d40ccb544e 8022476: cleanup some raw types and unchecked warnings in java.util.stream
mduigou
parents: 18789
diff changeset
   100
    @SuppressWarnings("rawtypes")
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   101
    private AbstractPipeline nextStage;
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
     * The number of intermediate operations between this pipeline object
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   105
     * and the stream source if sequential, or the previous stateful if parallel.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   106
     * Valid at the point of pipeline preparation for evaluation.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   107
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   108
    private int depth;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   109
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   110
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   111
     * The combined source and operation flags for the source and all operations
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   112
     * up to and including the operation represented by this pipeline object.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   113
     * Valid at the point of pipeline preparation for evaluation.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   114
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   115
    private int combinedFlags;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   116
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   117
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   118
     * The source spliterator. Only valid for the head pipeline.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   119
     * Before the pipeline is consumed if non-null then {@code sourceSupplier}
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   120
     * must be null. After the pipeline is consumed if non-null then is set to
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   121
     * null.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   122
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   123
    private Spliterator<?> sourceSpliterator;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   124
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   125
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   126
     * The source supplier. Only valid for the head pipeline. Before the
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   127
     * pipeline is consumed if non-null then {@code sourceSpliterator} must be
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   128
     * null. After the pipeline is consumed if non-null then is set to null.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   129
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   130
    private Supplier<? extends Spliterator<?>> sourceSupplier;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   131
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   132
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   133
     * True if this pipeline has been linked or consumed
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   134
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   135
    private boolean linkedOrConsumed;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   136
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   137
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   138
     * True if there are any stateful ops in the pipeline; only valid for the
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   139
     * source stage.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   140
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   141
    private boolean sourceAnyStateful;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   142
19800
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19220
diff changeset
   143
    private Runnable sourceCloseAction;
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19220
diff changeset
   144
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   145
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   146
     * True if pipeline is parallel, otherwise the pipeline is sequential; only
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   147
     * valid for the source stage.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   148
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   149
    private boolean parallel;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   150
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   151
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   152
     * Constructor for the head of a stream pipeline.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   153
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   154
     * @param source {@code Supplier<Spliterator>} describing the stream source
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   155
     * @param sourceFlags The source flags for the stream source, described in
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   156
     * {@link StreamOpFlag}
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   157
     * @param parallel True if the pipeline is parallel
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   158
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   159
    AbstractPipeline(Supplier<? extends Spliterator<?>> source,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   160
                     int sourceFlags, boolean parallel) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   161
        this.previousStage = null;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   162
        this.sourceSupplier = source;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   163
        this.sourceStage = this;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   164
        this.sourceOrOpFlags = sourceFlags & StreamOpFlag.STREAM_MASK;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   165
        // The following is an optimization of:
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   166
        // StreamOpFlag.combineOpFlags(sourceOrOpFlags, StreamOpFlag.INITIAL_OPS_VALUE);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   167
        this.combinedFlags = (~(sourceOrOpFlags << 1)) & StreamOpFlag.INITIAL_OPS_VALUE;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   168
        this.depth = 0;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   169
        this.parallel = parallel;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   170
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   171
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   172
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   173
     * Constructor for the head of a stream pipeline.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   174
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   175
     * @param source {@code Spliterator} describing the stream source
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   176
     * @param sourceFlags the source flags for the stream source, described in
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   177
     * {@link StreamOpFlag}
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   178
     * @param parallel {@code true} if the pipeline is parallel
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   179
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   180
    AbstractPipeline(Spliterator<?> source,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   181
                     int sourceFlags, boolean parallel) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   182
        this.previousStage = null;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   183
        this.sourceSpliterator = source;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   184
        this.sourceStage = this;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   185
        this.sourceOrOpFlags = sourceFlags & StreamOpFlag.STREAM_MASK;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   186
        // The following is an optimization of:
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   187
        // StreamOpFlag.combineOpFlags(sourceOrOpFlags, StreamOpFlag.INITIAL_OPS_VALUE);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   188
        this.combinedFlags = (~(sourceOrOpFlags << 1)) & StreamOpFlag.INITIAL_OPS_VALUE;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   189
        this.depth = 0;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   190
        this.parallel = parallel;
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
     * Constructor for appending an intermediate operation stage onto an
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   195
     * existing pipeline.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   196
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   197
     * @param previousStage the upstream pipeline stage
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   198
     * @param opFlags the operation flags for the new stage, described in
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   199
     * {@link StreamOpFlag}
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   200
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   201
    AbstractPipeline(AbstractPipeline<?, E_IN, ?> previousStage, int opFlags) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   202
        if (previousStage.linkedOrConsumed)
19800
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19220
diff changeset
   203
            throw new IllegalStateException(MSG_STREAM_LINKED);
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   204
        previousStage.linkedOrConsumed = true;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   205
        previousStage.nextStage = this;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   206
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   207
        this.previousStage = previousStage;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   208
        this.sourceOrOpFlags = opFlags & StreamOpFlag.OP_MASK;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   209
        this.combinedFlags = StreamOpFlag.combineOpFlags(opFlags, previousStage.combinedFlags);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   210
        this.sourceStage = previousStage.sourceStage;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   211
        if (opIsStateful())
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   212
            sourceStage.sourceAnyStateful = true;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   213
        this.depth = previousStage.depth + 1;
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
    // Terminal evaluation methods
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   218
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   219
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   220
     * Evaluate the pipeline with a terminal operation to produce a result.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   221
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   222
     * @param <R> the type of result
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   223
     * @param terminalOp the terminal operation to be applied to the pipeline.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   224
     * @return the result
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   225
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   226
    final <R> R evaluate(TerminalOp<E_OUT, R> terminalOp) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   227
        assert getOutputShape() == terminalOp.inputShape();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   228
        if (linkedOrConsumed)
19800
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19220
diff changeset
   229
            throw new IllegalStateException(MSG_STREAM_LINKED);
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   230
        linkedOrConsumed = true;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   231
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   232
        return isParallel()
19220
d3d40ccb544e 8022476: cleanup some raw types and unchecked warnings in java.util.stream
mduigou
parents: 18789
diff changeset
   233
               ? terminalOp.evaluateParallel(this, sourceSpliterator(terminalOp.getOpFlags()))
d3d40ccb544e 8022476: cleanup some raw types and unchecked warnings in java.util.stream
mduigou
parents: 18789
diff changeset
   234
               : terminalOp.evaluateSequential(this, sourceSpliterator(terminalOp.getOpFlags()));
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   235
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   236
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   237
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   238
     * Collect the elements output from the pipeline stage.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   239
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   240
     * @param generator the array generator to be used to create array instances
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   241
     * @return a flat array-backed Node that holds the collected output elements
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   242
     */
19220
d3d40ccb544e 8022476: cleanup some raw types and unchecked warnings in java.util.stream
mduigou
parents: 18789
diff changeset
   243
    @SuppressWarnings("unchecked")
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   244
    final Node<E_OUT> evaluateToArrayNode(IntFunction<E_OUT[]> generator) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   245
        if (linkedOrConsumed)
19800
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19220
diff changeset
   246
            throw new IllegalStateException(MSG_STREAM_LINKED);
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   247
        linkedOrConsumed = true;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   248
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   249
        // If the last intermediate operation is stateful then
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   250
        // evaluate directly to avoid an extra collection step
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   251
        if (isParallel() && previousStage != null && opIsStateful()) {
31248
0006a0e47a04 8129120: Terminal operation properties should not be back-propagated to upstream operations
psandoz
parents: 29617
diff changeset
   252
            // Set the depth of this, last, pipeline stage to zero to slice the
0006a0e47a04 8129120: Terminal operation properties should not be back-propagated to upstream operations
psandoz
parents: 29617
diff changeset
   253
            // pipeline such that this operation will not be included in the
0006a0e47a04 8129120: Terminal operation properties should not be back-propagated to upstream operations
psandoz
parents: 29617
diff changeset
   254
            // upstream slice and upstream operations will not be included
0006a0e47a04 8129120: Terminal operation properties should not be back-propagated to upstream operations
psandoz
parents: 29617
diff changeset
   255
            // in this slice
0006a0e47a04 8129120: Terminal operation properties should not be back-propagated to upstream operations
psandoz
parents: 29617
diff changeset
   256
            depth = 0;
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   257
            return opEvaluateParallel(previousStage, previousStage.sourceSpliterator(0), generator);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   258
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   259
        else {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   260
            return evaluate(sourceSpliterator(0), true, generator);
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
     * Gets the source stage spliterator if this pipeline stage is the source
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   266
     * stage.  The pipeline is consumed after this method is called and
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   267
     * returns successfully.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   268
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   269
     * @return the source stage spliterator
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   270
     * @throws IllegalStateException if this pipeline stage is not the source
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   271
     *         stage.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   272
     */
19220
d3d40ccb544e 8022476: cleanup some raw types and unchecked warnings in java.util.stream
mduigou
parents: 18789
diff changeset
   273
    @SuppressWarnings("unchecked")
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   274
    final Spliterator<E_OUT> sourceStageSpliterator() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   275
        if (this != sourceStage)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   276
            throw new IllegalStateException();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   277
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   278
        if (linkedOrConsumed)
19800
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19220
diff changeset
   279
            throw new IllegalStateException(MSG_STREAM_LINKED);
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   280
        linkedOrConsumed = true;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   281
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   282
        if (sourceStage.sourceSpliterator != null) {
19220
d3d40ccb544e 8022476: cleanup some raw types and unchecked warnings in java.util.stream
mduigou
parents: 18789
diff changeset
   283
            @SuppressWarnings("unchecked")
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   284
            Spliterator<E_OUT> s = sourceStage.sourceSpliterator;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   285
            sourceStage.sourceSpliterator = null;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   286
            return s;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   287
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   288
        else if (sourceStage.sourceSupplier != null) {
19220
d3d40ccb544e 8022476: cleanup some raw types and unchecked warnings in java.util.stream
mduigou
parents: 18789
diff changeset
   289
            @SuppressWarnings("unchecked")
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   290
            Spliterator<E_OUT> s = (Spliterator<E_OUT>) sourceStage.sourceSupplier.get();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   291
            sourceStage.sourceSupplier = null;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   292
            return s;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   293
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   294
        else {
19800
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19220
diff changeset
   295
            throw new IllegalStateException(MSG_CONSUMED);
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   296
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   297
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   298
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   299
    // BaseStream
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   300
18789
b518cd4045bc 8019551: Make BaseStream public
psandoz
parents: 18572
diff changeset
   301
    @Override
19220
d3d40ccb544e 8022476: cleanup some raw types and unchecked warnings in java.util.stream
mduigou
parents: 18789
diff changeset
   302
    @SuppressWarnings("unchecked")
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   303
    public final S sequential() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   304
        sourceStage.parallel = false;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   305
        return (S) this;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   306
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   307
18789
b518cd4045bc 8019551: Make BaseStream public
psandoz
parents: 18572
diff changeset
   308
    @Override
19220
d3d40ccb544e 8022476: cleanup some raw types and unchecked warnings in java.util.stream
mduigou
parents: 18789
diff changeset
   309
    @SuppressWarnings("unchecked")
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   310
    public final S parallel() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   311
        sourceStage.parallel = true;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   312
        return (S) this;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   313
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   314
19800
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19220
diff changeset
   315
    @Override
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19220
diff changeset
   316
    public void close() {
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19220
diff changeset
   317
        linkedOrConsumed = true;
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19220
diff changeset
   318
        sourceSupplier = null;
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19220
diff changeset
   319
        sourceSpliterator = null;
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19220
diff changeset
   320
        if (sourceStage.sourceCloseAction != null) {
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19220
diff changeset
   321
            Runnable closeAction = sourceStage.sourceCloseAction;
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19220
diff changeset
   322
            sourceStage.sourceCloseAction = null;
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19220
diff changeset
   323
            closeAction.run();
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19220
diff changeset
   324
        }
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19220
diff changeset
   325
    }
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19220
diff changeset
   326
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19220
diff changeset
   327
    @Override
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19220
diff changeset
   328
    @SuppressWarnings("unchecked")
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19220
diff changeset
   329
    public S onClose(Runnable closeHandler) {
36224
6a0a5bdfe79f 8147505: BaseStream.onClose() should not allow registering new handlers after stream is consumed
tvaleev
parents: 31644
diff changeset
   330
        if (linkedOrConsumed)
6a0a5bdfe79f 8147505: BaseStream.onClose() should not allow registering new handlers after stream is consumed
tvaleev
parents: 31644
diff changeset
   331
            throw new IllegalStateException(MSG_STREAM_LINKED);
25526
d3cbdae6e9f9 8044047: Missing null pointer checks for streams
psandoz
parents: 19800
diff changeset
   332
        Objects.requireNonNull(closeHandler);
19800
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19220
diff changeset
   333
        Runnable existingHandler = sourceStage.sourceCloseAction;
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19220
diff changeset
   334
        sourceStage.sourceCloseAction =
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19220
diff changeset
   335
                (existingHandler == null)
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19220
diff changeset
   336
                ? closeHandler
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19220
diff changeset
   337
                : Streams.composeWithExceptions(existingHandler, closeHandler);
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19220
diff changeset
   338
        return (S) this;
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19220
diff changeset
   339
    }
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19220
diff changeset
   340
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   341
    // Primitive specialization use co-variant overrides, hence is not final
18789
b518cd4045bc 8019551: Make BaseStream public
psandoz
parents: 18572
diff changeset
   342
    @Override
19220
d3d40ccb544e 8022476: cleanup some raw types and unchecked warnings in java.util.stream
mduigou
parents: 18789
diff changeset
   343
    @SuppressWarnings("unchecked")
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   344
    public Spliterator<E_OUT> spliterator() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   345
        if (linkedOrConsumed)
19800
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19220
diff changeset
   346
            throw new IllegalStateException(MSG_STREAM_LINKED);
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   347
        linkedOrConsumed = true;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   348
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   349
        if (this == sourceStage) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   350
            if (sourceStage.sourceSpliterator != null) {
19220
d3d40ccb544e 8022476: cleanup some raw types and unchecked warnings in java.util.stream
mduigou
parents: 18789
diff changeset
   351
                @SuppressWarnings("unchecked")
d3d40ccb544e 8022476: cleanup some raw types and unchecked warnings in java.util.stream
mduigou
parents: 18789
diff changeset
   352
                Spliterator<E_OUT> s = (Spliterator<E_OUT>) sourceStage.sourceSpliterator;
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   353
                sourceStage.sourceSpliterator = null;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   354
                return s;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   355
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   356
            else if (sourceStage.sourceSupplier != null) {
19220
d3d40ccb544e 8022476: cleanup some raw types and unchecked warnings in java.util.stream
mduigou
parents: 18789
diff changeset
   357
                @SuppressWarnings("unchecked")
d3d40ccb544e 8022476: cleanup some raw types and unchecked warnings in java.util.stream
mduigou
parents: 18789
diff changeset
   358
                Supplier<Spliterator<E_OUT>> s = (Supplier<Spliterator<E_OUT>>) sourceStage.sourceSupplier;
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   359
                sourceStage.sourceSupplier = null;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   360
                return lazySpliterator(s);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   361
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   362
            else {
19800
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19220
diff changeset
   363
                throw new IllegalStateException(MSG_CONSUMED);
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   364
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   365
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   366
        else {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   367
            return wrap(this, () -> sourceSpliterator(0), isParallel());
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   368
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   369
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   370
18789
b518cd4045bc 8019551: Make BaseStream public
psandoz
parents: 18572
diff changeset
   371
    @Override
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   372
    public final boolean isParallel() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   373
        return sourceStage.parallel;
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
     * Returns the composition of stream flags of the stream source and all
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   379
     * intermediate operations.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   380
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   381
     * @return the composition of stream flags of the stream source and all
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   382
     *         intermediate operations
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   383
     * @see StreamOpFlag
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   384
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   385
    final int getStreamFlags() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   386
        return StreamOpFlag.toStreamFlags(combinedFlags);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   387
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   388
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   389
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   390
     * Get the source spliterator for this pipeline stage.  For a sequential or
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   391
     * stateless parallel pipeline, this is the source spliterator.  For a
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   392
     * stateful parallel pipeline, this is a spliterator describing the results
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   393
     * of all computations up to and including the most recent stateful
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   394
     * operation.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   395
     */
19220
d3d40ccb544e 8022476: cleanup some raw types and unchecked warnings in java.util.stream
mduigou
parents: 18789
diff changeset
   396
    @SuppressWarnings("unchecked")
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   397
    private Spliterator<?> sourceSpliterator(int terminalFlags) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   398
        // Get the source spliterator of the pipeline
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   399
        Spliterator<?> spliterator = null;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   400
        if (sourceStage.sourceSpliterator != null) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   401
            spliterator = sourceStage.sourceSpliterator;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   402
            sourceStage.sourceSpliterator = null;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   403
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   404
        else if (sourceStage.sourceSupplier != null) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   405
            spliterator = (Spliterator<?>) sourceStage.sourceSupplier.get();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   406
            sourceStage.sourceSupplier = null;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   407
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   408
        else {
19800
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19220
diff changeset
   409
            throw new IllegalStateException(MSG_CONSUMED);
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   410
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   411
29617
4922c98744c7 8075307: Pipeline calculating inconsistent flag state for parallel stateful ops
psandoz
parents: 25859
diff changeset
   412
        if (isParallel() && sourceStage.sourceAnyStateful) {
31248
0006a0e47a04 8129120: Terminal operation properties should not be back-propagated to upstream operations
psandoz
parents: 29617
diff changeset
   413
            // Adapt the source spliterator, evaluating each stateful op
0006a0e47a04 8129120: Terminal operation properties should not be back-propagated to upstream operations
psandoz
parents: 29617
diff changeset
   414
            // in the pipeline up to and including this pipeline stage.
0006a0e47a04 8129120: Terminal operation properties should not be back-propagated to upstream operations
psandoz
parents: 29617
diff changeset
   415
            // The depth and flags of each pipeline stage are adjusted accordingly.
29617
4922c98744c7 8075307: Pipeline calculating inconsistent flag state for parallel stateful ops
psandoz
parents: 25859
diff changeset
   416
            int depth = 1;
4922c98744c7 8075307: Pipeline calculating inconsistent flag state for parallel stateful ops
psandoz
parents: 25859
diff changeset
   417
            for (@SuppressWarnings("rawtypes") AbstractPipeline u = sourceStage, p = sourceStage.nextStage, e = this;
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   418
                 u != e;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   419
                 u = p, p = p.nextStage) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   420
29617
4922c98744c7 8075307: Pipeline calculating inconsistent flag state for parallel stateful ops
psandoz
parents: 25859
diff changeset
   421
                int thisOpFlags = p.sourceOrOpFlags;
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   422
                if (p.opIsStateful()) {
31248
0006a0e47a04 8129120: Terminal operation properties should not be back-propagated to upstream operations
psandoz
parents: 29617
diff changeset
   423
                    depth = 0;
0006a0e47a04 8129120: Terminal operation properties should not be back-propagated to upstream operations
psandoz
parents: 29617
diff changeset
   424
29617
4922c98744c7 8075307: Pipeline calculating inconsistent flag state for parallel stateful ops
psandoz
parents: 25859
diff changeset
   425
                    if (StreamOpFlag.SHORT_CIRCUIT.isKnown(thisOpFlags)) {
4922c98744c7 8075307: Pipeline calculating inconsistent flag state for parallel stateful ops
psandoz
parents: 25859
diff changeset
   426
                        // Clear the short circuit flag for next pipeline stage
4922c98744c7 8075307: Pipeline calculating inconsistent flag state for parallel stateful ops
psandoz
parents: 25859
diff changeset
   427
                        // This stage encapsulates short-circuiting, the next
4922c98744c7 8075307: Pipeline calculating inconsistent flag state for parallel stateful ops
psandoz
parents: 25859
diff changeset
   428
                        // stage may not have any short-circuit operations, and
4922c98744c7 8075307: Pipeline calculating inconsistent flag state for parallel stateful ops
psandoz
parents: 25859
diff changeset
   429
                        // if so spliterator.forEachRemaining should be used
4922c98744c7 8075307: Pipeline calculating inconsistent flag state for parallel stateful ops
psandoz
parents: 25859
diff changeset
   430
                        // for traversal
4922c98744c7 8075307: Pipeline calculating inconsistent flag state for parallel stateful ops
psandoz
parents: 25859
diff changeset
   431
                        thisOpFlags = thisOpFlags & ~StreamOpFlag.IS_SHORT_CIRCUIT;
4922c98744c7 8075307: Pipeline calculating inconsistent flag state for parallel stateful ops
psandoz
parents: 25859
diff changeset
   432
                    }
4922c98744c7 8075307: Pipeline calculating inconsistent flag state for parallel stateful ops
psandoz
parents: 25859
diff changeset
   433
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   434
                    spliterator = p.opEvaluateParallelLazy(u, spliterator);
29617
4922c98744c7 8075307: Pipeline calculating inconsistent flag state for parallel stateful ops
psandoz
parents: 25859
diff changeset
   435
4922c98744c7 8075307: Pipeline calculating inconsistent flag state for parallel stateful ops
psandoz
parents: 25859
diff changeset
   436
                    // Inject or clear SIZED on the source pipeline stage
4922c98744c7 8075307: Pipeline calculating inconsistent flag state for parallel stateful ops
psandoz
parents: 25859
diff changeset
   437
                    // based on the stage's spliterator
4922c98744c7 8075307: Pipeline calculating inconsistent flag state for parallel stateful ops
psandoz
parents: 25859
diff changeset
   438
                    thisOpFlags = spliterator.hasCharacteristics(Spliterator.SIZED)
4922c98744c7 8075307: Pipeline calculating inconsistent flag state for parallel stateful ops
psandoz
parents: 25859
diff changeset
   439
                            ? (thisOpFlags & ~StreamOpFlag.NOT_SIZED) | StreamOpFlag.IS_SIZED
4922c98744c7 8075307: Pipeline calculating inconsistent flag state for parallel stateful ops
psandoz
parents: 25859
diff changeset
   440
                            : (thisOpFlags & ~StreamOpFlag.IS_SIZED) | StreamOpFlag.NOT_SIZED;
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   441
                }
31248
0006a0e47a04 8129120: Terminal operation properties should not be back-propagated to upstream operations
psandoz
parents: 29617
diff changeset
   442
                p.depth = depth++;
29617
4922c98744c7 8075307: Pipeline calculating inconsistent flag state for parallel stateful ops
psandoz
parents: 25859
diff changeset
   443
                p.combinedFlags = StreamOpFlag.combineOpFlags(thisOpFlags, u.combinedFlags);
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   444
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   445
        }
29617
4922c98744c7 8075307: Pipeline calculating inconsistent flag state for parallel stateful ops
psandoz
parents: 25859
diff changeset
   446
31248
0006a0e47a04 8129120: Terminal operation properties should not be back-propagated to upstream operations
psandoz
parents: 29617
diff changeset
   447
        if (terminalFlags != 0)  {
29617
4922c98744c7 8075307: Pipeline calculating inconsistent flag state for parallel stateful ops
psandoz
parents: 25859
diff changeset
   448
            // Apply flags from the terminal operation to last pipeline stage
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   449
            combinedFlags = StreamOpFlag.combineOpFlags(terminalFlags, combinedFlags);
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
        return spliterator;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   453
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   454
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   455
    // PipelineHelper
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   456
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   457
    @Override
18572
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   458
    final StreamShape getSourceShape() {
19220
d3d40ccb544e 8022476: cleanup some raw types and unchecked warnings in java.util.stream
mduigou
parents: 18789
diff changeset
   459
        @SuppressWarnings("rawtypes")
18572
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   460
        AbstractPipeline p = AbstractPipeline.this;
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   461
        while (p.depth > 0) {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   462
            p = p.previousStage;
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   463
        }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   464
        return p.getOutputShape();
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   465
    }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   466
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   467
    @Override
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   468
    final <P_IN> long exactOutputSizeIfKnown(Spliterator<P_IN> spliterator) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   469
        return StreamOpFlag.SIZED.isKnown(getStreamAndOpFlags()) ? spliterator.getExactSizeIfKnown() : -1;
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
    final <P_IN, S extends Sink<E_OUT>> S wrapAndCopyInto(S sink, Spliterator<P_IN> spliterator) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   474
        copyInto(wrapSink(Objects.requireNonNull(sink)), spliterator);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   475
        return sink;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   476
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   477
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   478
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   479
    final <P_IN> void copyInto(Sink<P_IN> wrappedSink, Spliterator<P_IN> spliterator) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   480
        Objects.requireNonNull(wrappedSink);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   481
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   482
        if (!StreamOpFlag.SHORT_CIRCUIT.isKnown(getStreamAndOpFlags())) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   483
            wrappedSink.begin(spliterator.getExactSizeIfKnown());
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   484
            spliterator.forEachRemaining(wrappedSink);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   485
            wrappedSink.end();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   486
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   487
        else {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   488
            copyIntoWithCancel(wrappedSink, spliterator);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   489
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   490
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   491
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   492
    @Override
19220
d3d40ccb544e 8022476: cleanup some raw types and unchecked warnings in java.util.stream
mduigou
parents: 18789
diff changeset
   493
    @SuppressWarnings("unchecked")
31644
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 31248
diff changeset
   494
    final <P_IN> boolean copyIntoWithCancel(Sink<P_IN> wrappedSink, Spliterator<P_IN> spliterator) {
19220
d3d40ccb544e 8022476: cleanup some raw types and unchecked warnings in java.util.stream
mduigou
parents: 18789
diff changeset
   495
        @SuppressWarnings({"rawtypes","unchecked"})
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   496
        AbstractPipeline p = AbstractPipeline.this;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   497
        while (p.depth > 0) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   498
            p = p.previousStage;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   499
        }
31644
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 31248
diff changeset
   500
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   501
        wrappedSink.begin(spliterator.getExactSizeIfKnown());
31644
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 31248
diff changeset
   502
        boolean cancelled = p.forEachWithCancel(spliterator, wrappedSink);
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   503
        wrappedSink.end();
31644
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 31248
diff changeset
   504
        return cancelled;
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   505
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   506
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   507
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   508
    final int getStreamAndOpFlags() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   509
        return combinedFlags;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   510
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   511
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   512
    final boolean isOrdered() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   513
        return StreamOpFlag.ORDERED.isKnown(combinedFlags);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   514
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   515
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   516
    @Override
19220
d3d40ccb544e 8022476: cleanup some raw types and unchecked warnings in java.util.stream
mduigou
parents: 18789
diff changeset
   517
    @SuppressWarnings("unchecked")
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   518
    final <P_IN> Sink<P_IN> wrapSink(Sink<E_OUT> sink) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   519
        Objects.requireNonNull(sink);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   520
19220
d3d40ccb544e 8022476: cleanup some raw types and unchecked warnings in java.util.stream
mduigou
parents: 18789
diff changeset
   521
        for ( @SuppressWarnings("rawtypes") AbstractPipeline p=AbstractPipeline.this; p.depth > 0; p=p.previousStage) {
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   522
            sink = p.opWrapSink(p.previousStage.combinedFlags, sink);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   523
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   524
        return (Sink<P_IN>) sink;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   525
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   526
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   527
    @Override
19220
d3d40ccb544e 8022476: cleanup some raw types and unchecked warnings in java.util.stream
mduigou
parents: 18789
diff changeset
   528
    @SuppressWarnings("unchecked")
18572
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   529
    final <P_IN> Spliterator<E_OUT> wrapSpliterator(Spliterator<P_IN> sourceSpliterator) {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   530
        if (depth == 0) {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   531
            return (Spliterator<E_OUT>) sourceSpliterator;
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   532
        }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   533
        else {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   534
            return wrap(this, () -> sourceSpliterator, isParallel());
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   535
        }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   536
    }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   537
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   538
    @Override
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   539
    @SuppressWarnings("unchecked")
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   540
    final <P_IN> Node<E_OUT> evaluate(Spliterator<P_IN> spliterator,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   541
                                      boolean flatten,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   542
                                      IntFunction<E_OUT[]> generator) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   543
        if (isParallel()) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   544
            // @@@ Optimize if op of this pipeline stage is a stateful op
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   545
            return evaluateToNode(this, spliterator, flatten, generator);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   546
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   547
        else {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   548
            Node.Builder<E_OUT> nb = makeNodeBuilder(
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   549
                    exactOutputSizeIfKnown(spliterator), generator);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   550
            return wrapAndCopyInto(nb, spliterator).build();
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
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   555
    // Shape-specific abstract methods, implemented by XxxPipeline classes
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
     * Get the output shape of the pipeline.  If the pipeline is the head,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   559
     * then it's output shape corresponds to the shape of the source.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   560
     * Otherwise, it's output shape corresponds to the output shape of the
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   561
     * associated operation.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   562
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   563
     * @return the output shape
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   564
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   565
    abstract StreamShape getOutputShape();
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
     * Collect elements output from a pipeline into a Node that holds elements
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   569
     * of this shape.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   570
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   571
     * @param helper the pipeline helper describing the pipeline stages
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   572
     * @param spliterator the source spliterator
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   573
     * @param flattenTree true if the returned node should be flattened
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   574
     * @param generator the array generator
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   575
     * @return a Node holding the output of the pipeline
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   576
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   577
    abstract <P_IN> Node<E_OUT> evaluateToNode(PipelineHelper<E_OUT> helper,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   578
                                               Spliterator<P_IN> spliterator,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   579
                                               boolean flattenTree,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   580
                                               IntFunction<E_OUT[]> generator);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   581
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   582
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   583
     * Create a spliterator that wraps a source spliterator, compatible with
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   584
     * this stream shape, and operations associated with a {@link
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   585
     * PipelineHelper}.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   586
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   587
     * @param ph the pipeline helper describing the pipeline stages
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   588
     * @param supplier the supplier of a spliterator
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   589
     * @return a wrapping spliterator compatible with this shape
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   590
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   591
    abstract <P_IN> Spliterator<E_OUT> wrap(PipelineHelper<E_OUT> ph,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   592
                                            Supplier<Spliterator<P_IN>> supplier,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   593
                                            boolean isParallel);
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
     * Create a lazy spliterator that wraps and obtains the supplied the
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   597
     * spliterator when a method is invoked on the lazy spliterator.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   598
     * @param supplier the supplier of a spliterator
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   599
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   600
    abstract Spliterator<E_OUT> lazySpliterator(Supplier<? extends Spliterator<E_OUT>> supplier);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   601
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   602
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   603
     * Traverse the elements of a spliterator compatible with this stream shape,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   604
     * pushing those elements into a sink.   If the sink requests cancellation,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   605
     * no further elements will be pulled or pushed.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   606
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   607
     * @param spliterator the spliterator to pull elements from
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   608
     * @param sink the sink to push elements to
31644
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 31248
diff changeset
   609
     * @return true if the cancellation was requested
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   610
     */
31644
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 31248
diff changeset
   611
    abstract boolean forEachWithCancel(Spliterator<E_OUT> spliterator, Sink<E_OUT> sink);
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   612
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   613
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   614
     * Make a node builder compatible with this stream shape.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   615
     *
19220
d3d40ccb544e 8022476: cleanup some raw types and unchecked warnings in java.util.stream
mduigou
parents: 18789
diff changeset
   616
     * @param exactSizeIfKnown if {@literal >=0}, then a node builder will be
d3d40ccb544e 8022476: cleanup some raw types and unchecked warnings in java.util.stream
mduigou
parents: 18789
diff changeset
   617
     * created that has a fixed capacity of at most sizeIfKnown elements. If
d3d40ccb544e 8022476: cleanup some raw types and unchecked warnings in java.util.stream
mduigou
parents: 18789
diff changeset
   618
     * {@literal < 0}, then the node builder has an unfixed capacity. A fixed
d3d40ccb544e 8022476: cleanup some raw types and unchecked warnings in java.util.stream
mduigou
parents: 18789
diff changeset
   619
     * capacity node builder will throw exceptions if an element is added after
d3d40ccb544e 8022476: cleanup some raw types and unchecked warnings in java.util.stream
mduigou
parents: 18789
diff changeset
   620
     * builder has reached capacity, or is built before the builder has reached
d3d40ccb544e 8022476: cleanup some raw types and unchecked warnings in java.util.stream
mduigou
parents: 18789
diff changeset
   621
     * capacity.
d3d40ccb544e 8022476: cleanup some raw types and unchecked warnings in java.util.stream
mduigou
parents: 18789
diff changeset
   622
     *
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   623
     * @param generator the array generator to be used to create instances of a
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   624
     * T[] array. For implementations supporting primitive nodes, this parameter
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   625
     * may be ignored.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   626
     * @return a node builder
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   627
     */
19220
d3d40ccb544e 8022476: cleanup some raw types and unchecked warnings in java.util.stream
mduigou
parents: 18789
diff changeset
   628
    @Override
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   629
    abstract Node.Builder<E_OUT> makeNodeBuilder(long exactSizeIfKnown,
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
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   633
    // Op-specific abstract methods, implemented by the operation class
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   634
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   635
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   636
     * Returns whether this operation is stateful or not.  If it is stateful,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   637
     * then the method
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   638
     * {@link #opEvaluateParallel(PipelineHelper, java.util.Spliterator, java.util.function.IntFunction)}
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   639
     * must be overridden.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   640
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   641
     * @return {@code true} if this operation is stateful
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   642
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   643
    abstract boolean opIsStateful();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   644
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   645
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   646
     * Accepts a {@code Sink} which will receive the results of this operation,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   647
     * and return a {@code Sink} which accepts elements of the input type of
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   648
     * this operation and which performs the operation, passing the results to
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   649
     * the provided {@code Sink}.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   650
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   651
     * @apiNote
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   652
     * The implementation may use the {@code flags} parameter to optimize the
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   653
     * sink wrapping.  For example, if the input is already {@code DISTINCT},
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   654
     * the implementation for the {@code Stream#distinct()} method could just
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   655
     * return the sink it was passed.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   656
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   657
     * @param flags The combined stream and operation flags up to, but not
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   658
     *        including, this operation
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   659
     * @param sink sink to which elements should be sent after processing
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   660
     * @return a sink which accepts elements, perform the operation upon
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   661
     *         each element, and passes the results (if any) to the provided
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   662
     *         {@code Sink}.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   663
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   664
    abstract Sink<E_IN> opWrapSink(int flags, Sink<E_OUT> sink);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   665
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   666
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   667
     * Performs a parallel evaluation of the operation using the specified
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   668
     * {@code PipelineHelper} which describes the upstream intermediate
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   669
     * operations.  Only called on stateful operations.  If {@link
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   670
     * #opIsStateful()} returns true then implementations must override the
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   671
     * default implementation.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   672
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   673
     * @implSpec The default implementation always throw
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   674
     * {@code UnsupportedOperationException}.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   675
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   676
     * @param helper the pipeline helper describing the pipeline stages
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   677
     * @param spliterator the source {@code Spliterator}
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   678
     * @param generator the array generator
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   679
     * @return a {@code Node} describing the result of the evaluation
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   680
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   681
    <P_IN> Node<E_OUT> opEvaluateParallel(PipelineHelper<E_OUT> helper,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   682
                                          Spliterator<P_IN> spliterator,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   683
                                          IntFunction<E_OUT[]> generator) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   684
        throw new UnsupportedOperationException("Parallel evaluation is not supported");
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   685
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   686
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   687
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   688
     * Returns a {@code Spliterator} describing a parallel evaluation of the
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   689
     * operation, using the specified {@code PipelineHelper} which describes the
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   690
     * upstream intermediate operations.  Only called on stateful operations.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   691
     * It is not necessary (though acceptable) to do a full computation of the
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   692
     * result here; it is preferable, if possible, to describe the result via a
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   693
     * lazily evaluated spliterator.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   694
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   695
     * @implSpec The default implementation behaves as if:
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   696
     * <pre>{@code
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   697
     *     return evaluateParallel(helper, i -> (E_OUT[]) new
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   698
     * Object[i]).spliterator();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   699
     * }</pre>
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   700
     * and is suitable for implementations that cannot do better than a full
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   701
     * synchronous evaluation.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   702
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   703
     * @param helper the pipeline helper
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   704
     * @param spliterator the source {@code Spliterator}
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   705
     * @return a {@code Spliterator} describing the result of the evaluation
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   706
     */
19220
d3d40ccb544e 8022476: cleanup some raw types and unchecked warnings in java.util.stream
mduigou
parents: 18789
diff changeset
   707
    @SuppressWarnings("unchecked")
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   708
    <P_IN> Spliterator<E_OUT> opEvaluateParallelLazy(PipelineHelper<E_OUT> helper,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   709
                                                     Spliterator<P_IN> spliterator) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   710
        return opEvaluateParallel(helper, spliterator, i -> (E_OUT[]) new Object[i]).spliterator();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   711
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   712
}