jdk/src/share/classes/java/util/stream/Streams.java
author psandoz
Tue, 30 Jul 2013 11:32:26 +0100
changeset 19072 8f176099ec00
parent 18825 06636235cd12
child 19188 bbf287c5cd92
permissions -rw-r--r--
8021863: Stream.concat incorrectly calculates unsized state Reviewed-by: chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
     1
/*
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
     2
 * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
     4
 *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
    10
 *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
    15
 * accompanied this code).
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
    16
 *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
    20
 *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
    23
 * questions.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
    24
 */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
    25
package java.util.stream;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
    26
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
    27
import java.util.Comparator;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
    28
import java.util.Spliterator;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
    29
import java.util.function.Consumer;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
    30
import java.util.function.DoubleConsumer;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
    31
import java.util.function.IntConsumer;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
    32
import java.util.function.LongConsumer;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
    33
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
    34
/**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
    35
 * Utility methods for operating on and creating streams.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
    36
 *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
    37
 * <p>Unless otherwise stated, streams are created as sequential streams.  A
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
    38
 * sequential stream can be transformed into a parallel stream by calling the
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
    39
 * {@code parallel()} method on the created stream.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
    40
 *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
    41
 * @since 1.8
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
    42
 */
18820
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
    43
final class Streams {
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
    44
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
    45
    private Streams() {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
    46
        throw new Error("no instances");
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
    47
    }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
    48
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
    49
    /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
    50
     * An object instance representing no value, that cannot be an actual
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
    51
     * data element of a stream.  Used when processing streams that can contain
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
    52
     * {@code null} elements to distinguish between a {@code null} value and no
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
    53
     * value.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
    54
     */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
    55
    static final Object NONE = new Object();
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
    56
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
    57
    /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
    58
     * An {@code int} range spliterator.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
    59
     */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
    60
    static final class RangeIntSpliterator implements Spliterator.OfInt {
18158
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
    61
        // Can never be greater that upTo, this avoids overflow if upper bound
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
    62
        // is Integer.MAX_VALUE
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
    63
        // All elements are traversed if from == upTo & last == 0
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
    64
        private int from;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
    65
        private final int upTo;
18158
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
    66
        // 1 if the range is closed and the last element has not been traversed
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
    67
        // Otherwise, 0 if the range is open, or is a closed range and all
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
    68
        // elements have been traversed
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
    69
        private int last;
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
    70
18158
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
    71
        RangeIntSpliterator(int from, int upTo, boolean closed) {
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
    72
            this(from, upTo, closed ? 1 : 0);
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
    73
        }
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
    74
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
    75
        private RangeIntSpliterator(int from, int upTo, int last) {
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
    76
            this.from = from;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
    77
            this.upTo = upTo;
18158
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
    78
            this.last = last;
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
    79
        }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
    80
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
    81
        @Override
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
    82
        public boolean tryAdvance(IntConsumer consumer) {
18158
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
    83
            final int i = from;
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
    84
            if (i < upTo) {
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
    85
                from++;
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
    86
                consumer.accept(i);
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
    87
                return true;
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
    88
            }
18158
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
    89
            else if (last > 0) {
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
    90
                last = 0;
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
    91
                consumer.accept(i);
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
    92
                return true;
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
    93
            }
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
    94
            return false;
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
    95
        }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
    96
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
    97
        @Override
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
    98
        public void forEachRemaining(IntConsumer consumer) {
18158
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
    99
            int i = from;
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   100
            final int hUpTo = upTo;
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   101
            int hLast = last;
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   102
            from = upTo;
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   103
            last = 0;
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   104
            while (i < hUpTo) {
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   105
                consumer.accept(i++);
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   106
            }
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   107
            if (hLast > 0) {
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   108
                // Last element of closed range
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   109
                consumer.accept(i);
18158
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   110
            }
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   111
        }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   112
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   113
        @Override
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   114
        public long estimateSize() {
18158
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   115
            // Ensure ranges of size > Integer.MAX_VALUE report the correct size
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   116
            return ((long) upTo) - from + last;
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   117
        }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   118
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   119
        @Override
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   120
        public int characteristics() {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   121
            return Spliterator.ORDERED | Spliterator.SIZED | Spliterator.SUBSIZED |
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   122
                   Spliterator.IMMUTABLE | Spliterator.NONNULL |
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   123
                   Spliterator.DISTINCT | Spliterator.SORTED;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   124
        }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   125
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   126
        @Override
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   127
        public Comparator<? super Integer> getComparator() {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   128
            return null;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   129
        }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   130
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   131
        @Override
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   132
        public Spliterator.OfInt trySplit() {
18158
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   133
            long size = estimateSize();
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   134
            return size <= 1
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   135
                   ? null
18158
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   136
                   // Left split always has a half-open range
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   137
                   : new RangeIntSpliterator(from, from = from + splitPoint(size), 0);
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   138
        }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   139
18158
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   140
        /**
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   141
         * The spliterator size below which the spliterator will be split
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   142
         * at the mid-point to produce balanced splits. Above this size the
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   143
         * spliterator will be split at a ratio of
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   144
         * 1:(RIGHT_BALANCED_SPLIT_RATIO - 1)
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   145
         * to produce right-balanced splits.
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   146
         *
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   147
         * <p>Such splitting ensures that for very large ranges that the left
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   148
         * side of the range will more likely be processed at a lower-depth
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   149
         * than a balanced tree at the expense of a higher-depth for the right
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   150
         * side of the range.
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   151
         *
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   152
         * <p>This is optimized for cases such as IntStream.ints() that is
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   153
         * implemented as range of 0 to Integer.MAX_VALUE but is likely to be
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   154
         * augmented with a limit operation that limits the number of elements
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   155
         * to a count lower than this threshold.
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   156
         */
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   157
        private static final int BALANCED_SPLIT_THRESHOLD = 1 << 24;
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   158
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   159
        /**
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   160
         * The split ratio of the left and right split when the spliterator
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   161
         * size is above BALANCED_SPLIT_THRESHOLD.
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   162
         */
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   163
        private static final int RIGHT_BALANCED_SPLIT_RATIO = 1 << 3;
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   164
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   165
        private int splitPoint(long size) {
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   166
            int d = (size < BALANCED_SPLIT_THRESHOLD) ? 2 : RIGHT_BALANCED_SPLIT_RATIO;
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   167
            // 2 <= size <= 2^32
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   168
            return (int) (size / d);
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   169
        }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   170
    }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   171
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   172
    /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   173
     * A {@code long} range spliterator.
18158
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   174
     *
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   175
     * This implementation cannot be used for ranges whose size is greater
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   176
     * than Long.MAX_VALUE
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   177
     */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   178
    static final class RangeLongSpliterator implements Spliterator.OfLong {
18158
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   179
        // Can never be greater that upTo, this avoids overflow if upper bound
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   180
        // is Long.MAX_VALUE
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   181
        // All elements are traversed if from == upTo & last == 0
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   182
        private long from;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   183
        private final long upTo;
18158
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   184
        // 1 if the range is closed and the last element has not been traversed
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   185
        // Otherwise, 0 if the range is open, or is a closed range and all
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   186
        // elements have been traversed
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   187
        private int last;
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   188
18158
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   189
        RangeLongSpliterator(long from, long upTo, boolean closed) {
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   190
            this(from, upTo, closed ? 1 : 0);
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   191
        }
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   192
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   193
        private RangeLongSpliterator(long from, long upTo, int last) {
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   194
            assert upTo - from + last > 0;
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   195
            this.from = from;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   196
            this.upTo = upTo;
18158
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   197
            this.last = last;
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   198
        }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   199
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   200
        @Override
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   201
        public boolean tryAdvance(LongConsumer consumer) {
18158
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   202
            final long i = from;
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   203
            if (i < upTo) {
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   204
                from++;
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   205
                consumer.accept(i);
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   206
                return true;
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   207
            }
18158
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   208
            else if (last > 0) {
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   209
                last = 0;
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   210
                consumer.accept(i);
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   211
                return true;
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   212
            }
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   213
            return false;
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   214
        }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   215
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   216
        @Override
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   217
        public void forEachRemaining(LongConsumer consumer) {
18158
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   218
            long i = from;
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   219
            final long hUpTo = upTo;
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   220
            int hLast = last;
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   221
            from = upTo;
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   222
            last = 0;
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   223
            while (i < hUpTo) {
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   224
                consumer.accept(i++);
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   225
            }
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   226
            if (hLast > 0) {
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   227
                // Last element of closed range
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   228
                consumer.accept(i);
18158
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   229
            }
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   230
        }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   231
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   232
        @Override
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   233
        public long estimateSize() {
18158
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   234
            return upTo - from + last;
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   235
        }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   236
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   237
        @Override
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   238
        public int characteristics() {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   239
            return Spliterator.ORDERED | Spliterator.SIZED | Spliterator.SUBSIZED |
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   240
                   Spliterator.IMMUTABLE | Spliterator.NONNULL |
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   241
                   Spliterator.DISTINCT | Spliterator.SORTED;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   242
        }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   243
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   244
        @Override
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   245
        public Comparator<? super Long> getComparator() {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   246
            return null;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   247
        }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   248
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   249
        @Override
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   250
        public Spliterator.OfLong trySplit() {
18158
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   251
            long size = estimateSize();
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   252
            return size <= 1
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   253
                   ? null
18158
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   254
                   // Left split always has a half-open range
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   255
                   : new RangeLongSpliterator(from, from = from + splitPoint(size), 0);
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   256
        }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   257
18158
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   258
        /**
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   259
         * The spliterator size below which the spliterator will be split
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   260
         * at the mid-point to produce balanced splits. Above this size the
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   261
         * spliterator will be split at a ratio of
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   262
         * 1:(RIGHT_BALANCED_SPLIT_RATIO - 1)
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   263
         * to produce right-balanced splits.
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   264
         *
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   265
         * <p>Such splitting ensures that for very large ranges that the left
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   266
         * side of the range will more likely be processed at a lower-depth
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   267
         * than a balanced tree at the expense of a higher-depth for the right
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   268
         * side of the range.
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   269
         *
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   270
         * <p>This is optimized for cases such as LongStream.longs() that is
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   271
         * implemented as range of 0 to Long.MAX_VALUE but is likely to be
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   272
         * augmented with a limit operation that limits the number of elements
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   273
         * to a count lower than this threshold.
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   274
         */
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   275
        private static final long BALANCED_SPLIT_THRESHOLD = 1 << 24;
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   276
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   277
        /**
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   278
         * The split ratio of the left and right split when the spliterator
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   279
         * size is above BALANCED_SPLIT_THRESHOLD.
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   280
         */
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   281
        private static final long RIGHT_BALANCED_SPLIT_RATIO = 1 << 3;
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   282
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   283
        private long splitPoint(long size) {
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   284
            long d = (size < BALANCED_SPLIT_THRESHOLD) ? 2 : RIGHT_BALANCED_SPLIT_RATIO;
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   285
            // 2 <= size <= Long.MAX_VALUE
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   286
            return size / d;
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   287
        }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   288
    }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   289
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   290
    private static abstract class AbstractStreamBuilderImpl<T, S extends Spliterator<T>> implements Spliterator<T> {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   291
        // >= 0 when building, < 0 when built
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   292
        // -1 == no elements
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   293
        // -2 == one element, held by first
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   294
        // -3 == two or more elements, held by buffer
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   295
        int count;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   296
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   297
        // Spliterator implementation for 0 or 1 element
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   298
        // count == -1 for no elements
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   299
        // count == -2 for one element held by first
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   300
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   301
        @Override
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   302
        public S trySplit() {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   303
            return null;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   304
        }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   305
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   306
        @Override
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   307
        public long estimateSize() {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   308
            return -count - 1;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   309
        }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   310
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   311
        @Override
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   312
        public int characteristics() {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   313
            return Spliterator.SIZED | Spliterator.SUBSIZED |
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   314
                   Spliterator.ORDERED | Spliterator.IMMUTABLE;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   315
        }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   316
    }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   317
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   318
    static final class StreamBuilderImpl<T>
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   319
            extends AbstractStreamBuilderImpl<T, Spliterator<T>>
18825
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
   320
            implements Stream.Builder<T> {
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   321
        // The first element in the stream
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   322
        // valid if count == 1
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   323
        T first;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   324
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   325
        // The first and subsequent elements in the stream
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   326
        // non-null if count == 2
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   327
        SpinedBuffer<T> buffer;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   328
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   329
        /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   330
         * Constructor for building a stream of 0 or more elements.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   331
         */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   332
        StreamBuilderImpl() { }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   333
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   334
        /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   335
         * Constructor for a singleton stream.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   336
         *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   337
         * @param t the single element
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   338
         */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   339
        StreamBuilderImpl(T t) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   340
            first = t;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   341
            count = -2;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   342
        }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   343
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   344
        // StreamBuilder implementation
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   345
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   346
        @Override
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   347
        public void accept(T t) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   348
            if (count == 0) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   349
                first = t;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   350
                count++;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   351
            }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   352
            else if (count > 0) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   353
                if (buffer == null) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   354
                    buffer = new SpinedBuffer<>();
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   355
                    buffer.accept(first);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   356
                    count++;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   357
                }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   358
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   359
                buffer.accept(t);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   360
            }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   361
            else {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   362
                throw new IllegalStateException();
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   363
            }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   364
        }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   365
