jdk/src/share/classes/java/util/stream/Streams.java
author briangoetz
Fri, 17 Jan 2014 12:28:59 -0500
changeset 22297 1c62c67d9dd2
parent 20507 8498104f92c3
permissions -rw-r--r--
8031373: Lint warnings in java.util.stream 8030079: Lint warnings in java.lang.invoke Reviewed-by: psandoz, forax
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;
19188
bbf287c5cd92 8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents: 19072
diff changeset
    28
import java.util.Objects;
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
    29
import java.util.Spliterator;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
    30
import java.util.function.Consumer;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
    31
import java.util.function.DoubleConsumer;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
    32
import java.util.function.IntConsumer;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
    33
import java.util.function.LongConsumer;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
    34
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
    35
/**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
    36
 * Utility methods for operating on and creating streams.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
    37
 *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
    38
 * <p>Unless otherwise stated, streams are created as sequential streams.  A
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
    39
 * sequential stream can be transformed into a parallel stream by calling the
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
    40
 * {@code parallel()} method on the created stream.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
    41
 *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
    42
 * @since 1.8
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
    43
 */
18820
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
    44
final class Streams {
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
    45
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
    46
    private Streams() {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
    47
        throw new Error("no instances");
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
    /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
    51
     * An object instance representing no value, that cannot be an actual
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
    52
     * data element of a stream.  Used when processing streams that can contain
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
    53
     * {@code null} elements to distinguish between a {@code null} value and no
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
    54
     * value.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
    55
     */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
    56
    static final Object NONE = new Object();
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
    57
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
    58
    /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
    59
     * An {@code int} range spliterator.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
    60
     */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
    61
    static final class RangeIntSpliterator implements Spliterator.OfInt {
18158
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
    62
        // Can never be greater that upTo, this avoids overflow if upper bound
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
    63
        // is Integer.MAX_VALUE
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
    64
        // All elements are traversed if from == upTo & last == 0
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
    65
        private int from;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
    66
        private final int upTo;
18158
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
    67
        // 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
    68
        // 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
    69
        // elements have been traversed
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
    70
        private int last;
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
    71
18158
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
    72
        RangeIntSpliterator(int from, int upTo, boolean closed) {
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
    73
            this(from, upTo, closed ? 1 : 0);
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
    74
        }
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
    75
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
    76
        private RangeIntSpliterator(int from, int upTo, int last) {
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
    77
            this.from = from;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
    78
            this.upTo = upTo;
18158
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
    79
            this.last = last;
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
    80
        }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
    81
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
    82
        @Override
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
    83
        public boolean tryAdvance(IntConsumer consumer) {
19188
bbf287c5cd92 8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents: 19072
diff changeset
    84
            Objects.requireNonNull(consumer);
bbf287c5cd92 8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents: 19072
diff changeset
    85
18158
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
    86
            final int i = from;
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
    87
            if (i < upTo) {
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
    88
                from++;
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
    89
                consumer.accept(i);
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
    90
                return true;
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
    91
            }
18158
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
    92
            else if (last > 0) {
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
    93
                last = 0;
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
    94
                consumer.accept(i);
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
    95
                return true;
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
    96
            }
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
    97
            return false;
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
    98
        }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
    99
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   100
        @Override
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   101
        public void forEachRemaining(IntConsumer consumer) {
19188
bbf287c5cd92 8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents: 19072
diff changeset
   102
            Objects.requireNonNull(consumer);
bbf287c5cd92 8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents: 19072
diff changeset
   103
18158
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   104
            int i = from;
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   105
            final int hUpTo = upTo;
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   106
            int hLast = last;
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   107
            from = upTo;
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   108
            last = 0;
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   109
            while (i < hUpTo) {
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   110
                consumer.accept(i++);
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   111
            }
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   112
            if (hLast > 0) {
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   113
                // Last element of closed range
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   114
                consumer.accept(i);
18158
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   115
            }
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   116
        }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   117
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   118
        @Override
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   119
        public long estimateSize() {
18158
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   120
            // Ensure ranges of size > Integer.MAX_VALUE report the correct size
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   121
            return ((long) upTo) - from + last;
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   122
        }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   123
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   124
        @Override
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   125
        public int characteristics() {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   126
            return Spliterator.ORDERED | Spliterator.SIZED | Spliterator.SUBSIZED |
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   127
                   Spliterator.IMMUTABLE | Spliterator.NONNULL |
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   128
                   Spliterator.DISTINCT | Spliterator.SORTED;
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 Comparator<? super Integer> getComparator() {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   133
            return null;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   134
        }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   135
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   136
        @Override
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   137
        public Spliterator.OfInt trySplit() {
18158
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   138
            long size = estimateSize();
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   139
            return size <= 1
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   140
                   ? null
18158
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   141
                   // Left split always has a half-open range
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   142
                   : new RangeIntSpliterator(from, from = from + splitPoint(size), 0);
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   143
        }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   144
18158
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   145
        /**
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   146
         * The spliterator size below which the spliterator will be split
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   147
         * at the mid-point to produce balanced splits. Above this size the
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   148
         * spliterator will be split at a ratio of
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   149
         * 1:(RIGHT_BALANCED_SPLIT_RATIO - 1)
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   150
         * to produce right-balanced splits.
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>Such splitting ensures that for very large ranges that the left
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   153
         * side of the range will more likely be processed at a lower-depth
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   154
         * 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
   155
         * side of the range.
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   156
         *
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   157
         * <p>This is optimized for cases such as IntStream.ints() that is
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   158
         * 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
   159
         * augmented with a limit operation that limits the number of elements
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   160
         * to a count lower than this threshold.
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   161
         */
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   162
        private static final int BALANCED_SPLIT_THRESHOLD = 1 << 24;
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   163
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   164
        /**
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   165
         * The split ratio of the left and right split when the spliterator
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   166
         * size is above BALANCED_SPLIT_THRESHOLD.
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   167
         */
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   168
        private static final int RIGHT_BALANCED_SPLIT_RATIO = 1 << 3;
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   169
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   170
        private int splitPoint(long size) {
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   171
            int d = (size < BALANCED_SPLIT_THRESHOLD) ? 2 : RIGHT_BALANCED_SPLIT_RATIO;
20507
8498104f92c3 8025534: Unsafe typecast in java.util.stream.Streams.Nodes
psandoz
parents: 19800
diff changeset
   172
            // Cast to int is safe since:
8498104f92c3 8025534: Unsafe typecast in java.util.stream.Streams.Nodes
psandoz
parents: 19800
diff changeset
   173
            //   2 <= size < 2^32
8498104f92c3 8025534: Unsafe typecast in java.util.stream.Streams.Nodes
psandoz
parents: 19800
diff changeset
   174
            //   2 <= d <= 8
18158
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   175
            return (int) (size / d);
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   176
        }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   177
    }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   178
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   179
    /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   180
     * A {@code long} range spliterator.
18158
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   181
     *
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   182
     * This implementation cannot be used for ranges whose size is greater
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   183
     * than Long.MAX_VALUE
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   184
     */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   185
    static final class RangeLongSpliterator implements Spliterator.OfLong {
18158
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   186
        // Can never be greater that upTo, this avoids overflow if upper bound
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   187
        // is Long.MAX_VALUE
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   188
        // All elements are traversed if from == upTo & last == 0
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   189
        private long from;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   190
        private final long upTo;
18158
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   191
        // 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
   192
        // 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
   193
        // elements have been traversed
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   194
        private int last;
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   195
18158
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   196
        RangeLongSpliterator(long from, long upTo, boolean closed) {
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   197
            this(from, upTo, closed ? 1 : 0);
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   198
        }
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   199
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   200
        private RangeLongSpliterator(long from, long upTo, int last) {
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   201
            assert upTo - from + last > 0;
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   202
            this.from = from;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   203
            this.upTo = upTo;
18158
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   204
            this.last = last;
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   205
        }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   206
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   207
        @Override
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   208
        public boolean tryAdvance(LongConsumer consumer) {
19188
bbf287c5cd92 8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents: 19072
diff changeset
   209
            Objects.requireNonNull(consumer);
bbf287c5cd92 8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents: 19072
diff changeset
   210
18158
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   211
            final long i = from;
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   212
            if (i < upTo) {
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   213
                from++;
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   214
                consumer.accept(i);
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   215
                return true;
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   216
            }
18158
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   217
            else if (last > 0) {
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   218
                last = 0;
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   219
                consumer.accept(i);
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   220
                return true;
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   221
            }
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   222
            return false;
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   223
        }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   224
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   225
        @Override
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   226
        public void forEachRemaining(LongConsumer consumer) {
19188
bbf287c5cd92 8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents: 19072
diff changeset
   227
            Objects.requireNonNull(consumer);
bbf287c5cd92 8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents: 19072
diff changeset
   228
18158
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   229
            long i = from;
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   230
            final long hUpTo = upTo;
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   231
            int hLast = last;
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   232
            from = upTo;
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   233
            last = 0;
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   234
            while (i < hUpTo) {
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   235
                consumer.accept(i++);
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   236
            }
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   237
            if (hLast > 0) {
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   238
                // Last element of closed range
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   239
                consumer.accept(i);
18158
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   240
            }
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   241
        }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   242
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   243
        @Override
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   244
        public long estimateSize() {
18158
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   245
            return upTo - from + last;
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   246
        }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   247
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   248
        @Override
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   249
        public int characteristics() {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   250
            return Spliterator.ORDERED | Spliterator.SIZED | Spliterator.SUBSIZED |
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   251
                   Spliterator.IMMUTABLE | Spliterator.NONNULL |
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   252
                   Spliterator.DISTINCT | Spliterator.SORTED;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   253
        }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   254
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   255
        @Override
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   256
        public Comparator<? super Long> getComparator() {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   257
            return null;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   258
        }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   259
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   260
        @Override
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   261
        public Spliterator.OfLong trySplit() {
18158
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   262
            long size = estimateSize();
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   263
            return size <= 1
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   264
                   ? null
18158
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   265
                   // Left split always has a half-open range
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   266
                   : new RangeLongSpliterator(from, from = from + splitPoint(size), 0);
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   267
        }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   268
18158
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   269
        /**
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   270
         * The spliterator size below which the spliterator will be split
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   271
         * at the mid-point to produce balanced splits. Above this size the
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   272
         * spliterator will be split at a ratio of
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   273
         * 1:(RIGHT_BALANCED_SPLIT_RATIO - 1)
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   274
         * to produce right-balanced splits.
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   275
         *
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   276
         * <p>Such splitting ensures that for very large ranges that the left
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   277
         * side of the range will more likely be processed at a lower-depth
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   278
         * 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
   279
         * side of the range.
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   280
         *
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   281
         * <p>This is optimized for cases such as LongStream.longs() that is
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   282
         * 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
   283
         * augmented with a limit operation that limits the number of elements
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   284
         * to a count lower than this threshold.
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   285
         */
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   286
        private static final long BALANCED_SPLIT_THRESHOLD = 1 << 24;
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   287
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   288
        /**
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   289
         * The split ratio of the left and right split when the spliterator
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   290
         * size is above BALANCED_SPLIT_THRESHOLD.
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   291
         */
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   292
        private static final long RIGHT_BALANCED_SPLIT_RATIO = 1 << 3;
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   293
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   294
        private long splitPoint(long size) {
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   295
            long d = (size < BALANCED_SPLIT_THRESHOLD) ? 2 : RIGHT_BALANCED_SPLIT_RATIO;
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   296
            // 2 <= size <= Long.MAX_VALUE
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   297
            return size / d;
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   298
        }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   299
    }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   300
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   301
    private static abstract class AbstractStreamBuilderImpl<T, S extends Spliterator<T>> implements Spliterator<T> {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   302
        // >= 0 when building, < 0 when built
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   303
        // -1 == no elements
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   304
        // -2 == one element, held by first
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   305
        // -3 == two or more elements, held by buffer
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   306
        int count;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   307
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   308
        // Spliterator implementation for 0 or 1 element
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   309
        // count == -1 for no elements
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   310
        // count == -2 for one element held by first
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   311
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   312
        @Override
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   313
        public S trySplit() {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   314
            return null;
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
        @Override
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   318
        public long estimateSize() {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   319
            return -count - 1;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   320
        }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   321
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   322
        @Override
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   323
        public int characteristics() {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   324
            return Spliterator.SIZED | Spliterator.SUBSIZED |
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   325
                   Spliterator.ORDERED | Spliterator.IMMUTABLE;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   326
        }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   327
    }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   328
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   329
    static final class StreamBuilderImpl<T>
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   330
            extends AbstractStreamBuilderImpl<T, Spliterator<T>>
18825
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
   331
            implements Stream.Builder<T> {
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   332
        // The first element in the stream
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   333
        // valid if count == 1
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   334
        T first;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   335
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   336
        // The first and subsequent elements in the stream
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   337
        // non-null if count == 2
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   338
        SpinedBuffer<T> buffer;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   339
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   340
        /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   341
         * Constructor for building a stream of 0 or more elements.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   342
         */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   343
        StreamBuilderImpl() { }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   344
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   345
        /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   346
         * Constructor for a singleton stream.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   347
         *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   348
         * @param t the single element
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   349
         */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   350
        StreamBuilderImpl(T t) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   351
            first = t;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   352
            count = -2;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   353
        }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   354
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   355
        // StreamBuilder implementation
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   356
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   357
        @Override
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   358
        public void accept(T t) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   359
            if (count == 0) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   360
                first = t;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   361
                count++;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   362
            }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   363
            else if (count > 0) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   364
                if (buffer == null) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   365
                    buffer = new SpinedBuffer<>();
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   366
                    buffer.accept(first);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   367
                    count++;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   368
                }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   369
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   370
                buffer.accept(t);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   371
            }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   372
            else {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   373
                throw new IllegalStateException();
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   374
            }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   375
        }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   376
