jdk/src/share/classes/java/util/stream/IntPipeline.java
author psandoz
Thu, 16 Jan 2014 18:20:31 +0100
changeset 22289 bb9c71b84919
parent 22111 83c31b33708e
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
/*
22111
83c31b33708e 8031187: DoubleStream.count is incorrect for a stream containing > Integer.MAX_VALUE elements
psandoz
parents: 20866
diff changeset
     2
 * Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved.
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
     4
 *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    10
 *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    15
 * accompanied this code).
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    16
 *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    20
 *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    23
 * questions.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    24
 */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    25
package java.util.stream;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    26
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    27
import java.util.IntSummaryStatistics;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    28
import java.util.Objects;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    29
import java.util.OptionalDouble;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    30
import java.util.OptionalInt;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    31
import java.util.PrimitiveIterator;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    32
import java.util.Spliterator;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    33
import java.util.Spliterators;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    34
import java.util.function.BiConsumer;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    35
import java.util.function.BinaryOperator;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    36
import java.util.function.IntBinaryOperator;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    37
import java.util.function.IntConsumer;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    38
import java.util.function.IntFunction;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    39
import java.util.function.IntPredicate;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    40
import java.util.function.IntToDoubleFunction;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    41
import java.util.function.IntToLongFunction;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    42
import java.util.function.IntUnaryOperator;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    43
import java.util.function.ObjIntConsumer;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    44
import java.util.function.Supplier;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    45
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    46
/**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    47
 * Abstract base class for an intermediate pipeline stage or pipeline source
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    48
 * stage implementing whose elements are of type {@code int}.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    49
 *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    50
 * @param <E_IN> type of elements in the upstream source
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    51
 * @since 1.8
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    52
 */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    53
