jdk/src/share/classes/java/util/stream/Streams.java
author henryjen
Tue, 03 Sep 2013 12:16:01 -0700
changeset 19800 6e1fef53ea55
parent 19188 bbf287c5cd92
child 20507 8498104f92c3
permissions -rw-r--r--
8017513: Support for closeable streams 8022237: j.u.s.BaseStream.onClose() has an issue in implementation or requires spec clarification 8022572: Same exception instances thrown from j.u.stream.Stream.onClose() handlers are not listed as suppressed Summary: BaseStream implements AutoCloseable; Remove CloseableStream and DelegatingStream Reviewed-by: alanb, mduigou, psandoz Contributed-by: brian.goetz@oracle.com
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;
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   172
            // 2 <= size <= 2^32
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   173
            return (int) (size / d);
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   174
        }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   175
    }
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
     * A {@code long} range spliterator.
18158
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   179
     *
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   180
     * This implementation cannot be used for ranges whose size is greater
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   181
     * than Long.MAX_VALUE
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   182
     */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   183
    static final class RangeLongSpliterator implements Spliterator.OfLong {
18158
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   184
        // Can never be greater that upTo, this avoids overflow if upper bound
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   185
        // is Long.MAX_VALUE
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   186
        // All elements are traversed if from == upTo & last == 0
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   187
        private long from;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   188
        private final long upTo;
18158
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   189
        // 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
   190
        // 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
   191
        // elements have been traversed
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   192
        private int last;
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   193
18158
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   194
        RangeLongSpliterator(long from, long upTo, boolean closed) {
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   195
            this(from, upTo, closed ? 1 : 0);
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   196
        }
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   197
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   198
        private RangeLongSpliterator(long from, long upTo, int last) {
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   199
            assert upTo - from + last > 0;
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   200
            this.from = from;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   201
            this.upTo = upTo;
18158
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   202
            this.last = last;
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   203
        }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   204
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   205
        @Override
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   206
        public boolean tryAdvance(LongConsumer consumer) {
19188
bbf287c5cd92 8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents: 19072
diff changeset
   207
            Objects.requireNonNull(consumer);
bbf287c5cd92 8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents: 19072
diff changeset
   208
18158
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   209
            final long i = from;
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   210
            if (i < upTo) {
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   211
                from++;
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   212
                consumer.accept(i);
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   213
                return true;
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   214
            }
18158
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   215
            else if (last > 0) {
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   216
                last = 0;
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   217
                consumer.accept(i);
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   218
                return true;
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   219
            }
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   220
            return false;
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   221
        }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   222
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   223
        @Override
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   224
        public void forEachRemaining(LongConsumer consumer) {
19188
bbf287c5cd92 8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents: 19072
diff changeset
   225
            Objects.requireNonNull(consumer);
bbf287c5cd92 8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents: 19072
diff changeset
   226
18158
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   227
            long i = from;
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   228
            final long hUpTo = upTo;
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   229
            int hLast = last;
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   230
            from = upTo;
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   231
            last = 0;
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   232
            while (i < hUpTo) {
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   233
                consumer.accept(i++);
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   234
            }
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   235
            if (hLast > 0) {
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   236
                // Last element of closed range
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   237
                consumer.accept(i);
18158
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   238
            }
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   239
        }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   240
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   241
        @Override
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   242
        public long estimateSize() {
18158
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   243
            return upTo - from + last;
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   244
        }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   245
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   246
        @Override
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   247
        public int characteristics() {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   248
            return Spliterator.ORDERED | Spliterator.SIZED | Spliterator.SUBSIZED |
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   249
                   Spliterator.IMMUTABLE | Spliterator.NONNULL |
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   250
                   Spliterator.DISTINCT | Spliterator.SORTED;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   251
        }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   252
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   253
        @Override
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   254
        public Comparator<? super Long> getComparator() {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   255
            return null;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   256
        }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   257
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   258
        @Override
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   259
        public Spliterator.OfLong trySplit() {
18158
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   260
            long size = estimateSize();
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   261
            return size <= 1
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   262
                   ? null
18158
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   263
                   // Left split always has a half-open range
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   264
                   : new RangeLongSpliterator(from, from = from + splitPoint(size), 0);
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   265
        }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   266
18158
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   267
        /**
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   268
         * The spliterator size below which the spliterator will be split
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   269
         * at the mid-point to produce balanced splits. Above this size the
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   270
         * spliterator will be split at a ratio of
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   271
         * 1:(RIGHT_BALANCED_SPLIT_RATIO - 1)
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   272
         * to produce right-balanced splits.
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   273
         *
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   274
         * <p>Such splitting ensures that for very large ranges that the left
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   275
         * side of the range will more likely be processed at a lower-depth
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   276
         * 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
   277
         * side of the range.
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   278
         *
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   279
         * <p>This is optimized for cases such as LongStream.longs() that is
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   280
         * 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
   281
         * augmented with a limit operation that limits the number of elements
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   282
         * to a count lower than this threshold.
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   283
         */
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   284
        private static final long BALANCED_SPLIT_THRESHOLD = 1 << 24;
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   285
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   286
        /**
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   287
         * The split ratio of the left and right split when the spliterator
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   288
         * size is above BALANCED_SPLIT_THRESHOLD.
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   289
         */
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   290
        private static final long RIGHT_BALANCED_SPLIT_RATIO = 1 << 3;
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 long splitPoint(long size) {
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   293
            long d = (size < BALANCED_SPLIT_THRESHOLD) ? 2 : RIGHT_BALANCED_SPLIT_RATIO;
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   294
            // 2 <= size <= Long.MAX_VALUE
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18153
diff changeset
   295
            return size / d;
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   296
        }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   297
    }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   298
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   299
    private static abstract class AbstractStreamBuilderImpl<T, S extends Spliterator<T>> implements Spliterator<T> {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   300
        // >= 0 when building, < 0 when built
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   301
        // -1 == no elements
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   302
        // -2 == one element, held by first
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   303
        // -3 == two or more elements, held by buffer
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   304
        int count;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   305
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   306
        // Spliterator implementation for 0 or 1 element
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   307
        // count == -1 for no elements
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   308
        // count == -2 for one element held by first
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   309
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   310
        @Override
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   311
        public S trySplit() {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   312
            return null;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   313
        }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   314
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   315
        @Override
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   316
        public long estimateSize() {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   317
            return -count - 1;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   318
        }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   319
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   320
        @Override
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   321
        public int characteristics() {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   322
            return Spliterator.SIZED | Spliterator.SUBSIZED |
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   323
                   Spliterator.ORDERED | Spliterator.IMMUTABLE;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   324
        }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   325
    }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   326
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   327
    static final class StreamBuilderImpl<T>
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   328
            extends AbstractStreamBuilderImpl<T, Spliterator<T>>
