jdk/src/share/classes/java/util/stream/AbstractPipeline.java
author mduigou
Wed, 24 Apr 2013 16:15:47 -0700
changeset 17182 b786c0de868c
child 18572 53b8b8c30086
permissions -rw-r--r--
8011920: Main streams implementation 8012542: Stream methods on Collection Reviewed-by: dholmes, mduigou Contributed-by: Brian Goetz <brian.goetz@oracle.com>, Mike Duigou <mike.duigou@oracle.com>, Paul Sandoz <paul.sandoz@oracle.com>
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
 * <p>{@code AbstractPipeline} implements a number of methods that are
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    57
 * specified in {@link BaseStream}, though it does not implement
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    58
 * {@code BaseStream} directly.  Subclasses of {@code AbstractPipeline}
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    59
 * will generally implement {@code BaseStream}.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    60
 *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    61
 * @implNote
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    62
 * <p>For sequential streams, and parallel streams without
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    63
 * <a href="package-summary.html#StreamOps">stateful intermediate
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    64
 * operations</a>, parallel streams, pipeline evaluation is done in a single
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    65
 * pass that "jams" all the operations together.  For parallel streams with
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    66
 * stateful operations, execution is divided into segments, where each
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    67
 * stateful operations marks the end of a segment, and each segment is
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    68
 * evaluated separately and the result used as the input to the next
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    69
 * segment.  In all cases, the source data is not consumed until a terminal
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    70
 * operation begins.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    71
 *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    72
 * @param <E_IN>  type of input elements
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    73
 * @param <E_OUT> type of output elements
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    74
 * @param <S> type of the subclass implementing {@code BaseStream}
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    75
 * @since 1.8
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    76
 */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    77