18825
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
   366
        public Stream.Builder<T> add(T t) {
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   367
            accept(t);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   368
            return this;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   369
        }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   370
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   371
        @Override
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   372
        public Stream<T> build() {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   373
            int c = count;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   374
            if (c >= 0) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   375
                // Switch count to negative value signalling the builder is built
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   376
                count = -count - 1;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   377
                // Use this spliterator if 0 or 1 elements, otherwise use
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   378
                // the spliterator of the spined buffer
18822
4b6be7c19547 8019395: Consolidate StreamSupport.{stream,parallelStream} into a single method
psandoz
parents: 18820
diff changeset
   379
                return (c < 2) ? StreamSupport.stream(this, false) : StreamSupport.stream(buffer.spliterator(), false);
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   380
            }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   381
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   382
            throw new IllegalStateException();
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   383
        }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   384
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   385
        // Spliterator implementation for 0 or 1 element
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   386
        // count == -1 for no elements
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   387
        // count == -2 for one element held by first
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   388
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   389
        @Override
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   390
        public boolean tryAdvance(Consumer<? super T> action) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   391
            if (count == -2) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   392
                action.accept(first);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   393
                count = -1;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   394
                return true;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   395
            }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   396
            else {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   397
                return false;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   398
            }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   399
        }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   400
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   401
        @Override
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   402
        public void forEachRemaining(Consumer<? super T> action) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   403
            if (count == -2) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   404
                action.accept(first);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   405
                count = -1;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   406
            }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   407
        }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   408
    }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   409
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   410
    static final class IntStreamBuilderImpl
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   411
            extends AbstractStreamBuilderImpl<Integer, Spliterator.OfInt>