abstract class IntPipeline<E_IN>
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    54
        extends AbstractPipeline<E_IN, Integer, IntStream>
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    55
        implements IntStream {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    56
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    57
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    58
     * Constructor for the head of a stream pipeline.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    59
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    60
     * @param source {@code Supplier<Spliterator>} describing the stream source
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    61
     * @param sourceFlags The source flags for the stream source, described in
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    62
     *        {@link StreamOpFlag}
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    63
     * @param parallel {@code true} if the pipeline is parallel
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    64
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    65
    IntPipeline(Supplier<? extends Spliterator<Integer>> source,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    66
                int sourceFlags, boolean parallel) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    67
        super(source, sourceFlags, parallel);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    68
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    69
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    70
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    71
     * Constructor for the head of a stream pipeline.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    72
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    73
     * @param source {@code Spliterator} describing the stream source
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    74
     * @param sourceFlags The source flags for the stream source, described in
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    75
     *        {@link StreamOpFlag}
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    76
     * @param parallel {@code true} if the pipeline is parallel
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    77
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    78
    IntPipeline(Spliterator<Integer> source,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    79
                int sourceFlags, boolean parallel) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    80
        super(source, sourceFlags, parallel);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    81
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    82
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    83
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    84
     * Constructor for appending an intermediate operation onto an existing
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    85
     * pipeline.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    86
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    87
     * @param upstream the upstream element source
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    88
     * @param opFlags the operation flags for the new operation
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    89
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    90
    IntPipeline(AbstractPipeline<?, E_IN, ?> upstream, int opFlags) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    91
        super(upstream, opFlags);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    92
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    93
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    94
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    95
     * Adapt a {@code Sink<Integer> to an {@code IntConsumer}, ideally simply
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    96
     * by casting.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    97
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    98
    private static IntConsumer adapt(Sink<Integer> sink) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    99
        if (sink instanceof IntConsumer) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   100
            return (IntConsumer) sink;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   101
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   102
        else {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   103
            if (Tripwire.ENABLED)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   104
                Tripwire.trip(AbstractPipeline.class,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   105
                              "using IntStream.adapt(Sink<Integer> s)");
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   106
            return sink::accept;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   107
        }
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
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   111
     * Adapt a {@code Spliterator<Integer>} to a {@code Spliterator.OfInt}.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   112
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   113
     * @implNote
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   114
     * The implementation attempts to cast to a Spliterator.OfInt, and throws an
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   115
     * exception if this cast is not possible.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   116
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   117
    private static Spliterator.OfInt adapt(Spliterator<Integer> s) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   118
        if (s instanceof Spliterator.OfInt) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   119
            return (Spliterator.OfInt) s;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   120
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   121
        else {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   122
            if (Tripwire.ENABLED)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   123
                Tripwire.trip(AbstractPipeline.class,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   124
                              "using IntStream.adapt(Spliterator<Integer> s)");
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   125
            throw new UnsupportedOperationException("IntStream.adapt(Spliterator<Integer> s)");
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   126
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   127
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   128
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   129
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   130
    // Shape-specific methods
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   131
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   132
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   133
    final StreamShape getOutputShape() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   134
        return StreamShape.INT_VALUE;
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
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   138
    final <P_IN> Node<Integer> evaluateToNode(PipelineHelper<Integer> helper,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   139
                                              Spliterator<P_IN> spliterator,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   140
                                              boolean flattenTree,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   141
                                              IntFunction<Integer[]> generator) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   142
        return Nodes.collectInt(helper, spliterator, flattenTree);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   143
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   144
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   145
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   146
    final <P_IN> Spliterator<Integer> wrap(PipelineHelper<Integer> ph,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   147
                                           Supplier<Spliterator<P_IN>> supplier,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   148
                                           boolean isParallel) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   149
        return new StreamSpliterators.IntWrappingSpliterator<>(ph, supplier, isParallel);
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
    @Override
19220
d3d40ccb544e 8022476: cleanup some raw types and unchecked warnings in java.util.stream
mduigou
parents: 18528
diff changeset
   153
    @SuppressWarnings("unchecked")
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   154
    final Spliterator.OfInt lazySpliterator(Supplier<? extends Spliterator<Integer>> supplier) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   155
        return new StreamSpliterators.DelegatingSpliterator.OfInt((Supplier<Spliterator.OfInt>) supplier);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   156
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   157
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   158
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   159
    final void forEachWithCancel(Spliterator<Integer> spliterator, Sink<Integer> sink) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   160
        Spliterator.OfInt spl = adapt(spliterator);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   161
        IntConsumer adaptedSink = adapt(sink);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   162
        do { } while (!sink.cancellationRequested() && spl.tryAdvance(adaptedSink));
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   163
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   164
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   165
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   166
    final Node.Builder<Integer> makeNodeBuilder(long exactSizeIfKnown,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   167
                                                IntFunction<Integer[]> generator) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   168
        return Nodes.intBuilder(exactSizeIfKnown);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   169
    }
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
    // IntStream
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   173
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   174
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   175
    public final PrimitiveIterator.OfInt iterator() {
18155
889970e5b728 8015792: Rename Spliterators.spliteratorFromIterator to Spliterators.iterator
psandoz
parents: 18154
diff changeset
   176
        return Spliterators.iterator(spliterator());
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   177
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   178
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   179
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   180
    public final Spliterator.OfInt spliterator() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   181
        return adapt(super.spliterator());
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   182
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   183
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   184
    // Stateless intermediate ops from IntStream
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   185
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   186
    @Override
18154
5ede18269905 8015798: Rename IntStream.longs/doubles and LongStream.doubles to asXxxStream
psandoz
parents: 17182
diff changeset
   187
    public final LongStream asLongStream() {
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   188
        return new LongPipeline.StatelessOp<Integer>(this, StreamShape.INT_VALUE,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   189
                                                     StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   190
            @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   191
            Sink<Integer> opWrapSink(int flags, Sink<Long> sink) {
19593
ce0cd954351c 8023681: Fix raw type warning caused by Sink
henryjen
parents: 19220
diff changeset
   192
                return new Sink.ChainedInt<Long>(sink) {
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   193
                    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   194
                    public void accept(int t) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   195
                        downstream.accept((long) t);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   196
                    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   197
                };
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   198
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   199
        };
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   200
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   201
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   202
    @Override
18154
5ede18269905 8015798: Rename IntStream.longs/doubles and LongStream.doubles to asXxxStream
psandoz
parents: 17182
diff changeset
   203
    public final DoubleStream asDoubleStream() {
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   204
        return new DoublePipeline.StatelessOp<Integer>(this, StreamShape.INT_VALUE,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   205
                                                       StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   206
            @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   207
            Sink<Integer> opWrapSink(int flags, Sink<Double> sink) {
19593
ce0cd954351c 8023681: Fix raw type warning caused by Sink
henryjen
parents: 19220
diff changeset
   208
                return new Sink.ChainedInt<Double>(sink) {
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   209
                    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   210
                    public void accept(int t) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   211
                        downstream.accept((double) t);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   212
                    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   213
                };
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   214
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   215
        };
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   216
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   217
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   218
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   219
    public final Stream<Integer> boxed() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   220
        return mapToObj(Integer::valueOf);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   221
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   222
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   223
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   224
    public final IntStream map(IntUnaryOperator mapper) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   225
        Objects.requireNonNull(mapper);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   226
        return new StatelessOp<Integer>(this, StreamShape.INT_VALUE,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   227
                                        StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   228
            @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   229
            Sink<Integer> opWrapSink(int flags, Sink<Integer> sink) {
19593
ce0cd954351c 8023681: Fix raw type warning caused by Sink
henryjen
parents: 19220
diff changeset
   230
                return new Sink.ChainedInt<Integer>(sink) {
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   231
                    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   232
                    public void accept(int t) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   233
                        downstream.accept(mapper.applyAsInt(t));
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   234
                    }
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
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   239
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   240
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   241
    public final <U> Stream<U> mapToObj(IntFunction<? extends U> mapper) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   242
        Objects.requireNonNull(mapper);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   243
        return new ReferencePipeline.StatelessOp<Integer, U>(this, StreamShape.INT_VALUE,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   244
                                                             StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   245
            @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   246
            Sink<Integer> opWrapSink(int flags, Sink<U> sink) {
19593
ce0cd954351c 8023681: Fix raw type warning caused by Sink
henryjen
parents: 19220
diff changeset
   247
                return new Sink.ChainedInt<U>(sink) {
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   248
                    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   249
                    public void accept(int t) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   250
                        downstream.accept(mapper.apply(t));
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   251
                    }
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
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   257
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   258
    public final LongStream mapToLong(IntToLongFunction mapper) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   259
        Objects.requireNonNull(mapper);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   260
        return new LongPipeline.StatelessOp<Integer>(this, StreamShape.INT_VALUE,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   261
                                                     StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   262
            @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   263
            Sink<Integer> opWrapSink(int flags, Sink<Long> sink) {
19593
ce0cd954351c 8023681: Fix raw type warning caused by Sink
henryjen
parents: 19220
diff changeset
   264
                return new Sink.ChainedInt<Long>(sink) {
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   265
                    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   266
                    public void accept(int t) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   267
                        downstream.accept(mapper.applyAsLong(t));
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   268
                    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   269
                };
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   270
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   271
        };
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   272
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   273
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   274
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   275
    public final DoubleStream mapToDouble(IntToDoubleFunction mapper) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   276
        Objects.requireNonNull(mapper);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   277
        return new DoublePipeline.StatelessOp<Integer>(this, StreamShape.INT_VALUE,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   278
                                                       StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   279
            @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   280
            Sink<Integer> opWrapSink(int flags, Sink<Double> sink) {
19593
ce0cd954351c 8023681: Fix raw type warning caused by Sink
henryjen
parents: 19220
diff changeset
   281
                return new Sink.ChainedInt<Double>(sink) {
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   282
                    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   283
                    public void accept(int t) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   284
                        downstream.accept(mapper.applyAsDouble(t));
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
            }
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
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   291
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   292
    public final IntStream flatMap(IntFunction<? extends IntStream> mapper) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   293
        return new StatelessOp<Integer>(this, StreamShape.INT_VALUE,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   294
                                        StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT | StreamOpFlag.NOT_SIZED) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   295
            @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   296
            Sink<Integer> opWrapSink(int flags, Sink<Integer> sink) {
19593
ce0cd954351c 8023681: Fix raw type warning caused by Sink
henryjen
parents: 19220
diff changeset
   297
                return new Sink.ChainedInt<Integer>(sink) {
18528
9b02581cf079 8016324: filter/flatMap pipeline sinks should pass size information to downstream sink
psandoz
parents: 18171
diff changeset
   298
                    @Override
9b02581cf079 8016324: filter/flatMap pipeline sinks should pass size information to downstream sink
psandoz
parents: 18171
diff changeset
   299
                    public void begin(long size) {
9b02581cf079 8016324: filter/flatMap pipeline sinks should pass size information to downstream sink
psandoz
parents: 18171
diff changeset
   300
                        downstream.begin(-1);
9b02581cf079 8016324: filter/flatMap pipeline sinks should pass size information to downstream sink
psandoz
parents: 18171
diff changeset
   301
                    }
9b02581cf079 8016324: filter/flatMap pipeline sinks should pass size information to downstream sink
psandoz
parents: 18171
diff changeset
   302
9b02581cf079 8016324: filter/flatMap pipeline sinks should pass size information to downstream sink
psandoz
parents: 18171
diff changeset
   303
                    @Override
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   304
                    public void accept(int t) {
19800
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19593
diff changeset
   305
                        try (IntStream result = mapper.apply(t)) {
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19593
diff changeset
   306
                            // We can do better that this too; optimize for depth=0 case and just grab spliterator and forEach it
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19593
diff changeset
   307
                            if (result != null)
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19593
diff changeset
   308
                                result.sequential().forEach(i -> downstream.accept(i));
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19593
diff changeset
   309
                        }
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   310
                    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   311
                };
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   312
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   313
        };
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   314
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   315
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   316
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   317
    public IntStream unordered() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   318
        if (!isOrdered())
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   319
            return this;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   320
        return new StatelessOp<Integer>(this, StreamShape.INT_VALUE, StreamOpFlag.NOT_ORDERED) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   321
            @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   322
            Sink<Integer> opWrapSink(int flags, Sink<Integer> sink) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   323
                return sink;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   324
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   325
        };
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   326
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   327
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   328
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   329
    public final IntStream filter(IntPredicate predicate) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   330
        Objects.requireNonNull(predicate);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   331
        return new StatelessOp<Integer>(this, StreamShape.INT_VALUE,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   332
                                        StreamOpFlag.NOT_SIZED) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   333
            @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   334
            Sink<Integer> opWrapSink(int flags, Sink<Integer> sink) {
19593
ce0cd954351c 8023681: Fix raw type warning caused by Sink
henryjen
parents: 19220
diff changeset
   335
                return new Sink.ChainedInt<Integer>(sink) {
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   336
                    @Override
18528
9b02581cf079 8016324: filter/flatMap pipeline sinks should pass size information to downstream sink
psandoz
parents: 18171
diff changeset
   337
                    public void begin(long size) {
9b02581cf079 8016324: filter/flatMap pipeline sinks should pass size information to downstream sink
psandoz
parents: 18171
diff changeset
   338
                        downstream.begin(-1);
9b02581cf079 8016324: filter/flatMap pipeline sinks should pass size information to downstream sink
psandoz
parents: 18171
diff changeset
   339
                    }
9b02581cf079 8016324: filter/flatMap pipeline sinks should pass size information to downstream sink
psandoz
parents: 18171
diff changeset
   340
9b02581cf079 8016324: filter/flatMap pipeline sinks should pass size information to downstream sink
psandoz
parents: 18171
diff changeset
   341
                    @Override
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   342
                    public void accept(int t) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   343
                        if (predicate.test(t))
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   344
                            downstream.accept(t);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   345
                    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   346
                };
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   347
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   348
        };
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   349
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   350
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   351
    @Override
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   352
    public final IntStream peek(IntConsumer action) {
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   353
        Objects.requireNonNull(action);
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   354
        return new StatelessOp<Integer>(this, StreamShape.INT_VALUE,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   355
                                        0) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   356
            @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   357
            Sink<Integer> opWrapSink(int flags, Sink<Integer> sink) {
19593
ce0cd954351c 8023681: Fix raw type warning caused by Sink
henryjen
parents: 19220
diff changeset
   358
                return new Sink.ChainedInt<Integer>(sink) {
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   359
                    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   360
                    public void accept(int t) {
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   361
                        action.accept(t);
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   362
                        downstream.accept(t);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   363
                    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   364
                };
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   365
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   366
        };
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
    // Stateful intermediate ops from IntStream
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   370
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   371
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   372
    public final IntStream limit(long maxSize) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   373
        if (maxSize < 0)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   374
            throw new IllegalArgumentException(Long.toString(maxSize));
20866
36155ee613ef 8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents: 19850
diff changeset
   375
        return SliceOps.makeInt(this, 0, maxSize);
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   376
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   377
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   378
    @Override
20866
36155ee613ef 8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents: 19850
diff changeset
   379
    public final IntStream skip(long n) {
36155ee613ef 8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents: 19850
diff changeset
   380
        if (n < 0)
36155ee613ef 8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents: 19850
diff changeset
   381
            throw new IllegalArgumentException(Long.toString(n));
36155ee613ef 8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents: 19850
diff changeset
   382
        if (n == 0)
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   383
            return this;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   384
        else
20866
36155ee613ef 8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents: 19850
diff changeset
   385
            return SliceOps.makeInt(this, n, -1);
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   386
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   387
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   388
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   389
    public final IntStream sorted() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   390
        return SortedOps.makeInt(this);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   391
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   392
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   393
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   394
    public final IntStream distinct() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   395
        // While functional and quick to implement, this approach is not very efficient.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   396
        // An efficient version requires an int-specific map/set implementation.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   397
        return boxed().distinct().mapToInt(i -> i);
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
    // Terminal ops from IntStream
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   401
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   402
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   403
    public void forEach(IntConsumer action) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   404
        evaluate(ForEachOps.makeInt(action, false));
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   405
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   406
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   407
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   408
    public void forEachOrdered(IntConsumer action) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   409
        evaluate(ForEachOps.makeInt(action, true));
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   410
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   411
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   412
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   413
    public final int sum() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   414
        return reduce(0, Integer::sum);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   415
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   416
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   417
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   418
    public final OptionalInt min() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   419
        return reduce(Math::min);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   420
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   421
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   422
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   423
    public final OptionalInt max() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   424
        return reduce(Math::max);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   425
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   426
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   427
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   428
    public final long count() {
22111
83c31b33708e 8031187: DoubleStream.count is incorrect for a stream containing > Integer.MAX_VALUE elements
psandoz
parents: 20866
diff changeset
   429
        return mapToLong(e -> 1L).sum();
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   430
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   431
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   432
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   433
    public final OptionalDouble average() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   434
        long[] avg = collect(() -> new long[2],
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   435
                             (ll, i) -> {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   436
                                 ll[0]++;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   437
                                 ll[1] += i;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   438
                             },
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   439
                             (ll, rr) -> {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   440
                                 ll[0] += rr[0];
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   441
                                 ll[1] += rr[1];
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   442
                             });
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   443
        return avg[0] > 0
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   444
               ? OptionalDouble.of((double) avg[1] / avg[0])
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   445
               : OptionalDouble.empty();
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
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   449
    public final IntSummaryStatistics summaryStatistics() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   450
        return collect(IntSummaryStatistics::new, IntSummaryStatistics::accept,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   451
                       IntSummaryStatistics::combine);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   452
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   453
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   454
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   455
    public final int reduce(int identity, IntBinaryOperator op) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   456
        return evaluate(ReduceOps.makeInt(identity, op));
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   457
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   458
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   459
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   460
    public final OptionalInt reduce(IntBinaryOperator op) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   461
        return evaluate(ReduceOps.makeInt(op));
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   462
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   463
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   464
    @Override
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   465
    public final <R> R collect(Supplier<R> supplier,
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   466
                               ObjIntConsumer<R> accumulator,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   467
                               BiConsumer<R, R> combiner) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   468
        BinaryOperator<R> operator = (left, right) -> {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   469
            combiner.accept(left, right);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   470
            return left;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   471
        };
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   472
        return evaluate(ReduceOps.makeInt(supplier, accumulator, operator));
17182
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
    public final boolean anyMatch(IntPredicate predicate) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   477
        return evaluate(MatchOps.makeInt(predicate, MatchOps.MatchKind.ANY));
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   478
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   479
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   480
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   481
    public final boolean allMatch(IntPredicate predicate) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   482
        return evaluate(MatchOps.makeInt(predicate, MatchOps.MatchKind.ALL));
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   483
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   484
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   485
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   486
    public final boolean noneMatch(IntPredicate predicate) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   487
        return evaluate(MatchOps.makeInt(predicate, MatchOps.MatchKind.NONE));
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   488
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   489
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   490
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   491
    public final OptionalInt findFirst() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   492
        return evaluate(FindOps.makeInt(true));
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   493
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   494
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   495
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   496
    public final OptionalInt findAny() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   497
        return evaluate(FindOps.makeInt(false));
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
    public final int[] toArray() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   502
        return Nodes.flattenInt((Node.OfInt) evaluateToArrayNode(Integer[]::new))
18171
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   503
                        .asPrimitiveArray();
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   504
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   505
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   506
    //
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   507
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   508
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   509
     * Source stage of an IntStream.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   510
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   511
     * @param <E_IN> type of elements in the upstream source
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   512
     * @since 1.8
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   513
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   514
    static class Head<E_IN> extends IntPipeline<E_IN> {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   515
        /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   516
         * Constructor for the source stage of an IntStream.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   517
         *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   518
         * @param source {@code Supplier<Spliterator>} describing the stream
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   519
         *               source
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   520
         * @param sourceFlags the source flags for the stream source, described
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   521
         *                    in {@link StreamOpFlag}
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   522
         * @param parallel {@code true} if the pipeline is parallel
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   523
         */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   524
        Head(Supplier<? extends Spliterator<Integer>> source,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   525
             int sourceFlags, boolean parallel) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   526
            super(source, sourceFlags, parallel);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   527
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   528
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   529
        /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   530
         * Constructor for the source stage of an IntStream.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   531
         *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   532
         * @param source {@code Spliterator} describing the stream source
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   533
         * @param sourceFlags the source flags for the stream source, described
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   534
         *                    in {@link StreamOpFlag}
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   535
         * @param parallel {@code true} if the pipeline is parallel
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   536
         */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   537
        Head(Spliterator<Integer> source,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   538
             int sourceFlags, boolean parallel) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   539
            super(source, sourceFlags, parallel);
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
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   543
        final boolean opIsStateful() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   544
            throw new UnsupportedOperationException();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   545
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   546
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   547
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   548
        final Sink<E_IN> opWrapSink(int flags, Sink<Integer> sink) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   549
            throw new UnsupportedOperationException();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   550
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   551
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   552
        // Optimized sequential terminal operations for the head of the pipeline
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   553
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   554
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   555
        public void forEach(IntConsumer action) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   556
            if (!isParallel()) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   557
                adapt(sourceStageSpliterator()).forEachRemaining(action);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   558
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   559
            else {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   560
                super.forEach(action);
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
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   564
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   565
        public void forEachOrdered(IntConsumer action) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   566
            if (!isParallel()) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   567
                adapt(sourceStageSpliterator()).forEachRemaining(action);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   568
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   569
            else {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   570
                super.forEachOrdered(action);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   571
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   572
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   573
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   574
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   575
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   576
     * Base class for a stateless intermediate stage of an IntStream
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   577
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   578
     * @param <E_IN> type of elements in the upstream source
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   579
     * @since 1.8
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   580
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   581
    abstract static class StatelessOp<E_IN> extends IntPipeline<E_IN> {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   582
        /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   583
         * Construct a new IntStream by appending a stateless intermediate
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   584
         * operation to an existing stream.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   585
         * @param upstream The upstream pipeline stage
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   586
         * @param inputShape The stream shape for the upstream pipeline stage
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   587
         * @param opFlags Operation flags for the new stage
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   588
         */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   589
        StatelessOp(AbstractPipeline<?, E_IN, ?> upstream,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   590
                    StreamShape inputShape,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   591
                    int opFlags) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   592
            super(upstream, opFlags);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   593
            assert upstream.getOutputShape() == inputShape;
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
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   597
        final boolean opIsStateful() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   598
            return false;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   599
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   600
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   601
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   602
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   603
     * Base class for a stateful intermediate stage of an IntStream.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   604
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   605
     * @param <E_IN> type of elements in the upstream source
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   606
     * @since 1.8
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   607
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   608
    abstract static class StatefulOp<E_IN> extends IntPipeline<E_IN> {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   609
        /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   610
         * Construct a new IntStream by appending a stateful intermediate
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   611
         * operation to an existing stream.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   612
         * @param upstream The upstream pipeline stage
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   613
         * @param inputShape The stream shape for the upstream pipeline stage
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   614
         * @param opFlags Operation flags for the new stage
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   615
         */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   616
        StatefulOp(AbstractPipeline<?, E_IN, ?> upstream,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   617
                   StreamShape inputShape,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   618
                   int opFlags) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   619
            super(upstream, opFlags);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   620
            assert upstream.getOutputShape() == inputShape;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   621
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   622
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   623
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   624
        final boolean opIsStateful() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   625
            return true;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   626
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   627
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   628
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   629
        abstract <P_IN> Node<Integer> opEvaluateParallel(PipelineHelper<Integer> helper,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   630
                                                         Spliterator<P_IN> spliterator,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   631
                                                         IntFunction<Integer[]> generator);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   632
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   633
}