18825
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
   329
            implements Stream.Builder<T> {
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   330
        // The first element in the stream
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   331
        // valid if count == 1
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   332
        T first;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   333
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   334
        // The first and subsequent elements in the stream
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   335
        // non-null if count == 2
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   336
        SpinedBuffer<T> buffer;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   337
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   338
        /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   339
         * Constructor for building a stream of 0 or more elements.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   340
         */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   341
        StreamBuilderImpl() { }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   342
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   343
        /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   344
         * Constructor for a singleton stream.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   345
         *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   346
         * @param t the single element
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   347
         */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   348
        StreamBuilderImpl(T t) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   349
            first = t;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   350
            count = -2;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   351
        }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   352
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   353
        // StreamBuilder implementation
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   354
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   355
        @Override
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   356
        public void accept(T t) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   357
            if (count == 0) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   358
                first = t;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   359
                count++;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   360
            }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   361
            else if (count > 0) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   362
                if (buffer == null) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   363
                    buffer = new SpinedBuffer<>();
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   364
                    buffer.accept(first);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   365
                    count++;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   366
                }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   367
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   368
                buffer.accept(t);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   369
            }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   370
            else {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   371
                throw new IllegalStateException();
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   372
            }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   373
        }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   374
18825
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
   375
        public Stream.Builder<T> add(T t) {
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   376
            accept(t);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   377
            return this;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   378
        }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   379
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   380
        @Override
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   381
        public Stream<T> build() {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   382
            int c = count;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   383
            if (c >= 0) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   384
                // Switch count to negative value signalling the builder is built
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   385
                count = -count - 1;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   386
                // Use this spliterator if 0 or 1 elements, otherwise use
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   387
                // the spliterator of the spined buffer
18822
4b6be7c19547 8019395: Consolidate StreamSupport.{stream,parallelStream} into a single method
psandoz
parents: 18820
diff changeset
   388
                return (c < 2) ? StreamSupport.stream(this, false) : StreamSupport.stream(buffer.spliterator(), false);
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   389
            }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   390
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   391
            throw new IllegalStateException();
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   392
        }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   393
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   394
        // Spliterator implementation for 0 or 1 element
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   395
        // count == -1 for no elements
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   396
        // count == -2 for one element held by first
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   397
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   398
        @Override
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   399
        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
   400
            Objects.requireNonNull(action);
