jdk/src/share/classes/java/util/stream/LongPipeline.java
author psandoz
Thu, 16 Jan 2014 18:20:31 +0100
changeset 22289 bb9c71b84919
parent 20866 36155ee613ef
child 25526 d3cbdae6e9f9
permissions -rw-r--r--
8029452: Fork/Join task ForEachOps.ForEachOrderedTask clarifications and minor improvements Reviewed-by: mduigou, briangoetz
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
     1
/*
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
     2
 * Copyright (c) 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.LongSummaryStatistics;
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.OptionalLong;
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.IntFunction;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    37
import java.util.function.LongBinaryOperator;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    38
import java.util.function.LongConsumer;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    39
import java.util.function.LongFunction;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    40
import java.util.function.LongPredicate;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    41
import java.util.function.LongToDoubleFunction;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    42
import java.util.function.LongToIntFunction;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    43
import java.util.function.LongUnaryOperator;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    44
import java.util.function.ObjLongConsumer;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    45
import java.util.function.Supplier;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    46
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    47
/**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    48
 * Abstract base class for an intermediate pipeline stage or pipeline source
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    49
 * stage implementing whose elements are of type {@code long}.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    50
 *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    51
 * @param <E_IN> type of elements in the upstream source
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    52
 * @since 1.8
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    53
 */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    54