18825
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
   377
        public Stream.Builder<T> add(T t) {
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   378
            accept(t);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   379
            return this;
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
        @Override
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   383
        public Stream<T> build() {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   384
            int c = count;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   385
            if (c >= 0) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   386
                // Switch count to negative value signalling the builder is built
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   387
                count = -count - 1;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   388
                // Use this spliterator if 0 or 1 elements, otherwise use
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   389
                // the spliterator of the spined buffer
18822
4b6be7c19547 8019395: Consolidate StreamSupport.{stream,parallelStream} into a single method
psandoz
parents: 18820
diff changeset
   390
                return (c < 2) ? StreamSupport.stream(this, false) : StreamSupport.stream(buffer.spliterator(), false);
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   391
            }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   392
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   393
            throw new IllegalStateException();
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   394
        }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   395
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   396
        // Spliterator implementation for 0 or 1 element
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   397
        // count == -1 for no elements
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   398
        // count == -2 for one element held by first
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   399
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   400
        @Override
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   401
        public boolean tryAdvance(Consumer<? super T> action) {
19188
bbf287c5cd92 8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents: 19072
diff changeset
   402
            Objects.requireNonNull(action);
bbf287c5cd92 8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents: 19072
diff changeset
   403
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   404
            if (count == -2) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   405
                action.accept(first);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   406
                count = -1;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   407
                return true;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   408
            }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   409
            else {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   410
                return false;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   411
            }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   412
        }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   413
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   414
        @Override
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   415
        public void forEachRemaining(Consumer<? super T> action) {
19188
bbf287c5cd92 8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents: 19072
diff changeset
   416
            Objects.requireNonNull(action);
bbf287c5cd92 8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents: 19072
diff changeset
   417
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   418
            if (count == -2) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   419
                action.accept(first);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   420
                count = -1;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   421
            }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   422
        }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   423
    }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   424
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   425
    static final class IntStreamBuilderImpl
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   426
            extends AbstractStreamBuilderImpl<Integer, Spliterator.OfInt>