bbf287c5cd92 8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents: 19072
diff changeset
   401
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   402
            if (count == -2) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   403
                action.accept(first);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   404
                count = -1;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   405
                return true;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   406
            }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   407
            else {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   408
                return false;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   409
            }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   410
        }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   411
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   412
        @Override
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   413
        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
   414
            Objects.requireNonNull(action);
bbf287c5cd92 8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents: 19072
diff changeset
   415
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   416
            if (count == -2) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   417
                action.accept(first);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   418
                count = -1;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   419
            }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   420
        }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   421
    }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   422
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   423
    static final class IntStreamBuilderImpl
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   424
            extends AbstractStreamBuilderImpl<Integer, Spliterator.OfInt>
18825
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
   425
            implements IntStream.Builder, Spliterator.OfInt {
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   426
        // The first element in the stream
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   427
        // valid if count == 1
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   428
        int first;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   429
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   430
        // The first and subsequent elements in the stream
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   431
        // non-null if count == 2
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   432
        SpinedBuffer.OfInt buffer;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   433
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   434
        /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   435
         * Constructor for building a stream of 0 or more elements.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   436
         */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   437
        IntStreamBuilderImpl() { }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   438
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   439
        /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   440
         * Constructor for a singleton stream.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   441
         *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   442
         * @param t the single element
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   443
         */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   444
        IntStreamBuilderImpl(int t) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   445
            first = t;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   446
            count = -2;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   447
        }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   448
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   449
        // StreamBuilder implementation
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   450
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   451
        @Override
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   452
        public void accept(int t) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   453
            if (count == 0) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   454
                first = t;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   455
                count++;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   456
            }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   457
            else if (count > 0) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   458
                if (buffer == null) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   459
                    buffer = new SpinedBuffer.OfInt();
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   460
                    buffer.accept(first);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   461
                    count++;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   462
                }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   463
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   464
                buffer.accept(t);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   465
            }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   466
            else {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   467
                throw new IllegalStateException();
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   468
            }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   469
        }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   470
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   471
        @Override
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   472
        public IntStream build() {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   473
            int c = count;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   474
            if (c >= 0) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   475
                // Switch count to negative value signalling the builder is built
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   476
                count = -count - 1;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   477
                // Use this spliterator if 0 or 1 elements, otherwise use
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   478
                // the spliterator of the spined buffer
18822
4b6be7c19547 8019395: Consolidate StreamSupport.{stream,parallelStream} into a single method
psandoz
parents: 18820
diff changeset
   479
                return (c < 2) ? StreamSupport.intStream(this, false) : StreamSupport.intStream(buffer.spliterator(), false);
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   480
            }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   481
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   482
            throw new IllegalStateException();
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   483
        }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   484
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   485
        // Spliterator implementation for 0 or 1 element
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   486
        // count == -1 for no elements
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   487
        // count == -2 for one element held by first
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   488
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   489
        @Override
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   490
        public boolean tryAdvance(IntConsumer action) {
19188
bbf287c5cd92 8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents: 19072
diff changeset
   491
            Objects.requireNonNull(action);
bbf287c5cd92 8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents: 19072
diff changeset
   492
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   493
            if (count == -2) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   494
                action.accept(first);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   495
                count = -1;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   496
                return true;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   497
            }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   498
            else {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   499
                return false;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   500
            }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   501
        }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   502
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   503
        @Override
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   504
        public void forEachRemaining(IntConsumer action) {
19188
bbf287c5cd92 8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents: 19072
diff changeset
   505
            Objects.requireNonNull(action);
bbf287c5cd92 8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents: 19072
diff changeset
   506
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   507
            if (count == -2) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   508
                action.accept(first);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   509
                count = -1;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   510
            }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   511
        }
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
    static final class LongStreamBuilderImpl
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   515
            extends AbstractStreamBuilderImpl<Long, Spliterator.OfLong>