abstract class LongPipeline<E_IN>
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    55
        extends AbstractPipeline<E_IN, Long, LongStream>
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    56
        implements LongStream {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    57
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    58
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    59
     * Constructor for the head of a stream pipeline.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    60
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    61
     * @param source {@code Supplier<Spliterator>} describing the stream source
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    62
     * @param sourceFlags the source flags for the stream source, described in
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    63
     *        {@link StreamOpFlag}
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    64
     * @param parallel {@code true} if the pipeline is parallel
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    65
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    66
    LongPipeline(Supplier<? extends Spliterator<Long>> source,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    67
                 int sourceFlags, boolean parallel) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    68
        super(source, sourceFlags, parallel);
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
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    72
     * Constructor for the head of a stream pipeline.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    73
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    74
     * @param source {@code Spliterator} describing the stream source
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    75
     * @param sourceFlags the source flags for the stream source, described in
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    76
     *        {@link StreamOpFlag}
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    77
     * @param parallel {@code true} if the pipeline is parallel
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    78
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    79
    LongPipeline(Spliterator<Long> source,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    80
                 int sourceFlags, boolean parallel) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    81
        super(source, sourceFlags, parallel);
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
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    85
     * Constructor for appending an intermediate operation onto an existing 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
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    89
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    90
    LongPipeline(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<Long> to an {@code LongConsumer}, 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 LongConsumer adapt(Sink<Long> sink) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    99
        if (sink instanceof LongConsumer) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   100
            return (LongConsumer) sink;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   101
        } else {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   102
            if (Tripwire.ENABLED)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   103
                Tripwire.trip(AbstractPipeline.class,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   104
                              "using LongStream.adapt(Sink<Long> s)");
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   105
            return sink::accept;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   106
        }
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
     * Adapt a {@code Spliterator<Long>} to a {@code Spliterator.OfLong}.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   111
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   112
     * @implNote
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   113
     * The implementation attempts to cast to a Spliterator.OfLong, and throws
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   114
     * an exception if this cast is not possible.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   115
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   116
    private static Spliterator.OfLong adapt(Spliterator<Long> s) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   117
        if (s instanceof Spliterator.OfLong) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   118
            return (Spliterator.OfLong) s;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   119
        } else {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   120
            if (Tripwire.ENABLED)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   121
                Tripwire.trip(AbstractPipeline.class,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   122
                              "using LongStream.adapt(Spliterator<Long> s)");
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   123
            throw new UnsupportedOperationException("LongStream.adapt(Spliterator<Long> s)");
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   124
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   125
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   126
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   127
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   128
    // Shape-specific methods
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   129
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   130
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   131
    final StreamShape getOutputShape() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   132
        return StreamShape.LONG_VALUE;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   133
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   134
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   135
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   136
    final <P_IN> Node<Long> evaluateToNode(PipelineHelper<Long> helper,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   137
                                           Spliterator<P_IN> spliterator,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   138
                                           boolean flattenTree,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   139
                                           IntFunction<Long[]> generator) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   140
        return Nodes.collectLong(helper, spliterator, flattenTree);
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
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   144
    final <P_IN> Spliterator<Long> wrap(PipelineHelper<Long> ph,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   145
                                        Supplier<Spliterator<P_IN>> supplier,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   146
                                        boolean isParallel) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   147
        return new StreamSpliterators.LongWrappingSpliterator<>(ph, supplier, isParallel);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   148
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   149
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   150
    @Override
19220
d3d40ccb544e 8022476: cleanup some raw types and unchecked warnings in java.util.stream
mduigou
parents: 18528
diff changeset
   151
    @SuppressWarnings("unchecked")
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   152
    final Spliterator.OfLong lazySpliterator(Supplier<? extends Spliterator<Long>> supplier) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   153
        return new StreamSpliterators.DelegatingSpliterator.OfLong((Supplier<Spliterator.OfLong>) supplier);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   154
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   155
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   156
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   157
    final void forEachWithCancel(Spliterator<Long> spliterator, Sink<Long> sink) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   158
        Spliterator.OfLong spl = adapt(spliterator);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   159
        LongConsumer adaptedSink =  adapt(sink);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   160
        do { } while (!sink.cancellationRequested() && spl.tryAdvance(adaptedSink));
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   161
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   162
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   163
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   164
    final Node.Builder<Long> makeNodeBuilder(long exactSizeIfKnown, IntFunction<Long[]> generator) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   165
        return Nodes.longBuilder(exactSizeIfKnown);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   166
    }
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
    // LongStream
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   170
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   171
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   172
    public final PrimitiveIterator.OfLong iterator() {
18155
889970e5b728 8015792: Rename Spliterators.spliteratorFromIterator to Spliterators.iterator
psandoz
parents: 18154
diff changeset
   173
        return Spliterators.iterator(spliterator());
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   174
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   175
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   176
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   177
    public final Spliterator.OfLong spliterator() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   178
        return adapt(super.spliterator());
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   179
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   180
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   181
    // Stateless intermediate ops from LongStream
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   183
    @Override
18154
5ede18269905 8015798: Rename IntStream.longs/doubles and LongStream.doubles to asXxxStream
psandoz
parents: 17182
diff changeset
   184
    public final DoubleStream asDoubleStream() {
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   185
        return new DoublePipeline.StatelessOp<Long>(this, StreamShape.LONG_VALUE,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   186
                                                    StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   187
            @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   188
            Sink<Long> opWrapSink(int flags, Sink<Double> sink) {
19593
ce0cd954351c 8023681: Fix raw type warning caused by Sink
henryjen
parents: 19220
diff changeset
   189
                return new Sink.ChainedLong<Double>(sink) {
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   190
                    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   191
                    public void accept(long t) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   192
                        downstream.accept((double) t);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   193
                    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   194
                };
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   195
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   196
        };
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   197
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   198
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   199
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   200
    public final Stream<Long> boxed() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   201
        return mapToObj(Long::valueOf);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   202
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   203
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   204
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   205
    public final LongStream map(LongUnaryOperator mapper) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   206
        Objects.requireNonNull(mapper);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   207
        return new StatelessOp<Long>(this, StreamShape.LONG_VALUE,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   208
                                     StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   209
            @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   210
            Sink<Long> opWrapSink(int flags, Sink<Long> sink) {
19593
ce0cd954351c 8023681: Fix raw type warning caused by Sink
henryjen
parents: 19220
diff changeset
   211
                return new Sink.ChainedLong<Long>(sink) {
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   212
                    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   213
                    public void accept(long t) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   214
                        downstream.accept(mapper.applyAsLong(t));
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
        };
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   219
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   220
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   221
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   222
    public final <U> Stream<U> mapToObj(LongFunction<? extends U> mapper) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   223
        Objects.requireNonNull(mapper);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   224
        return new ReferencePipeline.StatelessOp<Long, U>(this, StreamShape.LONG_VALUE,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   225
                                                          StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   226
            @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   227
            Sink<Long> opWrapSink(int flags, Sink<U> sink) {
19593
ce0cd954351c 8023681: Fix raw type warning caused by Sink
henryjen
parents: 19220
diff changeset
   228
                return new Sink.ChainedLong<U>(sink) {
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   229
                    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   230
                    public void accept(long t) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   231
                        downstream.accept(mapper.apply(t));
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
        };
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
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   239
    public final IntStream mapToInt(LongToIntFunction mapper) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   240
        Objects.requireNonNull(mapper);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   241
        return new IntPipeline.StatelessOp<Long>(this, StreamShape.LONG_VALUE,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   242
                                                 StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   243
            @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   244
            Sink<Long> opWrapSink(int flags, Sink<Integer> sink) {
19593
ce0cd954351c 8023681: Fix raw type warning caused by Sink
henryjen
parents: 19220
diff changeset
   245
                return new Sink.ChainedLong<Integer>(sink) {
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   246
                    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   247
                    public void accept(long t) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   248
                        downstream.accept(mapper.applyAsInt(t));
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   249
                    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   250
                };
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   251
            }
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
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   256
    public final DoubleStream mapToDouble(LongToDoubleFunction mapper) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   257
        Objects.requireNonNull(mapper);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   258
        return new DoublePipeline.StatelessOp<Long>(this, StreamShape.LONG_VALUE,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   259
                                                    StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   260
            @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   261
            Sink<Long> opWrapSink(int flags, Sink<Double> sink) {
19593
ce0cd954351c 8023681: Fix raw type warning caused by Sink
henryjen
parents: 19220
diff changeset
   262
                return new Sink.ChainedLong<Double>(sink) {
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   263
                    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   264
                    public void accept(long t) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   265
                        downstream.accept(mapper.applyAsDouble(t));
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   266
                    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   267
                };
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
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   273
    public final LongStream flatMap(LongFunction<? extends LongStream> mapper) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   274
        return new StatelessOp<Long>(this, StreamShape.LONG_VALUE,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   275
                                     StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT | StreamOpFlag.NOT_SIZED) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   276
            @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   277
            Sink<Long> opWrapSink(int flags, Sink<Long> sink) {
19593
ce0cd954351c 8023681: Fix raw type warning caused by Sink
henryjen
parents: 19220
diff changeset
   278
                return new Sink.ChainedLong<Long>(sink) {
18528
9b02581cf079 8016324: filter/flatMap pipeline sinks should pass size information to downstream sink
psandoz
parents: 18171
diff changeset
   279
                    @Override
9b02581cf079 8016324: filter/flatMap pipeline sinks should pass size information to downstream sink
psandoz
parents: 18171
diff changeset
   280
                    public void begin(long size) {
9b02581cf079 8016324: filter/flatMap pipeline sinks should pass size information to downstream sink
psandoz
parents: 18171
diff changeset
   281
                        downstream.begin(-1);
9b02581cf079 8016324: filter/flatMap pipeline sinks should pass size information to downstream sink
psandoz
parents: 18171
diff changeset
   282
                    }
9b02581cf079 8016324: filter/flatMap pipeline sinks should pass size information to downstream sink
psandoz
parents: 18171
diff changeset
   283
9b02581cf079 8016324: filter/flatMap pipeline sinks should pass size information to downstream sink
psandoz
parents: 18171
diff changeset
   284
                    @Override
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   285
                    public void accept(long t) {
19800
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19593
diff changeset
   286
                        try (LongStream result = mapper.apply(t)) {
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19593
diff changeset
   287
                            // 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
   288
                            if (result != null)
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19593
diff changeset
   289
                                result.sequential().forEach(i -> downstream.accept(i));
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19593
diff changeset
   290
                        }
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   291
                    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   292
                };
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   293
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   294
        };
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
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   298
    public LongStream unordered() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   299
        if (!isOrdered())
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   300
            return this;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   301
        return new StatelessOp<Long>(this, StreamShape.LONG_VALUE, StreamOpFlag.NOT_ORDERED) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   302
            @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   303
            Sink<Long> opWrapSink(int flags, Sink<Long> sink) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   304
                return sink;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   305
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   306
        };
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   307
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   308
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   309
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   310
    public final LongStream filter(LongPredicate predicate) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   311
        Objects.requireNonNull(predicate);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   312
        return new StatelessOp<Long>(this, StreamShape.LONG_VALUE,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   313
                                     StreamOpFlag.NOT_SIZED) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   314
            @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   315
            Sink<Long> opWrapSink(int flags, Sink<Long> sink) {
19593
ce0cd954351c 8023681: Fix raw type warning caused by Sink
henryjen
parents: 19220
diff changeset
   316
                return new Sink.ChainedLong<Long>(sink) {
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   317
                    @Override
18528
9b02581cf079 8016324: filter/flatMap pipeline sinks should pass size information to downstream sink
psandoz
parents: 18171
diff changeset
   318
                    public void begin(long size) {
9b02581cf079 8016324: filter/flatMap pipeline sinks should pass size information to downstream sink
psandoz
parents: 18171
diff changeset
   319
                        downstream.begin(-1);
9b02581cf079 8016324: filter/flatMap pipeline sinks should pass size information to downstream sink
psandoz
parents: 18171
diff changeset
   320
                    }
9b02581cf079 8016324: filter/flatMap pipeline sinks should pass size information to downstream sink
psandoz
parents: 18171
diff changeset
   321
9b02581cf079 8016324: filter/flatMap pipeline sinks should pass size information to downstream sink
psandoz
parents: 18171
diff changeset
   322
                    @Override
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   323
                    public void accept(long t) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   324
                        if (predicate.test(t))
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   325
                            downstream.accept(t);
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
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   329
        };
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   330
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   331
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   332
    @Override
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   333
    public final LongStream peek(LongConsumer action) {
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   334
        Objects.requireNonNull(action);
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   335
        return new StatelessOp<Long>(this, StreamShape.LONG_VALUE,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   336
                                     0) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   337
            @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   338
            Sink<Long> opWrapSink(int flags, Sink<Long> sink) {
19593
ce0cd954351c 8023681: Fix raw type warning caused by Sink
henryjen
parents: 19220
diff changeset
   339
                return new Sink.ChainedLong<Long>(sink) {
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   340
                    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   341
                    public void accept(long t) {
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   342
                        action.accept(t);
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   343
                        downstream.accept(t);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   344
                    }
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
    // Stateful intermediate ops from LongStream
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   351
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   352
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   353
    public final LongStream limit(long maxSize) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   354
        if (maxSize < 0)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   355
            throw new IllegalArgumentException(Long.toString(maxSize));
20866
36155ee613ef 8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents: 19850
diff changeset
   356
        return SliceOps.makeLong(this, 0, maxSize);
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   357
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   358
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   359
    @Override
20866
36155ee613ef 8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents: 19850
diff changeset
   360
    public final LongStream skip(long n) {
36155ee613ef 8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents: 19850
diff changeset
   361
        if (n < 0)
36155ee613ef 8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents: 19850
diff changeset
   362
            throw new IllegalArgumentException(Long.toString(n));
36155ee613ef 8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents: 19850
diff changeset
   363
        if (n == 0)
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   364
            return this;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   365
        else
20866
36155ee613ef 8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents: 19850
diff changeset
   366
            return SliceOps.makeLong(this, n, -1);
17182
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
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   370
    public final LongStream sorted() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   371
        return SortedOps.makeLong(this);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   372
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   373
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   374
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   375
    public final LongStream distinct() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   376
        // While functional and quick to implement, this approach is not very efficient.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   377
        // An efficient version requires a long-specific map/set implementation.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   378
        return boxed().distinct().mapToLong(i -> (long) i);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   379
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   380
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   381
    // Terminal ops from LongStream
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   382
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   383
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   384
    public void forEach(LongConsumer action) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   385
        evaluate(ForEachOps.makeLong(action, false));
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 void forEachOrdered(LongConsumer action) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   390
        evaluate(ForEachOps.makeLong(action, true));
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 long sum() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   395
        // use better algorithm to compensate for intermediate overflow?
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   396
        return reduce(0, Long::sum);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   397
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   398
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   399
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   400
    public final OptionalLong min() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   401
        return reduce(Math::min);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   402
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   403
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   404
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   405
    public final OptionalLong max() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   406
        return reduce(Math::max);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   407
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   408
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   409
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   410
    public final OptionalDouble average() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   411
        long[] avg = collect(() -> new long[2],
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   412
                             (ll, i) -> {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   413
                                 ll[0]++;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   414
                                 ll[1] += i;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   415
                             },
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   416
                             (ll, rr) -> {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   417
                                 ll[0] += rr[0];
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   418
                                 ll[1] += rr[1];
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   419
                             });
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   420
        return avg[0] > 0
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   421
               ? OptionalDouble.of((double) avg[1] / avg[0])
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   422
               : OptionalDouble.empty();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   423
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   424
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   425
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   426
    public final long count() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   427
        return map(e -> 1L).sum();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   428
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   429
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   430
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   431
    public final LongSummaryStatistics summaryStatistics() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   432
        return collect(LongSummaryStatistics::new, LongSummaryStatistics::accept,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   433
                       LongSummaryStatistics::combine);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   434
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   435
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   436
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   437
    public final long reduce(long identity, LongBinaryOperator op) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   438
        return evaluate(ReduceOps.makeLong(identity, op));
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   439
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   440
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   441
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   442
    public final OptionalLong reduce(LongBinaryOperator op) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   443
        return evaluate(ReduceOps.makeLong(op));
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   444
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   445
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   446
    @Override
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   447
    public final <R> R collect(Supplier<R> supplier,
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   448
                               ObjLongConsumer<R> accumulator,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   449
                               BiConsumer<R, R> combiner) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   450
        BinaryOperator<R> operator = (left, right) -> {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   451
            combiner.accept(left, right);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   452
            return left;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   453
        };
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   454
        return evaluate(ReduceOps.makeLong(supplier, accumulator, operator));
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   455
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   456
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   457
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   458
    public final boolean anyMatch(LongPredicate predicate) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   459
        return evaluate(MatchOps.makeLong(predicate, MatchOps.MatchKind.ANY));
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   460
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   461
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   462
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   463
    public final boolean allMatch(LongPredicate predicate) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   464
        return evaluate(MatchOps.makeLong(predicate, MatchOps.MatchKind.ALL));
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   465
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   466
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   467
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   468
    public final boolean noneMatch(LongPredicate predicate) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   469
        return evaluate(MatchOps.makeLong(predicate, MatchOps.MatchKind.NONE));
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   470
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   471
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   472
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   473
    public final OptionalLong findFirst() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   474
        return evaluate(FindOps.makeLong(true));
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   475
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   476
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   477
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   478
    public final OptionalLong findAny() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   479
        return evaluate(FindOps.makeLong(false));
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   480
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   481
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   482
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   483
    public final long[] toArray() {
18171
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   484
        return Nodes.flattenLong((Node.OfLong) evaluateToArrayNode(Long[]::new))
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   485
                .asPrimitiveArray();
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   486
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   487
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
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   491
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   492
     * Source stage of a LongPipeline.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   493
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   494
     * @param <E_IN> type of elements in the upstream source
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   495
     * @since 1.8
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   496
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   497
    static class Head<E_IN> extends LongPipeline<E_IN> {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   498
        /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   499
         * Constructor for the source stage of a LongStream.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   500
         *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   501
         * @param source {@code Supplier<Spliterator>} describing the stream
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   502
         *               source
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   503
         * @param sourceFlags the source flags for the stream source, described
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   504
         *                    in {@link StreamOpFlag}
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   505
         * @param parallel {@code true} if the pipeline is parallel
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   506
         */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   507
        Head(Supplier<? extends Spliterator<Long>> source,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   508
             int sourceFlags, boolean parallel) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   509
            super(source, sourceFlags, parallel);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   510
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   511
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   512
        /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   513
         * Constructor for the source stage of a LongStream.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   514
         *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   515
         * @param source {@code Spliterator} describing the stream source
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   516
         * @param sourceFlags the source flags for the stream source, described
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   517
         *                    in {@link StreamOpFlag}
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   518
         * @param parallel {@code true} if the pipeline is parallel
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   519
         */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   520
        Head(Spliterator<Long> source,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   521
             int sourceFlags, boolean parallel) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   522
            super(source, sourceFlags, parallel);
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
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   526
        final boolean opIsStateful() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   527
            throw new UnsupportedOperationException();
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
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   531
        final Sink<E_IN> opWrapSink(int flags, Sink<Long> sink) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   532
            throw new UnsupportedOperationException();
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
        // Optimized sequential terminal operations for the head of the pipeline
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   536
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   537
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   538
        public void forEach(LongConsumer action) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   539
            if (!isParallel()) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   540
                adapt(sourceStageSpliterator()).forEachRemaining(action);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   541
            } else {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   542
                super.forEach(action);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   543
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   544
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   545
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   546
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   547
        public void forEachOrdered(LongConsumer action) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   548
            if (!isParallel()) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   549
                adapt(sourceStageSpliterator()).forEachRemaining(action);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   550
            } else {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   551
                super.forEachOrdered(action);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   552
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   553
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   554
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   555
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   556
    /** Base class for a stateless intermediate stage of a LongStream.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   557
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   558
     * @param <E_IN> type of elements in the upstream source
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   559
     * @since 1.8
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   560
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   561
    abstract static class StatelessOp<E_IN> extends LongPipeline<E_IN> {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   562
        /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   563
         * Construct a new LongStream by appending a stateless intermediate
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   564
         * operation to an existing stream.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   565
         * @param upstream The upstream pipeline stage
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   566
         * @param inputShape The stream shape for the upstream pipeline stage
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   567
         * @param opFlags Operation flags for the new stage
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   568
         */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   569
        StatelessOp(AbstractPipeline<?, E_IN, ?> upstream,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   570
                    StreamShape inputShape,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   571
                    int opFlags) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   572
            super(upstream, opFlags);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   573
            assert upstream.getOutputShape() == inputShape;
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
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   577
        final boolean opIsStateful() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   578
            return false;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   579
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   580
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   581
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   582
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   583
     * Base class for a stateful intermediate stage of a LongStream.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   584
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   585
     * @param <E_IN> type of elements in the upstream source
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   586
     * @since 1.8
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   587
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   588
    abstract static class StatefulOp<E_IN> extends LongPipeline<E_IN> {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   589
        /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   590
         * Construct a new LongStream by appending a stateful intermediate
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   591
         * operation to an existing stream.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   592
         * @param upstream The upstream pipeline stage
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   593
         * @param inputShape The stream shape for the upstream pipeline stage
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   594
         * @param opFlags Operation flags for the new stage
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   595
         */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   596
        StatefulOp(AbstractPipeline<?, E_IN, ?> upstream,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   597
                   StreamShape inputShape,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   598
                   int opFlags) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   599
            super(upstream, opFlags);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   600
            assert upstream.getOutputShape() == inputShape;
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
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   604
        final boolean opIsStateful() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   605
            return true;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   606
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   607
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   608
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   609
        abstract <P_IN> Node<Long> opEvaluateParallel(PipelineHelper<Long> helper,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   610
                                                      Spliterator<P_IN> spliterator,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   611
                                                      IntFunction<Long[]> generator);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   612
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   613
}