18825
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
   412
            implements IntStream.Builder, Spliterator.OfInt {
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   413
        // The first element in the stream
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   414
        // valid if count == 1
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   415
        int first;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   416
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   417
        // The first and subsequent elements in the stream
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   418
        // non-null if count == 2
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   419
        SpinedBuffer.OfInt buffer;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   420
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   421
        /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   422
         * Constructor for building a stream of 0 or more elements.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   423
         */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   424
        IntStreamBuilderImpl() { }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   425
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   426
        /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   427
         * Constructor for a singleton stream.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   428
         *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   429
         * @param t the single element
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   430
         */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   431
        IntStreamBuilderImpl(int t) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   432
            first = t;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   433
            count = -2;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   434
        }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   435
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   436
        // StreamBuilder implementation
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   437
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   438
        @Override
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   439
        public void accept(int t) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   440
            if (count == 0) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   441
                first = t;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   442
                count++;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   443
            }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   444
            else if (count > 0) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   445
                if (buffer == null) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   446
                    buffer = new SpinedBuffer.OfInt();
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   447
                    buffer.accept(first);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   448
                    count++;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   449
                }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   450
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   451
                buffer.accept(t);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   452
            }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   453
            else {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   454
                throw new IllegalStateException();
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   455
            }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   456
        }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   457
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   458
        @Override
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   459
        public IntStream build() {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   460
            int c = count;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   461
            if (c >= 0) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   462
                // Switch count to negative value signalling the builder is built
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   463
                count = -count - 1;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   464
                // Use this spliterator if 0 or 1 elements, otherwise use
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   465
                // the spliterator of the spined buffer
18822
4b6be7c19547 8019395: Consolidate StreamSupport.{stream,parallelStream} into a single method
psandoz
parents: 18820
diff changeset
   466
                return (c < 2) ? StreamSupport.intStream(this, false) : StreamSupport.intStream(buffer.spliterator(), false);
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   467
            }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   468
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   469
            throw new IllegalStateException();
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   470
        }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   471
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   472
        // Spliterator implementation for 0 or 1 element
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   473
        // count == -1 for no elements
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   474
        // count == -2 for one element held by first
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   475
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   476
        @Override
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   477
        public boolean tryAdvance(IntConsumer action) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   478
            if (count == -2) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   479
                action.accept(first);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   480
                count = -1;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   481
                return true;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   482
            }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   483
            else {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   484
                return false;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   485
            }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   486
        }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   487
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   488
        @Override
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   489
        public void forEachRemaining(IntConsumer action) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   490
            if (count == -2) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   491
                action.accept(first);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   492
                count = -1;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   493
            }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   494
        }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   495
    }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   496
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   497
    static final class LongStreamBuilderImpl
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   498
            extends AbstractStreamBuilderImpl<Long, Spliterator.OfLong>