18825
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
   516
            implements LongStream.Builder, Spliterator.OfLong {
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   517
        // The first element in the stream
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   518
        // valid if count == 1
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   519
        long first;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   520
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   521
        // The first and subsequent elements in the stream
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   522
        // non-null if count == 2
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   523
        SpinedBuffer.OfLong buffer;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   524
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   525
        /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   526
         * Constructor for building a stream of 0 or more elements.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   527
         */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   528
        LongStreamBuilderImpl() { }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   529
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   530
        /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   531
         * Constructor for a singleton stream.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   532
         *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   533
         * @param t the single element
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   534
         */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   535
        LongStreamBuilderImpl(long t) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   536
            first = t;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   537
            count = -2;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   538
        }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   539
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   540
        // StreamBuilder implementation
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   541
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   542
        @Override
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   543
        public void accept(long t) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   544
            if (count == 0) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   545
                first = t;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   546
                count++;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   547
            }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   548
            else if (count > 0) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   549
                if (buffer == null) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   550
                    buffer = new SpinedBuffer.OfLong();
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   551
                    buffer.accept(first);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   552
                    count++;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   553
                }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   554
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   555
                buffer.accept(t);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   556
            }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   557
            else {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   558
                throw new IllegalStateException();
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   559
            }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   560
        }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   561
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   562
        @Override
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   563
        public LongStream build() {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   564
            int c = count;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   565
            if (c >= 0) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   566
                // Switch count to negative value signalling the builder is built
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   567
                count = -count - 1;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   568
                // Use this spliterator if 0 or 1 elements, otherwise use
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   569
                // the spliterator of the spined buffer
18822
4b6be7c19547 8019395: Consolidate StreamSupport.{stream,parallelStream} into a single method
psandoz
parents: 18820
diff changeset
   570
                return (c < 2) ? StreamSupport.longStream(this, false) : StreamSupport.longStream(buffer.spliterator(), false);
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   571
            }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   572
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   573
            throw new IllegalStateException();
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   574
        }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   575
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   576
        // Spliterator implementation for 0 or 1 element
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   577
        // count == -1 for no elements
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   578
        // count == -2 for one element held by first
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   579
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   580
        @Override
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   581
        public boolean tryAdvance(LongConsumer action) {
19188
bbf287c5cd92 8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents: 19072
diff changeset
   582
            Objects.requireNonNull(action);
bbf287c5cd92 8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents: 19072
diff changeset
   583
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   584
            if (count == -2) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   585
                action.accept(first);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   586
                count = -1;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   587
                return true;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   588
            }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   589
            else {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   590
                return false;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   591
            }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   592
        }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   593
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   594
        @Override
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   595
        public void forEachRemaining(LongConsumer action) {
19188
bbf287c5cd92 8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents: 19072
diff changeset
   596
            Objects.requireNonNull(action);
bbf287c5cd92 8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents: 19072
diff changeset
   597
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   598
            if (count == -2) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   599
                action.accept(first);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   600
                count = -1;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   601
            }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   602
        }
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
    static final class DoubleStreamBuilderImpl
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   606
            extends AbstractStreamBuilderImpl<Double, Spliterator.OfDouble>
