jdk/src/share/classes/java/util/stream/StreamSpliterators.java
author psandoz
Thu, 16 Jan 2014 18:20:31 +0100
changeset 22289 bb9c71b84919
parent 21422 6fca66995a27
child 22297 1c62c67d9dd2
permissions -rw-r--r--
8029452: Fork/Join task ForEachOps.ForEachOrderedTask clarifications and minor improvements Reviewed-by: mduigou, briangoetz
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
     1
/*
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
     2
 * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
     4
 *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    10
 *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    15
 * accompanied this code).
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    16
 *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    20
 *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    23
 * questions.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    24
 */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    25
package java.util.stream;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    26
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    27
import java.util.Comparator;
19188
bbf287c5cd92 8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents: 18572
diff changeset
    28
import java.util.Objects;
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    29
import java.util.Spliterator;
21422
6fca66995a27 8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents: 19850
diff changeset
    30
import java.util.concurrent.ConcurrentHashMap;
18572
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
    31
import java.util.concurrent.atomic.AtomicLong;
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    32
import java.util.function.BooleanSupplier;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    33
import java.util.function.Consumer;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    34
import java.util.function.DoubleConsumer;
18572
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
    35
import java.util.function.DoubleSupplier;
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    36
import java.util.function.IntConsumer;
18572
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
    37
import java.util.function.IntSupplier;
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    38
import java.util.function.LongConsumer;
18572
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
    39
import java.util.function.LongSupplier;
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    40
import java.util.function.Supplier;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    41
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    42
/**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    43
 * Spliterator implementations for wrapping and delegating spliterators, used
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    44
 * in the implementation of the {@link Stream#spliterator()} method.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    45
 *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    46
 * @since 1.8
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    47
 */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    48