18825
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
   499
            implements LongStream.Builder, Spliterator.OfLong {
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   500
        // The first element in the stream
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   501
        // valid if count == 1
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   502
        long first;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   503
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   504
        // The first and subsequent elements in the stream
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   505
        // non-null if count == 2
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   506
        SpinedBuffer.OfLong buffer;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   507
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   508
        /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   509
         * Constructor for building a stream of 0 or more elements.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   510
         */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   511
        LongStreamBuilderImpl() { }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   512
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   513
        /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   514
         * Constructor for a singleton stream.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   515
         *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   516
         * @param t the single element
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   517
         */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   518
        LongStreamBuilderImpl(long t) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   519
            first = t;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   520
            count = -2;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   521
        }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   522
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   523
        // StreamBuilder implementation
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   524
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   525
        @Override
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   526
        public void accept(long t) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   527
            if (count == 0) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   528
                first = t;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   529
                count++;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   530
            }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   531
            else if (count > 0) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   532
                if (buffer == null) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   533
                    buffer = new SpinedBuffer.OfLong();
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   534
                    buffer.accept(first);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   535
                    count++;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   536
                }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   537
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   538
                buffer.accept(t);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   539
            }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   540
            else {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   541
                throw new IllegalStateException();
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   542
            }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   543
        }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   544
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   545
        @Override
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   546
        public LongStream build() {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   547
            int c = count;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   548
            if (c >= 0) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   549
                // Switch count to negative value signalling the builder is built
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   550
                count = -count - 1;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   551
                // Use this spliterator if 0 or 1 elements, otherwise use
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   552
                // the spliterator of the spined buffer
18822
4b6be7c19547 8019395: Consolidate StreamSupport.{stream,parallelStream} into a single method
psandoz
parents: 18820
diff changeset
   553
                return (c < 2) ? StreamSupport.longStream(this, false) : StreamSupport.longStream(buffer.spliterator(), false);
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   554
            }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   555
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   556
            throw new IllegalStateException();
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   557
        }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   558
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   559
        // Spliterator implementation for 0 or 1 element
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   560
        // count == -1 for no elements
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   561
        // count == -2 for one element held by first
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   562
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   563
        @Override
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   564
        public boolean tryAdvance(LongConsumer action) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   565
            if (count == -2) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   566
                action.accept(first);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   567
                count = -1;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   568
                return true;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   569
            }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   570
            else {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   571
                return false;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   572
            }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   573
        }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   574
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   575
        @Override
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   576
        public void forEachRemaining(LongConsumer action) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   577
            if (count == -2) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   578
                action.accept(first);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   579
                count = -1;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   580
            }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   581
        }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   582
    }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   583
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   584
    static final class DoubleStreamBuilderImpl
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   585
            extends AbstractStreamBuilderImpl<Double, Spliterator.OfDouble>