18825
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
   607
            implements DoubleStream.Builder, Spliterator.OfDouble {
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   608
        // The first element in the stream
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   609
        // valid if count == 1
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   610
        double first;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   611
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   612
        // The first and subsequent elements in the stream
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   613
        // non-null if count == 2
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   614
        SpinedBuffer.OfDouble buffer;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   615
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   616
        /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   617
         * Constructor for building a stream of 0 or more elements.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   618
         */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   619
        DoubleStreamBuilderImpl() { }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   620
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   621
        /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   622
         * Constructor for a singleton stream.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   623
         *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   624
         * @param t the single element
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   625
         */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   626
        DoubleStreamBuilderImpl(double t) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   627
            first = t;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   628
            count = -2;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   629
        }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   630
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   631
        // StreamBuilder implementation
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   632
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   633
        @Override
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   634
        public void accept(double t) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   635
            if (count == 0) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   636
                first = t;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   637
                count++;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   638
            }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   639
            else if (count > 0) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   640
                if (buffer == null) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   641
                    buffer = new SpinedBuffer.OfDouble();
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   642
                    buffer.accept(first);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   643
                    count++;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   644
                }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   645
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   646
                buffer.accept(t);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   647
            }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   648
            else {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   649
                throw new IllegalStateException();
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   650
            }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   651
        }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   652
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   653
        @Override
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   654
        public DoubleStream build() {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   655
            int c = count;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   656
            if (c >= 0) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   657
                // Switch count to negative value signalling the builder is built
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   658
                count = -count - 1;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   659
                // Use this spliterator if 0 or 1 elements, otherwise use
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   660
                // the spliterator of the spined buffer
18822
4b6be7c19547 8019395: Consolidate StreamSupport.{stream,parallelStream} into a single method
psandoz
parents: 18820
diff changeset
   661
                return (c < 2) ? StreamSupport.doubleStream(this, false) : StreamSupport.doubleStream(buffer.spliterator(), false);
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   662
            }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   663
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   664
            throw new IllegalStateException();
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   665
        }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   666
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   667
        // Spliterator implementation for 0 or 1 element
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   668
        // count == -1 for no elements
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   669
        // count == -2 for one element held by first
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   670
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   671
        @Override
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   672
        public boolean tryAdvance(DoubleConsumer action) {
19188
bbf287c5cd92 8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents: 19072
diff changeset
   673
            Objects.requireNonNull(action);
bbf287c5cd92 8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents: 19072
diff changeset
   674
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   675
            if (count == -2) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   676
                action.accept(first);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   677
                count = -1;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   678
                return true;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   679
            }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   680
            else {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   681
                return false;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   682
            }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   683
        }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   684
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   685
        @Override
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   686
        public void forEachRemaining(DoubleConsumer action) {
19188
bbf287c5cd92 8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents: 19072
diff changeset
   687
            Objects.requireNonNull(action);
bbf287c5cd92 8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents: 19072
diff changeset
   688
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   689
            if (count == -2) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   690
                action.accept(first);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   691
                count = -1;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   692
            }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   693
        }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   694
    }