18825
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
   427
            implements IntStream.Builder, Spliterator.OfInt {
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   428
        // The first element in the stream
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   429
        // valid if count == 1
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   430
        int first;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   431
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   432
        // The first and subsequent elements in the stream
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   433
        // non-null if count == 2
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   434
        SpinedBuffer.OfInt buffer;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   435
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   436
        /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   437
         * Constructor for building a stream of 0 or more elements.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   438
         */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   439
        IntStreamBuilderImpl() { }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   440
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   441
        /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   442
         * Constructor for a singleton stream.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   443
         *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   444
         * @param t the single element
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   445
         */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   446
        IntStreamBuilderImpl(int t) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   447
            first = t;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   448
            count = -2;
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
        // StreamBuilder implementation
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   452
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   453
        @Override
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   454
        public void accept(int t) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   455
            if (count == 0) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   456
                first = t;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   457
                count++;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   458
            }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   459
            else if (count > 0) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   460
                if (buffer == null) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   461
                    buffer = new SpinedBuffer.OfInt();
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   462
                    buffer.accept(first);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   463
                    count++;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   464
                }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   465
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   466
                buffer.accept(t);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   467
            }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   468
            else {
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
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   473
        @Override
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   474
        public IntStream build() {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   475
            int c = count;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   476
            if (c >= 0) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   477
                // Switch count to negative value signalling the builder is built
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   478
                count = -count - 1;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   479
                // Use this spliterator if 0 or 1 elements, otherwise use
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   480
                // the spliterator of the spined buffer
18822
4b6be7c19547 8019395: Consolidate StreamSupport.{stream,parallelStream} into a single method
psandoz
parents: 18820
diff changeset
   481
                return (c < 2) ? StreamSupport.intStream(this, false) : StreamSupport.intStream(buffer.spliterator(), false);
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   482
            }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   483
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   484
            throw new IllegalStateException();
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
        // Spliterator implementation for 0 or 1 element
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   488
        // count == -1 for no elements
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   489
        // count == -2 for one element held by first
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   490
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   491
        @Override
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   492
        public boolean tryAdvance(IntConsumer action) {
19188
bbf287c5cd92 8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents: 19072
diff changeset
   493
            Objects.requireNonNull(action);
bbf287c5cd92 8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents: 19072
diff changeset
   494
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   495
            if (count == -2) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   496
                action.accept(first);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   497
                count = -1;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   498
                return true;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   499
            }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   500
            else {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   501
                return false;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   502
            }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   503
        }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   504
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   505
        @Override
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   506
        public void forEachRemaining(IntConsumer action) {
19188
bbf287c5cd92 8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents: 19072
diff changeset
   507
            Objects.requireNonNull(action);
bbf287c5cd92 8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents: 19072
diff changeset
   508
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   509
            if (count == -2) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   510
                action.accept(first);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   511
                count = -1;
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
    }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   515
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   516
    static final class LongStreamBuilderImpl
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   517
            extends AbstractStreamBuilderImpl<Long, Spliterator.OfLong>