18825
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
   586
            implements DoubleStream.Builder, Spliterator.OfDouble {
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   587
        // The first element in the stream
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   588
        // valid if count == 1
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   589
        double first;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   590
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   591
        // The first and subsequent elements in the stream
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   592
        // non-null if count == 2
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   593
        SpinedBuffer.OfDouble buffer;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   594
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   595
        /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   596
         * Constructor for building a stream of 0 or more elements.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   597
         */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   598
        DoubleStreamBuilderImpl() { }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   599
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   600
        /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   601
         * Constructor for a singleton stream.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   602
         *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   603
         * @param t the single element
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   604
         */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   605
        DoubleStreamBuilderImpl(double t) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   606
            first = t;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   607
            count = -2;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   608
        }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   609
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   610
        // StreamBuilder implementation
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   611
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   612
        @Override
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   613
        public void accept(double t) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   614
            if (count == 0) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   615
                first = t;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   616
                count++;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   617
            }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   618
            else if (count > 0) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   619
                if (buffer == null) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   620
                    buffer = new SpinedBuffer.OfDouble();
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   621
                    buffer.accept(first);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   622
                    count++;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   623
                }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   624
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   625
                buffer.accept(t);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   626
            }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   627
            else {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   628
                throw new IllegalStateException();
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   629
            }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   630
        }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   631
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   632
        @Override
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   633
        public DoubleStream build() {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   634
            int c = count;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   635
            if (c >= 0) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   636
                // Switch count to negative value signalling the builder is built
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   637
                count = -count - 1;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   638
                // Use this spliterator if 0 or 1 elements, otherwise use
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   639
                // the spliterator of the spined buffer
18822
4b6be7c19547 8019395: Consolidate StreamSupport.{stream,parallelStream} into a single method
psandoz
parents: 18820
diff changeset
   640
                return (c < 2) ? StreamSupport.doubleStream(this, false) : StreamSupport.doubleStream(buffer.spliterator(), false);
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   641
            }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   642
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   643
            throw new IllegalStateException();
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   644
        }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   645
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   646
        // Spliterator implementation for 0 or 1 element
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   647
        // count == -1 for no elements
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   648
        // count == -2 for one element held by first
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   649
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   650
        @Override
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   651
        public boolean tryAdvance(DoubleConsumer action) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   652
            if (count == -2) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   653
                action.accept(first);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   654
                count = -1;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   655
                return true;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   656
            }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   657
            else {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   658
                return false;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   659
            }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   660
        }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   661
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   662
        @Override
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   663
        public void forEachRemaining(DoubleConsumer action) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   664
            if (count == -2) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   665
                action.accept(first);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   666
                count = -1;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   667
            }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   668
        }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   669
    }