18820
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   695
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   696
    abstract static class ConcatSpliterator<T, T_SPLITR extends Spliterator<T>>
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   697
            implements Spliterator<T> {
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   698
        protected final T_SPLITR aSpliterator;
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   699
        protected final T_SPLITR bSpliterator;
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   700
        // True when no split has occurred, otherwise false
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   701
        boolean beforeSplit;
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   702
        // Never read after splitting
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   703
        final boolean unsized;
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   704
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   705
        public ConcatSpliterator(T_SPLITR aSpliterator, T_SPLITR bSpliterator) {
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   706
            this.aSpliterator = aSpliterator;
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   707
            this.bSpliterator = bSpliterator;
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   708
            beforeSplit = true;
19072
8f176099ec00 8021863: Stream.concat incorrectly calculates unsized state
psandoz
parents: 18825
diff changeset
   709
            // The spliterator is known to be unsized before splitting if the
8f176099ec00 8021863: Stream.concat incorrectly calculates unsized state
psandoz
parents: 18825
diff changeset
   710
            // sum of the estimates overflows.
8f176099ec00 8021863: Stream.concat incorrectly calculates unsized state
psandoz
parents: 18825
diff changeset
   711
            unsized = aSpliterator.estimateSize() + bSpliterator.estimateSize() < 0;
18820
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   712
        }
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   713
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   714
        @Override
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   715
        public T_SPLITR trySplit() {
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   716
            T_SPLITR ret = beforeSplit ? aSpliterator : (T_SPLITR) bSpliterator.trySplit();
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   717
            beforeSplit = false;
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   718
            return ret;
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   719
        }
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   720
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   721
        @Override
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   722
        public boolean tryAdvance(Consumer<? super T> consumer) {
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   723
            boolean hasNext;
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   724
            if (beforeSplit) {
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   725
                hasNext = aSpliterator.tryAdvance(consumer);
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   726
                if (!hasNext) {
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   727
                    beforeSplit = false;
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   728
                    hasNext = bSpliterator.tryAdvance(consumer);
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   729
                }
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   730
            }
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   731
            else
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   732
                hasNext = bSpliterator.tryAdvance(consumer);
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   733
            return hasNext;
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   734
        }
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   735
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   736
        @Override
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   737
        public void forEachRemaining(Consumer<? super T> consumer) {
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   738
            if (beforeSplit)
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   739
                aSpliterator.forEachRemaining(consumer);
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   740
            bSpliterator.forEachRemaining(consumer);
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   741
        }
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   742
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   743
        @Override
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   744
        public long estimateSize() {
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   745
            if (beforeSplit) {
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   746
                // If one or both estimates are Long.MAX_VALUE then the sum
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   747
                // will either be Long.MAX_VALUE or overflow to a negative value
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   748
                long size = aSpliterator.estimateSize() + bSpliterator.estimateSize();
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   749
                return (size >= 0) ? size : Long.MAX_VALUE;
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   750
            }
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   751
            else {
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   752
                return bSpliterator.estimateSize();
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   753
            }
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   754
        }
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   755
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   756
        @Override
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   757
        public int characteristics() {
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   758
            if (beforeSplit) {
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   759
                // Concatenation loses DISTINCT and SORTED characteristics
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   760
                return aSpliterator.characteristics() & bSpliterator.characteristics()
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   761
                       & ~(Spliterator.DISTINCT | Spliterator.SORTED
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   762
                           | (unsized ? Spliterator.SIZED | Spliterator.SUBSIZED : 0));
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   763
            }
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   764
            else {
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   765
                return bSpliterator.characteristics();
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   766
            }
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   767
        }
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   768
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   769
        @Override
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   770
        public Comparator<? super T> getComparator() {
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   771
            if (beforeSplit)
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   772
                throw new IllegalStateException();
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   773
            return bSpliterator.getComparator();
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   774
        }
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   775
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   776
        static class OfRef<T> extends ConcatSpliterator<T, Spliterator<T>> {
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   777
            OfRef(Spliterator<T> aSpliterator, Spliterator<T> bSpliterator) {
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   778
                super(aSpliterator, bSpliterator);
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   779
            }
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   780
        }
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   781
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   782
        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
   783
                extends ConcatSpliterator<T, T_SPLITR>
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   784
                implements Spliterator.OfPrimitive<T, T_CONS, T_SPLITR> {
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   785
            private OfPrimitive(T_SPLITR aSpliterator, T_SPLITR bSpliterator) {
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   786
                super(aSpliterator, bSpliterator);
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   787
            }
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   788
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   789
            @Override
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   790
            public boolean tryAdvance(T_CONS action) {
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   791
                boolean hasNext;
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   792
                if (beforeSplit) {
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   793
                    hasNext = aSpliterator.tryAdvance(action);
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   794
                    if (!hasNext) {
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   795
                        beforeSplit = false;
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   796
                        hasNext = bSpliterator.tryAdvance(action);
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   797
                    }
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   798
                }
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   799
                else
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   800
                    hasNext = bSpliterator.tryAdvance(action);
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   801
                return hasNext;
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   802
            }
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   803
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   804
            @Override
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   805
            public void forEachRemaining(T_CONS action) {
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   806
                if (beforeSplit)
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   807
                    aSpliterator.forEachRemaining(action);
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   808
                bSpliterator.forEachRemaining(action);
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   809
            }
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   810
        }
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   811
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   812
        static class OfInt
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   813
                extends ConcatSpliterator.OfPrimitive<Integer, IntConsumer, Spliterator.OfInt>
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   814
                implements Spliterator.OfInt {
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   815
            OfInt(Spliterator.OfInt aSpliterator, Spliterator.OfInt bSpliterator) {
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   816
                super(aSpliterator, bSpliterator);
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   817
            }
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   818
        }
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   819
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   820
        static class OfLong
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   821
                extends ConcatSpliterator.OfPrimitive<Long, LongConsumer, Spliterator.OfLong>
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   822
                implements Spliterator.OfLong {
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   823
            OfLong(Spliterator.OfLong aSpliterator, Spliterator.OfLong bSpliterator) {
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   824
                super(aSpliterator, bSpliterator);
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   825
            }
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   826
        }
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   827
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   828
        static class OfDouble
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   829
                extends ConcatSpliterator.OfPrimitive<Double, DoubleConsumer, Spliterator.OfDouble>
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   830
                implements Spliterator.OfDouble {
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   831
            OfDouble(Spliterator.OfDouble aSpliterator, Spliterator.OfDouble bSpliterator) {
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   832
                super(aSpliterator, bSpliterator);
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   833
            }
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   834
        }
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18158
diff changeset
   835
    }
