src/java.base/share/classes/java/util/stream/WhileOps.java
author psandoz
Wed, 20 Dec 2017 11:40:45 -0800
changeset 48410 8aab0cea56bf
parent 47216 71c04702a3d5
child 53563 a4b7ea85d668
permissions -rw-r--r--
8193856: takeWhile produces incorrect result with elements produced by flatMap Reviewed-by: smarks
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
31644
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
     1
/*
48410
8aab0cea56bf 8193856: takeWhile produces incorrect result with elements produced by flatMap
psandoz
parents: 47216
diff changeset
     2
 * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
31644
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
     4
 *
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    10
 *
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    15
 * accompanied this code).
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    16
 *
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    20
 *
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    23
 * questions.
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    24
 */
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    25
package java.util.stream;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    26
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    27
import java.util.Comparator;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    28
import java.util.Objects;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    29
import java.util.Spliterator;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    30
import java.util.concurrent.CountedCompleter;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    31
import java.util.concurrent.atomic.AtomicBoolean;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    32
import java.util.function.Consumer;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    33
import java.util.function.DoubleConsumer;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    34
import java.util.function.DoublePredicate;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    35
import java.util.function.IntConsumer;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    36
import java.util.function.IntFunction;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    37
import java.util.function.IntPredicate;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    38
import java.util.function.LongConsumer;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    39
import java.util.function.LongPredicate;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    40
import java.util.function.Predicate;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    41
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    42
/**
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    43
 * Factory for instances of a takeWhile and dropWhile operations
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    44
 * that produce subsequences of their input stream.
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    45
 *
35302
e4d2275861c3 8136494: Update "@since 1.9" to "@since 9" to match java.version.specification
iris
parents: 32649
diff changeset
    46
 * @since 9
31644
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    47
 */
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    48
final class WhileOps {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    49
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    50
    static final int TAKE_FLAGS = StreamOpFlag.NOT_SIZED | StreamOpFlag.IS_SHORT_CIRCUIT;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    51
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    52
    static final int DROP_FLAGS = StreamOpFlag.NOT_SIZED;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    53
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    54
    /**
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    55
     * Appends a "takeWhile" operation to the provided Stream.
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    56
     *
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    57
     * @param <T> the type of both input and output elements
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    58
     * @param upstream a reference stream with element type T
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    59
     * @param predicate the predicate that returns false to halt taking.
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    60
     */
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    61
    static <T> Stream<T> makeTakeWhileRef(AbstractPipeline<?, T, ?> upstream,
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    62
                                          Predicate<? super T> predicate) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    63
        Objects.requireNonNull(predicate);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    64
        return new ReferencePipeline.StatefulOp<T, T>(upstream, StreamShape.REFERENCE, TAKE_FLAGS) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    65
            @Override
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    66
            <P_IN> Spliterator<T> opEvaluateParallelLazy(PipelineHelper<T> helper,
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    67
                                                         Spliterator<P_IN> spliterator) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    68
                if (StreamOpFlag.ORDERED.isKnown(helper.getStreamAndOpFlags())) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    69
                    return opEvaluateParallel(helper, spliterator, Nodes.castingArray())
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    70
                            .spliterator();
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    71
                }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    72
                else {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    73
                    return new UnorderedWhileSpliterator.OfRef.Taking<>(
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    74
                            helper.wrapSpliterator(spliterator), false, predicate);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    75
                }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    76
            }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    77
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    78
            @Override
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    79
            <P_IN> Node<T> opEvaluateParallel(PipelineHelper<T> helper,
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    80
                                              Spliterator<P_IN> spliterator,
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    81
                                              IntFunction<T[]> generator) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    82
                return new TakeWhileTask<>(this, helper, spliterator, generator)
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    83
                        .invoke();
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    84
            }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    85
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    86
            @Override
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    87
            Sink<T> opWrapSink(int flags, Sink<T> sink) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    88
                return new Sink.ChainedReference<T, T>(sink) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    89
                    boolean take = true;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    90
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    91
                    @Override
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    92
                    public void begin(long size) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    93
                        downstream.begin(-1);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    94
                    }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    95
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    96
                    @Override
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    97
                    public void accept(T t) {
48410
8aab0cea56bf 8193856: takeWhile produces incorrect result with elements produced by flatMap
psandoz
parents: 47216
diff changeset
    98
                        if (take && (take = predicate.test(t))) {
31644
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    99
                            downstream.accept(t);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   100
                        }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   101
                    }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   102
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   103
                    @Override
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   104
                    public boolean cancellationRequested() {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   105
                        return !take || downstream.cancellationRequested();
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   106
                    }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   107
                };
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   108
            }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   109
        };
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   110
    }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   111
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   112
    /**
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   113
     * Appends a "takeWhile" operation to the provided IntStream.
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   114
     *
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   115
     * @param upstream a reference stream with element type T
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   116
     * @param predicate the predicate that returns false to halt taking.
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   117
     */
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   118
    static IntStream makeTakeWhileInt(AbstractPipeline<?, Integer, ?> upstream,
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   119
                                      IntPredicate predicate) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   120
        Objects.requireNonNull(predicate);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   121
        return new IntPipeline.StatefulOp<Integer>(upstream, StreamShape.INT_VALUE, TAKE_FLAGS) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   122
            @Override
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   123
            <P_IN> Spliterator<Integer> opEvaluateParallelLazy(PipelineHelper<Integer> helper,
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   124
                                                               Spliterator<P_IN> spliterator) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   125
                if (StreamOpFlag.ORDERED.isKnown(helper.getStreamAndOpFlags())) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   126
                    return opEvaluateParallel(helper, spliterator, Integer[]::new)
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   127
                            .spliterator();
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   128
                }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   129
                else {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   130
                    return new UnorderedWhileSpliterator.OfInt.Taking(
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   131
                            (Spliterator.OfInt) helper.wrapSpliterator(spliterator), false, predicate);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   132
                }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   133
            }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   134
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   135
            @Override
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   136
            <P_IN> Node<Integer> opEvaluateParallel(PipelineHelper<Integer> helper,
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   137
                                                    Spliterator<P_IN> spliterator,
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   138
                                                    IntFunction<Integer[]> generator) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   139
                return new TakeWhileTask<>(this, helper, spliterator, generator)
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   140
                        .invoke();
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   141
            }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   142
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   143
            @Override
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   144
            Sink<Integer> opWrapSink(int flags, Sink<Integer> sink) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   145
                return new Sink.ChainedInt<Integer>(sink) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   146
                    boolean take = true;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   147
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   148
                    @Override
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   149
                    public void begin(long size) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   150
                        downstream.begin(-1);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   151
                    }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   152
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   153
                    @Override
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   154
                    public void accept(int t) {
48410
8aab0cea56bf 8193856: takeWhile produces incorrect result with elements produced by flatMap
psandoz
parents: 47216
diff changeset
   155
                        if (take && (take = predicate.test(t))) {
31644
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   156
                            downstream.accept(t);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   157
                        }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   158
                    }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   159
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   160
                    @Override
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   161
                    public boolean cancellationRequested() {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   162
                        return !take || downstream.cancellationRequested();
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   163
                    }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   164
                };
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   165
            }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   166
        };
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   167
    }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   168
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   169
    /**
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   170
     * Appends a "takeWhile" operation to the provided LongStream.
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   171
     *
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   172
     * @param upstream a reference stream with element type T
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   173
     * @param predicate the predicate that returns false to halt taking.
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   174
     */
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   175
    static LongStream makeTakeWhileLong(AbstractPipeline<?, Long, ?> upstream,
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   176
                                        LongPredicate predicate) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   177
        Objects.requireNonNull(predicate);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   178
        return new LongPipeline.StatefulOp<Long>(upstream, StreamShape.LONG_VALUE, TAKE_FLAGS) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   179
            @Override
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   180
            <P_IN> Spliterator<Long> opEvaluateParallelLazy(PipelineHelper<Long> helper,
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   181
                                                            Spliterator<P_IN> spliterator) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   182
                if (StreamOpFlag.ORDERED.isKnown(helper.getStreamAndOpFlags())) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   183
                    return opEvaluateParallel(helper, spliterator, Long[]::new)
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   184
                            .spliterator();
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   185
                }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   186
                else {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   187
                    return new UnorderedWhileSpliterator.OfLong.Taking(
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   188
                            (Spliterator.OfLong) helper.wrapSpliterator(spliterator), false, predicate);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   189
                }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   190
            }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   191
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   192
            @Override
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   193
            <P_IN> Node<Long> opEvaluateParallel(PipelineHelper<Long> helper,
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   194
                                                 Spliterator<P_IN> spliterator,
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   195
                                                 IntFunction<Long[]> generator) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   196
                return new TakeWhileTask<>(this, helper, spliterator, generator)
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   197
                        .invoke();
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   198
            }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   199
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   200
            @Override
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   201
            Sink<Long> opWrapSink(int flags, Sink<Long> sink) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   202
                return new Sink.ChainedLong<Long>(sink) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   203
                    boolean take = true;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   204
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   205
                    @Override
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   206
                    public void begin(long size) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   207
                        downstream.begin(-1);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   208
                    }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   209
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   210
                    @Override
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   211
                    public void accept(long t) {
48410
8aab0cea56bf 8193856: takeWhile produces incorrect result with elements produced by flatMap
psandoz
parents: 47216
diff changeset
   212
                        if (take && (take = predicate.test(t))) {
31644
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   213
                            downstream.accept(t);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   214
                        }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   215
                    }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   216
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   217
                    @Override
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   218
                    public boolean cancellationRequested() {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   219
                        return !take || downstream.cancellationRequested();
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   220
                    }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   221
                };
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   222
            }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   223
        };
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   224
    }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   225
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   226
    /**
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   227
     * Appends a "takeWhile" operation to the provided DoubleStream.
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   228
     *
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   229
     * @param upstream a reference stream with element type T
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   230
     * @param predicate the predicate that returns false to halt taking.
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   231
     */
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   232
    static DoubleStream makeTakeWhileDouble(AbstractPipeline<?, Double, ?> upstream,
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   233
                                            DoublePredicate predicate) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   234
        Objects.requireNonNull(predicate);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   235
        return new DoublePipeline.StatefulOp<Double>(upstream, StreamShape.DOUBLE_VALUE, TAKE_FLAGS) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   236
            @Override
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   237
            <P_IN> Spliterator<Double> opEvaluateParallelLazy(PipelineHelper<Double> helper,
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   238
                                                              Spliterator<P_IN> spliterator) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   239
                if (StreamOpFlag.ORDERED.isKnown(helper.getStreamAndOpFlags())) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   240
                    return opEvaluateParallel(helper, spliterator, Double[]::new)
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   241
                            .spliterator();
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   242
                }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   243
                else {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   244
                    return new UnorderedWhileSpliterator.OfDouble.Taking(
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   245
                            (Spliterator.OfDouble) helper.wrapSpliterator(spliterator), false, predicate);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   246
                }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   247
            }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   248
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   249
            @Override
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   250
            <P_IN> Node<Double> opEvaluateParallel(PipelineHelper<Double> helper,
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   251
                                                   Spliterator<P_IN> spliterator,
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   252
                                                   IntFunction<Double[]> generator) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   253
                return new TakeWhileTask<>(this, helper, spliterator, generator)
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   254
                        .invoke();
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   255
            }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   256
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   257
            @Override
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   258
            Sink<Double> opWrapSink(int flags, Sink<Double> sink) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   259
                return new Sink.ChainedDouble<Double>(sink) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   260
                    boolean take = true;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   261
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   262
                    @Override
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   263
                    public void begin(long size) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   264
                        downstream.begin(-1);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   265
                    }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   266
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   267
                    @Override
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   268
                    public void accept(double t) {
48410
8aab0cea56bf 8193856: takeWhile produces incorrect result with elements produced by flatMap
psandoz
parents: 47216
diff changeset
   269
                        if (take && (take = predicate.test(t))) {
31644
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   270
                            downstream.accept(t);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   271
                        }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   272
                    }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   273
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   274
                    @Override
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   275
                    public boolean cancellationRequested() {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   276
                        return !take || downstream.cancellationRequested();
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   277
                    }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   278
                };
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   279
            }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   280
        };
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   281
    }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   282
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   283
    /**
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   284
     * A specialization for the dropWhile operation that controls if
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   285
     * elements to be dropped are counted and passed downstream.
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   286
     * <p>
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   287
     * This specialization is utilized by the {@link TakeWhileTask} for
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   288
     * pipelines that are ordered.  In such cases elements cannot be dropped
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   289
     * until all elements have been collected.
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   290
     *
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   291
     * @param <T> the type of both input and output elements
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   292
     */
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   293
    interface DropWhileOp<T> {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   294
        /**
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   295
         * Accepts a {@code Sink} which will receive the results of this
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   296
         * dropWhile operation, and return a {@code DropWhileSink} which
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   297
         * accepts
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   298
         * elements and which performs the dropWhile operation passing the
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   299
         * results to the provided {@code Sink}.
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   300
         *
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   301
         * @param sink sink to which elements should be sent after processing
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   302
         * @param retainAndCountDroppedElements true if elements to be dropped
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   303
         * are counted and passed to the sink, otherwise such elements
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   304
         * are actually dropped and not passed to the sink.
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   305
         * @return a dropWhile sink
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   306
         */
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   307
        DropWhileSink<T> opWrapSink(Sink<T> sink, boolean retainAndCountDroppedElements);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   308
    }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   309
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   310
    /**
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   311
     * A specialization for a dropWhile sink.
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   312
     *
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   313
     * @param <T> the type of both input and output elements
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   314
     */
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   315
    interface DropWhileSink<T> extends Sink<T> {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   316
        /**
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   317
         * @return the could of elements that would have been dropped and
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   318
         * instead were passed downstream.
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   319
         */
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   320
        long getDropCount();
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   321
    }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   322
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   323
    /**
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   324
     * Appends a "dropWhile" operation to the provided Stream.
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   325
     *
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   326
     * @param <T> the type of both input and output elements
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   327
     * @param upstream a reference stream with element type T
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   328
     * @param predicate the predicate that returns false to halt dropping.
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   329
     */
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   330
    static <T> Stream<T> makeDropWhileRef(AbstractPipeline<?, T, ?> upstream,
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   331
                                          Predicate<? super T> predicate) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   332
        Objects.requireNonNull(predicate);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   333
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   334
        class Op extends ReferencePipeline.StatefulOp<T, T> implements DropWhileOp<T> {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   335
            public Op(AbstractPipeline<?, T, ?> upstream, StreamShape inputShape, int opFlags) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   336
                super(upstream, inputShape, opFlags);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   337
            }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   338
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   339
            @Override
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   340
            <P_IN> Spliterator<T> opEvaluateParallelLazy(PipelineHelper<T> helper,
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   341
                                                         Spliterator<P_IN> spliterator) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   342
                if (StreamOpFlag.ORDERED.isKnown(helper.getStreamAndOpFlags())) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   343
                    return opEvaluateParallel(helper, spliterator, Nodes.castingArray())
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   344
                            .spliterator();
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   345
                }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   346
                else {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   347
                    return new UnorderedWhileSpliterator.OfRef.Dropping<>(
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   348
                            helper.wrapSpliterator(spliterator), false, predicate);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   349
                }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   350
            }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   351
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   352
            @Override
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   353
            <P_IN> Node<T> opEvaluateParallel(PipelineHelper<T> helper,
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   354
                                              Spliterator<P_IN> spliterator,
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   355
                                              IntFunction<T[]> generator) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   356
                return new DropWhileTask<>(this, helper, spliterator, generator)
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   357
                        .invoke();
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   358
            }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   359
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   360
            @Override
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   361
            Sink<T> opWrapSink(int flags, Sink<T> sink) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   362
                return opWrapSink(sink, false);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   363
            }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   364
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   365
            public DropWhileSink<T> opWrapSink(Sink<T> sink, boolean retainAndCountDroppedElements) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   366
                class OpSink extends Sink.ChainedReference<T, T> implements DropWhileSink<T> {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   367
                    long dropCount;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   368
                    boolean take;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   369
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   370
                    OpSink() {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   371
                        super(sink);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   372
                    }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   373
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   374
                    @Override
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   375
                    public void accept(T t) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   376
                        boolean takeElement = take || (take = !predicate.test(t));
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   377
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   378
                        // If ordered and element is dropped increment index
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   379
                        // for possible future truncation
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   380
                        if (retainAndCountDroppedElements && !takeElement)
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   381
                            dropCount++;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   382
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   383
                        // If ordered need to process element, otherwise
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   384
                        // skip if element is dropped
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   385
                        if (retainAndCountDroppedElements || takeElement)
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   386
                            downstream.accept(t);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   387
                    }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   388
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   389
                    @Override
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   390
                    public long getDropCount() {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   391
                        return dropCount;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   392
                    }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   393
                }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   394
                return new OpSink();
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   395
            }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   396
        }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   397
        return new Op(upstream, StreamShape.REFERENCE, DROP_FLAGS);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   398
    }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   399
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   400
    /**
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   401
     * Appends a "dropWhile" operation to the provided IntStream.
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   402
     *
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   403
     * @param upstream a reference stream with element type T
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   404
     * @param predicate the predicate that returns false to halt dropping.
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   405
     */
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   406
    static IntStream makeDropWhileInt(AbstractPipeline<?, Integer, ?> upstream,
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   407
                                      IntPredicate predicate) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   408
        Objects.requireNonNull(predicate);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   409
        class Op extends IntPipeline.StatefulOp<Integer> implements DropWhileOp<Integer> {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   410
            public Op(AbstractPipeline<?, Integer, ?> upstream, StreamShape inputShape, int opFlags) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   411
                super(upstream, inputShape, opFlags);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   412
            }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   413
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   414
            @Override
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   415
            <P_IN> Spliterator<Integer> opEvaluateParallelLazy(PipelineHelper<Integer> helper,
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   416
                                                               Spliterator<P_IN> spliterator) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   417
                if (StreamOpFlag.ORDERED.isKnown(helper.getStreamAndOpFlags())) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   418
                    return opEvaluateParallel(helper, spliterator, Integer[]::new)
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   419
                            .spliterator();
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   420
                }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   421
                else {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   422
                    return new UnorderedWhileSpliterator.OfInt.Dropping(
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   423
                            (Spliterator.OfInt) helper.wrapSpliterator(spliterator), false, predicate);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   424
                }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   425
            }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   426
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   427
            @Override
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   428
            <P_IN> Node<Integer> opEvaluateParallel(PipelineHelper<Integer> helper,
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   429
                                                    Spliterator<P_IN> spliterator,
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   430
                                                    IntFunction<Integer[]> generator) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   431
                return new DropWhileTask<>(this, helper, spliterator, generator)
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   432
                        .invoke();
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   433
            }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   434
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   435
            @Override
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   436
            Sink<Integer> opWrapSink(int flags, Sink<Integer> sink) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   437
                return opWrapSink(sink, false);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   438
            }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   439
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   440
            public DropWhileSink<Integer> opWrapSink(Sink<Integer> sink, boolean retainAndCountDroppedElements) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   441
                class OpSink extends Sink.ChainedInt<Integer> implements DropWhileSink<Integer> {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   442
                    long dropCount;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   443
                    boolean take;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   444
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   445
                    OpSink() {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   446
                        super(sink);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   447
                    }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   448
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   449
                    @Override
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   450
                    public void accept(int t) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   451
                        boolean takeElement = take || (take = !predicate.test(t));
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   452
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   453
                        // If ordered and element is dropped increment index
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   454
                        // for possible future truncation
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   455
                        if (retainAndCountDroppedElements && !takeElement)
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   456
                            dropCount++;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   457
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   458
                        // If ordered need to process element, otherwise
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   459
                        // skip if element is dropped
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   460
                        if (retainAndCountDroppedElements || takeElement)
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   461
                            downstream.accept(t);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   462
                    }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   463
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   464
                    @Override
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   465
                    public long getDropCount() {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   466
                        return dropCount;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   467
                    }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   468
                }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   469
                return new OpSink();
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   470
            }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   471
        }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   472
        return new Op(upstream, StreamShape.INT_VALUE, DROP_FLAGS);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   473
    }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   474
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   475
    /**
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   476
     * Appends a "dropWhile" operation to the provided LongStream.
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   477
     *
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   478
     * @param upstream a reference stream with element type T
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   479
     * @param predicate the predicate that returns false to halt dropping.
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   480
     */
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   481
    static LongStream makeDropWhileLong(AbstractPipeline<?, Long, ?> upstream,
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   482
                                        LongPredicate predicate) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   483
        Objects.requireNonNull(predicate);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   484
        class Op extends LongPipeline.StatefulOp<Long> implements DropWhileOp<Long> {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   485
            public Op(AbstractPipeline<?, Long, ?> upstream, StreamShape inputShape, int opFlags) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   486
                super(upstream, inputShape, opFlags);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   487
            }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   488
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   489
            @Override
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   490
            <P_IN> Spliterator<Long> opEvaluateParallelLazy(PipelineHelper<Long> helper,
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   491
                                                            Spliterator<P_IN> spliterator) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   492
                if (StreamOpFlag.ORDERED.isKnown(helper.getStreamAndOpFlags())) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   493
                    return opEvaluateParallel(helper, spliterator, Long[]::new)
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   494
                            .spliterator();
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   495
                }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   496
                else {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   497
                    return new UnorderedWhileSpliterator.OfLong.Dropping(
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   498
                            (Spliterator.OfLong) helper.wrapSpliterator(spliterator), false, predicate);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   499
                }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   500
            }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   501
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   502
            @Override
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   503
            <P_IN> Node<Long> opEvaluateParallel(PipelineHelper<Long> helper,
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   504
                                                 Spliterator<P_IN> spliterator,
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   505
                                                 IntFunction<Long[]> generator) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   506
                return new DropWhileTask<>(this, helper, spliterator, generator)
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   507
                        .invoke();
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   508
            }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   509
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   510
            @Override
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   511
            Sink<Long> opWrapSink(int flags, Sink<Long> sink) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   512
                return opWrapSink(sink, false);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   513
            }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   514
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   515
            public DropWhileSink<Long> opWrapSink(Sink<Long> sink, boolean retainAndCountDroppedElements) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   516
                class OpSink extends Sink.ChainedLong<Long> implements DropWhileSink<Long> {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   517
                    long dropCount;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   518
                    boolean take;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   519
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   520
                    OpSink() {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   521
                        super(sink);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   522
                    }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   523
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   524
                    @Override
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   525
                    public void accept(long t) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   526
                        boolean takeElement = take || (take = !predicate.test(t));
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   527
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   528
                        // If ordered and element is dropped increment index
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   529
                        // for possible future truncation
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   530
                        if (retainAndCountDroppedElements && !takeElement)
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   531
                            dropCount++;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   532
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   533
                        // If ordered need to process element, otherwise
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   534
                        // skip if element is dropped
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   535
                        if (retainAndCountDroppedElements || takeElement)
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   536
                            downstream.accept(t);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   537
                    }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   538
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   539
                    @Override
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   540
                    public long getDropCount() {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   541
                        return dropCount;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   542
                    }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   543
                }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   544
                return new OpSink();
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   545
            }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   546
        }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   547
        return new Op(upstream, StreamShape.LONG_VALUE, DROP_FLAGS);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   548
    }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   549
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   550
    /**
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   551
     * Appends a "dropWhile" operation to the provided DoubleStream.
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   552
     *
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   553
     * @param upstream a reference stream with element type T
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   554
     * @param predicate the predicate that returns false to halt dropping.
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   555
     */
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   556
    static DoubleStream makeDropWhileDouble(AbstractPipeline<?, Double, ?> upstream,
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   557
                                            DoublePredicate predicate) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   558
        Objects.requireNonNull(predicate);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   559
        class Op extends DoublePipeline.StatefulOp<Double> implements DropWhileOp<Double> {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   560
            public Op(AbstractPipeline<?, Double, ?> upstream, StreamShape inputShape, int opFlags) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   561
                super(upstream, inputShape, opFlags);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   562
            }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   563
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   564
            @Override
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   565
            <P_IN> Spliterator<Double> opEvaluateParallelLazy(PipelineHelper<Double> helper,
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   566
                                                              Spliterator<P_IN> spliterator) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   567
                if (StreamOpFlag.ORDERED.isKnown(helper.getStreamAndOpFlags())) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   568
                    return opEvaluateParallel(helper, spliterator, Double[]::new)
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   569
                            .spliterator();
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   570
                }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   571
                else {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   572
                    return new UnorderedWhileSpliterator.OfDouble.Dropping(
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   573
                            (Spliterator.OfDouble) helper.wrapSpliterator(spliterator), false, predicate);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   574
                }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   575
            }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   576
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   577
            @Override
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   578
            <P_IN> Node<Double> opEvaluateParallel(PipelineHelper<Double> helper,
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   579
                                                   Spliterator<P_IN> spliterator,
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   580
                                                   IntFunction<Double[]> generator) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   581
                return new DropWhileTask<>(this, helper, spliterator, generator)
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   582
                        .invoke();
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   583
            }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   584
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   585
            @Override
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   586
            Sink<Double> opWrapSink(int flags, Sink<Double> sink) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   587
                return opWrapSink(sink, false);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   588
            }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   589
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   590
            public DropWhileSink<Double> opWrapSink(Sink<Double> sink, boolean retainAndCountDroppedElements) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   591
                class OpSink extends Sink.ChainedDouble<Double> implements DropWhileSink<Double> {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   592
                    long dropCount;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   593
                    boolean take;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   594
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   595
                    OpSink() {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   596
                        super(sink);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   597
                    }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   598
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   599
                    @Override
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   600
                    public void accept(double t) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   601
                        boolean takeElement = take || (take = !predicate.test(t));
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   602
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   603
                        // If ordered and element is dropped increment index
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   604
                        // for possible future truncation
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   605
                        if (retainAndCountDroppedElements && !takeElement)
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   606
                            dropCount++;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   607
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   608
                        // If ordered need to process element, otherwise
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   609
                        // skip if element is dropped
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   610
                        if (retainAndCountDroppedElements || takeElement)
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   611
                            downstream.accept(t);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   612
                    }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   613
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   614
                    @Override
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   615
                    public long getDropCount() {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   616
                        return dropCount;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   617
                    }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   618
                }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   619
                return new OpSink();
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   620
            }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   621
        }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   622
        return new Op(upstream, StreamShape.DOUBLE_VALUE, DROP_FLAGS);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   623
    }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   624
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   625
    //
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   626
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   627
    /**
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   628
     * A spliterator supporting takeWhile and dropWhile operations over an
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   629
     * underlying spliterator whose covered elements have no encounter order.
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   630
     * <p>
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   631
     * Concrete subclasses of this spliterator support reference and primitive
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   632
     * types for takeWhile and dropWhile.
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   633
     * <p>
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   634
     * For the takeWhile operation if during traversal taking completes then
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   635
     * taking is cancelled globally for the splitting and traversal of all
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   636
     * related spliterators.
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   637
     * Cancellation is governed by a shared {@link AtomicBoolean} instance.  A
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   638
     * spliterator in the process of taking when cancellation occurs will also
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   639
     * be cancelled but not necessarily immediately.  To reduce contention on
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   640
     * the {@link AtomicBoolean} instance, cancellation make be acted on after
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   641
     * a small number of additional elements have been traversed.
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   642
     * <p>
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   643
     * For the dropWhile operation if during traversal dropping completes for
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   644
     * some, but not all elements, then it is cancelled globally for the
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   645
     * traversal of all related spliterators (splitting is not cancelled).
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   646
     * Cancellation is governed in the same manner as for the takeWhile
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   647
     * operation.
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   648
     *
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   649
     * @param <T> the type of elements returned by this spliterator
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   650
     * @param <T_SPLITR> the type of the spliterator
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   651
     */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 31644