18820
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   670
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   671
    abstract static class ConcatSpliterator<T, T_SPLITR extends Spliterator<T>>
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   672
            implements Spliterator<T> {
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   673
        protected final T_SPLITR aSpliterator;
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   674
        protected final T_SPLITR bSpliterator;
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   675
        // True when no split has occurred, otherwise false
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   676
        boolean beforeSplit;
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   677
        // Never read after splitting
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   678
        final boolean unsized;
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   679
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   680
        public ConcatSpliterator(T_SPLITR aSpliterator, T_SPLITR bSpliterator) {
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   681
            this.aSpliterator = aSpliterator;
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   682
            this.bSpliterator = bSpliterator;
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   683
            beforeSplit = true;
19072
8f176099ec00 8021863: Stream.concat incorrectly calculates unsized state
psandoz
parents: 18825
diff changeset
   684
            // The spliterator is known to be unsized before splitting if the
8f176099ec00 8021863: Stream.concat incorrectly calculates unsized state
psandoz
parents: 18825
diff changeset
   685
            // sum of the estimates overflows.
8f176099ec00 8021863: Stream.concat incorrectly calculates unsized state
psandoz
parents: 18825
diff changeset
   686
            unsized = aSpliterator.estimateSize() + bSpliterator.estimateSize() < 0;
18820
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   687
        }
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   688
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   689
        @Override
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   690
        public T_SPLITR trySplit() {
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   691
            T_SPLITR ret = beforeSplit ? aSpliterator : (T_SPLITR) bSpliterator.trySplit();
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   692
            beforeSplit = false;
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   693
            return ret;
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   694
        }
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   695
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   696
        @Override
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   697
        public boolean tryAdvance(Consumer<? super T> consumer) {
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   698
            boolean hasNext;
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   699
            if (beforeSplit) {
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   700
                hasNext = aSpliterator.tryAdvance(consumer);
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   701
                if (!hasNext) {
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   702
                    beforeSplit = false;
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   703
                    hasNext = bSpliterator.tryAdvance(consumer);
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   704
                }
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   705
            }
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   706
            else
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   707
                hasNext = bSpliterator.tryAdvance(consumer);
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   708
            return hasNext;
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   709
        }
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   710
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   711
        @Override
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   712
        public void forEachRemaining(Consumer<? super T> consumer) {
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   713
            if (beforeSplit)
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   714
                aSpliterator.forEachRemaining(consumer);
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   715
            bSpliterator.forEachRemaining(consumer);
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   716
        }
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   717
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   718
        @Override
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   719
        public long estimateSize() {
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   720
            if (beforeSplit) {
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   721
                // If one or both estimates are Long.MAX_VALUE then the sum
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   722
                // will either be Long.MAX_VALUE or overflow to a negative value
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   723
                long size = aSpliterator.estimateSize() + bSpliterator.estimateSize();
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   724
                return (size >= 0) ? size : Long.MAX_VALUE;
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   725
            }
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   726
            else {
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   727
                return bSpliterator.estimateSize();
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   728
            }
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   729
        }
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   730
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   731
        @Override
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   732
        public int characteristics() {
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   733
            if (beforeSplit) {
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   734
                // Concatenation loses DISTINCT and SORTED characteristics
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   735
                return aSpliterator.characteristics() & bSpliterator.characteristics()
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   736
                       & ~(Spliterator.DISTINCT | Spliterator.SORTED
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   737
                           | (unsized ? Spliterator.SIZED | Spliterator.SUBSIZED : 0));
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   738
            }
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   739
            else {
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   740
                return bSpliterator.characteristics();
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   741
            }
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   742
        }
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   743
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   744
        @Override
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   745
        public Comparator<? super T> getComparator() {
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   746
            if (beforeSplit)
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   747
                throw new IllegalStateException();
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   748
            return bSpliterator.getComparator();
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   749
        }
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   750
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   751
        static class OfRef<T> extends ConcatSpliterator<T, Spliterator<T>> {
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   752
            OfRef(Spliterator<T> aSpliterator, Spliterator<T> bSpliterator) {
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   753
                super(aSpliterator, bSpliterator);
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   754
            }
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   755
        }
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   756
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   757
        private static abstract class OfPrimitive<T, T_CONS, T_SPLITR extends Spliterator.OfPrimitive<T, T_CONS, T_SPLITR>>
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   758
                extends ConcatSpliterator<T, T_SPLITR>
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   759
                implements Spliterator.OfPrimitive<T, T_CONS, T_SPLITR> {
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   760
            private OfPrimitive(T_SPLITR aSpliterator, T_SPLITR bSpliterator) {
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   761
                super(aSpliterator, bSpliterator);
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   762
            }
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   763
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   764
            @Override
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   765
            public boolean tryAdvance(T_CONS action) {
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   766
                boolean hasNext;
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   767
                if (beforeSplit) {
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   768
                    hasNext = aSpliterator.tryAdvance(action);
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   769
                    if (!hasNext) {
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   770
                        beforeSplit = false;
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   771
                        hasNext = bSpliterator.tryAdvance(action);
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   772
                    }
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   773
                }
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   774
                else
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   775
                    hasNext = bSpliterator.tryAdvance(action);
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   776
                return hasNext;
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   777
            }
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   778
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   779
            @Override
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   780
            public void forEachRemaining(T_CONS action) {
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   781
                if (beforeSplit)
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   782
                    aSpliterator.forEachRemaining(action);
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   783
                bSpliterator.forEachRemaining(action);
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   784
            }
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   785
        }
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   786
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   787
        static class OfInt
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   788
                extends ConcatSpliterator.OfPrimitive<Integer, IntConsumer, Spliterator.OfInt>
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   789
                implements Spliterator.OfInt {
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   790
            OfInt(Spliterator.OfInt aSpliterator, Spliterator.OfInt bSpliterator) {
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   791
                super(aSpliterator, bSpliterator);
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   792
            }
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   793
        }
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   794
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   795
        static class OfLong
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   796
                extends ConcatSpliterator.OfPrimitive<Long, LongConsumer, Spliterator.OfLong>
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   797
                implements Spliterator.OfLong {
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   798
            OfLong(Spliterator.OfLong aSpliterator, Spliterator.OfLong bSpliterator) {
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   799
                super(aSpliterator, bSpliterator);
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   800
            }
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   801
        }
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   802
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   803
        static class OfDouble
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   804
                extends ConcatSpliterator.OfPrimitive<Double, DoubleConsumer, Spliterator.OfDouble>
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   805
                implements Spliterator.OfDouble {
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   806
            OfDouble(Spliterator.OfDouble aSpliterator, Spliterator.OfDouble bSpliterator) {
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   807
                super(aSpliterator, bSpliterator);
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   808
            }
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   809
        }
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   810
    }
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   811
}