18825
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
   518
            implements LongStream.Builder, Spliterator.OfLong {
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   519
        // The first element in the stream
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   520
        // valid if count == 1
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   521
        long first;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   522
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   523
        // The first and subsequent elements in the stream
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   524
        // non-null if count == 2
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   525
        SpinedBuffer.OfLong buffer;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   526
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   527
        /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   528
         * Constructor for building a stream of 0 or more elements.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   529
         */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   530
        LongStreamBuilderImpl() { }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   531
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   532
        /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   533
         * Constructor for a singleton stream.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   534
         *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   535
         * @param t the single element
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   536
         */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   537
        LongStreamBuilderImpl(long t) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   538
            first = t;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   539
            count = -2;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   540
        }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   541
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   542
        // StreamBuilder implementation
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   543
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   544
        @Override
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   545
        public void accept(long t) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   546
            if (count == 0) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   547
                first = t;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   548
                count++;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   549
            }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   550
            else if (count > 0) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   551
                if (buffer == null) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   552
                    buffer = new SpinedBuffer.OfLong();
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   553
                    buffer.accept(first);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   554
                    count++;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   555
                }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   556
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   557
                buffer.accept(t);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   558
            }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   559
            else {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   560
                throw new IllegalStateException();
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   561
            }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   562
        }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   563
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   564
        @Override
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   565
        public LongStream build() {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   566
            int c = count;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   567
            if (c >= 0) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   568
                // Switch count to negative value signalling the builder is built
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   569
                count = -count - 1;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   570
                // Use this spliterator if 0 or 1 elements, otherwise use
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   571
                // the spliterator of the spined buffer
18822
4b6be7c19547 8019395: Consolidate StreamSupport.{stream,parallelStream} into a single method
psandoz
parents: 18820
diff changeset
   572
                return (c < 2) ? StreamSupport.longStream(this, false) : StreamSupport.longStream(buffer.spliterator(), false);
17195
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
            throw new IllegalStateException();
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   576
        }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   577
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   578
        // Spliterator implementation for 0 or 1 element
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   579
        // count == -1 for no elements
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   580
        // count == -2 for one element held by first
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   581
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   582
        @Override
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   583
        public boolean tryAdvance(LongConsumer action) {
19188
bbf287c5cd92 8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents: 19072
diff changeset
   584
            Objects.requireNonNull(action);
bbf287c5cd92 8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents: 19072
diff changeset
   585
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   586
            if (count == -2) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   587
                action.accept(first);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   588
                count = -1;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   589
                return true;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   590
            }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   591
            else {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   592
                return false;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   593
            }
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
        @Override
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   597
        public void forEachRemaining(LongConsumer action) {
19188
bbf287c5cd92 8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents: 19072
diff changeset
   598
            Objects.requireNonNull(action);
bbf287c5cd92 8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents: 19072
diff changeset
   599
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   600
            if (count == -2) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   601
                action.accept(first);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   602
                count = -1;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   603
            }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   604
        }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   605
    }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   606
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   607
    static final class DoubleStreamBuilderImpl
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   608
            extends AbstractStreamBuilderImpl<Double, Spliterator.OfDouble>