19800
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19188
diff changeset
   836
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19188
diff changeset
   837
    /**
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19188
diff changeset
   838
     * Given two Runnables, return a Runnable that executes both in sequence,
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19188
diff changeset
   839
     * even if the first throws an exception, and if both throw exceptions, add
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19188
diff changeset
   840
     * any exceptions thrown by the second as suppressed exceptions of the first.
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19188
diff changeset
   841
     */
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19188
diff changeset
   842
    static Runnable composeWithExceptions(Runnable a, Runnable b) {
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19188
diff changeset
   843
        return new Runnable() {
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19188
diff changeset
   844
            @Override
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19188
diff changeset
   845
            public void run() {
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19188
diff changeset
   846
                try {
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19188
diff changeset
   847
                    a.run();
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19188
diff changeset
   848
                }
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19188
diff changeset
   849
                catch (Throwable e1) {
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19188
diff changeset
   850
                    try {
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19188
diff changeset
   851
                        b.run();
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19188
diff changeset
   852
                    }
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19188
diff changeset
   853
                    catch (Throwable e2) {
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19188
diff changeset
   854
                        try {
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19188
diff changeset
   855
                            e1.addSuppressed(e2);
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19188
diff changeset
   856
                        } catch (Throwable ignore) {}
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19188
diff changeset
   857
                    }
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19188
diff changeset
   858
                    throw e1;
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19188
diff changeset
   859
                }
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19188
diff changeset
   860
                b.run();
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19188
diff changeset
   861
            }
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19188
diff changeset
   862
        };
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19188
diff changeset
   863
    }
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
     * Given two streams, return a Runnable that
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19188
diff changeset
   867
     * executes both of their {@link BaseStream#close} methods in sequence,
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19188
diff changeset
   868
     * even if the first throws an exception, and if both throw exceptions, add
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19188
diff changeset
   869
     * any exceptions thrown by the second as suppressed exceptions of the first.
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19188
diff changeset
   870
     */
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19188
diff changeset
   871
    static Runnable composedClose(BaseStream<?, ?> a, BaseStream<?, ?> b) {
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19188
diff changeset
   872
        return new Runnable() {
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19188
diff changeset
   873
            @Override
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19188
diff changeset
   874
            public void run() {
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19188
diff changeset
   875
                try {
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19188
diff changeset
   876
                    a.close();
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19188
diff changeset
   877
                }
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19188
diff changeset
   878
                catch (Throwable e1) {
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19188
diff changeset
   879
                    try {
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19188
diff changeset
   880
                        b.close();
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19188
diff changeset
   881
                    }
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19188
diff changeset
   882
                    catch (Throwable e2) {
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19188
diff changeset
   883
                        try {
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19188
diff changeset
   884
                            e1.addSuppressed(e2);
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19188
diff changeset
   885
                        } catch (Throwable ignore) {}
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19188
diff changeset
   886
                    }
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19188
diff changeset
   887
                    throw e1;
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19188
diff changeset
   888
                }
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19188
diff changeset
   889
                b.close();
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19188
diff changeset
   890
            }
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19188
diff changeset
   891
        };
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19188
diff changeset
   892
    }
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents:
diff changeset
   893
}