diff changeset
   652
    abstract static class UnorderedWhileSpliterator<T, T_SPLITR extends Spliterator<T>> implements Spliterator<T> {
31644
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   653
        // Power of two constant minus one used for modulus of count
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   654
        static final int CANCEL_CHECK_COUNT = (1 << 6) - 1;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   655
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   656
        // The underlying spliterator
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   657
        final T_SPLITR s;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   658
        // True if no splitting should be performed, if true then
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   659
        // this spliterator may be used for an underlying spliterator whose
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   660
        // covered elements have an encounter order
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   661
        // See use in stream take/dropWhile default default methods
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   662
        final boolean noSplitting;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   663
        // True when operations are cancelled for all related spliterators
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   664
        // For taking, spliterators cannot split or traversed
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   665
        // For dropping, spliterators cannot be traversed
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   666
        final AtomicBoolean cancel;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   667
        // True while taking or dropping should be performed when traversing
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   668
        boolean takeOrDrop = true;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   669
        // The count of elements traversed
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   670
        int count;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   671
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   672
        UnorderedWhileSpliterator(T_SPLITR s, boolean noSplitting) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   673
            this.s = s;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   674
            this.noSplitting = noSplitting;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   675
            this.cancel = new AtomicBoolean();
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   676
        }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   677
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   678
        UnorderedWhileSpliterator(T_SPLITR s, UnorderedWhileSpliterator<T, T_SPLITR> parent) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   679
            this.s = s;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   680
            this.noSplitting = parent.noSplitting;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   681
            this.cancel = parent.cancel;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   682
        }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   683
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   684
        @Override
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   685
        public long estimateSize() {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   686
            return s.estimateSize();
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   687
        }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   688
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   689
        @Override
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   690
        public int characteristics() {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   691
            // Size is not known
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   692
            return s.characteristics() & ~(Spliterator.SIZED | Spliterator.SUBSIZED);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   693
        }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   694
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   695
        @Override
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   696
        public long getExactSizeIfKnown() {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   697
            return -1L;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   698
        }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   699
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   700
        @Override
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   701
        public Comparator<? super T> getComparator() {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   702
            return s.getComparator();
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   703
        }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   704
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   705
        @Override
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   706
        public T_SPLITR trySplit() {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   707
            @SuppressWarnings("unchecked")
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   708
            T_SPLITR ls = noSplitting ? null : (T_SPLITR) s.trySplit();
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   709
            return ls != null ? makeSpliterator(ls) : null;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   710
        }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   711
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   712
        boolean checkCancelOnCount() {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   713
            return count != 0 || !cancel.get();
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   714
        }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   715
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   716
        abstract T_SPLITR makeSpliterator(T_SPLITR s);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   717
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 31644
diff changeset
   718
        abstract static class OfRef<T> extends UnorderedWhileSpliterator<T, Spliterator<T>> implements Consumer<T> {
31644
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   719
            final Predicate<? super T> p;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   720
            T t;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   721
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   722
            OfRef(Spliterator<T> s, boolean noSplitting, Predicate<? super T> p) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   723
                super(s, noSplitting);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   724
                this.p = p;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   725
            }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   726
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   727
            OfRef(Spliterator<T> s, OfRef<T> parent) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   728
                super(s, parent);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   729
                this.p = parent.p;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   730
            }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   731
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   732
            @Override
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   733
            public void accept(T t) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   734
                count = (count + 1) & CANCEL_CHECK_COUNT;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   735
                this.t = t;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   736
            }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   737
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   738
            static final class Taking<T> extends OfRef<T> {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   739
                Taking(Spliterator<T> s, boolean noSplitting, Predicate<? super T> p) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   740
                    super(s, noSplitting, p);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   741
                }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   742
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   743
                Taking(Spliterator<T> s, Taking<T> parent) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   744
                    super(s, parent);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   745
                }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   746
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   747
                @Override
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   748
                public boolean tryAdvance(Consumer<? super T> action) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   749
                    boolean test = true;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   750
                    if (takeOrDrop &&               // If can take
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   751
                        checkCancelOnCount() && // and if not cancelled
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   752
                        s.tryAdvance(this) &&   // and if advanced one element
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   753
                        (test = p.test(t))) {   // and test on element passes
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   754
                        action.accept(t);           // then accept element
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   755
                        return true;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   756
                    }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   757
                    else {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   758
                        // Taking is finished
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   759
                        takeOrDrop = false;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   760
                        // Cancel all further traversal and splitting operations
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   761
                        // only if test of element failed (short-circuited)
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   762
                        if (!test)
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   763
                            cancel.set(true);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   764
                        return false;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   765
                    }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   766
                }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   767
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   768
                @Override
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   769
                public Spliterator<T> trySplit() {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   770
                    // Do not split if all operations are cancelled
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   771
                    return cancel.get() ? null : super.trySplit();
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   772
                }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   773
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   774
                @Override
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   775
                Spliterator<T> makeSpliterator(Spliterator<T> s) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   776
                    return new Taking<>(s, this);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   777
                }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   778
            }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   779
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   780
            static final class Dropping<T> extends OfRef<T> {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   781
                Dropping(Spliterator<T> s, boolean noSplitting, Predicate<? super T> p) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   782
                    super(s, noSplitting, p);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   783
                }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   784
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   785
                Dropping(Spliterator<T> s, Dropping<T> parent) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   786
                    super(s, parent);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   787
                }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   788
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   789
                @Override
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   790
                public boolean tryAdvance(Consumer<? super T> action) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   791
                    if (takeOrDrop) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   792
                        takeOrDrop = false;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   793
                        boolean adv;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   794
                        boolean dropped = false;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   795
                        while ((adv = s.tryAdvance(this)) &&  // If advanced one element
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   796
                               checkCancelOnCount() &&        // and if not cancelled
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   797
                               p.test(t)) {                   // and test on element passes
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   798
                            dropped = true;                   // then drop element
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   799
                        }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   800
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   801
                        // Report advanced element, if any
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   802
                        if (adv) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   803
                            // Cancel all further dropping if one or more elements
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   804
                            // were previously dropped
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   805
                            if (dropped)
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   806
                                cancel.set(true);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   807
                            action.accept(t);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   808
                        }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   809
                        return adv;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   810
                    }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   811
                    else {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   812
                        return s.tryAdvance(action);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   813
                    }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   814
                }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   815
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   816
                @Override
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   817
                Spliterator<T> makeSpliterator(Spliterator<T> s) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   818
                    return new Dropping<>(s, this);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   819
                }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   820
            }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   821
        }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   822
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 31644
diff changeset
   823
        abstract static class OfInt extends UnorderedWhileSpliterator<Integer, Spliterator.OfInt> implements IntConsumer, Spliterator.OfInt {
31644
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   824
            final IntPredicate p;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   825
            int t;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   826
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   827
            OfInt(Spliterator.OfInt s, boolean noSplitting, IntPredicate p) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   828
                super(s, noSplitting);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   829
                this.p = p;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   830
            }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   831
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   832
            OfInt(Spliterator.OfInt s, UnorderedWhileSpliterator.OfInt parent) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   833
                super(s, parent);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   834
                this.p = parent.p;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   835
            }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   836
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   837
            @Override
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   838
            public void accept(int t) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   839
                count = (count + 1) & CANCEL_CHECK_COUNT;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   840
                this.t = t;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   841
            }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   842
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   843
            static final class Taking extends UnorderedWhileSpliterator.OfInt {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   844
                Taking(Spliterator.OfInt s, boolean noSplitting, IntPredicate p) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   845
                    super(s, noSplitting, p);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   846
                }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   847
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   848
                Taking(Spliterator.OfInt s, UnorderedWhileSpliterator.OfInt parent) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   849
                    super(s, parent);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   850
                }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   851
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   852
                @Override
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   853
                public boolean tryAdvance(IntConsumer action) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   854
                    boolean test = true;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   855
                    if (takeOrDrop &&               // If can take
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   856
                        checkCancelOnCount() && // and if not cancelled
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   857
                        s.tryAdvance(this) &&   // and if advanced one element
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   858
                        (test = p.test(t))) {   // and test on element passes
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   859
                        action.accept(t);           // then accept element
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   860
                        return true;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   861
                    }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   862
                    else {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   863
                        // Taking is finished
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   864
                        takeOrDrop = false;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   865
                        // Cancel all further traversal and splitting operations
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   866
                        // only if test of element failed (short-circuited)
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   867
                        if (!test)
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   868
                            cancel.set(true);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   869
                        return false;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   870
                    }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   871
                }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   872
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   873
                @Override
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   874
                public Spliterator.OfInt trySplit() {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   875
                    // Do not split if all operations are cancelled
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   876
                    return cancel.get() ? null : super.trySplit();
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   877
                }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   878
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   879
                @Override
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   880
                Spliterator.OfInt makeSpliterator(Spliterator.OfInt s) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   881
                    return new Taking(s, this);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   882
                }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   883
            }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   884
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   885
            static final class Dropping extends UnorderedWhileSpliterator.OfInt {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   886
                Dropping(Spliterator.OfInt s, boolean noSplitting, IntPredicate p) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   887
                    super(s, noSplitting, p);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   888
                }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   889
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   890
                Dropping(Spliterator.OfInt s, UnorderedWhileSpliterator.OfInt parent) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   891
                    super(s, parent);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   892
                }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   893
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   894
                @Override
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   895
                public boolean tryAdvance(IntConsumer action) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   896
                    if (takeOrDrop) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   897
                        takeOrDrop = false;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   898
                        boolean adv;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   899
                        boolean dropped = false;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   900
                        while ((adv = s.tryAdvance(this)) &&  // If advanced one element
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   901
                               checkCancelOnCount() &&        // and if not cancelled
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   902
                               p.test(t)) {                   // and test on element passes
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   903
                            dropped = true;                   // then drop element
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   904
                        }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   905
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   906
                        // Report advanced element, if any
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   907
                        if (adv) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   908
                            // Cancel all further dropping if one or more elements
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   909
                            // were previously dropped
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   910
                            if (dropped)
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   911
                                cancel.set(true);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   912
                            action.accept(t);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   913
                        }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   914
                        return adv;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   915
                    }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   916
                    else {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   917
                        return s.tryAdvance(action);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   918
                    }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   919
                }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   920
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   921
                @Override
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   922
                Spliterator.OfInt makeSpliterator(Spliterator.OfInt s) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   923
                    return new Dropping(s, this);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   924
                }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   925
            }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   926
        }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   927
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 31644
diff changeset
   928
        abstract static class OfLong extends UnorderedWhileSpliterator<Long, Spliterator.OfLong> implements LongConsumer, Spliterator.OfLong {
31644
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   929
            final LongPredicate p;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   930
            long t;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   931
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   932
            OfLong(Spliterator.OfLong s, boolean noSplitting, LongPredicate p) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   933
                super(s, noSplitting);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   934
                this.p = p;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   935
            }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   936
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   937
            OfLong(Spliterator.OfLong s, UnorderedWhileSpliterator.OfLong parent) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   938
                super(s, parent);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   939
                this.p = parent.p;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   940
            }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   941
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   942
            @Override
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   943
            public void accept(long t) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   944
                count = (count + 1) & CANCEL_CHECK_COUNT;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   945
                this.t = t;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   946
            }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   947
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   948
            static final class Taking extends UnorderedWhileSpliterator.OfLong {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   949
                Taking(Spliterator.OfLong s, boolean noSplitting, LongPredicate p) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   950
                    super(s, noSplitting, p);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   951
                }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   952
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   953
                Taking(Spliterator.OfLong s, UnorderedWhileSpliterator.OfLong parent) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   954
                    super(s, parent);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   955
                }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   956
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   957
                @Override
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   958
                public boolean tryAdvance(LongConsumer action) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   959
                    boolean test = true;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   960
                    if (takeOrDrop &&               // If can take
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   961
                        checkCancelOnCount() && // and if not cancelled
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   962
                        s.tryAdvance(this) &&   // and if advanced one element
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   963
                        (test = p.test(t))) {   // and test on element passes
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   964
                        action.accept(t);           // then accept element
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   965
                        return true;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   966
                    }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   967
                    else {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   968
                        // Taking is finished
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   969
                        takeOrDrop = false;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   970
                        // Cancel all further traversal and splitting operations
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   971
                        // only if test of element failed (short-circuited)
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   972
                        if (!test)
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   973
                            cancel.set(true);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   974
                        return false;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   975
                    }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   976
                }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   977
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   978
                @Override
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   979
                public Spliterator.OfLong trySplit() {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   980
                    // Do not split if all operations are cancelled
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   981
                    return cancel.get() ? null : super.trySplit();
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   982
                }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   983
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   984
                @Override
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   985
                Spliterator.OfLong makeSpliterator(Spliterator.OfLong s) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   986
                    return new Taking(s, this);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   987
                }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   988
            }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   989
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   990
            static final class Dropping extends UnorderedWhileSpliterator.OfLong {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   991
                Dropping(Spliterator.OfLong s, boolean noSplitting, LongPredicate p) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   992
                    super(s, noSplitting, p);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   993
                }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   994
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   995
                Dropping(Spliterator.OfLong s, UnorderedWhileSpliterator.OfLong parent) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   996
                    super(s, parent);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   997
                }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   998
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   999
                @Override
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1000
                public boolean tryAdvance(LongConsumer action) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1001
                    if (takeOrDrop) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1002
                        takeOrDrop = false;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1003
                        boolean adv;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1004
                        boolean dropped = false;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1005
                        while ((adv = s.tryAdvance(this)) &&  // If advanced one element
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1006
                               checkCancelOnCount() &&        // and if not cancelled
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1007
                               p.test(t)) {                   // and test on element passes
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1008
                            dropped = true;                   // then drop element
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1009
                        }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1010
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1011
                        // Report advanced element, if any
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1012
                        if (adv) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1013
                            // Cancel all further dropping if one or more elements
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1014
                            // were previously dropped
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1015
                            if (dropped)
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1016
                                cancel.set(true);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1017
                            action.accept(t);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1018
                        }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1019
                        return adv;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1020
                    }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1021
                    else {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1022
                        return s.tryAdvance(action);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1023
                    }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1024
                }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1025
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1026
                @Override
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1027
                Spliterator.OfLong makeSpliterator(Spliterator.OfLong s) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1028
                    return new Dropping(s, this);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1029
                }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1030
            }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1031
        }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1032
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 31644
diff changeset
  1033
        abstract static class OfDouble extends UnorderedWhileSpliterator<Double, Spliterator.OfDouble> implements DoubleConsumer, Spliterator.OfDouble {
31644
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1034
            final DoublePredicate p;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1035
            double t;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1036
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1037
            OfDouble(Spliterator.OfDouble s, boolean noSplitting, DoublePredicate p) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1038
                super(s, noSplitting);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1039
                this.p = p;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1040
            }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1041
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1042
            OfDouble(Spliterator.OfDouble s, UnorderedWhileSpliterator.OfDouble parent) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1043
                super(s, parent);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1044
                this.p = parent.p;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1045
            }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1046
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1047
            @Override
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1048
            public void accept(double t) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1049
                count = (count + 1) & CANCEL_CHECK_COUNT;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1050
                this.t = t;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1051
            }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1052
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1053
            static final class Taking extends UnorderedWhileSpliterator.OfDouble {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1054
                Taking(Spliterator.OfDouble s, boolean noSplitting, DoublePredicate p) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1055
                    super(s, noSplitting, p);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1056
                }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1057
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1058
                Taking(Spliterator.OfDouble s, UnorderedWhileSpliterator.OfDouble parent) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1059
                    super(s, parent);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1060
                }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1061
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1062
                @Override
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1063
                public boolean tryAdvance(DoubleConsumer action) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1064
                    boolean test = true;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1065
                    if (takeOrDrop &&               // If can take
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1066
                        checkCancelOnCount() && // and if not cancelled
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1067
                        s.tryAdvance(this) &&   // and if advanced one element
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1068
                        (test = p.test(t))) {   // and test on element passes
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1069
                        action.accept(t);           // then accept element
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1070
                        return true;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1071
                    }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1072
                    else {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1073
                        // Taking is finished
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1074
                        takeOrDrop = false;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1075
                        // Cancel all further traversal and splitting operations
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1076
                        // only if test of element failed (short-circuited)
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1077
                        if (!test)
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1078
                            cancel.set(true);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1079
                        return false;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1080
                    }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1081
                }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1082
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1083
                @Override
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1084
                public Spliterator.OfDouble trySplit() {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1085
                    // Do not split if all operations are cancelled
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1086
                    return cancel.get() ? null : super.trySplit();
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1087
                }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1088
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1089
                @Override
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1090
                Spliterator.OfDouble makeSpliterator(Spliterator.OfDouble s) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1091
                    return new Taking(s, this);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1092
                }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1093
            }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1094
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1095
            static final class Dropping extends UnorderedWhileSpliterator.OfDouble {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1096
                Dropping(Spliterator.OfDouble s, boolean noSplitting, DoublePredicate p) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1097
                    super(s, noSplitting, p);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1098
                }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1099
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1100
                Dropping(Spliterator.OfDouble s, UnorderedWhileSpliterator.OfDouble parent) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1101
                    super(s, parent);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1102
                }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1103
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1104
                @Override
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1105
                public boolean tryAdvance(DoubleConsumer action) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1106
                    if (takeOrDrop) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1107
                        takeOrDrop = false;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1108
                        boolean adv;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1109
                        boolean dropped = false;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1110
                        while ((adv = s.tryAdvance(this)) &&  // If advanced one element
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1111
                               checkCancelOnCount() &&        // and if not cancelled
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1112
                               p.test(t)) {                   // and test on element passes
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1113
                            dropped = true;                   // then drop element
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1114
                        }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1115
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1116
                        // Report advanced element, if any
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1117
                        if (adv) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1118
                            // Cancel all further dropping if one or more elements
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1119
                            // were previously dropped
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1120
                            if (dropped)
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1121
                                cancel.set(true);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1122
                            action.accept(t);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1123
                        }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1124
                        return adv;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1125
                    }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1126
                    else {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1127
                        return s.tryAdvance(action);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1128
                    }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1129
                }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1130
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1131
                @Override
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1132
                Spliterator.OfDouble makeSpliterator(Spliterator.OfDouble s) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1133
                    return new Dropping(s, this);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1134
                }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1135
            }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1136
        }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1137
    }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1138
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1139
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1140
    //
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1141
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1142
    /**
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1143
     * {@code ForkJoinTask} implementing takeWhile computation.
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1144
     * <p>
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1145
     * If the pipeline has encounter order then all tasks to the right of
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1146
     * a task where traversal was short-circuited are cancelled.
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1147
     * The results of completed (and cancelled) tasks are discarded.
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1148
     * The result of merging a short-circuited left task and right task (which
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1149
     * may or may not be short-circuited) is that left task.
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1150
     * <p>
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1151
     * If the pipeline has no encounter order then all tasks to the right of
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1152
     * a task where traversal was short-circuited are cancelled.
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1153
     * The results of completed (and possibly cancelled) tasks are not
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1154
     * discarded, as there is no need to throw away computed results.
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1155
     * The result of merging does not change if a left task was
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1156
     * short-circuited.
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1157
     * No attempt is made, once a leaf task stopped taking, for it to cancel
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1158
     * all other tasks, and further more, short-circuit the computation with its
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1159
     * result.
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1160
     *
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1161
     * @param <P_IN> Input element type to the stream pipeline
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1162
     * @param <P_OUT> Output element type from the stream pipeline
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1163
     */
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1164
    @SuppressWarnings("serial")
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1165
    private static final class TakeWhileTask<P_IN, P_OUT>
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1166
            extends AbstractShortCircuitTask<P_IN, P_OUT, Node<P_OUT>, TakeWhileTask<P_IN, P_OUT>> {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1167
        private final AbstractPipeline<P_OUT, P_OUT, ?> op;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1168
        private final IntFunction<P_OUT[]> generator;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1169
        private final boolean isOrdered;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1170
        private long thisNodeSize;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1171
        // True if a short-circuited
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1172
        private boolean shortCircuited;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1173
        // True if completed, must be set after the local result
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1174
        private volatile boolean completed;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1175
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1176
        TakeWhileTask(AbstractPipeline<P_OUT, P_OUT, ?> op,
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1177
                      PipelineHelper<P_OUT> helper,
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1178
                      Spliterator<P_IN> spliterator,
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1179
                      IntFunction<P_OUT[]> generator) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1180
            super(helper, spliterator);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1181
            this.op = op;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1182
            this.generator = generator;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1183
            this.isOrdered = StreamOpFlag.ORDERED.isKnown(helper.getStreamAndOpFlags());
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1184
        }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1185
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1186
        TakeWhileTask(TakeWhileTask<P_IN, P_OUT> parent, Spliterator<P_IN> spliterator) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1187
            super(parent, spliterator);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1188
            this.op = parent.op;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1189
            this.generator = parent.generator;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1190
            this.isOrdered = parent.isOrdered;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1191
        }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1192
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1193
        @Override
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1194
        protected TakeWhileTask<P_IN, P_OUT> makeChild(Spliterator<P_IN> spliterator) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1195
            return new TakeWhileTask<>(this, spliterator);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1196
        }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1197
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1198
        @Override
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1199
        protected final Node<P_OUT> getEmptyResult() {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1200
            return Nodes.emptyNode(op.getOutputShape());
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1201
        }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1202
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1203
        @Override
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1204
        protected final Node<P_OUT> doLeaf() {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1205
            Node.Builder<P_OUT> builder = helper.makeNodeBuilder(-1, generator);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1206
            Sink<P_OUT> s = op.opWrapSink(helper.getStreamAndOpFlags(), builder);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1207
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1208
            if (shortCircuited = helper.copyIntoWithCancel(helper.wrapSink(s), spliterator)) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1209
                // Cancel later nodes if the predicate returned false
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1210
                // during traversal
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1211
                cancelLaterNodes();
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1212
            }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1213
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1214
            Node<P_OUT> node = builder.build();
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1215
            thisNodeSize = node.count();
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1216
            return node;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1217
        }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1218
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1219
        @Override
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1220
        public final void onCompletion(CountedCompleter<?> caller) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1221
            if (!isLeaf()) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1222
                Node<P_OUT> result;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1223
                shortCircuited = leftChild.shortCircuited | rightChild.shortCircuited;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1224
                if (isOrdered && canceled) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1225
                    thisNodeSize = 0;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1226
                    result = getEmptyResult();
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1227
                }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1228
                else if (isOrdered && leftChild.shortCircuited) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1229
                    // If taking finished on the left node then
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1230
                    // use the left node result
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1231
                    thisNodeSize = leftChild.thisNodeSize;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1232
                    result = leftChild.getLocalResult();
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1233
                }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1234
                else {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1235
                    thisNodeSize = leftChild.thisNodeSize + rightChild.thisNodeSize;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1236
                    result = merge();
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1237
                }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1238
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1239
                setLocalResult(result);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1240
            }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1241
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1242
            completed = true;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1243
            super.onCompletion(caller);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1244
        }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1245
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1246
        Node<P_OUT> merge() {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1247
            if (leftChild.thisNodeSize == 0) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1248
                // If the left node size is 0 then
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1249
                // use the right node result
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1250
                return rightChild.getLocalResult();
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1251
            }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1252
            else if (rightChild.thisNodeSize == 0) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1253
                // If the right node size is 0 then
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1254
                // use the left node result
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1255
                return leftChild.getLocalResult();
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1256
            }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1257
            else {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1258
                // Combine the left and right nodes
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1259
                return Nodes.conc(op.getOutputShape(),
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1260
                                  leftChild.getLocalResult(), rightChild.getLocalResult());
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1261
            }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1262
        }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1263
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1264
        @Override
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1265
        protected void cancel() {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1266
            super.cancel();
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1267
            if (isOrdered && completed)
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1268
                // If the task is completed then clear the result, if any
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1269
                // to aid GC
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1270
                setLocalResult(getEmptyResult());
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1271
        }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1272
    }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1273
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1274
    /**
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1275
     * {@code ForkJoinTask} implementing dropWhile computation.
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1276
     * <p>
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1277
     * If the pipeline has encounter order then each leaf task will not
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1278
     * drop elements but will obtain a count of the elements that would have
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1279
     * been otherwise dropped. That count is used as an index to track
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1280
     * elements to be dropped. Merging will update the index so it corresponds
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1281
     * to the index that is the end of the global prefix of elements to be
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1282
     * dropped. The root is truncated according to that index.
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1283
     * <p>
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1284
     * If the pipeline has no encounter order then each leaf task will drop
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1285
     * elements. Leaf tasks are ordinarily merged. No truncation of the root
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1286
     * node is required.
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1287
     * No attempt is made, once a leaf task stopped dropping, for it to cancel
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1288
     * all other tasks, and further more, short-circuit the computation with
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1289
     * its result.
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1290
     *
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1291
     * @param <P_IN> Input element type to the stream pipeline
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1292
     * @param <P_OUT> Output element type from the stream pipeline
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1293
     */
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1294
    @SuppressWarnings("serial")
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1295
    private static final class DropWhileTask<P_IN, P_OUT>
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1296
            extends AbstractTask<P_IN, P_OUT, Node<P_OUT>, DropWhileTask<P_IN, P_OUT>> {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1297
        private final AbstractPipeline<P_OUT, P_OUT, ?> op;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1298
        private final IntFunction<P_OUT[]> generator;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1299
        private final boolean isOrdered;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1300
        private long thisNodeSize;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1301
        // The index from which elements of the node should be taken
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1302
        // i.e. the node should be truncated from [takeIndex, thisNodeSize)
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1303
        // Equivalent to the count of dropped elements
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1304
        private long index;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1305
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1306
        DropWhileTask(AbstractPipeline<P_OUT, P_OUT, ?> op,
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1307
                      PipelineHelper<P_OUT> helper,
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1308
                      Spliterator<P_IN> spliterator,
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1309
                      IntFunction<P_OUT[]> generator) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1310
            super(helper, spliterator);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1311
            assert op instanceof DropWhileOp;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1312
            this.op = op;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1313
            this.generator = generator;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1314
            this.isOrdered = StreamOpFlag.ORDERED.isKnown(helper.getStreamAndOpFlags());
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1315
        }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1316
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1317
        DropWhileTask(DropWhileTask<P_IN, P_OUT> parent, Spliterator<P_IN> spliterator) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1318
            super(parent, spliterator);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1319
            this.op = parent.op;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1320
            this.generator = parent.generator;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1321
            this.isOrdered = parent.isOrdered;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1322
        }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1323
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1324
        @Override
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1325
        protected DropWhileTask<P_IN, P_OUT> makeChild(Spliterator<P_IN> spliterator) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1326
            return new DropWhileTask<>(this, spliterator);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1327
        }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1328
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1329
        @Override
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1330
        protected final Node<P_OUT> doLeaf() {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1331
            boolean isChild = !isRoot();
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1332
            // If this not the root and pipeline is ordered and size is known
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1333
            // then pre-size the builder
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1334
            long sizeIfKnown = isChild && isOrdered && StreamOpFlag.SIZED.isPreserved(op.sourceOrOpFlags)
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1335
                               ? op.exactOutputSizeIfKnown(spliterator)
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1336
                               : -1;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1337
            Node.Builder<P_OUT> builder = helper.makeNodeBuilder(sizeIfKnown, generator);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1338
            @SuppressWarnings("unchecked")
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1339
            DropWhileOp<P_OUT> dropOp = (DropWhileOp<P_OUT>) op;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1340
            // If this leaf is the root then there is no merging on completion
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1341
            // and there is no need to retain dropped elements
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1342
            DropWhileSink<P_OUT> s = dropOp.opWrapSink(builder, isOrdered && isChild);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1343
            helper.wrapAndCopyInto(s, spliterator);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1344
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1345
            Node<P_OUT> node = builder.build();
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1346
            thisNodeSize = node.count();
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1347
            index = s.getDropCount();
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1348
            return node;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1349
        }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1350
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1351
        @Override
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1352
        public final void onCompletion(CountedCompleter<?> caller) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1353
            if (!isLeaf()) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1354
                if (isOrdered) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1355
                    index = leftChild.index;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1356
                    // If a contiguous sequence of dropped elements
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1357
                    // include those of the right node, if any
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1358
                    if (index == leftChild.thisNodeSize)
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1359
                        index += rightChild.index;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1360
                }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1361
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1362
                thisNodeSize = leftChild.thisNodeSize + rightChild.thisNodeSize;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1363
                Node<P_OUT> result = merge();
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1364
                setLocalResult(isRoot() ? doTruncate(result) : result);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1365
            }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1366
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1367
            super.onCompletion(caller);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1368
        }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1369
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1370
        private Node<P_OUT> merge() {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1371
            if (leftChild.thisNodeSize == 0) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1372
                // If the left node size is 0 then
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1373
                // use the right node result
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1374
                return rightChild.getLocalResult();
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1375
            }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1376
            else if (rightChild.thisNodeSize == 0) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1377
                // If the right node size is 0 then
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1378
                // use the left node result
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1379
                return leftChild.getLocalResult();
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1380
            }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1381
            else {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1382
                // Combine the left and right nodes
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1383
                return Nodes.conc(op.getOutputShape(),
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1384
                                  leftChild.getLocalResult(), rightChild.getLocalResult());
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1385
            }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1386
        }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1387
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1388
        private Node<P_OUT> doTruncate(Node<P_OUT> input) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1389
            return isOrdered
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1390
                   ? input.truncate(index, input.count(), generator)
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1391
                   : input;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1392
        }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1393
    }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
  1394
}