abstract class AbstractPipeline<E_IN, E_OUT, S extends BaseStream<E_OUT, S>>
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    78
        extends PipelineHelper<E_OUT> {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    79
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    80
     * Backlink to the head of the pipeline chain (self if this is the source
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    81
     * stage).
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    82
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    83
    private final AbstractPipeline sourceStage;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    84
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    85
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    86
     * The "upstream" pipeline, or null if this is the source stage.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    87
     */
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
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   100
    private AbstractPipeline nextStage;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   101
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   102
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   103
     * The number of intermediate operations between this pipeline object
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   104
     * and the stream source if sequential, or the previous stateful if parallel.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   105
     * Valid at the point of pipeline preparation for evaluation.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   106
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   107
    private int depth;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   108
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   109
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   110
     * The combined source and operation flags for the source and all operations
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   111
     * up to and including the operation represented by this pipeline object.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   112
     * Valid at the point of pipeline preparation for evaluation.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   113
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   114
    private int combinedFlags;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   115
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   116
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   117
     * The source spliterator. Only valid for the head pipeline.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   118
     * Before the pipeline is consumed if non-null then {@code sourceSupplier}
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   119
     * must be null. After the pipeline is consumed if non-null then is set to
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   120
     * null.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   121
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   122
    private Spliterator<?> sourceSpliterator;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   123
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   124
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   125
     * The source supplier. Only valid for the head pipeline. Before the
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   126
     * pipeline is consumed if non-null then {@code sourceSpliterator} must be
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   127
     * null. After the pipeline is consumed if non-null then is set to null.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   128
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   129
    private Supplier<? extends Spliterator<?>> sourceSupplier;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   130
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   131
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   132
     * True if this pipeline has been linked or consumed
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   133
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   134
    private boolean linkedOrConsumed;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   135
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   136
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   137
     * True if there are any stateful ops in the pipeline; only valid for the
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   138
     * source stage.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   139
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   140
    private boolean sourceAnyStateful;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   141
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   142
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   143
     * True if pipeline is parallel, otherwise the pipeline is sequential; only
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   144
     * valid for the source stage.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   145
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   146
    private boolean parallel;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   147
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   148
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   149
     * Constructor for the head of a stream pipeline.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   150
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   151
     * @param source {@code Supplier<Spliterator>} describing the stream source
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   152
     * @param sourceFlags The source flags for the stream source, described in
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   153
     * {@link StreamOpFlag}
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   154
     * @param parallel True if the pipeline is parallel
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   155
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   156
    AbstractPipeline(Supplier<? extends Spliterator<?>> source,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   157
                     int sourceFlags, boolean parallel) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   158
        this.previousStage = null;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   159
        this.sourceSupplier = source;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   160
        this.sourceStage = this;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   161
        this.sourceOrOpFlags = sourceFlags & StreamOpFlag.STREAM_MASK;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   162
        // The following is an optimization of:
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   163
        // StreamOpFlag.combineOpFlags(sourceOrOpFlags, StreamOpFlag.INITIAL_OPS_VALUE);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   164
        this.combinedFlags = (~(sourceOrOpFlags << 1)) & StreamOpFlag.INITIAL_OPS_VALUE;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   165
        this.depth = 0;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   166
        this.parallel = parallel;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   167
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   168
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   169
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   170
     * Constructor for the head of a stream pipeline.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   171
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   172
     * @param source {@code Spliterator} describing the stream source
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   173
     * @param sourceFlags the source flags for the stream source, described in
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   174
     * {@link StreamOpFlag}
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   175
     * @param parallel {@code true} if the pipeline is parallel
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   176
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   177
    AbstractPipeline(Spliterator<?> source,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   178
                     int sourceFlags, boolean parallel) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   179
        this.previousStage = null;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   180
        this.sourceSpliterator = source;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   181
        this.sourceStage = this;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   182
        this.sourceOrOpFlags = sourceFlags & StreamOpFlag.STREAM_MASK;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   183
        // The following is an optimization of:
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   184
        // StreamOpFlag.combineOpFlags(sourceOrOpFlags, StreamOpFlag.INITIAL_OPS_VALUE);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   185
        this.combinedFlags = (~(sourceOrOpFlags << 1)) & StreamOpFlag.INITIAL_OPS_VALUE;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   186
        this.depth = 0;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   187
        this.parallel = parallel;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   188
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   189
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   190
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   191
     * Constructor for appending an intermediate operation stage onto an
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   192
     * existing pipeline.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   193
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   194
     * @param previousStage the upstream pipeline stage
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   195
     * @param opFlags the operation flags for the new stage, described in
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   196
     * {@link StreamOpFlag}
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   197
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   198
    AbstractPipeline(AbstractPipeline<?, E_IN, ?> previousStage, int opFlags) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   199
        if (previousStage.linkedOrConsumed)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   200
            throw new IllegalStateException("stream has already been operated upon");
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   201
        previousStage.linkedOrConsumed = true;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   202
        previousStage.nextStage = this;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   203
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   204
        this.previousStage = previousStage;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   205
        this.sourceOrOpFlags = opFlags & StreamOpFlag.OP_MASK;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   206
        this.combinedFlags = StreamOpFlag.combineOpFlags(opFlags, previousStage.combinedFlags);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   207
        this.sourceStage = previousStage.sourceStage;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   208
        if (opIsStateful())
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   209
            sourceStage.sourceAnyStateful = true;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   210
        this.depth = previousStage.depth + 1;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   211
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   212
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   213
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   214
    // Terminal evaluation methods
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
     * Evaluate the pipeline with a terminal operation to produce a result.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   218
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   219
     * @param <R> the type of result
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   220
     * @param terminalOp the terminal operation to be applied to the pipeline.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   221
     * @return the result
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   222
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   223
    final <R> R evaluate(TerminalOp<E_OUT, R> terminalOp) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   224
        assert getOutputShape() == terminalOp.inputShape();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   225
        if (linkedOrConsumed)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   226
            throw new IllegalStateException("stream has already been operated upon");
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   227
        linkedOrConsumed = true;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   228
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   229
        return isParallel()
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   230
               ? (R) terminalOp.evaluateParallel(this, sourceSpliterator(terminalOp.getOpFlags()))
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   231
               : (R) terminalOp.evaluateSequential(this, sourceSpliterator(terminalOp.getOpFlags()));
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   232
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   233
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   234
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   235
     * Collect the elements output from the pipeline stage.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   236
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   237
     * @param generator the array generator to be used to create array instances
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   238
     * @return a flat array-backed Node that holds the collected output elements
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   239
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   240
    final Node<E_OUT> evaluateToArrayNode(IntFunction<E_OUT[]> generator) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   241
        if (linkedOrConsumed)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   242
            throw new IllegalStateException("stream has already been operated upon");
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   243
        linkedOrConsumed = true;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   244
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   245
        // If the last intermediate operation is stateful then
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   246
        // evaluate directly to avoid an extra collection step
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   247
        if (isParallel() && previousStage != null && opIsStateful()) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   248
            return opEvaluateParallel(previousStage, previousStage.sourceSpliterator(0), generator);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   249
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   250
        else {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   251
            return evaluate(sourceSpliterator(0), true, generator);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   252
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   253
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   254
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   255
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   256
     * Gets the source stage spliterator if this pipeline stage is the source
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   257
     * stage.  The pipeline is consumed after this method is called and
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   258
     * returns successfully.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   259
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   260
     * @return the source stage spliterator
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   261
     * @throws IllegalStateException if this pipeline stage is not the source
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   262
     *         stage.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   263
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   264
    final Spliterator<E_OUT> sourceStageSpliterator() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   265
        if (this != sourceStage)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   266
            throw new IllegalStateException();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   267
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   268
        if (linkedOrConsumed)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   269
            throw new IllegalStateException("stream has already been operated upon");
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   270
        linkedOrConsumed = true;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   271
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   272
        if (sourceStage.sourceSpliterator != null) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   273
            Spliterator<E_OUT> s = sourceStage.sourceSpliterator;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   274
            sourceStage.sourceSpliterator = null;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   275
            return s;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   276
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   277
        else if (sourceStage.sourceSupplier != null) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   278
            Spliterator<E_OUT> s = (Spliterator<E_OUT>) sourceStage.sourceSupplier.get();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   279
            sourceStage.sourceSupplier = null;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   280
            return s;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   281
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   282
        else {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   283
            throw new IllegalStateException("source already consumed");
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   284
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   285
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   286
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   287
    // BaseStream
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   288
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   289
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   290
     * Implements {@link BaseStream#sequential()}
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   291
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   292
    public final S sequential() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   293
        sourceStage.parallel = false;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   294
        return (S) this;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   295
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   296
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   297
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   298
     * Implements {@link BaseStream#parallel()}
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   299
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   300
    public final S parallel() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   301
        sourceStage.parallel = true;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   302
        return (S) this;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   303
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   304
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   305
    // Primitive specialization use co-variant overrides, hence is not final
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   306
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   307
     * Implements {@link BaseStream#spliterator()}
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   308
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   309
    public Spliterator<E_OUT> spliterator() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   310
        if (linkedOrConsumed)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   311
            throw new IllegalStateException("stream has already been operated upon");
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   312
        linkedOrConsumed = true;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   313
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   314
        if (this == sourceStage) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   315
            if (sourceStage.sourceSpliterator != null) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   316
                Spliterator<E_OUT> s = sourceStage.sourceSpliterator;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   317
                sourceStage.sourceSpliterator = null;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   318
                return s;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   319
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   320
            else if (sourceStage.sourceSupplier != null) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   321
                Supplier<Spliterator<E_OUT>> s = sourceStage.sourceSupplier;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   322
                sourceStage.sourceSupplier = null;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   323
                return lazySpliterator(s);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   324
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   325
            else {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   326
                throw new IllegalStateException("source already consumed");
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   327
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   328
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   329
        else {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   330
            return wrap(this, () -> sourceSpliterator(0), isParallel());
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   331
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   332
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   333
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   334
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   335
     * Implements {@link BaseStream#isParallel()}
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   336
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   337
    public final boolean isParallel() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   338
        return sourceStage.parallel;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   339
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   340
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   341
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   342
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   343
     * Returns the composition of stream flags of the stream source and all
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   344
     * intermediate operations.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   345
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   346
     * @return the composition of stream flags of the stream source and all
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   347
     *         intermediate operations
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   348
     * @see StreamOpFlag
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   349
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   350
    final int getStreamFlags() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   351
        return StreamOpFlag.toStreamFlags(combinedFlags);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   352
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   353
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   354
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   355
     * Prepare the pipeline for a parallel execution.  As the pipeline is built,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   356
     * the flags and depth indicators are set up for a sequential execution.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   357
     * If the execution is parallel, and there are any stateful operations, then
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   358
     * some of these need to be adjusted, as well as adjusting for flags from
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   359
     * the terminal operation (such as back-propagating UNORDERED).
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   360
     * Need not be called for a sequential execution.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   361
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   362
     * @param terminalFlags Operation flags for the terminal operation
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   363
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   364
    private void parallelPrepare(int terminalFlags) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   365
        AbstractPipeline backPropagationHead = sourceStage;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   366
        if (sourceStage.sourceAnyStateful) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   367
            int depth = 1;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   368
            for (AbstractPipeline u = sourceStage, p = sourceStage.nextStage;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   369
                 p != null;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   370
                 u = p, p = p.nextStage) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   371
                int thisOpFlags = p.sourceOrOpFlags;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   372
                if (p.opIsStateful()) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   373
                    // If the stateful operation is a short-circuit operation
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   374
                    // then move the back propagation head forwards
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   375
                    // NOTE: there are no size-injecting ops
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   376
                    if (StreamOpFlag.SHORT_CIRCUIT.isKnown(thisOpFlags)) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   377
                        backPropagationHead = p;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   378
                    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   379
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   380
                    depth = 0;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   381
                    // The following injects size, it is equivalent to:
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   382
                    // StreamOpFlag.combineOpFlags(StreamOpFlag.IS_SIZED, p.combinedFlags);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   383
                    thisOpFlags = (thisOpFlags & ~StreamOpFlag.NOT_SIZED) | StreamOpFlag.IS_SIZED;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   384
                }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   385
                p.depth = depth++;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   386
                p.combinedFlags = StreamOpFlag.combineOpFlags(thisOpFlags, u.combinedFlags);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   387
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   388
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   389
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   390
        // Apply the upstream terminal flags
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   391
        if (terminalFlags != 0) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   392
            int upstreamTerminalFlags = terminalFlags & StreamOpFlag.UPSTREAM_TERMINAL_OP_MASK;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   393
            for (AbstractPipeline p = backPropagationHead; p.nextStage != null; p = p.nextStage) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   394
                p.combinedFlags = StreamOpFlag.combineOpFlags(upstreamTerminalFlags, p.combinedFlags);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   395
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   396
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   397
            combinedFlags = StreamOpFlag.combineOpFlags(terminalFlags, combinedFlags);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   398
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   399
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   400
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   401
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   402
     * Get the source spliterator for this pipeline stage.  For a sequential or
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   403
     * stateless parallel pipeline, this is the source spliterator.  For a
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   404
     * stateful parallel pipeline, this is a spliterator describing the results
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   405
     * of all computations up to and including the most recent stateful
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   406
     * operation.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   407
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   408
    private Spliterator<?> sourceSpliterator(int terminalFlags) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   409
        // Get the source spliterator of the pipeline
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   410
        Spliterator<?> spliterator = null;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   411
        if (sourceStage.sourceSpliterator != null) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   412
            spliterator = sourceStage.sourceSpliterator;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   413
            sourceStage.sourceSpliterator = null;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   414
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   415
        else if (sourceStage.sourceSupplier != null) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   416
            spliterator = (Spliterator<?>) sourceStage.sourceSupplier.get();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   417
            sourceStage.sourceSupplier = null;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   418
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   419
        else {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   420
            throw new IllegalStateException("source already consumed");
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
        if (isParallel()) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   424
            // @@@ Merge parallelPrepare with the loop below and use the
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   425
            //     spliterator characteristics to determine if SIZED
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   426
            //     should be injected
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   427
            parallelPrepare(terminalFlags);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   428
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   429
            // Adapt the source spliterator, evaluating each stateful op
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   430
            // in the pipeline up to and including this pipeline stage
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   431
            for (AbstractPipeline u = sourceStage, p = sourceStage.nextStage, e = this;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   432
                 u != e;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   433
                 u = p, p = p.nextStage) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   434
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   435
                if (p.opIsStateful()) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   436
                    spliterator = p.opEvaluateParallelLazy(u, spliterator);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   437
                }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   438
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   439
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   440
        else if (terminalFlags != 0)  {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   441
            combinedFlags = StreamOpFlag.combineOpFlags(terminalFlags, combinedFlags);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   442
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   443
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   444
        return spliterator;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   445
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   446
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   447
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   448
    // PipelineHelper
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   449
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   450
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   451
    final <P_IN> long exactOutputSizeIfKnown(Spliterator<P_IN> spliterator) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   452
        return StreamOpFlag.SIZED.isKnown(getStreamAndOpFlags()) ? spliterator.getExactSizeIfKnown() : -1;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   453
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   454
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   455
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   456
    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
   457
        copyInto(wrapSink(Objects.requireNonNull(sink)), spliterator);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   458
        return sink;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   459
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   460
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   461
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   462
    final <P_IN> void copyInto(Sink<P_IN> wrappedSink, Spliterator<P_IN> spliterator) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   463
        Objects.requireNonNull(wrappedSink);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   464
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   465
        if (!StreamOpFlag.SHORT_CIRCUIT.isKnown(getStreamAndOpFlags())) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   466
            wrappedSink.begin(spliterator.getExactSizeIfKnown());
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   467
            spliterator.forEachRemaining(wrappedSink);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   468
            wrappedSink.end();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   469
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   470
        else {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   471
            copyIntoWithCancel(wrappedSink, 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
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   476
    final <P_IN> void copyIntoWithCancel(Sink<P_IN> wrappedSink, Spliterator<P_IN> spliterator) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   477
        AbstractPipeline p = AbstractPipeline.this;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   478
        while (p.depth > 0) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   479
            p = p.previousStage;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   480
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   481
        wrappedSink.begin(spliterator.getExactSizeIfKnown());
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   482
        p.forEachWithCancel(spliterator, wrappedSink);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   483
        wrappedSink.end();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   484
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   485
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   486
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   487
    final int getStreamAndOpFlags() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   488
        return combinedFlags;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   489
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   490
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   491
    final boolean isOrdered() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   492
        return StreamOpFlag.ORDERED.isKnown(combinedFlags);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   493
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   494
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   495
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   496
    final <P_IN> Sink<P_IN> wrapSink(Sink<E_OUT> sink) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   497
        Objects.requireNonNull(sink);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   498
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   499
        for (AbstractPipeline p=AbstractPipeline.this; p.depth > 0; p=p.previousStage) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   500
            sink = p.opWrapSink(p.previousStage.combinedFlags, sink);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   501
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   502
        return (Sink<P_IN>) sink;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   503
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   504
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   505
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   506
    @SuppressWarnings("unchecked")
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   507
    final <P_IN> Node<E_OUT> evaluate(Spliterator<P_IN> spliterator,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   508
                                      boolean flatten,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   509
                                      IntFunction<E_OUT[]> generator) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   510
        if (isParallel()) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   511
            // @@@ Optimize if op of this pipeline stage is a stateful op
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   512
            return evaluateToNode(this, spliterator, flatten, generator);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   513
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   514
        else {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   515
            Node.Builder<E_OUT> nb = makeNodeBuilder(
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   516
                    exactOutputSizeIfKnown(spliterator), generator);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   517
            return wrapAndCopyInto(nb, spliterator).build();
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
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   521
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   522
    // Shape-specific abstract methods, implemented by XxxPipeline classes
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   523
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   524
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   525
     * Get the output shape of the pipeline.  If the pipeline is the head,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   526
     * then it's output shape corresponds to the shape of the source.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   527
     * Otherwise, it's output shape corresponds to the output shape of the
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   528
     * associated operation.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   529
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   530
     * @return the output shape
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   531
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   532
    abstract StreamShape getOutputShape();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   533
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   534
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   535
     * Collect elements output from a pipeline into a Node that holds elements
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   536
     * of this shape.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   537
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   538
     * @param helper the pipeline helper describing the pipeline stages
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   539
     * @param spliterator the source spliterator
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   540
     * @param flattenTree true if the returned node should be flattened
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   541
     * @param generator the array generator
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   542
     * @return a Node holding the output of the pipeline
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   543
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   544
    abstract <P_IN> Node<E_OUT> evaluateToNode(PipelineHelper<E_OUT> helper,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   545
                                               Spliterator<P_IN> spliterator,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   546
                                               boolean flattenTree,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   547
                                               IntFunction<E_OUT[]> generator);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   548
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   549
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   550
     * Create a spliterator that wraps a source spliterator, compatible with
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   551
     * this stream shape, and operations associated with a {@link
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   552
     * PipelineHelper}.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   553
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   554
     * @param ph the pipeline helper describing the pipeline stages
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   555
     * @param supplier the supplier of a spliterator
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   556
     * @return a wrapping spliterator compatible with this shape
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   557
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   558
    abstract <P_IN> Spliterator<E_OUT> wrap(PipelineHelper<E_OUT> ph,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   559
                                            Supplier<Spliterator<P_IN>> supplier,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   560
                                            boolean isParallel);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   561
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   562
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   563
     * Create a lazy spliterator that wraps and obtains the supplied the
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   564
     * spliterator when a method is invoked on the lazy spliterator.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   565
     * @param supplier the supplier of a spliterator
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   566
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   567
    abstract Spliterator<E_OUT> lazySpliterator(Supplier<? extends Spliterator<E_OUT>> supplier);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   568
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   569
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   570
     * Traverse the elements of a spliterator compatible with this stream shape,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   571
     * pushing those elements into a sink.   If the sink requests cancellation,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   572
     * no further elements will be pulled or pushed.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   573
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   574
     * @param spliterator the spliterator to pull elements from
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   575
     * @param sink the sink to push elements to
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   576
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   577
    abstract void forEachWithCancel(Spliterator<E_OUT> spliterator, Sink<E_OUT> sink);
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
     * Make a node builder compatible with this stream shape.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   581
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   582
     * @param exactSizeIfKnown if {@literal >=0}, then a node builder will be created that
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   583
     * has a fixed capacity of at most sizeIfKnown elements. If {@literal < 0},
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   584
     * then the node builder has an unfixed capacity. A fixed capacity node
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   585
     * builder will throw exceptions if an element is added after builder has
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   586
     * reached capacity, or is built before the builder has reached capacity.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   587
     * @param generator the array generator to be used to create instances of a
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   588
     * T[] array. For implementations supporting primitive nodes, this parameter
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   589
     * may be ignored.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   590
     * @return a node builder
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   591
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   592
    abstract Node.Builder<E_OUT> makeNodeBuilder(long exactSizeIfKnown,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   593
                                                 IntFunction<E_OUT[]> generator);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   594
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   595
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   596
    // Op-specific abstract methods, implemented by the operation class
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   597
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   598
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   599
     * Returns whether this operation is stateful or not.  If it is stateful,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   600
     * then the method
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   601
     * {@link #opEvaluateParallel(PipelineHelper, java.util.Spliterator, java.util.function.IntFunction)}
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   602
     * must be overridden.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   603
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   604
     * @return {@code true} if this operation is stateful
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   605
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   606
    abstract boolean opIsStateful();
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
     * Accepts a {@code Sink} which will receive the results of this operation,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   610
     * and return a {@code Sink} which accepts elements of the input type of
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   611
     * this operation and which performs the operation, passing the results to
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   612
     * the provided {@code Sink}.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   613
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   614
     * @apiNote
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   615
     * The implementation may use the {@code flags} parameter to optimize the
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   616
     * sink wrapping.  For example, if the input is already {@code DISTINCT},
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   617
     * the implementation for the {@code Stream#distinct()} method could just
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   618
     * return the sink it was passed.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   619
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   620
     * @param flags The combined stream and operation flags up to, but not
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   621
     *        including, this operation
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   622
     * @param sink sink to which elements should be sent after processing
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   623
     * @return a sink which accepts elements, perform the operation upon
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   624
     *         each element, and passes the results (if any) to the provided
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   625
     *         {@code Sink}.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   626
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   627
    abstract Sink<E_IN> opWrapSink(int flags, Sink<E_OUT> sink);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   628
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   629
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   630
     * Performs a parallel evaluation of the operation using the specified
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   631
     * {@code PipelineHelper} which describes the upstream intermediate
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   632
     * operations.  Only called on stateful operations.  If {@link
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   633
     * #opIsStateful()} returns true then implementations must override the
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   634
     * default implementation.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   635
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   636
     * @implSpec The default implementation always throw
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   637
     * {@code UnsupportedOperationException}.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   638
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   639
     * @param helper the pipeline helper describing the pipeline stages
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   640
     * @param spliterator the source {@code Spliterator}
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   641
     * @param generator the array generator
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   642
     * @return a {@code Node} describing the result of the evaluation
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   643
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   644
    <P_IN> Node<E_OUT> opEvaluateParallel(PipelineHelper<E_OUT> helper,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   645
                                          Spliterator<P_IN> spliterator,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   646
                                          IntFunction<E_OUT[]> generator) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   647
        throw new UnsupportedOperationException("Parallel evaluation is not supported");
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   648
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   649
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   650
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   651
     * Returns a {@code Spliterator} describing a parallel evaluation of the
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   652
     * operation, using the specified {@code PipelineHelper} which describes the
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   653
     * upstream intermediate operations.  Only called on stateful operations.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   654
     * It is not necessary (though acceptable) to do a full computation of the
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   655
     * result here; it is preferable, if possible, to describe the result via a
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   656
     * lazily evaluated spliterator.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   657
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   658
     * @implSpec The default implementation behaves as if:
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   659
     * <pre>{@code
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   660
     *     return evaluateParallel(helper, i -> (E_OUT[]) new
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   661
     * Object[i]).spliterator();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   662
     * }</pre>
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   663
     * and is suitable for implementations that cannot do better than a full
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   664
     * synchronous evaluation.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   665
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   666
     * @param helper the pipeline helper
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   667
     * @param spliterator the source {@code Spliterator}
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   668
     * @return a {@code Spliterator} describing the result of the evaluation
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   669
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   670
    <P_IN> Spliterator<E_OUT> opEvaluateParallelLazy(PipelineHelper<E_OUT> helper,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   671
                                                     Spliterator<P_IN> spliterator) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   672
        return opEvaluateParallel(helper, spliterator, i -> (E_OUT[]) new Object[i]).spliterator();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   673
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   674
}