jdk/src/share/classes/java/util/stream/AbstractPipeline.java
author psandoz
Thu, 16 Jan 2014 18:20:31 +0100
changeset 22289 bb9c71b84919
parent 19800 6e1fef53ea55
child 25526 d3cbdae6e9f9
permissions -rw-r--r--
8029452: Fork/Join task ForEachOps.ForEachOrderedTask clarifications and minor improvements Reviewed-by: mduigou, briangoetz
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
     1
/*
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
     2
 * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
     4
 *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    10
 *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    15
 * accompanied this code).
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    16
 *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    20
 *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    23
 * questions.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    24
 */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    25
package java.util.stream;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    26
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    27
import java.util.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()) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   252
            return opEvaluateParallel(previousStage, previousStage.sourceSpliterator(0), generator);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   253
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   254
        else {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   255
            return evaluate(sourceSpliterator(0), true, generator);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   256
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   257
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   258
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   259
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   260
     * Gets the source stage spliterator if this pipeline stage is the source
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   261
     * stage.  The pipeline is consumed after this method is called and
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   262
     * returns successfully.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   263
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   264
     * @return the source stage spliterator
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   265
     * @throws IllegalStateException if this pipeline stage is not the source
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   266
     *         stage.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   267
     */
19220
d3d40ccb544e 8022476: cleanup some raw types and unchecked warnings in java.util.stream
mduigou
parents: 18789
diff changeset
   268
    @SuppressWarnings("unchecked")
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   269
    final Spliterator<E_OUT> sourceStageSpliterator() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   270
        if (this != sourceStage)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   271
            throw new IllegalStateException();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   272
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   273
        if (linkedOrConsumed)
19800
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19220
diff changeset
   274
            throw new IllegalStateException(MSG_STREAM_LINKED);
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   275
        linkedOrConsumed = true;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   276
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   277
        if (sourceStage.sourceSpliterator != null) {
19220
d3d40ccb544e 8022476: cleanup some raw types and unchecked warnings in java.util.stream
mduigou
parents: 18789
diff changeset
   278
            @SuppressWarnings("unchecked")
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   279
            Spliterator<E_OUT> s = sourceStage.sourceSpliterator;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   280
            sourceStage.sourceSpliterator = null;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   281
            return s;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   282
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   283
        else if (sourceStage.sourceSupplier != null) {
19220
d3d40ccb544e 8022476: cleanup some raw types and unchecked warnings in java.util.stream
mduigou
parents: 18789
diff changeset
   284
            @SuppressWarnings("unchecked")
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   285
            Spliterator<E_OUT> s = (Spliterator<E_OUT>) sourceStage.sourceSupplier.get();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   286
            sourceStage.sourceSupplier = null;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   287
            return s;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   288
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   289
        else {
19800
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19220
diff changeset
   290
            throw new IllegalStateException(MSG_CONSUMED);
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   291
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   292
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   293
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   294
    // BaseStream
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   295
18789
b518cd4045bc 8019551: Make BaseStream public
psandoz
parents: 18572
diff changeset
   296
    @Override
19220
d3d40ccb544e 8022476: cleanup some raw types and unchecked warnings in java.util.stream
mduigou
parents: 18789
diff changeset
   297
    @SuppressWarnings("unchecked")
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   298
    public final S sequential() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   299
        sourceStage.parallel = false;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   300
        return (S) this;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   301
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   302
18789
b518cd4045bc 8019551: Make BaseStream public
psandoz
parents: 18572
diff changeset
   303
    @Override
19220
d3d40ccb544e 8022476: cleanup some raw types and unchecked warnings in java.util.stream
mduigou
parents: 18789
diff changeset
   304
    @SuppressWarnings("unchecked")
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   305
    public final S parallel() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   306
        sourceStage.parallel = true;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   307
        return (S) this;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   308
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   309
19800
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19220
diff changeset
   310
    @Override
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19220
diff changeset
   311
    public void close() {
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19220
diff changeset
   312
        linkedOrConsumed = true;
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19220
diff changeset
   313
        sourceSupplier = null;
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19220
diff changeset
   314
        sourceSpliterator = null;
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19220
diff changeset
   315
        if (sourceStage.sourceCloseAction != null) {
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19220
diff changeset
   316
            Runnable closeAction = sourceStage.sourceCloseAction;
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19220
diff changeset
   317
            sourceStage.sourceCloseAction = null;
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19220
diff changeset
   318
            closeAction.run();
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19220
diff changeset
   319
        }
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19220
diff changeset
   320
    }
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19220
diff changeset
   321
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19220
diff changeset
   322
    @Override
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19220
diff changeset
   323
    @SuppressWarnings("unchecked")
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19220
diff changeset
   324
    public S onClose(Runnable closeHandler) {
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19220
diff changeset
   325
        Runnable existingHandler = sourceStage.sourceCloseAction;
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19220
diff changeset
   326
        sourceStage.sourceCloseAction =
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19220
diff changeset
   327
                (existingHandler == null)
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19220
diff changeset
   328
                ? closeHandler
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19220
diff changeset
   329
                : Streams.composeWithExceptions(existingHandler, closeHandler);
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19220
diff changeset
   330
        return (S) this;
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19220
diff changeset
   331
    }
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19220
diff changeset
   332
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   333
    // Primitive specialization use co-variant overrides, hence is not final
18789
b518cd4045bc 8019551: Make BaseStream public
psandoz
parents: 18572
diff changeset
   334
    @Override
19220
d3d40ccb544e 8022476: cleanup some raw types and unchecked warnings in java.util.stream
mduigou
parents: 18789
diff changeset
   335
    @SuppressWarnings("unchecked")
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   336
    public Spliterator<E_OUT> spliterator() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   337
        if (linkedOrConsumed)
19800
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19220
diff changeset
   338
            throw new IllegalStateException(MSG_STREAM_LINKED);
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   339
        linkedOrConsumed = true;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   340
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   341
        if (this == sourceStage) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   342
            if (sourceStage.sourceSpliterator != null) {
19220
d3d40ccb544e 8022476: cleanup some raw types and unchecked warnings in java.util.stream
mduigou
parents: 18789
diff changeset
   343
                @SuppressWarnings("unchecked")
d3d40ccb544e 8022476: cleanup some raw types and unchecked warnings in java.util.stream
mduigou
parents: 18789
diff changeset
   344
                Spliterator<E_OUT> s = (Spliterator<E_OUT>) sourceStage.sourceSpliterator;
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   345
                sourceStage.sourceSpliterator = null;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   346
                return s;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   347
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   348
            else if (sourceStage.sourceSupplier != null) {
19220
d3d40ccb544e 8022476: cleanup some raw types and unchecked warnings in java.util.stream
mduigou
parents: 18789
diff changeset
   349
                @SuppressWarnings("unchecked")
d3d40ccb544e 8022476: cleanup some raw types and unchecked warnings in java.util.stream
mduigou
parents: 18789
diff changeset
   350
                Supplier<Spliterator<E_OUT>> s = (Supplier<Spliterator<E_OUT>>) sourceStage.sourceSupplier;
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   351
                sourceStage.sourceSupplier = null;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   352
                return lazySpliterator(s);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   353
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   354
            else {
19800
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19220
diff changeset
   355
                throw new IllegalStateException(MSG_CONSUMED);
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   356
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   357
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   358
        else {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   359
            return wrap(this, () -> sourceSpliterator(0), isParallel());
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   360
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   361
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   362
18789
b518cd4045bc 8019551: Make BaseStream public
psandoz
parents: 18572
diff changeset
   363
    @Override
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   364
    public final boolean isParallel() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   365
        return sourceStage.parallel;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   366
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   367
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   368
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   369
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   370
     * Returns the composition of stream flags of the stream source and all
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   371
     * intermediate operations.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   372
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   373
     * @return the composition of stream flags of the stream source and all
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   374
     *         intermediate operations
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   375
     * @see StreamOpFlag
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   376
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   377
    final int getStreamFlags() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   378
        return StreamOpFlag.toStreamFlags(combinedFlags);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   379
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   380
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   381
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   382
     * Prepare the pipeline for a parallel execution.  As the pipeline is built,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   383
     * the flags and depth indicators are set up for a sequential execution.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   384
     * If the execution is parallel, and there are any stateful operations, then
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   385
     * some of these need to be adjusted, as well as adjusting for flags from
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   386
     * the terminal operation (such as back-propagating UNORDERED).
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   387
     * Need not be called for a sequential execution.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   388
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   389
     * @param terminalFlags Operation flags for the terminal operation
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   390
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   391
    private void parallelPrepare(int terminalFlags) {
19220
d3d40ccb544e 8022476: cleanup some raw types and unchecked warnings in java.util.stream
mduigou
parents: 18789
diff changeset
   392
        @SuppressWarnings("rawtypes")
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   393
        AbstractPipeline backPropagationHead = sourceStage;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   394
        if (sourceStage.sourceAnyStateful) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   395
            int depth = 1;
19220
d3d40ccb544e 8022476: cleanup some raw types and unchecked warnings in java.util.stream
mduigou
parents: 18789
diff changeset
   396
            for (  @SuppressWarnings("rawtypes") AbstractPipeline u = sourceStage, p = sourceStage.nextStage;
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   397
                 p != null;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   398
                 u = p, p = p.nextStage) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   399
                int thisOpFlags = p.sourceOrOpFlags;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   400
                if (p.opIsStateful()) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   401
                    // If the stateful operation is a short-circuit operation
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   402
                    // then move the back propagation head forwards
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   403
                    // NOTE: there are no size-injecting ops
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   404
                    if (StreamOpFlag.SHORT_CIRCUIT.isKnown(thisOpFlags)) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   405
                        backPropagationHead = p;
18572
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   406
                        // Clear the short circuit flag for next pipeline stage
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   407
                        // This stage encapsulates short-circuiting, the next
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   408
                        // stage may not have any short-circuit operations, and
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   409
                        // if so spliterator.forEachRemaining should be be used
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   410
                        // for traversal
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   411
                        thisOpFlags = thisOpFlags & ~StreamOpFlag.IS_SHORT_CIRCUIT;
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   412
                    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   413
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   414
                    depth = 0;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   415
                    // The following injects size, it is equivalent to:
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   416
                    // StreamOpFlag.combineOpFlags(StreamOpFlag.IS_SIZED, p.combinedFlags);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   417
                    thisOpFlags = (thisOpFlags & ~StreamOpFlag.NOT_SIZED) | StreamOpFlag.IS_SIZED;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   418
                }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   419
                p.depth = depth++;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   420
                p.combinedFlags = StreamOpFlag.combineOpFlags(thisOpFlags, u.combinedFlags);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   421
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   422
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   423
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   424
        // Apply the upstream terminal flags
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   425
        if (terminalFlags != 0) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   426
            int upstreamTerminalFlags = terminalFlags & StreamOpFlag.UPSTREAM_TERMINAL_OP_MASK;
19220
d3d40ccb544e 8022476: cleanup some raw types and unchecked warnings in java.util.stream
mduigou
parents: 18789
diff changeset
   427
            for ( @SuppressWarnings("rawtypes") AbstractPipeline p = backPropagationHead; p.nextStage != null; p = p.nextStage) {
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   428
                p.combinedFlags = StreamOpFlag.combineOpFlags(upstreamTerminalFlags, p.combinedFlags);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   429
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   430
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   431
            combinedFlags = StreamOpFlag.combineOpFlags(terminalFlags, combinedFlags);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   432
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   433
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   434
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   435
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   436
     * Get the source spliterator for this pipeline stage.  For a sequential or
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   437
     * stateless parallel pipeline, this is the source spliterator.  For a
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   438
     * stateful parallel pipeline, this is a spliterator describing the results
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   439
     * of all computations up to and including the most recent stateful
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   440
     * operation.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   441
     */
19220
d3d40ccb544e 8022476: cleanup some raw types and unchecked warnings in java.util.stream
mduigou
parents: 18789
diff changeset
   442
    @SuppressWarnings("unchecked")
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   443
    private Spliterator<?> sourceSpliterator(int terminalFlags) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   444
        // Get the source spliterator of the pipeline
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   445
        Spliterator<?> spliterator = null;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   446
        if (sourceStage.sourceSpliterator != null) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   447
            spliterator = sourceStage.sourceSpliterator;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   448
            sourceStage.sourceSpliterator = null;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   449
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   450
        else if (sourceStage.sourceSupplier != null) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   451
            spliterator = (Spliterator<?>) sourceStage.sourceSupplier.get();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   452
            sourceStage.sourceSupplier = null;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   453
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   454
        else {
19800
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19220
diff changeset
   455
            throw new IllegalStateException(MSG_CONSUMED);
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   456
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   457
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   458
        if (isParallel()) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   459
            // @@@ Merge parallelPrepare with the loop below and use the
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   460
            //     spliterator characteristics to determine if SIZED
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   461
            //     should be injected
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   462
            parallelPrepare(terminalFlags);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   463
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   464
            // Adapt the source spliterator, evaluating each stateful op
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   465
            // in the pipeline up to and including this pipeline stage
19220
d3d40ccb544e 8022476: cleanup some raw types and unchecked warnings in java.util.stream
mduigou
parents: 18789
diff changeset
   466
            for ( @SuppressWarnings("rawtypes") AbstractPipeline u = sourceStage, p = sourceStage.nextStage, e = this;
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   467
                 u != e;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   468
                 u = p, p = p.nextStage) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   469
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   470
                if (p.opIsStateful()) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   471
                    spliterator = p.opEvaluateParallelLazy(u, spliterator);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   472
                }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   473
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   474
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   475
        else if (terminalFlags != 0)  {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   476
            combinedFlags = StreamOpFlag.combineOpFlags(terminalFlags, combinedFlags);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   477
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   478
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   479
        return spliterator;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   480
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   481
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   482
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   483
    // PipelineHelper
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   484
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   485
    @Override
18572
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   486
    final StreamShape getSourceShape() {
19220
d3d40ccb544e 8022476: cleanup some raw types and unchecked warnings in java.util.stream
mduigou
parents: 18789
diff changeset
   487
        @SuppressWarnings("rawtypes")
18572
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   488
        AbstractPipeline p = AbstractPipeline.this;
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   489
        while (p.depth > 0) {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   490
            p = p.previousStage;
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   491
        }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   492
        return p.getOutputShape();
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   493
    }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   494
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   495
    @Override
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   496
    final <P_IN> long exactOutputSizeIfKnown(Spliterator<P_IN> spliterator) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   497
        return StreamOpFlag.SIZED.isKnown(getStreamAndOpFlags()) ? spliterator.getExactSizeIfKnown() : -1;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   498
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   499
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   500
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   501
    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
   502
        copyInto(wrapSink(Objects.requireNonNull(sink)), spliterator);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   503
        return sink;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   504
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   505
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   506
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   507
    final <P_IN> void copyInto(Sink<P_IN> wrappedSink, Spliterator<P_IN> spliterator) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   508
        Objects.requireNonNull(wrappedSink);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   509
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   510
        if (!StreamOpFlag.SHORT_CIRCUIT.isKnown(getStreamAndOpFlags())) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   511
            wrappedSink.begin(spliterator.getExactSizeIfKnown());
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   512
            spliterator.forEachRemaining(wrappedSink);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   513
            wrappedSink.end();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   514
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   515
        else {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   516
            copyIntoWithCancel(wrappedSink, spliterator);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   517
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   518
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   519
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   520
    @Override
19220
d3d40ccb544e 8022476: cleanup some raw types and unchecked warnings in java.util.stream
mduigou
parents: 18789
diff changeset
   521
    @SuppressWarnings("unchecked")
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   522
    final <P_IN> void 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
   523
        @SuppressWarnings({"rawtypes","unchecked"})
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   524
        AbstractPipeline p = AbstractPipeline.this;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   525
        while (p.depth > 0) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   526
            p = p.previousStage;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   527
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   528
        wrappedSink.begin(spliterator.getExactSizeIfKnown());
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   529
        p.forEachWithCancel(spliterator, wrappedSink);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   530
        wrappedSink.end();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   531
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   532
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   533
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   534
    final int getStreamAndOpFlags() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   535
        return combinedFlags;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   536
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   537
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   538
    final boolean isOrdered() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   539
        return StreamOpFlag.ORDERED.isKnown(combinedFlags);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   540
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   541
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   542
    @Override
19220
d3d40ccb544e 8022476: cleanup some raw types and unchecked warnings in java.util.stream
mduigou
parents: 18789
diff changeset
   543
    @SuppressWarnings("unchecked")
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   544
    final <P_IN> Sink<P_IN> wrapSink(Sink<E_OUT> sink) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   545
        Objects.requireNonNull(sink);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   546
19220
d3d40ccb544e 8022476: cleanup some raw types and unchecked warnings in java.util.stream
mduigou
parents: 18789
diff changeset
   547
        for ( @SuppressWarnings("rawtypes") AbstractPipeline p=AbstractPipeline.this; p.depth > 0; p=p.previousStage) {
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   548
            sink = p.opWrapSink(p.previousStage.combinedFlags, sink);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   549
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   550
        return (Sink<P_IN>) sink;
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
    @Override
19220
d3d40ccb544e 8022476: cleanup some raw types and unchecked warnings in java.util.stream
mduigou
parents: 18789
diff changeset
   554
    @SuppressWarnings("unchecked")
18572
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   555
    final <P_IN> Spliterator<E_OUT> wrapSpliterator(Spliterator<P_IN> sourceSpliterator) {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   556
        if (depth == 0) {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   557
            return (Spliterator<E_OUT>) sourceSpliterator;
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   558
        }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   559
        else {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   560
            return wrap(this, () -> sourceSpliterator, isParallel());
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   561
        }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   562
    }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   563
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   564
    @Override
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   565
    @SuppressWarnings("unchecked")
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   566
    final <P_IN> Node<E_OUT> evaluate(Spliterator<P_IN> spliterator,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   567
                                      boolean flatten,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   568
                                      IntFunction<E_OUT[]> generator) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   569
        if (isParallel()) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   570
            // @@@ Optimize if op of this pipeline stage is a stateful op
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   571
            return evaluateToNode(this, spliterator, flatten, generator);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   572
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   573
        else {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   574
            Node.Builder<E_OUT> nb = makeNodeBuilder(
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   575
                    exactOutputSizeIfKnown(spliterator), generator);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   576
            return wrapAndCopyInto(nb, spliterator).build();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   577
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   578
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   579
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   580
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   581
    // Shape-specific abstract methods, implemented by XxxPipeline classes
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   582
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   583
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   584
     * Get the output shape of the pipeline.  If the pipeline is the head,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   585
     * then it's output shape corresponds to the shape of the source.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   586
     * Otherwise, it's output shape corresponds to the output shape of the
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   587
     * associated operation.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   588
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   589
     * @return the output shape
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   590
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   591
    abstract StreamShape getOutputShape();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   592
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   593
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   594
     * Collect elements output from a pipeline into a Node that holds elements
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   595
     * of this shape.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   596
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   597
     * @param helper the pipeline helper describing the pipeline stages
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   598
     * @param spliterator the source spliterator
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   599
     * @param flattenTree true if the returned node should be flattened
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   600
     * @param generator the array generator
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   601
     * @return a Node holding the output of the pipeline
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   602
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   603
    abstract <P_IN> Node<E_OUT> evaluateToNode(PipelineHelper<E_OUT> helper,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   604
                                               Spliterator<P_IN> spliterator,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   605
                                               boolean flattenTree,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   606
                                               IntFunction<E_OUT[]> generator);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   607
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   608
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   609
     * Create a spliterator that wraps a source spliterator, compatible with
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   610
     * this stream shape, and operations associated with a {@link
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   611
     * PipelineHelper}.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   612
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   613
     * @param ph the pipeline helper describing the pipeline stages
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   614
     * @param supplier the supplier of a spliterator
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   615
     * @return a wrapping spliterator compatible with this shape
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   616
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   617
    abstract <P_IN> Spliterator<E_OUT> wrap(PipelineHelper<E_OUT> ph,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   618
                                            Supplier<Spliterator<P_IN>> supplier,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   619
                                            boolean isParallel);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   620
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   621
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   622
     * Create a lazy spliterator that wraps and obtains the supplied the
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   623
     * spliterator when a method is invoked on the lazy spliterator.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   624
     * @param supplier the supplier of a spliterator
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   625
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   626
    abstract Spliterator<E_OUT> lazySpliterator(Supplier<? extends Spliterator<E_OUT>> supplier);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   627
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   628
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   629
     * Traverse the elements of a spliterator compatible with this stream shape,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   630
     * pushing those elements into a sink.   If the sink requests cancellation,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   631
     * no further elements will be pulled or pushed.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   632
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   633
     * @param spliterator the spliterator to pull elements from
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   634
     * @param sink the sink to push elements to
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   635
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   636
    abstract void forEachWithCancel(Spliterator<E_OUT> spliterator, Sink<E_OUT> sink);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   637
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   638
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   639
     * Make a node builder compatible with this stream shape.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   640
     *
19220
d3d40ccb544e 8022476: cleanup some raw types and unchecked warnings in java.util.stream
mduigou
parents: 18789
diff changeset
   641
     * @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
   642
     * 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
   643
     * {@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
   644
     * 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
   645
     * 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
   646
     * capacity.
d3d40ccb544e 8022476: cleanup some raw types and unchecked warnings in java.util.stream
mduigou
parents: 18789
diff changeset
   647
     *
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   648
     * @param generator the array generator to be used to create instances of a
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   649
     * T[] array. For implementations supporting primitive nodes, this parameter
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   650
     * may be ignored.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   651
     * @return a node builder
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   652
     */
19220
d3d40ccb544e 8022476: cleanup some raw types and unchecked warnings in java.util.stream
mduigou
parents: 18789
diff changeset
   653
    @Override
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   654
    abstract Node.Builder<E_OUT> makeNodeBuilder(long exactSizeIfKnown,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   655
                                                 IntFunction<E_OUT[]> generator);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   656
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   657
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   658
    // Op-specific abstract methods, implemented by the operation class
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   659
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   660
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   661
     * Returns whether this operation is stateful or not.  If it is stateful,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   662
     * then the method
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   663
     * {@link #opEvaluateParallel(PipelineHelper, java.util.Spliterator, java.util.function.IntFunction)}
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   664
     * must be overridden.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   665
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   666
     * @return {@code true} if this operation is stateful
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   667
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   668
    abstract boolean opIsStateful();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   669
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   670
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   671
     * Accepts a {@code Sink} which will receive the results of this operation,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   672
     * and return a {@code Sink} which accepts elements of the input type of
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   673
     * this operation and which performs the operation, passing the results to
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   674
     * the provided {@code Sink}.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   675
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   676
     * @apiNote
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   677
     * The implementation may use the {@code flags} parameter to optimize the
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   678
     * sink wrapping.  For example, if the input is already {@code DISTINCT},
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   679
     * the implementation for the {@code Stream#distinct()} method could just
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   680
     * return the sink it was passed.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   681
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   682
     * @param flags The combined stream and operation flags up to, but not
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   683
     *        including, this operation
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   684
     * @param sink sink to which elements should be sent after processing
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   685
     * @return a sink which accepts elements, perform the operation upon
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   686
     *         each element, and passes the results (if any) to the provided
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   687
     *         {@code Sink}.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   688
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   689
    abstract Sink<E_IN> opWrapSink(int flags, Sink<E_OUT> sink);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   690
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   691
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   692
     * Performs a parallel evaluation of the operation using the specified
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   693
     * {@code PipelineHelper} which describes the upstream intermediate
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   694
     * operations.  Only called on stateful operations.  If {@link
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   695
     * #opIsStateful()} returns true then implementations must override the
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   696
     * default implementation.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   697
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   698
     * @implSpec The default implementation always throw
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   699
     * {@code UnsupportedOperationException}.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   700
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   701
     * @param helper the pipeline helper describing the pipeline stages
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   702
     * @param spliterator the source {@code Spliterator}
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   703
     * @param generator the array generator
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   704
     * @return a {@code Node} describing the result of the evaluation
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   705
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   706
    <P_IN> Node<E_OUT> opEvaluateParallel(PipelineHelper<E_OUT> helper,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   707
                                          Spliterator<P_IN> spliterator,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   708
                                          IntFunction<E_OUT[]> generator) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   709
        throw new UnsupportedOperationException("Parallel evaluation is not supported");
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   710
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   711
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   712
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   713
     * Returns a {@code Spliterator} describing a parallel evaluation of the
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   714
     * operation, using the specified {@code PipelineHelper} which describes the
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   715
     * upstream intermediate operations.  Only called on stateful operations.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   716
     * It is not necessary (though acceptable) to do a full computation of the
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   717
     * result here; it is preferable, if possible, to describe the result via a
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   718
     * lazily evaluated spliterator.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   719
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   720
     * @implSpec The default implementation behaves as if:
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   721
     * <pre>{@code
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   722
     *     return evaluateParallel(helper, i -> (E_OUT[]) new
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   723
     * Object[i]).spliterator();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   724
     * }</pre>
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   725
     * and is suitable for implementations that cannot do better than a full
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   726
     * synchronous evaluation.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   727
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   728
     * @param helper the pipeline helper
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   729
     * @param spliterator the source {@code Spliterator}
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   730
     * @return a {@code Spliterator} describing the result of the evaluation
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   731
     */
19220
d3d40ccb544e 8022476: cleanup some raw types and unchecked warnings in java.util.stream
mduigou
parents: 18789
diff changeset
   732
    @SuppressWarnings("unchecked")
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   733
    <P_IN> Spliterator<E_OUT> opEvaluateParallelLazy(PipelineHelper<E_OUT> helper,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   734
                                                     Spliterator<P_IN> spliterator) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   735
        return opEvaluateParallel(helper, spliterator, i -> (E_OUT[]) new Object[i]).spliterator();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   736
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   737
}