class StreamSpliterators {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    49
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    50
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    51
     * Abstract wrapping spliterator that binds to the spliterator of a
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    52
     * pipeline helper on first operation.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    53
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    54
     * <p>This spliterator is not late-binding and will bind to the source
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    55
     * spliterator when first operated on.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    56
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    57
     * <p>A wrapping spliterator produced from a sequential stream
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    58
     * cannot be split if there are stateful operations present.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    59
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    60
    private static abstract class AbstractWrappingSpliterator<P_IN, P_OUT,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    61
                                                              T_BUFFER extends AbstractSpinedBuffer>
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    62
            implements Spliterator<P_OUT> {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    63
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    64
        // @@@ Detect if stateful operations are present or not
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    65
        //     If not then can split otherwise cannot
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    66
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    67
        /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    68
         * True if this spliterator supports splitting
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    69
         */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    70
        final boolean isParallel;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    71
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    72
        final PipelineHelper<P_OUT> ph;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    73
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    74
        /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    75
         * Supplier for the source spliterator.  Client provides either a
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    76
         * spliterator or a supplier.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    77
         */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    78
        private Supplier<Spliterator<P_IN>> spliteratorSupplier;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    79
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    80
        /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    81
         * Source spliterator.  Either provided from client or obtained from
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    82
         * supplier.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    83
         */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    84
        Spliterator<P_IN> spliterator;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    85
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    86
        /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    87
         * Sink chain for the downstream stages of the pipeline, ultimately
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    88
         * leading to the buffer. Used during partial traversal.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    89
         */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    90
        Sink<P_IN> bufferSink;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    91
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    92
        /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    93
         * A function that advances one element of the spliterator, pushing
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    94
         * it to bufferSink.  Returns whether any elements were processed.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    95
         * Used during partial traversal.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    96
         */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    97
        BooleanSupplier pusher;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    98
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    99
        /** Next element to consume from the buffer, used during partial traversal */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   100
        long nextToConsume;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   101
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   102
        /** Buffer into which elements are pushed.  Used during partial traversal. */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   103
        T_BUFFER buffer;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   104
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   105
        /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   106
         * True if full traversal has occurred (with possible cancelation).
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   107
         * If doing a partial traversal, there may be still elements in buffer.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   108
         */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   109
        boolean finished;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   110
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   111
        /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   112
         * Construct an AbstractWrappingSpliterator from a
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   113
         * {@code Supplier<Spliterator>}.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   114
         */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   115
        AbstractWrappingSpliterator(PipelineHelper<P_OUT> ph,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   116
                                    Supplier<Spliterator<P_IN>> spliteratorSupplier,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   117
                                    boolean parallel) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   118
            this.ph = ph;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   119
            this.spliteratorSupplier = spliteratorSupplier;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   120
            this.spliterator = null;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   121
            this.isParallel = parallel;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   122
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   123
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   124
        /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   125
         * Construct an AbstractWrappingSpliterator from a
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   126
         * {@code Spliterator}.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   127
         */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   128
        AbstractWrappingSpliterator(PipelineHelper<P_OUT> ph,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   129
                                    Spliterator<P_IN> spliterator,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   130
                                    boolean parallel) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   131
            this.ph = ph;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   132
            this.spliteratorSupplier = null;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   133
            this.spliterator = spliterator;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   134
            this.isParallel = parallel;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   135
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   136
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   137
        /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   138
         * Called before advancing to set up spliterator, if needed.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   139
         */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   140
        final void init() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   141
            if (spliterator == null) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   142
                spliterator = spliteratorSupplier.get();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   143
                spliteratorSupplier = null;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   144
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   145
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   146
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   147
        /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   148
         * Get an element from the source, pushing it into the sink chain,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   149
         * setting up the buffer if needed
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   150
         * @return whether there are elements to consume from the buffer
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   151
         */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   152
        final boolean doAdvance() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   153
            if (buffer == null) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   154
                if (finished)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   155
                    return false;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   156
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   157
                init();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   158
                initPartialTraversalState();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   159
                nextToConsume = 0;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   160
                bufferSink.begin(spliterator.getExactSizeIfKnown());
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   161
                return fillBuffer();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   162
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   163
            else {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   164
                ++nextToConsume;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   165
                boolean hasNext = nextToConsume < buffer.count();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   166
                if (!hasNext) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   167
                    nextToConsume = 0;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   168
                    buffer.clear();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   169
                    hasNext = fillBuffer();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   170
                }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   171
                return hasNext;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   172
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   173
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   174
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   175
        /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   176
         * Invokes the shape-specific constructor with the provided arguments
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   177
         * and returns the result.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   178
         */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   179
        abstract AbstractWrappingSpliterator<P_IN, P_OUT, ?> wrap(Spliterator<P_IN> s);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   180
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   181
        /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   182
         * Initializes buffer, sink chain, and pusher for a shape-specific
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   183
         * implementation.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   184
         */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   185
        abstract void initPartialTraversalState();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   186
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   187
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   188
        public Spliterator<P_OUT> trySplit() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   189
            if (isParallel && !finished) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   190
                init();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   191
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   192
                Spliterator<P_IN> split = spliterator.trySplit();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   193
                return (split == null) ? null : wrap(split);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   194
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   195
            else
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   196
                return null;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   197
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   198
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   199
        /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   200
         * If the buffer is empty, push elements into the sink chain until
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   201
         * the source is empty or cancellation is requested.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   202
         * @return whether there are elements to consume from the buffer
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   203
         */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   204
        private boolean fillBuffer() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   205
            while (buffer.count() == 0) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   206
                if (bufferSink.cancellationRequested() || !pusher.getAsBoolean()) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   207
                    if (finished)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   208
                        return false;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   209
                    else {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   210
                        bufferSink.end(); // might trigger more elements
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   211
                        finished = true;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   212
                    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   213
                }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   214
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   215
            return true;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   216
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   217
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   218
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   219
        public final long estimateSize() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   220
            init();
18572
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   221
            // Use the estimate of the wrapped spliterator
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   222
            // Note this may not be accurate if there are filter/flatMap
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   223
            // operations filtering or adding elements to the stream
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   224
            return spliterator.estimateSize();
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   225
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   226
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   227
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   228
        public final long getExactSizeIfKnown() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   229
            init();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   230
            return StreamOpFlag.SIZED.isKnown(ph.getStreamAndOpFlags())
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   231
                   ? spliterator.getExactSizeIfKnown()
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   232
                   : -1;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   233
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   234
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   235
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   236
        public final int characteristics() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   237
            init();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   238
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   239
            // Get the characteristics from the pipeline
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   240
            int c = StreamOpFlag.toCharacteristics(StreamOpFlag.toStreamFlags(ph.getStreamAndOpFlags()));
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   241
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   242
            // Mask off the size and uniform characteristics and replace with
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   243
            // those of the spliterator
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   244
            // Note that a non-uniform spliterator can change from something
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   245
            // with an exact size to an estimate for a sub-split, for example
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   246
            // with HashSet where the size is known at the top level spliterator
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   247
            // but for sub-splits only an estimate is known
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   248
            if ((c & Spliterator.SIZED) != 0) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   249
                c &= ~(Spliterator.SIZED | Spliterator.SUBSIZED);
18572
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   250
                c |= (spliterator.characteristics() & (Spliterator.SIZED | Spliterator.SUBSIZED));
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   251
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   252
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   253
            return c;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   254
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   255
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   256
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   257
        public Comparator<? super P_OUT> getComparator() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   258
            if (!hasCharacteristics(SORTED))
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   259
                throw new IllegalStateException();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   260
            return null;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   261
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   262
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   263
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   264
        public final String toString() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   265
            return String.format("%s[%s]", getClass().getName(), spliterator);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   266
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   267
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   268
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   269
    static final class WrappingSpliterator<P_IN, P_OUT>
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   270
            extends AbstractWrappingSpliterator<P_IN, P_OUT, SpinedBuffer<P_OUT>> {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   271
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   272
        WrappingSpliterator(PipelineHelper<P_OUT> ph,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   273
                            Supplier<Spliterator<P_IN>> supplier,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   274
                            boolean parallel) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   275
            super(ph, supplier, parallel);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   276
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   277
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   278
        WrappingSpliterator(PipelineHelper<P_OUT> ph,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   279
                            Spliterator<P_IN> spliterator,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   280
                            boolean parallel) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   281
            super(ph, spliterator, parallel);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   282
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   283
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   284
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   285
        WrappingSpliterator<P_IN, P_OUT> wrap(Spliterator<P_IN> s) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   286
            return new WrappingSpliterator<>(ph, s, isParallel);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   287
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   288
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   289
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   290
        void initPartialTraversalState() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   291
            SpinedBuffer<P_OUT> b = new SpinedBuffer<>();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   292
            buffer = b;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   293
            bufferSink = ph.wrapSink(b::accept);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   294
            pusher = () -> spliterator.tryAdvance(bufferSink);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   295
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   296
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   297
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   298
        public boolean tryAdvance(Consumer<? super P_OUT> consumer) {
19188
bbf287c5cd92 8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents: 18572
diff changeset
   299
            Objects.requireNonNull(consumer);
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   300
            boolean hasNext = doAdvance();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   301
            if (hasNext)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   302
                consumer.accept(buffer.get(nextToConsume));
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   303
            return hasNext;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   304
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   305
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   306
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   307
        public void forEachRemaining(Consumer<? super P_OUT> consumer) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   308
            if (buffer == null && !finished) {
19188
bbf287c5cd92 8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents: 18572
diff changeset
   309
                Objects.requireNonNull(consumer);
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   310
                init();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   311
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   312
                ph.wrapAndCopyInto((Sink<P_OUT>) consumer::accept, spliterator);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   313
                finished = true;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   314
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   315
            else {
18572
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   316
                do { } while (tryAdvance(consumer));
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   317
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   318
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   319
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   320
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   321
    static final class IntWrappingSpliterator<P_IN>
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   322
            extends AbstractWrappingSpliterator<P_IN, Integer, SpinedBuffer.OfInt>
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   323
            implements Spliterator.OfInt {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   324
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   325
        IntWrappingSpliterator(PipelineHelper<Integer> ph,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   326
                               Supplier<Spliterator<P_IN>> supplier,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   327
                               boolean parallel) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   328
            super(ph, supplier, parallel);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   329
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   330
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   331
        IntWrappingSpliterator(PipelineHelper<Integer> ph,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   332
                               Spliterator<P_IN> spliterator,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   333
                               boolean parallel) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   334
            super(ph, spliterator, parallel);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   335
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   336
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   337
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   338
        AbstractWrappingSpliterator<P_IN, Integer, ?> wrap(Spliterator<P_IN> s) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   339
            return new IntWrappingSpliterator<>(ph, s, isParallel);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   340
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   341
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   342
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   343
        void initPartialTraversalState() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   344
            SpinedBuffer.OfInt b = new SpinedBuffer.OfInt();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   345
            buffer = b;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   346
            bufferSink = ph.wrapSink((Sink.OfInt) b::accept);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   347
            pusher = () -> spliterator.tryAdvance(bufferSink);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   348
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   349
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   350
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   351
        public Spliterator.OfInt trySplit() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   352
            return (Spliterator.OfInt) super.trySplit();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   353
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   354
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   355
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   356
        public boolean tryAdvance(IntConsumer consumer) {
19188
bbf287c5cd92 8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents: 18572
diff changeset
   357
            Objects.requireNonNull(consumer);
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   358
            boolean hasNext = doAdvance();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   359
            if (hasNext)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   360
                consumer.accept(buffer.get(nextToConsume));
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   361
            return hasNext;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   362
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   363
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   364
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   365
        public void forEachRemaining(IntConsumer consumer) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   366
            if (buffer == null && !finished) {
19188
bbf287c5cd92 8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents: 18572
diff changeset
   367
                Objects.requireNonNull(consumer);
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   368
                init();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   369
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   370
                ph.wrapAndCopyInto((Sink.OfInt) consumer::accept, spliterator);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   371
                finished = true;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   372
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   373
            else {
18572
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   374
                do { } while (tryAdvance(consumer));
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   375
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   376
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   377
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   378
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   379
    static final class LongWrappingSpliterator<P_IN>
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   380
            extends AbstractWrappingSpliterator<P_IN, Long, SpinedBuffer.OfLong>
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   381
            implements Spliterator.OfLong {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   382
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   383
        LongWrappingSpliterator(PipelineHelper<Long> ph,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   384
                                Supplier<Spliterator<P_IN>> supplier,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   385
                                boolean parallel) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   386
            super(ph, supplier, parallel);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   387
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   388
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   389
        LongWrappingSpliterator(PipelineHelper<Long> ph,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   390
                                Spliterator<P_IN> spliterator,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   391
                                boolean parallel) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   392
            super(ph, spliterator, parallel);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   393
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   394
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   395
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   396
        AbstractWrappingSpliterator<P_IN, Long, ?> wrap(Spliterator<P_IN> s) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   397
            return new LongWrappingSpliterator<>(ph, s, isParallel);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   398
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   399
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   400
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   401
        void initPartialTraversalState() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   402
            SpinedBuffer.OfLong b = new SpinedBuffer.OfLong();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   403
            buffer = b;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   404
            bufferSink = ph.wrapSink((Sink.OfLong) b::accept);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   405
            pusher = () -> spliterator.tryAdvance(bufferSink);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   406
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   407
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   408
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   409
        public Spliterator.OfLong trySplit() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   410
            return (Spliterator.OfLong) super.trySplit();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   411
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   412
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   413
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   414
        public boolean tryAdvance(LongConsumer consumer) {
19188
bbf287c5cd92 8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents: 18572
diff changeset
   415
            Objects.requireNonNull(consumer);
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   416
            boolean hasNext = doAdvance();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   417
            if (hasNext)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   418
                consumer.accept(buffer.get(nextToConsume));
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   419
            return hasNext;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   420
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   421
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   422
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   423
        public void forEachRemaining(LongConsumer consumer) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   424
            if (buffer == null && !finished) {
19188
bbf287c5cd92 8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents: 18572
diff changeset
   425
                Objects.requireNonNull(consumer);
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   426
                init();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   427
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   428
                ph.wrapAndCopyInto((Sink.OfLong) consumer::accept, spliterator);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   429
                finished = true;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   430
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   431
            else {
18572
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   432
                do { } while (tryAdvance(consumer));
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   433
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   434
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   435
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   436
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   437
    static final class DoubleWrappingSpliterator<P_IN>
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   438
            extends AbstractWrappingSpliterator<P_IN, Double, SpinedBuffer.OfDouble>
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   439
            implements Spliterator.OfDouble {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   440
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   441
        DoubleWrappingSpliterator(PipelineHelper<Double> ph,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   442
                                  Supplier<Spliterator<P_IN>> supplier,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   443
                                  boolean parallel) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   444
            super(ph, supplier, parallel);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   445
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   446
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   447
        DoubleWrappingSpliterator(PipelineHelper<Double> ph,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   448
                                  Spliterator<P_IN> spliterator,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   449
                                  boolean parallel) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   450
            super(ph, spliterator, parallel);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   451
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   452
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   453
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   454
        AbstractWrappingSpliterator<P_IN, Double, ?> wrap(Spliterator<P_IN> s) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   455
            return new DoubleWrappingSpliterator<>(ph, s, isParallel);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   456
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   457
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   458
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   459
        void initPartialTraversalState() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   460
            SpinedBuffer.OfDouble b = new SpinedBuffer.OfDouble();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   461
            buffer = b;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   462
            bufferSink = ph.wrapSink((Sink.OfDouble) b::accept);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   463
            pusher = () -> spliterator.tryAdvance(bufferSink);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   464
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   465
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   466
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   467
        public Spliterator.OfDouble trySplit() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   468
            return (Spliterator.OfDouble) super.trySplit();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   469
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   470
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   471
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   472
        public boolean tryAdvance(DoubleConsumer consumer) {
19188
bbf287c5cd92 8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents: 18572
diff changeset
   473
            Objects.requireNonNull(consumer);
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   474
            boolean hasNext = doAdvance();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   475
            if (hasNext)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   476
                consumer.accept(buffer.get(nextToConsume));
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   477
            return hasNext;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   478
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   479
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   480
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   481
        public void forEachRemaining(DoubleConsumer consumer) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   482
            if (buffer == null && !finished) {
19188
bbf287c5cd92 8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents: 18572
diff changeset
   483
                Objects.requireNonNull(consumer);
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   484
                init();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   485
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   486
                ph.wrapAndCopyInto((Sink.OfDouble) consumer::accept, spliterator);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   487
                finished = true;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   488
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   489
            else {
18572
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   490
                do { } while (tryAdvance(consumer));
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   491
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   492
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   493
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   494
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   495
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   496
     * Spliterator implementation that delegates to an underlying spliterator,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   497
     * acquiring the spliterator from a {@code Supplier<Spliterator>} on the
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   498
     * first call to any spliterator method.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   499
     * @param <T>
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   500
     */
18572
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   501
    static class DelegatingSpliterator<T, T_SPLITR extends Spliterator<T>>
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   502
            implements Spliterator<T> {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   503
        private final Supplier<? extends T_SPLITR> supplier;
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   504
18572
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   505
        private T_SPLITR s;
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   506
18572
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   507
        DelegatingSpliterator(Supplier<? extends T_SPLITR> supplier) {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   508
            this.supplier = supplier;
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   509
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   510
18572
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   511
        T_SPLITR get() {
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   512
            if (s == null) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   513
                s = supplier.get();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   514
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   515
            return s;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   516
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   517
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   518
        @Override
18572
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   519
        public T_SPLITR trySplit() {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   520
            return (T_SPLITR) get().trySplit();
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   521
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   522
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   523
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   524
        public boolean tryAdvance(Consumer<? super T> consumer) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   525
            return get().tryAdvance(consumer);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   526
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   527
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   528
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   529
        public void forEachRemaining(Consumer<? super T> consumer) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   530
            get().forEachRemaining(consumer);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   531
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   532
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   533
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   534
        public long estimateSize() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   535
            return get().estimateSize();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   536
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   537
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   538
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   539
        public int characteristics() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   540
            return get().characteristics();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   541
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   542
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   543
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   544
        public Comparator<? super T> getComparator() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   545
            return get().getComparator();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   546
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   547
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   548
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   549
        public long getExactSizeIfKnown() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   550
            return get().getExactSizeIfKnown();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   551
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   552
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   553
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   554
        public String toString() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   555
            return getClass().getName() + "[" + get() + "]";
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   556
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   557
18572
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   558
        static class OfPrimitive<T, T_CONS, T_SPLITR extends Spliterator.OfPrimitive<T, T_CONS, T_SPLITR>>
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   559
            extends DelegatingSpliterator<T, T_SPLITR>
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   560
            implements Spliterator.OfPrimitive<T, T_CONS, T_SPLITR> {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   561
            OfPrimitive(Supplier<? extends T_SPLITR> supplier) {
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   562
                super(supplier);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   563
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   564
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   565
            @Override
18572
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   566
            public boolean tryAdvance(T_CONS consumer) {
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   567
                return get().tryAdvance(consumer);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   568
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   569
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   570
            @Override
18572
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   571
            public void forEachRemaining(T_CONS consumer) {
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   572
                get().forEachRemaining(consumer);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   573
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   574
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   575
18572
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   576
        static final class OfInt
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   577
                extends OfPrimitive<Integer, IntConsumer, Spliterator.OfInt>
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   578
                implements Spliterator.OfInt {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   579
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   580
            OfInt(Supplier<Spliterator.OfInt> supplier) {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   581
                super(supplier);
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   582
            }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   583
        }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   584
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   585
        static final class OfLong
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   586
                extends OfPrimitive<Long, LongConsumer, Spliterator.OfLong>
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   587
                implements Spliterator.OfLong {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   588
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   589
            OfLong(Supplier<Spliterator.OfLong> supplier) {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   590
                super(supplier);
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   591
            }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   592
        }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   593
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   594
        static final class OfDouble
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   595
                extends OfPrimitive<Double, DoubleConsumer, Spliterator.OfDouble>
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   596
                implements Spliterator.OfDouble {
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   597
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   598
            OfDouble(Supplier<Spliterator.OfDouble> supplier) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   599
                super(supplier);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   600
            }
18572
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   601
        }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   602
    }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   603
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   604
    /**
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   605
     * A slice Spliterator from a source Spliterator that reports
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   606
     * {@code SUBSIZED}.
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   607
     *
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   608
     */
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   609
    static abstract class SliceSpliterator<T, T_SPLITR extends Spliterator<T>> {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   610
        // The start index of the slice
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   611
        final long sliceOrigin;
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   612
        // One past the last index of the slice
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   613
        final long sliceFence;
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   614
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   615
        // The spliterator to slice
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   616
        T_SPLITR s;
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   617
        // current (absolute) index, modified on advance/split
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   618
        long index;
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   619
        // one past last (absolute) index or sliceFence, which ever is smaller
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   620
        long fence;
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   621
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   622
        SliceSpliterator(T_SPLITR s, long sliceOrigin, long sliceFence, long origin, long fence) {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   623
            assert s.hasCharacteristics(Spliterator.SUBSIZED);
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   624
            this.s = s;
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   625
            this.sliceOrigin = sliceOrigin;
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   626
            this.sliceFence = sliceFence;
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   627
            this.index = origin;
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   628
            this.fence = fence;
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   629
        }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   630
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   631
        protected abstract T_SPLITR makeSpliterator(T_SPLITR s, long sliceOrigin, long sliceFence, long origin, long fence);
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   632
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   633
        public T_SPLITR trySplit() {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   634
            if (sliceOrigin >= fence)
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   635
                return null;
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   636
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   637
            if (index >= fence)
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   638
                return null;
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   639
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   640
            // Keep splitting until the left and right splits intersect with the slice
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   641
            // thereby ensuring the size estimate decreases.
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   642
            // This also avoids creating empty spliterators which can result in
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   643
            // existing and additionally created F/J tasks that perform
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   644
            // redundant work on no elements.
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   645
            while (true) {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   646
                T_SPLITR leftSplit = (T_SPLITR) s.trySplit();
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   647
                if (leftSplit == null)
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   648
                    return null;
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   649
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   650
                long leftSplitFenceUnbounded = index + leftSplit.estimateSize();
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   651
                long leftSplitFence = Math.min(leftSplitFenceUnbounded, sliceFence);
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   652
                if (sliceOrigin >= leftSplitFence) {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   653
                    // The left split does not intersect with, and is to the left of, the slice
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   654
                    // The right split does intersect
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   655
                    // Discard the left split and split further with the right split
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   656
                    index = leftSplitFence;
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   657
                }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   658
                else if (leftSplitFence >= sliceFence) {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   659
                    // The right split does not intersect with, and is to the right of, the slice
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   660
                    // The left split does intersect
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   661
                    // Discard the right split and split further with the left split
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   662
                    s = leftSplit;
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   663
                    fence = leftSplitFence;
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   664
                }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   665
                else if (index >= sliceOrigin && leftSplitFenceUnbounded <= sliceFence) {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   666
                    // The left split is contained within the slice, return the underlying left split
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   667
                    // Right split is contained within or intersects with the slice
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   668
                    index = leftSplitFence;
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   669
                    return leftSplit;
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   670
                } else {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   671
                    // The left split intersects with the slice
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   672
                    // Right split is contained within or intersects with the slice
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   673
                    return makeSpliterator(leftSplit, sliceOrigin, sliceFence, index, index = leftSplitFence);
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   674
                }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   675
            }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   676
        }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   677
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   678
        public long estimateSize() {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   679
            return (sliceOrigin < fence)
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   680
                   ? fence - Math.max(sliceOrigin, index) : 0;
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   681
        }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   682
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   683
        public int characteristics() {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   684
            return s.characteristics();
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   685
        }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   686
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   687
        static final class OfRef<T>
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   688
                extends SliceSpliterator<T, Spliterator<T>>
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   689
                implements Spliterator<T> {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   690
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   691
            OfRef(Spliterator<T> s, long sliceOrigin, long sliceFence) {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   692
                this(s, sliceOrigin, sliceFence, 0, Math.min(s.estimateSize(), sliceFence));
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   693
            }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   694
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   695
            private OfRef(Spliterator<T> s,
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   696
                          long sliceOrigin, long sliceFence, long origin, long fence) {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   697
                super(s, sliceOrigin, sliceFence, origin, fence);
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   698
            }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   699
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   700
            @Override
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   701
            protected Spliterator<T> makeSpliterator(Spliterator<T> s,
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   702
                                                     long sliceOrigin, long sliceFence,
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   703
                                                     long origin, long fence) {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   704
                return new OfRef<>(s, sliceOrigin, sliceFence, origin, fence);
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   705
            }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   706
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   707
            @Override
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   708
            public boolean tryAdvance(Consumer<? super T> action) {
19188
bbf287c5cd92 8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents: 18572
diff changeset
   709
                Objects.requireNonNull(action);
bbf287c5cd92 8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents: 18572
diff changeset
   710
18572
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   711
                if (sliceOrigin >= fence)
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   712
                    return false;
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   713
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   714
                while (sliceOrigin > index) {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   715
                    s.tryAdvance(e -> {});
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   716
                    index++;
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   717
                }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   718
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   719
                if (index >= fence)
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   720
                    return false;
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   721
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   722
                index++;
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   723
                return s.tryAdvance(action);
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   724
            }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   725
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   726
            @Override
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   727
            public void forEachRemaining(Consumer<? super T> action) {
19188
bbf287c5cd92 8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents: 18572
diff changeset
   728
                Objects.requireNonNull(action);
bbf287c5cd92 8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents: 18572
diff changeset
   729
18572
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   730
                if (sliceOrigin >= fence)
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   731
                    return;
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   732
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   733
                if (index >= fence)
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   734
                    return;
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   735
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   736
                if (index >= sliceOrigin && (index + s.estimateSize()) <= sliceFence) {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   737
                    // The spliterator is contained within the slice
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   738
                    s.forEachRemaining(action);
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   739
                    index = fence;
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   740
                } else {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   741
                    // The spliterator intersects with the slice
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   742
                    while (sliceOrigin > index) {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   743
                        s.tryAdvance(e -> {});
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   744
                        index++;
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   745
                    }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   746
                    // Traverse elements up to the fence
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   747
                    for (;index < fence; index++) {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   748
                        s.tryAdvance(action);
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   749
                    }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   750
                }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   751
            }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   752
        }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   753
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   754
        static abstract class OfPrimitive<T,
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   755
                T_SPLITR extends Spliterator.OfPrimitive<T, T_CONS, T_SPLITR>,
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   756
                T_CONS>
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   757
                extends SliceSpliterator<T, T_SPLITR>
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   758
                implements Spliterator.OfPrimitive<T, T_CONS, T_SPLITR> {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   759
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   760
            OfPrimitive(T_SPLITR s, long sliceOrigin, long sliceFence) {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   761
                this(s, sliceOrigin, sliceFence, 0, Math.min(s.estimateSize(), sliceFence));
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   762
            }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   763
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   764
            private OfPrimitive(T_SPLITR s,
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   765
                                long sliceOrigin, long sliceFence, long origin, long fence) {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   766
                super(s, sliceOrigin, sliceFence, origin, fence);
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   767
            }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   768
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   769
            @Override
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   770
            public boolean tryAdvance(T_CONS action) {
19188
bbf287c5cd92 8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents: 18572
diff changeset
   771
                Objects.requireNonNull(action);
bbf287c5cd92 8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents: 18572
diff changeset
   772
18572
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   773
                if (sliceOrigin >= fence)
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   774
                    return false;
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   775
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   776
                while (sliceOrigin > index) {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   777
                    s.tryAdvance(emptyConsumer());
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   778
                    index++;
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   779
                }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   780
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   781
                if (index >= fence)
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   782
                    return false;
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   783
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   784
                index++;
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   785
                return s.tryAdvance(action);
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   786
            }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   787
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   788
            @Override
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   789
            public void forEachRemaining(T_CONS action) {
19188
bbf287c5cd92 8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents: 18572
diff changeset
   790
                Objects.requireNonNull(action);
bbf287c5cd92 8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents: 18572
diff changeset
   791
18572
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   792
                if (sliceOrigin >= fence)
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   793
                    return;
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   794
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   795
                if (index >= fence)
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   796
                    return;
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   797
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   798
                if (index >= sliceOrigin && (index + s.estimateSize()) <= sliceFence) {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   799
                    // The spliterator is contained within the slice
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   800
                    s.forEachRemaining(action);
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   801
                    index = fence;
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   802
                } else {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   803
                    // The spliterator intersects with the slice
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   804
                    while (sliceOrigin > index) {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   805
                        s.tryAdvance(emptyConsumer());
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   806
                        index++;
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   807
                    }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   808
                    // Traverse elements up to the fence
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   809
                    for (;index < fence; index++) {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   810
                        s.tryAdvance(action);
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   811
                    }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   812
                }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   813
            }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   814
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   815
            protected abstract T_CONS emptyConsumer();
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   816
        }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   817
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   818
        static final class OfInt extends OfPrimitive<Integer, Spliterator.OfInt, IntConsumer>
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   819
                implements Spliterator.OfInt {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   820
            OfInt(Spliterator.OfInt s, long sliceOrigin, long sliceFence) {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   821
                super(s, sliceOrigin, sliceFence);
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   822
            }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   823
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   824
            OfInt(Spliterator.OfInt s,
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   825
                  long sliceOrigin, long sliceFence, long origin, long fence) {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   826
                super(s, sliceOrigin, sliceFence, origin, fence);
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   827
            }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   828
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   829
            @Override
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   830
            protected Spliterator.OfInt makeSpliterator(Spliterator.OfInt s,
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   831
                                                        long sliceOrigin, long sliceFence,
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   832
                                                        long origin, long fence) {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   833
                return new SliceSpliterator.OfInt(s, sliceOrigin, sliceFence, origin, fence);
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   834
            }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   835
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   836
            @Override
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   837
            protected IntConsumer emptyConsumer() {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   838
                return e -> {};
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   839
            }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   840
        }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   841
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   842
        static final class OfLong extends OfPrimitive<Long, Spliterator.OfLong, LongConsumer>
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   843
                implements Spliterator.OfLong {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   844
            OfLong(Spliterator.OfLong s, long sliceOrigin, long sliceFence) {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   845
                super(s, sliceOrigin, sliceFence);
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   846
            }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   847
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   848
            OfLong(Spliterator.OfLong s,
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   849
                   long sliceOrigin, long sliceFence, long origin, long fence) {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   850
                super(s, sliceOrigin, sliceFence, origin, fence);
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   851
            }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   852
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   853
            @Override
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   854
            protected Spliterator.OfLong makeSpliterator(Spliterator.OfLong s,
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   855
                                                         long sliceOrigin, long sliceFence,
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   856
                                                         long origin, long fence) {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   857
                return new SliceSpliterator.OfLong(s, sliceOrigin, sliceFence, origin, fence);
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   858
            }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   859
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   860
            @Override
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   861
            protected LongConsumer emptyConsumer() {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   862
                return e -> {};
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   863
            }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   864
        }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   865
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   866
        static final class OfDouble extends OfPrimitive<Double, Spliterator.OfDouble, DoubleConsumer>
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   867
                implements Spliterator.OfDouble {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   868
            OfDouble(Spliterator.OfDouble s, long sliceOrigin, long sliceFence) {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   869
                super(s, sliceOrigin, sliceFence);
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   870
            }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   871
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   872
            OfDouble(Spliterator.OfDouble s,
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   873
                     long sliceOrigin, long sliceFence, long origin, long fence) {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   874
                super(s, sliceOrigin, sliceFence, origin, fence);
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   875
            }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   876
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   877
            @Override
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   878
            protected Spliterator.OfDouble makeSpliterator(Spliterator.OfDouble s,
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   879
                                                           long sliceOrigin, long sliceFence,
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   880
                                                           long origin, long fence) {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   881
                return new SliceSpliterator.OfDouble(s, sliceOrigin, sliceFence, origin, fence);
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   882
            }
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   883
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   884
            @Override
18572
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   885
            protected DoubleConsumer emptyConsumer() {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   886
                return e -> {};
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   887
            }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   888
        }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   889
    }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   890
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   891
    /**
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   892
     * A slice Spliterator that does not preserve order, if any, of a source
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   893
     * Spliterator.
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   894
     *
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   895
     * Note: The source spliterator may report {@code ORDERED} since that
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   896
     * spliterator be the result of a previous pipeline stage that was
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   897
     * collected to a {@code Node}. It is the order of the pipeline stage
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   898
     * that governs whether the this slice spliterator is to be used or not.
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   899
     */
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   900
    static abstract class UnorderedSliceSpliterator<T, T_SPLITR extends Spliterator<T>> {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   901
        static final int CHUNK_SIZE = 1 << 7;
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   902
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   903
        // The spliterator to slice
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   904
        protected final T_SPLITR s;
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   905
        protected final boolean unlimited;
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   906
        private final long skipThreshold;
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   907
        private final AtomicLong permits;
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   908
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   909
        UnorderedSliceSpliterator(T_SPLITR s, long skip, long limit) {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   910
            this.s = s;
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   911
            this.unlimited = limit < 0;
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   912
            this.skipThreshold = limit >= 0 ? limit : 0;
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   913
            this.permits = new AtomicLong(limit >= 0 ? skip + limit : skip);
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   914
        }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   915
19220
d3d40ccb544e 8022476: cleanup some raw types and unchecked warnings in java.util.stream
mduigou
parents: 19188
diff changeset
   916
        UnorderedSliceSpliterator(T_SPLITR s,
d3d40ccb544e 8022476: cleanup some raw types and unchecked warnings in java.util.stream
mduigou
parents: 19188
diff changeset
   917
                                  UnorderedSliceSpliterator<T, T_SPLITR> parent) {
18572
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   918
            this.s = s;
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   919
            this.unlimited = parent.unlimited;
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   920
            this.permits = parent.permits;
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   921
            this.skipThreshold = parent.skipThreshold;
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   922
        }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   923
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   924
        /**
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   925
         * Acquire permission to skip or process elements.  The caller must
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   926
         * first acquire the elements, then consult this method for guidance
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   927
         * as to what to do with the data.
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   928
         *
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   929
         * <p>We use an {@code AtomicLong} to atomically maintain a counter,
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   930
         * which is initialized as skip+limit if we are limiting, or skip only
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   931
         * if we are not limiting.  The user should consult the method
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   932
         * {@code checkPermits()} before acquiring data elements.
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   933
         *
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   934
         * @param numElements the number of elements the caller has in hand
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   935
         * @return the number of elements that should be processed; any
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   936
         * remaining elements should be discarded.
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   937
         */
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   938
        protected final long acquirePermits(long numElements) {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   939
            long remainingPermits;
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   940
            long grabbing;
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   941
            // permits never increase, and don't decrease below zero
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   942
            assert numElements > 0;
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   943
            do {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   944
                remainingPermits = permits.get();
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   945
                if (remainingPermits == 0)
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   946
                    return unlimited ? numElements : 0;
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   947
                grabbing = Math.min(remainingPermits, numElements);
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   948
            } while (grabbing > 0 &&
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   949
                     !permits.compareAndSet(remainingPermits, remainingPermits - grabbing));
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   950
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   951
            if (unlimited)
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   952
                return Math.max(numElements - grabbing, 0);
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   953
            else if (remainingPermits > skipThreshold)
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   954
                return Math.max(grabbing - (remainingPermits - skipThreshold), 0);
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   955
            else
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   956
                return grabbing;
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   957
        }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   958
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   959
        enum PermitStatus { NO_MORE, MAYBE_MORE, UNLIMITED }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   960
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   961
        /** Call to check if permits might be available before acquiring data */
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   962
        protected final PermitStatus permitStatus() {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   963
            if (permits.get() > 0)
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   964
                return PermitStatus.MAYBE_MORE;
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   965
            else
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   966
                return unlimited ?  PermitStatus.UNLIMITED : PermitStatus.NO_MORE;
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   967
        }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   968
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   969
        public final T_SPLITR trySplit() {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   970
            // Stop splitting when there are no more limit permits
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   971
            if (permits.get() == 0)
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   972
                return null;
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   973
            T_SPLITR split = (T_SPLITR) s.trySplit();
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   974
            return split == null ? null : makeSpliterator(split);
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   975
        }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   976
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   977
        protected abstract T_SPLITR makeSpliterator(T_SPLITR s);
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   978
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   979
        public final long estimateSize() {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   980
            return s.estimateSize();
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   981
        }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   982
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   983
        public final int characteristics() {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   984
            return s.characteristics() &
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   985
                   ~(Spliterator.SIZED | Spliterator.SUBSIZED | Spliterator.ORDERED);
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   986
        }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   987
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   988
        static final class OfRef<T> extends UnorderedSliceSpliterator<T, Spliterator<T>>
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   989
                implements Spliterator<T>, Consumer<T> {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   990
            T tmpSlot;
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   991
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   992
            OfRef(Spliterator<T> s, long skip, long limit) {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   993
                super(s, skip, limit);
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   994
            }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   995
19220
d3d40ccb544e 8022476: cleanup some raw types and unchecked warnings in java.util.stream
mduigou
parents: 19188
diff changeset
   996
            OfRef(Spliterator<T> s, OfRef<T> parent) {
18572
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   997
                super(s, parent);
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   998
            }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
   999
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1000
            @Override
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1001
            public final void accept(T t) {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1002
                tmpSlot = t;
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1003
            }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1004
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1005
            @Override
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1006
            public boolean tryAdvance(Consumer<? super T> action) {
19188
bbf287c5cd92 8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents: 18572
diff changeset
  1007
                Objects.requireNonNull(action);
bbf287c5cd92 8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents: 18572
diff changeset
  1008
18572
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1009
                while (permitStatus() != PermitStatus.NO_MORE) {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1010
                    if (!s.tryAdvance(this))
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1011
                        return false;
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1012
                    else if (acquirePermits(1) == 1) {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1013
                        action.accept(tmpSlot);
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1014
                        tmpSlot = null;
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1015
                        return true;
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1016
                    }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1017
                }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1018
                return false;
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1019
            }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1020
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1021
            @Override
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1022
            public void forEachRemaining(Consumer<? super T> action) {
19188
bbf287c5cd92 8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents: 18572
diff changeset
  1023
                Objects.requireNonNull(action);
bbf287c5cd92 8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents: 18572
diff changeset
  1024
18572
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1025
                ArrayBuffer.OfRef<T> sb = null;
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1026
                PermitStatus permitStatus;
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1027
                while ((permitStatus = permitStatus()) != PermitStatus.NO_MORE) {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1028
                    if (permitStatus == PermitStatus.MAYBE_MORE) {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1029
                        // Optimistically traverse elements up to a threshold of CHUNK_SIZE
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1030
                        if (sb == null)
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1031
                            sb = new ArrayBuffer.OfRef<>(CHUNK_SIZE);
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1032
                        else
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1033
                            sb.reset();
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1034
                        long permitsRequested = 0;
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1035
                        do { } while (s.tryAdvance(sb) && ++permitsRequested < CHUNK_SIZE);
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1036
                        if (permitsRequested == 0)
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1037
                            return;
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1038
                        sb.forEach(action, acquirePermits(permitsRequested));
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1039
                    }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1040
                    else {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1041
                        // Must be UNLIMITED; let 'er rip
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1042
                        s.forEachRemaining(action);
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1043
                        return;
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1044
                    }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1045
                }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1046
            }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1047
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1048
            @Override
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1049
            protected Spliterator<T> makeSpliterator(Spliterator<T> s) {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1050
                return new UnorderedSliceSpliterator.OfRef<>(s, this);
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1051
            }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1052
        }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1053
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1054
        /**
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1055
         * Concrete sub-types must also be an instance of type {@code T_CONS}.
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1056
         *
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1057
         * @param <T_BUFF> the type of the spined buffer. Must also be a type of
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1058
         *        {@code T_CONS}.
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1059
         */
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1060
        static abstract class OfPrimitive<
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1061
                T,
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1062
                T_CONS,
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1063
                T_BUFF extends ArrayBuffer.OfPrimitive<T_CONS>,
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1064
                T_SPLITR extends Spliterator.OfPrimitive<T, T_CONS, T_SPLITR>>
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1065
                extends UnorderedSliceSpliterator<T, T_SPLITR>
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1066
                implements Spliterator.OfPrimitive<T, T_CONS, T_SPLITR> {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1067
            OfPrimitive(T_SPLITR s, long skip, long limit) {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1068
                super(s, skip, limit);
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1069
            }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1070
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1071
            OfPrimitive(T_SPLITR s, UnorderedSliceSpliterator.OfPrimitive parent) {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1072
                super(s, parent);
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1073
            }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1074
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1075
            @Override
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1076
            public boolean tryAdvance(T_CONS action) {
19188
bbf287c5cd92 8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents: 18572
diff changeset
  1077
                Objects.requireNonNull(action);
bbf287c5cd92 8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents: 18572
diff changeset
  1078
18572
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1079
                while (permitStatus() != PermitStatus.NO_MORE) {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1080
                    if (!s.tryAdvance((T_CONS) this))
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1081
                        return false;
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1082
                    else if (acquirePermits(1) == 1) {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1083
                        acceptConsumed(action);
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1084
                        return true;
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1085
                    }
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1086
                }
18572
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1087
                return false;
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1088
            }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1089
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1090
            protected abstract void acceptConsumed(T_CONS action);
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1091
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1092
            @Override
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1093
            public void forEachRemaining(T_CONS action) {
19188
bbf287c5cd92 8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents: 18572
diff changeset
  1094
                Objects.requireNonNull(action);
bbf287c5cd92 8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents: 18572
diff changeset
  1095
18572
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1096
                T_BUFF sb = null;
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1097
                PermitStatus permitStatus;
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1098
                while ((permitStatus = permitStatus()) != PermitStatus.NO_MORE) {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1099
                    if (permitStatus == PermitStatus.MAYBE_MORE) {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1100
                        // Optimistically traverse elements up to a threshold of CHUNK_SIZE
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1101
                        if (sb == null)
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1102
                            sb = bufferCreate(CHUNK_SIZE);
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1103
                        else
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1104
                            sb.reset();
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1105
                        @SuppressWarnings("unchecked")
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1106
                        T_CONS sbc = (T_CONS) sb;
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1107
                        long permitsRequested = 0;
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1108
                        do { } while (s.tryAdvance(sbc) && ++permitsRequested < CHUNK_SIZE);
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1109
                        if (permitsRequested == 0)
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1110
                            return;
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1111
                        sb.forEach(action, acquirePermits(permitsRequested));
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1112
                    }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1113
                    else {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1114
                        // Must be UNLIMITED; let 'er rip
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1115
                        s.forEachRemaining(action);
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1116
                        return;
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1117
                    }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1118
                }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1119
            }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1120
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1121
            protected abstract T_BUFF bufferCreate(int initialCapacity);
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1122
        }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1123
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1124
        static final class OfInt
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1125
                extends OfPrimitive<Integer, IntConsumer, ArrayBuffer.OfInt, Spliterator.OfInt>
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1126
                implements Spliterator.OfInt, IntConsumer {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1127
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1128
            int tmpValue;
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1129
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1130
            OfInt(Spliterator.OfInt s, long skip, long limit) {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1131
                super(s, skip, limit);
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1132
            }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1133
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1134
            OfInt(Spliterator.OfInt s, UnorderedSliceSpliterator.OfInt parent) {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1135
                super(s, parent);
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1136
            }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1137
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1138
            @Override
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1139
            public void accept(int value) {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1140
                tmpValue = value;
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1141
            }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1142
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1143
            @Override
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1144
            protected void acceptConsumed(IntConsumer action) {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1145
                action.accept(tmpValue);
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1146
            }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1147
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1148
            @Override
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1149
            protected ArrayBuffer.OfInt bufferCreate(int initialCapacity) {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1150
                return new ArrayBuffer.OfInt(initialCapacity);
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1151
            }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1152
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1153
            @Override
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1154
            protected Spliterator.OfInt makeSpliterator(Spliterator.OfInt s) {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1155
                return new UnorderedSliceSpliterator.OfInt(s, this);
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1156
            }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1157
        }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1158
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1159
        static final class OfLong
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1160
                extends OfPrimitive<Long, LongConsumer, ArrayBuffer.OfLong, Spliterator.OfLong>
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1161
                implements Spliterator.OfLong, LongConsumer {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1162
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1163
            long tmpValue;
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1164
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1165
            OfLong(Spliterator.OfLong s, long skip, long limit) {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1166
                super(s, skip, limit);
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1167
            }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1168
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1169
            OfLong(Spliterator.OfLong s, UnorderedSliceSpliterator.OfLong parent) {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1170
                super(s, parent);
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1171
            }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1172
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1173
            @Override
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1174
            public void accept(long value) {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1175
                tmpValue = value;
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1176
            }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1177
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1178
            @Override
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1179
            protected void acceptConsumed(LongConsumer action) {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1180
                action.accept(tmpValue);
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1181
            }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1182
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1183
            @Override
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1184
            protected ArrayBuffer.OfLong bufferCreate(int initialCapacity) {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1185
                return new ArrayBuffer.OfLong(initialCapacity);
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1186
            }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1187
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1188
            @Override
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1189
            protected Spliterator.OfLong makeSpliterator(Spliterator.OfLong s) {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1190
                return new UnorderedSliceSpliterator.OfLong(s, this);
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1191
            }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1192
        }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1193
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1194
        static final class OfDouble
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1195
                extends OfPrimitive<Double, DoubleConsumer, ArrayBuffer.OfDouble, Spliterator.OfDouble>
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1196
                implements Spliterator.OfDouble, DoubleConsumer {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1197
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1198
            double tmpValue;
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1199
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1200
            OfDouble(Spliterator.OfDouble s, long skip, long limit) {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1201
                super(s, skip, limit);
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1202
            }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1203
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1204
            OfDouble(Spliterator.OfDouble s, UnorderedSliceSpliterator.OfDouble parent) {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1205
                super(s, parent);
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1206
            }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1207
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1208
            @Override
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1209
            public void accept(double value) {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1210
                tmpValue = value;
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1211
            }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1212
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1213
            @Override
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1214
            protected void acceptConsumed(DoubleConsumer action) {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1215
                action.accept(tmpValue);
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1216
            }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1217
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1218
            @Override
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1219
            protected ArrayBuffer.OfDouble bufferCreate(int initialCapacity) {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1220
                return new ArrayBuffer.OfDouble(initialCapacity);
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1221
            }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1222
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1223
            @Override
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1224
            protected Spliterator.OfDouble makeSpliterator(Spliterator.OfDouble s) {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1225
                return new UnorderedSliceSpliterator.OfDouble(s, this);
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1226
            }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1227
        }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1228
    }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1229
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1230
    /**
21422
6fca66995a27 8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents: 19850
diff changeset
  1231
     * A wrapping spliterator that only reports distinct elements of the
6fca66995a27 8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents: 19850
diff changeset
  1232
     * underlying spliterator. Does not preserve size and encounter order.
6fca66995a27 8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents: 19850
diff changeset
  1233
     */
6fca66995a27 8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents: 19850
diff changeset
  1234
    static final class DistinctSpliterator<T> implements Spliterator<T>, Consumer<T> {
6fca66995a27 8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents: 19850
diff changeset
  1235
6fca66995a27 8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents: 19850
diff changeset
  1236
        // The value to represent null in the ConcurrentHashMap
6fca66995a27 8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents: 19850
diff changeset
  1237
        private static final Object NULL_VALUE = new Object();
6fca66995a27 8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents: 19850
diff changeset
  1238
6fca66995a27 8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents: 19850
diff changeset
  1239
        // The underlying spliterator
6fca66995a27 8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents: 19850
diff changeset
  1240
        private final Spliterator<T> s;
6fca66995a27 8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents: 19850
diff changeset
  1241
6fca66995a27 8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents: 19850
diff changeset
  1242
        // ConcurrentHashMap holding distinct elements as keys
6fca66995a27 8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents: 19850
diff changeset
  1243
        private final ConcurrentHashMap<T, Boolean> seen;
6fca66995a27 8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents: 19850
diff changeset
  1244
6fca66995a27 8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents: 19850
diff changeset
  1245
        // Temporary element, only used with tryAdvance
6fca66995a27 8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents: 19850
diff changeset
  1246
        private T tmpSlot;
6fca66995a27 8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents: 19850
diff changeset
  1247
6fca66995a27 8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents: 19850
diff changeset
  1248
        DistinctSpliterator(Spliterator<T> s) {
6fca66995a27 8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents: 19850
diff changeset
  1249
            this(s, new ConcurrentHashMap<>());
6fca66995a27 8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents: 19850
diff changeset
  1250
        }
6fca66995a27 8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents: 19850
diff changeset
  1251
6fca66995a27 8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents: 19850
diff changeset
  1252
        private DistinctSpliterator(Spliterator<T> s, ConcurrentHashMap<T, Boolean> seen) {
6fca66995a27 8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents: 19850
diff changeset
  1253
            this.s = s;
6fca66995a27 8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents: 19850
diff changeset
  1254
            this.seen = seen;
6fca66995a27 8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents: 19850
diff changeset
  1255
        }
6fca66995a27 8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents: 19850
diff changeset
  1256
6fca66995a27 8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents: 19850
diff changeset
  1257
        @Override
6fca66995a27 8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents: 19850
diff changeset
  1258
        public void accept(T t) {
6fca66995a27 8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents: 19850
diff changeset
  1259
            this.tmpSlot = t;
6fca66995a27 8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents: 19850
diff changeset
  1260
        }
6fca66995a27 8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents: 19850
diff changeset
  1261
6fca66995a27 8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents: 19850
diff changeset
  1262
        @SuppressWarnings("unchecked")
6fca66995a27 8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents: 19850
diff changeset
  1263
        private T mapNull(T t) {
6fca66995a27 8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents: 19850
diff changeset
  1264
            return t != null ? t : (T) NULL_VALUE;
6fca66995a27 8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents: 19850
diff changeset
  1265
        }
6fca66995a27 8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents: 19850
diff changeset
  1266
6fca66995a27 8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents: 19850
diff changeset
  1267
        @Override
6fca66995a27 8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents: 19850
diff changeset
  1268
        public boolean tryAdvance(Consumer<? super T> action) {
6fca66995a27 8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents: 19850
diff changeset
  1269
            while (s.tryAdvance(this)) {
6fca66995a27 8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents: 19850
diff changeset
  1270
                if (seen.putIfAbsent(mapNull(tmpSlot), Boolean.TRUE) == null) {
6fca66995a27 8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents: 19850
diff changeset
  1271
                    action.accept(tmpSlot);
6fca66995a27 8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents: 19850
diff changeset
  1272
                    tmpSlot = null;
6fca66995a27 8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents: 19850
diff changeset
  1273
                    return true;
6fca66995a27 8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents: 19850
diff changeset
  1274
                }
6fca66995a27 8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents: 19850
diff changeset
  1275
            }
6fca66995a27 8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents: 19850
diff changeset
  1276
            return false;
6fca66995a27 8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents: 19850
diff changeset
  1277
        }
6fca66995a27 8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents: 19850
diff changeset
  1278
6fca66995a27 8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents: 19850
diff changeset
  1279
        @Override
6fca66995a27 8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents: 19850
diff changeset
  1280
        public void forEachRemaining(Consumer<? super T> action) {
6fca66995a27 8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents: 19850
diff changeset
  1281
            s.forEachRemaining(t -> {
6fca66995a27 8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents: 19850
diff changeset
  1282
                if (seen.putIfAbsent(mapNull(t), Boolean.TRUE) == null) {
6fca66995a27 8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents: 19850
diff changeset
  1283
                    action.accept(t);
6fca66995a27 8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents: 19850
diff changeset
  1284
                }
6fca66995a27 8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents: 19850
diff changeset
  1285
            });
6fca66995a27 8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents: 19850
diff changeset
  1286
        }
6fca66995a27 8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents: 19850
diff changeset
  1287
6fca66995a27 8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents: 19850
diff changeset
  1288
        @Override
6fca66995a27 8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents: 19850
diff changeset
  1289
        public Spliterator<T> trySplit() {
6fca66995a27 8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents: 19850
diff changeset
  1290
            Spliterator<T> split = s.trySplit();
6fca66995a27 8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents: 19850
diff changeset
  1291
            return (split != null) ? new DistinctSpliterator<>(split, seen) : null;
6fca66995a27 8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents: 19850
diff changeset
  1292
        }
6fca66995a27 8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents: 19850
diff changeset
  1293
6fca66995a27 8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents: 19850
diff changeset
  1294
        @Override
6fca66995a27 8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents: 19850
diff changeset
  1295
        public long estimateSize() {
6fca66995a27 8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents: 19850
diff changeset
  1296
            return s.estimateSize();
6fca66995a27 8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents: 19850
diff changeset
  1297
        }
6fca66995a27 8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents: 19850
diff changeset
  1298
6fca66995a27 8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents: 19850
diff changeset
  1299
        @Override
6fca66995a27 8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents: 19850
diff changeset
  1300
        public int characteristics() {
6fca66995a27 8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents: 19850
diff changeset
  1301
            return (s.characteristics() & ~(Spliterator.SIZED | Spliterator.SUBSIZED |
6fca66995a27 8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents: 19850
diff changeset
  1302
                                            Spliterator.SORTED | Spliterator.ORDERED))
6fca66995a27 8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents: 19850
diff changeset
  1303
                   | Spliterator.DISTINCT;
6fca66995a27 8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents: 19850
diff changeset
  1304
        }
6fca66995a27 8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents: 19850
diff changeset
  1305
6fca66995a27 8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents: 19850
diff changeset
  1306
        @Override
6fca66995a27 8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents: 19850
diff changeset
  1307
        public Comparator<? super T> getComparator() {
6fca66995a27 8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents: 19850
diff changeset
  1308
            return s.getComparator();
6fca66995a27 8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents: 19850
diff changeset
  1309
        }
6fca66995a27 8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents: 19850
diff changeset
  1310
    }
6fca66995a27 8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents: 19850
diff changeset
  1311
6fca66995a27 8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents: 19850
diff changeset
  1312
    /**
18572
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1313
     * A Spliterator that infinitely supplies elements in no particular order.
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1314
     *
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1315
     * <p>Splitting divides the estimated size in two and stops when the
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1316
     * estimate size is 0.
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1317
     *
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1318
     * <p>The {@code forEachRemaining} method if invoked will never terminate.
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1319
     * The {@coe tryAdvance} method always returns true.
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1320
     *
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1321
     */
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1322
    static abstract class InfiniteSupplyingSpliterator<T> implements Spliterator<T> {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1323
        long estimate;
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1324
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1325
        protected InfiniteSupplyingSpliterator(long estimate) {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1326
            this.estimate = estimate;
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1327
        }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1328
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1329
        @Override
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1330
        public long estimateSize() {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1331
            return estimate;
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1332
        }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1333
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1334
        @Override
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1335
        public int characteristics() {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1336
            return IMMUTABLE;
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1337
        }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1338
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1339
        static final class OfRef<T> extends InfiniteSupplyingSpliterator<T> {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1340
            final Supplier<T> s;
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1341
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1342
            OfRef(long size, Supplier<T> s) {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1343
                super(size);
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1344
                this.s = s;
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1345
            }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1346
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1347
            @Override
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1348
            public boolean tryAdvance(Consumer<? super T> action) {
19188
bbf287c5cd92 8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents: 18572
diff changeset
  1349
                Objects.requireNonNull(action);
bbf287c5cd92 8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents: 18572
diff changeset
  1350
18572
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1351
                action.accept(s.get());
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1352
                return true;
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1353
            }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1354
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1355
            @Override
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1356
            public Spliterator<T> trySplit() {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1357
                if (estimate == 0)
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1358
                    return null;
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1359
                return new InfiniteSupplyingSpliterator.OfRef<>(estimate >>>= 1, s);
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1360
            }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1361
        }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1362
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1363
        static final class OfInt extends InfiniteSupplyingSpliterator<Integer>
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1364
                implements Spliterator.OfInt {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1365
            final IntSupplier s;
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1366
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1367
            OfInt(long size, IntSupplier s) {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1368
                super(size);
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1369
                this.s = s;
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1370
            }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1371
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1372
            @Override
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1373
            public boolean tryAdvance(IntConsumer action) {
19188
bbf287c5cd92 8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents: 18572
diff changeset
  1374
                Objects.requireNonNull(action);
bbf287c5cd92 8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents: 18572
diff changeset
  1375
18572
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1376
                action.accept(s.getAsInt());
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1377
                return true;
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1378
            }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1379
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1380
            @Override
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1381
            public Spliterator.OfInt trySplit() {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1382
                if (estimate == 0)
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1383
                    return null;
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1384
                return new InfiniteSupplyingSpliterator.OfInt(estimate = estimate >>> 1, s);
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1385
            }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1386
        }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1387
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1388
        static final class OfLong extends InfiniteSupplyingSpliterator<Long>
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1389
                implements Spliterator.OfLong {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1390
            final LongSupplier s;
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1391
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1392
            OfLong(long size, LongSupplier s) {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1393
                super(size);
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1394
                this.s = s;
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1395
            }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1396
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1397
            @Override
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1398
            public boolean tryAdvance(LongConsumer action) {
19188
bbf287c5cd92 8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents: 18572
diff changeset
  1399
                Objects.requireNonNull(action);
bbf287c5cd92 8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents: 18572
diff changeset
  1400
18572
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1401
                action.accept(s.getAsLong());
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1402
                return true;
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1403
            }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1404
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1405
            @Override
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1406
            public Spliterator.OfLong trySplit() {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1407
                if (estimate == 0)
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1408
                    return null;
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1409
                return new InfiniteSupplyingSpliterator.OfLong(estimate = estimate >>> 1, s);
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1410
            }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1411
        }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1412
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1413
        static final class OfDouble extends InfiniteSupplyingSpliterator<Double>
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1414
                implements Spliterator.OfDouble {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1415
            final DoubleSupplier s;
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1416
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1417
            OfDouble(long size, DoubleSupplier s) {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1418
                super(size);
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1419
                this.s = s;
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1420
            }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1421
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1422
            @Override
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1423
            public boolean tryAdvance(DoubleConsumer action) {
19188
bbf287c5cd92 8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents: 18572
diff changeset
  1424
                Objects.requireNonNull(action);
bbf287c5cd92 8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents: 18572
diff changeset
  1425
18572
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1426
                action.accept(s.getAsDouble());
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1427
                return true;
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1428
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1429
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1430
            @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1431
            public Spliterator.OfDouble trySplit() {
18572
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1432
                if (estimate == 0)
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1433
                    return null;
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1434
                return new InfiniteSupplyingSpliterator.OfDouble(estimate = estimate >>> 1, s);
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1435
            }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1436
        }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1437
    }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1438
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1439
    // @@@ Consolidate with Node.Builder
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1440
    static abstract class ArrayBuffer {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1441
        int index;
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1442
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1443
        void reset() {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1444
            index = 0;
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1445
        }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1446
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1447
        static final class OfRef<T> extends ArrayBuffer implements Consumer<T> {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1448
            final Object[] array;
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1449
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1450
            OfRef(int size) {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1451
                this.array = new Object[size];
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1452
            }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1453
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1454
            @Override
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1455
            public void accept(T t) {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1456
                array[index++] = t;
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1457
            }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1458
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1459
            public void forEach(Consumer<? super T> action, long fence) {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1460
                for (int i = 0; i < fence; i++) {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1461
                    @SuppressWarnings("unchecked")
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1462
                    T t = (T) array[i];
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1463
                    action.accept(t);
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1464
                }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1465
            }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1466
        }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1467
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1468
        static abstract class OfPrimitive<T_CONS> extends ArrayBuffer {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1469
            int index;
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1470
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1471
            @Override
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1472
            void reset() {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1473
                index = 0;
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1474
            }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1475
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1476
            abstract void forEach(T_CONS action, long fence);
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1477
        }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1478
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1479
        static final class OfInt extends OfPrimitive<IntConsumer>
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1480
                implements IntConsumer {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1481
            final int[] array;
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1482
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1483
            OfInt(int size) {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1484
                this.array = new int[size];
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1485
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1486
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1487
            @Override
18572
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1488
            public void accept(int t) {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1489
                array[index++] = t;
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1490
            }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1491
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1492
            @Override
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1493
            public void forEach(IntConsumer action, long fence) {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1494
                for (int i = 0; i < fence; i++) {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1495
                    action.accept(array[i]);
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1496
                }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1497
            }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1498
        }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1499
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1500
        static final class OfLong extends OfPrimitive<LongConsumer>
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1501
                implements LongConsumer {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1502
            final long[] array;
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1503
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1504
            OfLong(int size) {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1505
                this.array = new long[size];
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1506
            }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1507
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1508
            @Override
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1509
            public void accept(long t) {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1510
                array[index++] = t;
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1511
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1512
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1513
            @Override
18572
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1514
            public void forEach(LongConsumer action, long fence) {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1515
                for (int i = 0; i < fence; i++) {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1516
                    action.accept(array[i]);
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1517
                }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1518
            }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1519
        }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1520
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1521
        static final class OfDouble extends OfPrimitive<DoubleConsumer>
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1522
                implements DoubleConsumer {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1523
            final double[] array;
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1524
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1525
            OfDouble(int size) {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1526
                this.array = new double[size];
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1527
            }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1528
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1529
            @Override
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1530
            public void accept(double t) {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1531
                array[index++] = t;
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1532
            }
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1533
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1534
            @Override
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1535
            void forEach(DoubleConsumer action, long fence) {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1536
                for (int i = 0; i < fence; i++) {
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1537
                    action.accept(array[i]);
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17182
diff changeset
  1538
                }
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1539
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1540
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1541
    }
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19220
diff changeset
  1542
}
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19220
diff changeset
  1543