18825
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
   609
            implements DoubleStream.Builder, Spliterator.OfDouble {
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   610
        // The first element in the stream
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   611
        // valid if count == 1
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   612
        double first;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   613
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   614
        // The first and subsequent elements in the stream
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   615
        // non-null if count == 2
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   616
        SpinedBuffer.OfDouble buffer;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   617
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   618
        /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   619
         * Constructor for building a stream of 0 or more elements.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   620
         */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   621
        DoubleStreamBuilderImpl() { }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   622
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   623
        /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   624
         * Constructor for a singleton stream.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   625
         *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   626
         * @param t the single element
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   627
         */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   628
        DoubleStreamBuilderImpl(double t) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   629
            first = t;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   630
            count = -2;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   631
        }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   632
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   633
        // StreamBuilder implementation
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   634
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   635
        @Override
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   636
        public void accept(double t) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   637
            if (count == 0) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   638
                first = t;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   639
                count++;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   640
            }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   641
            else if (count > 0) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   642
                if (buffer == null) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   643
                    buffer = new SpinedBuffer.OfDouble();
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   644
                    buffer.accept(first);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   645
                    count++;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   646
                }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   647
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   648
                buffer.accept(t);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   649
            }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   650
            else {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   651
                throw new IllegalStateException();
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   652
            }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   653
        }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   654
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   655
        @Override
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   656
        public DoubleStream build() {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   657
            int c = count;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   658
            if (c >= 0) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   659
                // Switch count to negative value signalling the builder is built
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   660
                count = -count - 1;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   661
                // Use this spliterator if 0 or 1 elements, otherwise use
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   662
                // the spliterator of the spined buffer
18822
4b6be7c19547 8019395: Consolidate StreamSupport.{stream,parallelStream} into a single method
psandoz
parents: 18820
diff changeset
   663
                return (c < 2) ? StreamSupport.doubleStream(this, false) : StreamSupport.doubleStream(buffer.spliterator(), false);
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   664
            }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   665
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   666
            throw new IllegalStateException();
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
        // Spliterator implementation for 0 or 1 element
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   670
        // count == -1 for no elements
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   671
        // count == -2 for one element held by first
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   672
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   673
        @Override
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   674
        public boolean tryAdvance(DoubleConsumer action) {
19188
bbf287c5cd92 8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents: 19072
diff changeset
   675
            Objects.requireNonNull(action);
bbf287c5cd92 8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents: 19072
diff changeset
   676
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   677
            if (count == -2) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   678
                action.accept(first);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   679
                count = -1;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   680
                return true;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   681
            }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   682
            else {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   683
                return false;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   684
            }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   685
        }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   686
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   687
        @Override
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   688
        public void forEachRemaining(DoubleConsumer action) {
19188
bbf287c5cd92 8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents: 19072
diff changeset
   689
            Objects.requireNonNull(action);
bbf287c5cd92 8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents: 19072
diff changeset
   690
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   691
            if (count == -2) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   692
                action.accept(first);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   693
                count = -1;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   694
            }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   695
        }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   696
    }
18820
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   697
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   698
    abstract static class ConcatSpliterator<T, T_SPLITR extends Spliterator<T>>
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   699
            implements Spliterator<T> {
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   700
        protected final T_SPLITR aSpliterator;
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   701
        protected final T_SPLITR bSpliterator;
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   702
        // True when no split has occurred, otherwise false
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   703
        boolean beforeSplit;
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   704
        // Never read after splitting
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   705
        final boolean unsized;
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   706
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   707
        public ConcatSpliterator(T_SPLITR aSpliterator, T_SPLITR bSpliterator) {
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   708
            this.aSpliterator = aSpliterator;
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   709
            this.bSpliterator = bSpliterator;
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   710
            beforeSplit = true;
19072
8f176099ec00 8021863: Stream.concat incorrectly calculates unsized state
psandoz
parents: 18825
diff changeset
   711
            // The spliterator is known to be unsized before splitting if the
8f176099ec00 8021863: Stream.concat incorrectly calculates unsized state
psandoz
parents: 18825
diff changeset
   712
            // sum of the estimates overflows.
8f176099ec00 8021863: Stream.concat incorrectly calculates unsized state
psandoz
parents: 18825
diff changeset
   713
            unsized = aSpliterator.estimateSize() + bSpliterator.estimateSize() < 0;
18820
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   714
        }
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   715
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   716
        @Override
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   717
        public T_SPLITR trySplit() {
22297
1c62c67d9dd2 8031373: Lint warnings in java.util.stream
briangoetz
parents: 20507
diff changeset
   718
            @SuppressWarnings("unchecked")
18820
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   719
            T_SPLITR ret = beforeSplit ? aSpliterator : (T_SPLITR) bSpliterator.trySplit();
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   720
            beforeSplit = false;
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   721
            return ret;
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   722
        }
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   723
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   724
        @Override
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   725
        public boolean tryAdvance(Consumer<? super T> consumer) {
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   726
            boolean hasNext;
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   727
            if (beforeSplit) {
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   728
                hasNext = aSpliterator.tryAdvance(consumer);
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   729
                if (!hasNext) {
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   730
                    beforeSplit = false;
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   731
                    hasNext = bSpliterator.tryAdvance(consumer);
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   732
                }
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   733
            }
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   734
            else
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   735
                hasNext = bSpliterator.tryAdvance(consumer);
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   736
            return hasNext;
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   737
        }
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   738
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   739
        @Override
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   740
        public void forEachRemaining(Consumer<? super T> consumer) {
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   741
            if (beforeSplit)
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   742
                aSpliterator.forEachRemaining(consumer);
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   743
            bSpliterator.forEachRemaining(consumer);
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   744
        }
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   745
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   746
        @Override
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   747
        public long estimateSize() {
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   748
            if (beforeSplit) {
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   749
                // If one or both estimates are Long.MAX_VALUE then the sum
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   750
                // will either be Long.MAX_VALUE or overflow to a negative value
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   751
                long size = aSpliterator.estimateSize() + bSpliterator.estimateSize();
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   752
                return (size >= 0) ? size : Long.MAX_VALUE;
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   753
            }
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   754
            else {
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   755
                return bSpliterator.estimateSize();
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   756
            }
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   757
        }
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   758
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   759
        @Override
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   760
        public int characteristics() {
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   761
            if (beforeSplit) {
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   762
                // Concatenation loses DISTINCT and SORTED characteristics
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   763
                return aSpliterator.characteristics() & bSpliterator.characteristics()
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   764
                       & ~(Spliterator.DISTINCT | Spliterator.SORTED
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   765
                           | (unsized ? Spliterator.SIZED | Spliterator.SUBSIZED : 0));
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   766
            }
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   767
            else {
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   768
                return bSpliterator.characteristics();
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   769
            }
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   770
        }
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   771
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   772
        @Override
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   773
        public Comparator<? super T> getComparator() {
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   774
            if (beforeSplit)
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   775
                throw new IllegalStateException();
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   776
            return bSpliterator.getComparator();
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
        static class OfRef<T> extends ConcatSpliterator<T, Spliterator<T>> {
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   780
            OfRef(Spliterator<T> aSpliterator, Spliterator<T> bSpliterator) {
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   781
                super(aSpliterator, bSpliterator);
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   782
            }
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   783
        }
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   784
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   785
        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
   786
                extends ConcatSpliterator<T, T_SPLITR>
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   787
                implements Spliterator.OfPrimitive<T, T_CONS, T_SPLITR> {
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   788
            private OfPrimitive(T_SPLITR aSpliterator, T_SPLITR bSpliterator) {
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   789
                super(aSpliterator, bSpliterator);
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   790
            }
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   791
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   792
            @Override
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   793
            public boolean tryAdvance(T_CONS action) {
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   794
                boolean hasNext;
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   795
                if (beforeSplit) {
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   796
                    hasNext = aSpliterator.tryAdvance(action);
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   797
                    if (!hasNext) {
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   798
                        beforeSplit = false;
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   799
                        hasNext = bSpliterator.tryAdvance(action);
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
                else
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   803
                    hasNext = bSpliterator.tryAdvance(action);
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   804
                return hasNext;
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   805
            }
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   806
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   807
            @Override
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   808
            public void forEachRemaining(T_CONS action) {
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   809
                if (beforeSplit)
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   810
                    aSpliterator.forEachRemaining(action);
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   811
                bSpliterator.forEachRemaining(action);
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   812
            }
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   813
        }
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   814
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   815
        static class OfInt
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   816
                extends ConcatSpliterator.OfPrimitive<Integer, IntConsumer, Spliterator.OfInt>
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   817
                implements Spliterator.OfInt {
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   818
            OfInt(Spliterator.OfInt aSpliterator, Spliterator.OfInt bSpliterator) {
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   819
                super(aSpliterator, bSpliterator);
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   820
            }
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   821
        }
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   822
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   823
        static class OfLong
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   824
                extends ConcatSpliterator.OfPrimitive<Long, LongConsumer, Spliterator.OfLong>
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   825
                implements Spliterator.OfLong {
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   826
            OfLong(Spliterator.OfLong aSpliterator, Spliterator.OfLong bSpliterator) {
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   827
                super(aSpliterator, bSpliterator);
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   828
            }
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   829
        }
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   830
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   831
        static class OfDouble
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   832
                extends ConcatSpliterator.OfPrimitive<Double, DoubleConsumer, Spliterator.OfDouble>
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   833
                implements Spliterator.OfDouble {
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   834
            OfDouble(Spliterator.OfDouble aSpliterator, Spliterator.OfDouble bSpliterator) {
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   835
                super(aSpliterator, bSpliterator);
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   836
            }
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   837
        }
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   838
    }
19800
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19188
diff changeset
   839
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19188
diff changeset
   840
    /**
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19188
diff changeset
   841
     * Given two Runnables, return a Runnable that executes both in sequence,
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19188
diff changeset
   842
     * even if the first throws an exception, and if both throw exceptions, add
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19188
diff changeset
   843
     * any exceptions thrown by the second as suppressed exceptions of the first.
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19188
diff changeset
   844
     */
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19188
diff changeset
   845
    static Runnable composeWithExceptions(Runnable a, Runnable b) {
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19188
diff changeset
   846
        return new Runnable() {
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19188
diff changeset
   847
            @Override
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19188
diff changeset
   848
            public void run() {
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19188
diff changeset
   849
                try {
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19188
diff changeset
   850
                    a.run();
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19188
diff changeset
   851
                }
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19188
diff changeset
   852
                catch (Throwable e1) {
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19188
diff changeset
   853
                    try {
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19188
diff changeset
   854
                        b.run();
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19188
diff changeset
   855
                    }
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19188
diff changeset
   856
                    catch (Throwable e2) {
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19188
diff changeset
   857
                        try {
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19188
diff changeset
   858
                            e1.addSuppressed(e2);
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19188
diff changeset
   859
                        } catch (Throwable ignore) {}
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19188
diff changeset
   860
                    }
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19188
diff changeset
   861
                    throw e1;
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19188
diff changeset
   862
                }
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19188
diff changeset
   863
                b.run();
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19188
diff changeset
   864
            }
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19188
diff changeset
   865
        };
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19188
diff changeset
   866
    }
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19188
diff changeset
   867
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19188
diff changeset
   868
    /**
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19188
diff changeset
   869
     * Given two streams, return a Runnable that
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19188
diff changeset
   870
     * executes both of their {@link BaseStream#close} methods in sequence,
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19188
diff changeset
   871
     * even if the first throws an exception, and if both throw exceptions, add
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19188
diff changeset
   872
     * any exceptions thrown by the second as suppressed exceptions of the first.
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19188
diff changeset
   873
     */
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19188
diff changeset
   874
    static Runnable composedClose(BaseStream<?, ?> a, BaseStream<?, ?> b) {
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19188
diff changeset
   875
        return new Runnable() {
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19188
diff changeset
   876
            @Override
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19188
diff changeset
   877
            public void run() {
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19188
diff changeset
   878
                try {
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19188
diff changeset
   879
                    a.close();
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19188
diff changeset
   880
                }
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19188
diff changeset
   881
                catch (Throwable e1) {
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19188
diff changeset
   882
                    try {
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19188
diff changeset
   883
                        b.close();
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19188
diff changeset
   884
                    }
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19188
diff changeset
   885
                    catch (Throwable e2) {
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19188
diff changeset
   886
                        try {
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19188
diff changeset
   887
                            e1.addSuppressed(e2);
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19188
diff changeset
   888
                        } catch (Throwable ignore) {}
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19188
diff changeset
   889
                    }
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19188
diff changeset
   890
                    throw e1;
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19188
diff changeset
   891
                }
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19188
diff changeset
   892
                b.close();
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19188
diff changeset
   893
            }
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19188
diff changeset
   894
        };
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19188
diff changeset
   895
    }
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   896
}