jdk/src/share/classes/java/util/Spliterators.java
author briangoetz
Tue, 16 Apr 2013 13:51:53 -0400
changeset 16929 c984ae5655cb
child 18155 889970e5b728
permissions -rw-r--r--
8010096: Initial java.util.Spliterator putback Reviewed-by: mduigou, alanb, chegar, darcy Contributed-by: Paul Sandoz <paul.sandoz@oracle.com>, Brian Goetz <brian.goetz@oracle.com>, Doug Lea <dl@cs.oswego.edu>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
     1
/*
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
     2
 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
     4
 *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    10
 *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    15
 * accompanied this code).
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    16
 *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    20
 *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    23
 * questions.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    24
 */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    25
package java.util;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    26
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    27
import java.util.function.Consumer;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    28
import java.util.function.DoubleConsumer;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    29
import java.util.function.IntConsumer;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    30
import java.util.function.LongConsumer;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    31
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    32
/**
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    33
 * Static classes and methods for operating on or creating instances of
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    34
 * {@link Spliterator} and its primitive specializations
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    35
 * {@link Spliterator.OfInt}, {@link Spliterator.OfLong}, and
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    36
 * {@link Spliterator.OfDouble}.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    37
 *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    38
 * @see Spliterator
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    39
 * @since 1.8
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    40
 */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    41
public final class Spliterators {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    42
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    43
    // Suppresses default constructor, ensuring non-instantiability.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    44
    private Spliterators() {}
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    45
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    46
    // Empty spliterators
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    47
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    48
    /**
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    49
     * Creates an empty {@code Spliterator}
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    50
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    51
     * <p>The empty spliterator reports {@link Spliterator#SIZED} and
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    52
     * {@link Spliterator#SUBSIZED}.  Calls to
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    53
     * {@link java.util.Spliterator#trySplit()} always return {@code null}.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    54
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    55
     * @param <T> Type of elements
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    56
     * @return An empty spliterator
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    57
     */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    58
    @SuppressWarnings("unchecked")
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    59
    public static <T> Spliterator<T> emptySpliterator() {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    60
        return (Spliterator<T>) EMPTY_SPLITERATOR;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    61
    }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    62
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    63
    private static final Spliterator<Object> EMPTY_SPLITERATOR =
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    64
            new EmptySpliterator.OfRef<>();
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    65
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    66
    /**
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    67
     * Creates an empty {@code Spliterator.OfInt}
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    68
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    69
     * <p>The empty spliterator reports {@link Spliterator#SIZED} and
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    70
     * {@link Spliterator#SUBSIZED}.  Calls to
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    71
     * {@link java.util.Spliterator#trySplit()} always return {@code null}.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    72
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    73
     * @return An empty spliterator
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    74
     */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    75
    public static Spliterator.OfInt emptyIntSpliterator() {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    76
        return EMPTY_INT_SPLITERATOR;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    77
    }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    78
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    79
    private static final Spliterator.OfInt EMPTY_INT_SPLITERATOR =
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    80
            new EmptySpliterator.OfInt();
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    81
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    82
    /**
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    83
     * Creates an empty {@code Spliterator.OfLong}
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    84
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    85
     * <p>The empty spliterator reports {@link Spliterator#SIZED} and
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    86
     * {@link Spliterator#SUBSIZED}.  Calls to
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    87
     * {@link java.util.Spliterator#trySplit()} always return {@code null}.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    88
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    89
     * @return An empty spliterator
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    90
     */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    91
    public static Spliterator.OfLong emptyLongSpliterator() {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    92
        return EMPTY_LONG_SPLITERATOR;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    93
    }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    94
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    95
    private static final Spliterator.OfLong EMPTY_LONG_SPLITERATOR =
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    96
            new EmptySpliterator.OfLong();
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    97
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    98
    /**
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    99
     * Creates an empty {@code Spliterator.OfDouble}
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   100
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   101
     * <p>The empty spliterator reports {@link Spliterator#SIZED} and
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   102
     * {@link Spliterator#SUBSIZED}.  Calls to
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   103
     * {@link java.util.Spliterator#trySplit()} always return {@code null}.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   104
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   105
     * @return An empty spliterator
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   106
     */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   107
    public static Spliterator.OfDouble emptyDoubleSpliterator() {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   108
        return EMPTY_DOUBLE_SPLITERATOR;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   109
    }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   110
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   111
    private static final Spliterator.OfDouble EMPTY_DOUBLE_SPLITERATOR =
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   112
            new EmptySpliterator.OfDouble();
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   113
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   114
    // Array-based spliterators
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   115
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   116
    /**
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   117
     * Creates a {@code Spliterator} covering the elements of a given array,
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   118
     * using a customized set of spliterator characteristics.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   119
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   120
     * <p>This method is provided as an implementation convenience for
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   121
     * Spliterators which store portions of their elements in arrays, and need
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   122
     * fine control over Spliterator characteristics.  Most other situations in
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   123
     * which a Spliterator for an array is needed should use
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   124
     * {@link Arrays#spliterator(Object[])}.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   125
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   126
     * <p>The returned spliterator always reports the characteristics
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   127
     * {@code SIZED} and {@code SUBSIZED}.  The caller may provide additional
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   128
     * characteristics for the spliterator to report; it is common to
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   129
     * additionally specify {@code IMMUTABLE} and {@code ORDERED}.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   130
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   131
     * @param <T> Type of elements
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   132
     * @param array The array, assumed to be unmodified during use
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   133
     * @param additionalCharacteristics Additional spliterator characteristics
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   134
     *        of this spliterator's source or elements beyond {@code SIZED} and
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   135
     *        {@code SUBSIZED} which are are always reported
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   136
     * @return A spliterator for an array
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   137
     * @throws NullPointerException if the given array is {@code null}
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   138
     * @see Arrays#spliterator(Object[])
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   139
     */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   140
    public static <T> Spliterator<T> spliterator(Object[] array,
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   141
                                                 int additionalCharacteristics) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   142
        return new ArraySpliterator<>(Objects.requireNonNull(array),
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   143
                                      additionalCharacteristics);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   144
    }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   145
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   146
    /**
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   147
     * Creates a {@code Spliterator} covering a range of elements of a given
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   148
     * array, using a customized set of spliterator characteristics.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   149
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   150
     * <p>This method is provided as an implementation convenience for
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   151
     * Spliterators which store portions of their elements in arrays, and need
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   152
     * fine control over Spliterator characteristics.  Most other situations in
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   153
     * which a Spliterator for an array is needed should use
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   154
     * {@link Arrays#spliterator(Object[])}.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   155
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   156
     * <p>The returned spliterator always reports the characteristics
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   157
     * {@code SIZED} and {@code SUBSIZED}.  The caller may provide additional
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   158
     * characteristics for the spliterator to report; it is common to
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   159
     * additionally specify {@code IMMUTABLE} and {@code ORDERED}.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   160
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   161
     * @param <T> Type of elements
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   162
     * @param array The array, assumed to be unmodified during use
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   163
     * @param fromIndex The least index (inclusive) to cover
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   164
     * @param toIndex One past the greatest index to cover
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   165
     * @param additionalCharacteristics Additional spliterator characteristics
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   166
     *        of this spliterator's source or elements beyond {@code SIZED} and
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   167
     *        {@code SUBSIZED} which are are always reported
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   168
     * @return A spliterator for an array
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   169
     * @throws NullPointerException if the given array is {@code null}
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   170
     * @throws ArrayIndexOutOfBoundsException if {@code fromIndex} is negative,
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   171
     *         {@code toIndex} is less than {@code fromIndex}, or
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   172
     *         {@code toIndex} is greater than the array size
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   173
     * @see Arrays#spliterator(Object[], int, int)
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   174
     */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   175
    public static <T> Spliterator<T> spliterator(Object[] array, int fromIndex, int toIndex,
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   176
                                                 int additionalCharacteristics) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   177
        checkFromToBounds(Objects.requireNonNull(array).length, fromIndex, toIndex);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   178
        return new ArraySpliterator<>(array, fromIndex, toIndex, additionalCharacteristics);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   179
    }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   180
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   181
    /**
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   182
     * Creates a {@code Spliterator.OfInt} covering the elements of a given array,
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   183
     * using a customized set of spliterator characteristics.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   184
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   185
     * <p>This method is provided as an implementation convenience for
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   186
     * Spliterators which store portions of their elements in arrays, and need
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   187
     * fine control over Spliterator characteristics.  Most other situations in
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   188
     * which a Spliterator for an array is needed should use
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   189
     * {@link Arrays#spliterator(int[])}.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   190
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   191
     * <p>The returned spliterator always reports the characteristics
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   192
     * {@code SIZED} and {@code SUBSIZED}.  The caller may provide additional
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   193
     * characteristics for the spliterator to report; it is common to
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   194
     * additionally specify {@code IMMUTABLE} and {@code ORDERED}.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   195
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   196
     * @param array The array, assumed to be unmodified during use
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   197
     * @param additionalCharacteristics Additional spliterator characteristics
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   198
     *        of this spliterator's source or elements beyond {@code SIZED} and
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   199
     *        {@code SUBSIZED} which are are always reported
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   200
     * @return A spliterator for an array
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   201
     * @throws NullPointerException if the given array is {@code null}
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   202
     * @see Arrays#spliterator(int[])
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   203
     */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   204
    public static Spliterator.OfInt spliterator(int[] array,
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   205
                                                int additionalCharacteristics) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   206
        return new IntArraySpliterator(Objects.requireNonNull(array), additionalCharacteristics);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   207
    }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   208
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   209
    /**
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   210
     * Creates a {@code Spliterator.OfInt} covering a range of elements of a
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   211
     * given array, using a customized set of spliterator characteristics.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   212
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   213
     * <p>This method is provided as an implementation convenience for
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   214
     * Spliterators which store portions of their elements in arrays, and need
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   215
     * fine control over Spliterator characteristics.  Most other situations in
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   216
     * which a Spliterator for an array is needed should use
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   217
     * {@link Arrays#spliterator(int[], int, int)}.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   218
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   219
     * <p>The returned spliterator always reports the characteristics
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   220
     * {@code SIZED} and {@code SUBSIZED}.  The caller may provide additional
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   221
     * characteristics for the spliterator to report; it is common to
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   222
     * additionally specify {@code IMMUTABLE} and {@code ORDERED}.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   223
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   224
     * @param array The array, assumed to be unmodified during use
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   225
     * @param fromIndex The least index (inclusive) to cover
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   226
     * @param toIndex One past the greatest index to cover
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   227
     * @param additionalCharacteristics Additional spliterator characteristics
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   228
     *        of this spliterator's source or elements beyond {@code SIZED} and
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   229
     *        {@code SUBSIZED} which are are always reported
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   230
     * @return A spliterator for an array
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   231
     * @throws NullPointerException if the given array is {@code null}
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   232
     * @throws ArrayIndexOutOfBoundsException if {@code fromIndex} is negative,
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   233
     *         {@code toIndex} is less than {@code fromIndex}, or
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   234
     *         {@code toIndex} is greater than the array size
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   235
     * @see Arrays#spliterator(int[], int, int)
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   236
     */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   237
    public static Spliterator.OfInt spliterator(int[] array, int fromIndex, int toIndex,
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   238
                                                int additionalCharacteristics) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   239
        checkFromToBounds(Objects.requireNonNull(array).length, fromIndex, toIndex);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   240
        return new IntArraySpliterator(array, fromIndex, toIndex, additionalCharacteristics);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   241
    }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   242
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   243
    /**
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   244
     * Creates a {@code Spliterator.OfLong} covering the elements of a given array,
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   245
     * using a customized set of spliterator characteristics.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   246
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   247
     * <p>This method is provided as an implementation convenience for
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   248
     * Spliterators which store portions of their elements in arrays, and need
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   249
     * fine control over Spliterator characteristics.  Most other situations in
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   250
     * which a Spliterator for an array is needed should use
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   251
     * {@link Arrays#spliterator(long[])}.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   252
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   253
     * <p>The returned spliterator always reports the characteristics
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   254
     * {@code SIZED} and {@code SUBSIZED}.  The caller may provide additional
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   255
     * characteristics for the spliterator to report; it is common to
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   256
     * additionally specify {@code IMMUTABLE} and {@code ORDERED}.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   257
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   258
     * @param array The array, assumed to be unmodified during use
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   259
     * @param additionalCharacteristics Additional spliterator characteristics
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   260
     *        of this spliterator's source or elements beyond {@code SIZED} and
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   261
     *        {@code SUBSIZED} which are are always reported
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   262
     * @return A spliterator for an array
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   263
     * @throws NullPointerException if the given array is {@code null}
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   264
     * @see Arrays#spliterator(long[])
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   265
     */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   266
    public static Spliterator.OfLong spliterator(long[] array,
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   267
                                                 int additionalCharacteristics) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   268
        return new LongArraySpliterator(Objects.requireNonNull(array), additionalCharacteristics);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   269
    }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   270
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   271
    /**
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   272
     * Creates a {@code Spliterator.OfLong} covering a range of elements of a
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   273
     * given array, using a customized set of spliterator characteristics.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   274
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   275
     * <p>This method is provided as an implementation convenience for
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   276
     * Spliterators which store portions of their elements in arrays, and need
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   277
     * fine control over Spliterator characteristics.  Most other situations in
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   278
     * which a Spliterator for an array is needed should use
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   279
     * {@link Arrays#spliterator(long[], int, int)}.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   280
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   281
     * <p>The returned spliterator always reports the characteristics
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   282
     * {@code SIZED} and {@code SUBSIZED}.  The caller may provide additional
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   283
     * characteristics for the spliterator to report.  (For example, if it is
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   284
     * known the array will not be further modified, specify {@code IMMUTABLE};
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   285
     * if the array data is considered to have an an encounter order, specify
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   286
     * {@code ORDERED}).  The method {@link Arrays#spliterator(long[], int, int)} can
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   287
     * often be used instead, which returns a spliterator that reports
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   288
     * {@code SIZED}, {@code SUBSIZED}, {@code IMMUTABLE}, and {@code ORDERED}.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   289
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   290
     * @param array The array, assumed to be unmodified during use
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   291
     * @param fromIndex The least index (inclusive) to cover
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   292
     * @param toIndex One past the greatest index to cover
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   293
     * @param additionalCharacteristics Additional spliterator characteristics
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   294
     *        of this spliterator's source or elements beyond {@code SIZED} and
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   295
     *        {@code SUBSIZED} which are are always reported
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   296
     * @return A spliterator for an array
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   297
     * @throws NullPointerException if the given array is {@code null}
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   298
     * @throws ArrayIndexOutOfBoundsException if {@code fromIndex} is negative,
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   299
     *         {@code toIndex} is less than {@code fromIndex}, or
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   300
     *         {@code toIndex} is greater than the array size
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   301
     * @see Arrays#spliterator(long[], int, int)
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   302
     */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   303
    public static Spliterator.OfLong spliterator(long[] array, int fromIndex, int toIndex,
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   304
                                                 int additionalCharacteristics) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   305
        checkFromToBounds(Objects.requireNonNull(array).length, fromIndex, toIndex);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   306
        return new LongArraySpliterator(array, fromIndex, toIndex, additionalCharacteristics);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   307
    }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   308
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   309
    /**
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   310
     * Creates a {@code Spliterator.OfDouble} covering the elements of a given array,
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   311
     * using a customized set of spliterator characteristics.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   312
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   313
     * <p>This method is provided as an implementation convenience for
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   314
     * Spliterators which store portions of their elements in arrays, and need
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   315
     * fine control over Spliterator characteristics.  Most other situations in
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   316
     * which a Spliterator for an array is needed should use
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   317
     * {@link Arrays#spliterator(double[])}.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   318
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   319
     * <p>The returned spliterator always reports the characteristics
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   320
     * {@code SIZED} and {@code SUBSIZED}.  The caller may provide additional
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   321
     * characteristics for the spliterator to report; it is common to
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   322
     * additionally specify {@code IMMUTABLE} and {@code ORDERED}.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   323
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   324
     * @param array The array, assumed to be unmodified during use
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   325
     * @param additionalCharacteristics Additional spliterator characteristics
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   326
     *        of this spliterator's source or elements beyond {@code SIZED} and
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   327
     *        {@code SUBSIZED} which are are always reported
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   328
     * @return A spliterator for an array
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   329
     * @throws NullPointerException if the given array is {@code null}
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   330
     * @see Arrays#spliterator(double[])
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   331
     */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   332
    public static Spliterator.OfDouble spliterator(double[] array,
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   333
                                                   int additionalCharacteristics) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   334
        return new DoubleArraySpliterator(Objects.requireNonNull(array), additionalCharacteristics);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   335
    }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   336
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   337
    /**
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   338
     * Creates a {@code Spliterator.OfDouble} covering a range of elements of a
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   339
     * given array, using a customized set of spliterator characteristics.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   340
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   341
     * <p>This method is provided as an implementation convenience for
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   342
     * Spliterators which store portions of their elements in arrays, and need
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   343
     * fine control over Spliterator characteristics.  Most other situations in
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   344
     * which a Spliterator for an array is needed should use
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   345
     * {@link Arrays#spliterator(double[], int, int)}.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   346
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   347
     * <p>The returned spliterator always reports the characteristics
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   348
     * {@code SIZED} and {@code SUBSIZED}.  The caller may provide additional
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   349
     * characteristics for the spliterator to report.  (For example, if it is
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   350
     * known the array will not be further modified, specify {@code IMMUTABLE};
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   351
     * if the array data is considered to have an an encounter order, specify
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   352
     * {@code ORDERED}).  The method {@link Arrays#spliterator(long[], int, int)} can
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   353
     * often be used instead, which returns a spliterator that reports
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   354
     * {@code SIZED}, {@code SUBSIZED}, {@code IMMUTABLE}, and {@code ORDERED}.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   355
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   356
     * @param array The array, assumed to be unmodified during use
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   357
     * @param fromIndex The least index (inclusive) to cover
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   358
     * @param toIndex One past the greatest index to cover
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   359
     * @param additionalCharacteristics Additional spliterator characteristics
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   360
     *        of this spliterator's source or elements beyond {@code SIZED} and
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   361
     *        {@code SUBSIZED} which are are always reported
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   362
     * @return A spliterator for an array
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   363
     * @throws NullPointerException if the given array is {@code null}
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   364
     * @throws ArrayIndexOutOfBoundsException if {@code fromIndex} is negative,
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   365
     *         {@code toIndex} is less than {@code fromIndex}, or
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   366
     *         {@code toIndex} is greater than the array size
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   367
     * @see Arrays#spliterator(double[], int, int)
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   368
     */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   369
    public static Spliterator.OfDouble spliterator(double[] array, int fromIndex, int toIndex,
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   370
                                                   int additionalCharacteristics) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   371
        checkFromToBounds(Objects.requireNonNull(array).length, fromIndex, toIndex);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   372
        return new DoubleArraySpliterator(array, fromIndex, toIndex, additionalCharacteristics);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   373
    }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   374
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   375
    /**
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   376
     * Validate inclusive start index and exclusive end index against the length
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   377
     * of an array.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   378
     * @param arrayLength The length of the array
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   379
     * @param origin The inclusive start index
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   380
     * @param fence The exclusive end index
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   381
     * @throws ArrayIndexOutOfBoundsException if the start index is greater than
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   382
     * the end index, if the start index is negative, or the end index is
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   383
     * greater than the array length
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   384
     */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   385
    private static void checkFromToBounds(int arrayLength, int origin, int fence) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   386
        if (origin > fence) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   387
            throw new IllegalArgumentException(
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   388
                    "origin(" + origin + ") > fence(" + fence + ")");
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   389
        }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   390
        if (origin < 0) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   391
            throw new ArrayIndexOutOfBoundsException(origin);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   392
        }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   393
        if (fence > arrayLength) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   394
            throw new ArrayIndexOutOfBoundsException(fence);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   395
        }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   396
    }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   397
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   398
    // Iterator-based spliterators
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   399
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   400
    /**
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   401
     * Creates a {@code Spliterator} using the given collection's
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   402
     * {@link java.util.Collection#iterator()} as the source of elements, and
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   403
     * reporting its {@link java.util.Collection#size()} as its initial size.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   404
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   405
     * <p>The spliterator is
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   406
     * <em><a href="Spliterator.html#binding">late-binding</a></em>, inherits
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   407
     * the <em>fail-fast</em> properties of the collection's iterator, and
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   408
     * implements {@code trySplit} to permit limited parallelism.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   409
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   410
     * @param <T> Type of elements
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   411
     * @param c The collection
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   412
     * @param additionalCharacteristics Additional spliterator characteristics
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   413
     *        of this spliterator's source or elements beyond {@code SIZED} and
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   414
     *        {@code SUBSIZED} which are are always reported
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   415
     * @return A spliterator from an iterator
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   416
     * @throws NullPointerException if the given collection is {@code null}
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   417
     */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   418
    public static <T> Spliterator<T> spliterator(Collection<? extends T> c,
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   419
                                                 int additionalCharacteristics) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   420
        return new IteratorSpliterator<>(Objects.requireNonNull(c),
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   421
                                         additionalCharacteristics);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   422
    }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   423
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   424
    /**
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   425
     * Creates a {@code Spliterator} using a given {@code Iterator}
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   426
     * as the source of elements, and with a given initially reported size.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   427
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   428
     * <p>The spliterator is not
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   429
     * <em><a href="Spliterator.html#binding">late-binding</a></em>, inherits
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   430
     * the <em>fail-fast</em> properties of the iterator, and implements
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   431
     * {@code trySplit} to permit limited parallelism.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   432
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   433
     * <p>Traversal of elements should be accomplished through the spliterator.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   434
     * The behaviour of splitting and traversal is undefined if the iterator is
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   435
     * operated on after the spliterator is returned, or the initially reported
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   436
     * size is not equal to the actual number of elements in the source.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   437
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   438
     * @param <T> Type of elements
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   439
     * @param iterator The iterator for the source
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   440
     * @param size The number of elements in the source, to be reported as
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   441
     *        initial {@code estimateSize}
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   442
     * @param additionalCharacteristics Additional spliterator characteristics
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   443
     *        of this spliterator's source or elements beyond {@code SIZED} and
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   444
     *        {@code SUBSIZED} which are are always reported
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   445
     * @return A spliterator from an iterator
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   446
     * @throws NullPointerException if the given iterator is {@code null}
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   447
     */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   448
    public static <T> Spliterator<T> spliterator(Iterator<? extends T> iterator,
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   449
                                                 long size,
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   450
                                                 int additionalCharacteristics) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   451
        return new IteratorSpliterator<>(Objects.requireNonNull(iterator), size,
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   452
                                         additionalCharacteristics);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   453
    }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   454
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   455
    /**
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   456
     * Creates a {@code Spliterator} using a given {@code Iterator}
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   457
     * as the source of elements, with no initial size estimate.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   458
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   459
     * <p>The spliterator is not
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   460
     * <em><a href="Spliterator.html#binding">late-binding</a></em>, inherits
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   461
     * the <em>fail-fast</em> properties of the iterator, and implements
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   462
     * {@code trySplit} to permit limited parallelism.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   463
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   464
     * <p>Traversal of elements should be accomplished through the spliterator.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   465
     * The behaviour of splitting and traversal is undefined if the iterator is
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   466
     * operated on after the spliterator is returned.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   467
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   468
     * @param <T> Type of elements
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   469
     * @param iterator The iterator for the source
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   470
     * @param characteristics Properties of this spliterator's source
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   471
     *        or elements ({@code SIZED} and {@code SUBSIZED}, if supplied, are
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   472
     *        ignored and are not reported.)
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   473
     * @return A spliterator from an iterator
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   474
     * @throws NullPointerException if the given iterator is {@code null}
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   475
     */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   476
    public static <T> Spliterator<T> spliteratorUnknownSize(Iterator<? extends T> iterator,
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   477
                                                            int characteristics) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   478
        return new IteratorSpliterator<>(Objects.requireNonNull(iterator), characteristics);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   479
    }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   480
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   481
    /**
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   482
     * Creates a {@code Spliterator.OfInt} using a given
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   483
     * {@code IntStream.IntIterator} as the source of elements, and with a given
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   484
     * initially reported size.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   485
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   486
     * <p>The spliterator is not
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   487
     * <em><a href="Spliterator.html#binding">late-binding</a></em>, inherits
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   488
     * the <em>fail-fast</em> properties of the iterator, and implements
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   489
     * {@code trySplit} to permit limited parallelism.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   490
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   491
     * <p>Traversal of elements should be accomplished through the spliterator.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   492
     * The behaviour of splitting and traversal is undefined if the iterator is
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   493
     * operated on after the spliterator is returned, or the initially reported
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   494
     * size is not equal to the actual number of elements in the source.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   495
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   496
     * @param iterator The iterator for the source
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   497
     * @param size The number of elements in the source, to be reported as
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   498
     *        initial {@code estimateSize}.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   499
     * @param additionalCharacteristics Additional spliterator characteristics
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   500
     *        of this spliterator's source or elements beyond {@code SIZED} and
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   501
     *        {@code SUBSIZED} which are are always reported
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   502
     * @return A spliterator from an iterator
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   503
     * @throws NullPointerException if the given iterator is {@code null}
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   504
     */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   505
    public static Spliterator.OfInt spliterator(PrimitiveIterator.OfInt iterator,
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   506
                                                long size,
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   507
                                                int additionalCharacteristics) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   508
        return new IntIteratorSpliterator(Objects.requireNonNull(iterator),
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   509
                                          size, additionalCharacteristics);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   510
    }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   511
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   512
    /**
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   513
     * Creates a {@code Spliterator.OfInt} using a given
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   514
     * {@code IntStream.IntIterator} as the source of elements, with no initial
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   515
     * size estimate.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   516
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   517
     * <p>The spliterator is not
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   518
     * <em><a href="Spliterator.html#binding">late-binding</a></em>, inherits
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   519
     * the <em>fail-fast</em> properties of the iterator, and implements
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   520
     * {@code trySplit} to permit limited parallelism.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   521
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   522
     * <p>Traversal of elements should be accomplished through the spliterator.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   523
     * The behaviour of splitting and traversal is undefined if the iterator is
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   524
     * operated on after the spliterator is returned.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   525
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   526
     * @param iterator The iterator for the source
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   527
     * @param characteristics Properties of this spliterator's source
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   528
     *        or elements ({@code SIZED} and {@code SUBSIZED}, if supplied, are
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   529
     *        ignored and are not reported.)
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   530
     * @return A spliterator from an iterator
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   531
     * @throws NullPointerException if the given iterator is {@code null}
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   532
     */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   533
    public static Spliterator.OfInt spliteratorUnknownSize(PrimitiveIterator.OfInt iterator,
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   534
                                                           int characteristics) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   535
        return new IntIteratorSpliterator(Objects.requireNonNull(iterator), characteristics);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   536
    }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   537
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   538
    /**
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   539
     * Creates a {@code Spliterator.OfLong} using a given
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   540
     * {@code LongStream.LongIterator} as the source of elements, and with a
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   541
     * given initially reported size.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   542
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   543
     * <p>The spliterator is not
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   544
     * <em><a href="Spliterator.html#binding">late-binding</a></em>, inherits
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   545
     * the <em>fail-fast</em> properties of the iterator, and implements
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   546
     * {@code trySplit} to permit limited parallelism.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   547
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   548
     * <p>Traversal of elements should be accomplished through the spliterator.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   549
     * The behaviour of splitting and traversal is undefined if the iterator is
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   550
     * operated on after the spliterator is returned, or the initially reported
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   551
     * size is not equal to the actual number of elements in the source.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   552
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   553
     * @param iterator The iterator for the source
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   554
     * @param size The number of elements in the source, to be reported as
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   555
     *        initial {@code estimateSize}.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   556
     * @param additionalCharacteristics Additional spliterator characteristics
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   557
     *        of this spliterator's source or elements beyond {@code SIZED} and
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   558
     *        {@code SUBSIZED} which are are always reported
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   559
     * @return A spliterator from an iterator
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   560
     * @throws NullPointerException if the given iterator is {@code null}
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   561
     */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   562
    public static Spliterator.OfLong spliterator(PrimitiveIterator.OfLong iterator,
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   563
                                                 long size,
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   564
                                                 int additionalCharacteristics) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   565
        return new LongIteratorSpliterator(Objects.requireNonNull(iterator),
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   566
                                           size, additionalCharacteristics);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   567
    }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   568
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   569
    /**
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   570
     * Creates a {@code Spliterator.OfLong} using a given
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   571
     * {@code LongStream.LongIterator} as the source of elements, with no
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   572
     * initial size estimate.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   573
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   574
     * <p>The spliterator is not
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   575
     * <em><a href="Spliterator.html#binding">late-binding</a></em>, inherits
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   576
     * the <em>fail-fast</em> properties of the iterator, and implements
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   577
     * {@code trySplit} to permit limited parallelism.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   578
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   579
     * <p>Traversal of elements should be accomplished through the spliterator.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   580
     * The behaviour of splitting and traversal is undefined if the iterator is
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   581
     * operated on after the spliterator is returned.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   582
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   583
     * @param iterator The iterator for the source
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   584
     * @param characteristics Properties of this spliterator's source
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   585
     *        or elements ({@code SIZED} and {@code SUBSIZED}, if supplied, are
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   586
     *        ignored and are not reported.)
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   587
     * @return A spliterator from an iterator
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   588
     * @throws NullPointerException if the given iterator is {@code null}
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   589
     */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   590
    public static Spliterator.OfLong spliteratorUnknownSize(PrimitiveIterator.OfLong iterator,
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   591
                                                            int characteristics) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   592
        return new LongIteratorSpliterator(Objects.requireNonNull(iterator), characteristics);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   593
    }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   594
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   595
    /**
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   596
     * Creates a {@code Spliterator.OfDouble} using a given
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   597
     * {@code DoubleStream.DoubleIterator} as the source of elements, and with a
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   598
     * given initially reported size.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   599
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   600
     * <p>The spliterator is not
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   601
     * <em><a href="Spliterator.html#binding">late-binding</a></em>, inherits
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   602
     * the <em>fail-fast</em> properties of the iterator, and implements
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   603
     * {@code trySplit} to permit limited parallelism.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   604
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   605
     * <p>Traversal of elements should be accomplished through the spliterator.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   606
     * The behaviour of splitting and traversal is undefined if the iterator is
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   607
     * operated on after the spliterator is returned, or the initially reported
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   608
     * size is not equal to the actual number of elements in the source.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   609
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   610
     * @param iterator The iterator for the source
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   611
     * @param size The number of elements in the source, to be reported as
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   612
     *        initial {@code estimateSize}
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   613
     * @param additionalCharacteristics Additional spliterator characteristics
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   614
     *        of this spliterator's source or elements beyond {@code SIZED} and
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   615
     *        {@code SUBSIZED} which are are always reported
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   616
     * @return A spliterator from an iterator
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   617
     * @throws NullPointerException if the given iterator is {@code null}
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   618
     */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   619
    public static Spliterator.OfDouble spliterator(PrimitiveIterator.OfDouble iterator,
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   620
                                                   long size,
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   621
                                                   int additionalCharacteristics) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   622
        return new DoubleIteratorSpliterator(Objects.requireNonNull(iterator),
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   623
                                             size, additionalCharacteristics);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   624
    }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   625
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   626
    /**
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   627
     * Creates a {@code Spliterator.OfDouble} using a given
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   628
     * {@code DoubleStream.DoubleIterator} as the source of elements, with no
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   629
     * initial size estimate.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   630
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   631
     * <p>The spliterator is not
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   632
     * <em><a href="Spliterator.html#binding">late-binding</a></em>, inherits
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   633
     * the <em>fail-fast</em> properties of the iterator, and implements
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   634
     * {@code trySplit} to permit limited parallelism.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   635
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   636
     * <p>Traversal of elements should be accomplished through the spliterator.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   637
     * The behaviour of splitting and traversal is undefined if the iterator is
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   638
     * operated on after the spliterator is returned.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   639
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   640
     * @param iterator The iterator for the source
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   641
     * @param characteristics Properties of this spliterator's source
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   642
     *        or elements ({@code SIZED} and {@code SUBSIZED}, if supplied, are
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   643
     *        ignored and are not reported.)
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   644
     * @return A spliterator from an iterator
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   645
     * @throws NullPointerException if the given iterator is {@code null}
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   646
     */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   647
    public static Spliterator.OfDouble spliteratorUnknownSize(PrimitiveIterator.OfDouble iterator,
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   648
                                                              int characteristics) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   649
        return new DoubleIteratorSpliterator(Objects.requireNonNull(iterator), characteristics);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   650
    }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   651
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   652
    // Iterators from Spliterators
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   653
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   654
    /**
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   655
     * Creates an {@code Iterator} from a {@code Spliterator}.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   656
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   657
     * <p>Traversal of elements should be accomplished through the iterator.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   658
     * The behaviour of traversal is undefined if the spliterator is operated
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   659
     * after the iterator is returned.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   660
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   661
     * @param <T> Type of elements
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   662
     * @param spliterator The spliterator
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   663
     * @return An iterator
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   664
     * @throws NullPointerException if the given spliterator is {@code null}
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   665
     */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   666
    public static<T> Iterator<T> iteratorFromSpliterator(Spliterator<? extends T> spliterator) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   667
        Objects.requireNonNull(spliterator);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   668
        class Adapter implements Iterator<T>, Consumer<T> {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   669
            boolean valueReady = false;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   670
            T nextElement;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   671
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   672
            @Override
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   673
            public void accept(T t) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   674
                valueReady = true;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   675
                nextElement = t;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   676
            }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   677
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   678
            @Override
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   679
            public boolean hasNext() {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   680
                if (!valueReady)
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   681
                    spliterator.tryAdvance(this);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   682
                return valueReady;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   683
            }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   684
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   685
            @Override
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   686
            public T next() {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   687
                if (!valueReady && !hasNext())
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   688
                    throw new NoSuchElementException();
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   689
                else {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   690
                    valueReady = false;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   691
                    return nextElement;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   692
                }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   693
            }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   694
        }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   695
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   696
        return new Adapter();
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   697
    }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   698
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   699
    /**
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   700
     * Creates an {@code PrimitiveIterator.OfInt} from a
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   701
     * {@code Spliterator.OfInt}.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   702
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   703
     * <p>Traversal of elements should be accomplished through the iterator.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   704
     * The behaviour of traversal is undefined if the spliterator is operated
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   705
     * after the iterator is returned.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   706
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   707
     * @param spliterator The spliterator
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   708
     * @return An iterator
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   709
     * @throws NullPointerException if the given spliterator is {@code null}
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   710
     */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   711
    public static PrimitiveIterator.OfInt iteratorFromSpliterator(Spliterator.OfInt spliterator) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   712
        Objects.requireNonNull(spliterator);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   713
        class Adapter implements PrimitiveIterator.OfInt, IntConsumer {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   714
            boolean valueReady = false;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   715
            int nextElement;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   716
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   717
            @Override
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   718
            public void accept(int t) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   719
                valueReady = true;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   720
                nextElement = t;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   721
            }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   722
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   723
            @Override
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   724
            public boolean hasNext() {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   725
                if (!valueReady)
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   726
                    spliterator.tryAdvance(this);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   727
                return valueReady;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   728
            }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   729
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   730
            @Override
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   731
            public int nextInt() {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   732
                if (!valueReady && !hasNext())
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   733
                    throw new NoSuchElementException();
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   734
                else {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   735
                    valueReady = false;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   736
                    return nextElement;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   737
                }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   738
            }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   739
        }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   740
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   741
        return new Adapter();
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   742
    }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   743
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   744
    /**
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   745
     * Creates an {@code PrimitiveIterator.OfLong} from a
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   746
     * {@code Spliterator.OfLong}.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   747
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   748
     * <p>Traversal of elements should be accomplished through the iterator.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   749
     * The behaviour of traversal is undefined if the spliterator is operated
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   750
     * after the iterator is returned.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   751
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   752
     * @param spliterator The spliterator
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   753
     * @return An iterator
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   754
     * @throws NullPointerException if the given spliterator is {@code null}
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   755
     */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   756
    public static PrimitiveIterator.OfLong iteratorFromSpliterator(Spliterator.OfLong spliterator) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   757
        Objects.requireNonNull(spliterator);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   758
        class Adapter implements PrimitiveIterator.OfLong, LongConsumer {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   759
            boolean valueReady = false;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   760
            long nextElement;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   761
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   762
            @Override
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   763
            public void accept(long t) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   764
                valueReady = true;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   765
                nextElement = t;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   766
            }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   767
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   768
            @Override
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   769
            public boolean hasNext() {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   770
                if (!valueReady)
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   771
                    spliterator.tryAdvance(this);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   772
                return valueReady;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   773
            }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   774
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   775
            @Override
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   776
            public long nextLong() {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   777
                if (!valueReady && !hasNext())
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   778
                    throw new NoSuchElementException();
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   779
                else {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   780
                    valueReady = false;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   781
                    return nextElement;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   782
                }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   783
            }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   784
        }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   785
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   786
        return new Adapter();
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   787
    }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   788
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   789
    /**
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   790
     * Creates an {@code PrimitiveIterator.OfDouble} from a
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   791
     * {@code Spliterator.OfDouble}.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   792
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   793
     * <p>Traversal of elements should be accomplished through the iterator.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   794
     * The behaviour of traversal is undefined if the spliterator is operated
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   795
     * after the iterator is returned.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   796
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   797
     * @param spliterator The spliterator
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   798
     * @return An iterator
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   799
     * @throws NullPointerException if the given spliterator is {@code null}
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   800
     */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   801
    public static PrimitiveIterator.OfDouble iteratorFromSpliterator(Spliterator.OfDouble spliterator) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   802
        Objects.requireNonNull(spliterator);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   803
        class Adapter implements PrimitiveIterator.OfDouble, DoubleConsumer {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   804
            boolean valueReady = false;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   805
            double nextElement;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   806
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   807
            @Override
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   808
            public void accept(double t) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   809
                valueReady = true;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   810
                nextElement = t;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   811
            }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   812
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   813
            @Override
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   814
            public boolean hasNext() {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   815
                if (!valueReady)
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   816
                    spliterator.tryAdvance(this);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   817
                return valueReady;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   818
            }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   819
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   820
            @Override
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   821
            public double nextDouble() {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   822
                if (!valueReady && !hasNext())
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   823
                    throw new NoSuchElementException();
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   824
                else {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   825
                    valueReady = false;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   826
                    return nextElement;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   827
                }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   828
            }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   829
        }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   830
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   831
        return new Adapter();
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   832
    }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   833
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   834
    // Implementations
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   835
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   836
    private static abstract class EmptySpliterator<T, S extends Spliterator<T>, C> {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   837
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   838
        EmptySpliterator() { }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   839
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   840
        public S trySplit() {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   841
            return null;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   842
        }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   843
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   844
        public boolean tryAdvance(C consumer) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   845
            Objects.requireNonNull(consumer);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   846
            return false;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   847
        }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   848
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   849
        public void forEachRemaining(C consumer) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   850
            Objects.requireNonNull(consumer);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   851
        }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   852
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   853
        public long estimateSize() {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   854
            return 0;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   855
        }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   856
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   857
        public int characteristics() {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   858
            return Spliterator.SIZED | Spliterator.SUBSIZED;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   859
        }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   860
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   861
        private static final class OfRef<T>
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   862
                extends EmptySpliterator<T, Spliterator<T>, Consumer<? super T>>
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   863
                implements Spliterator<T> {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   864
            OfRef() { }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   865
        }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   866
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   867
        private static final class OfInt
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   868
                extends EmptySpliterator<Integer, Spliterator.OfInt, IntConsumer>
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   869
                implements Spliterator.OfInt {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   870
            OfInt() { }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   871
        }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   872
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   873
        private static final class OfLong
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   874
                extends EmptySpliterator<Long, Spliterator.OfLong, LongConsumer>
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   875
                implements Spliterator.OfLong {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   876
            OfLong() { }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   877
        }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   878
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   879
        private static final class OfDouble
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   880
                extends EmptySpliterator<Double, Spliterator.OfDouble, DoubleConsumer>
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   881
                implements Spliterator.OfDouble {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   882
            OfDouble() { }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   883
        }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   884
    }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   885
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   886
    // Array-based spliterators
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   887
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   888
    /**
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   889
     * A Spliterator designed for use by sources that traverse and split
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   890
     * elements maintained in an unmodifiable {@code Object[]} array.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   891
     */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   892
    static final class ArraySpliterator<T> implements Spliterator<T> {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   893
        /**
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   894
         * The array, explicitly typed as Object[]. Unlike in some other
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   895
         * classes (see for example CR 6260652), we do not need to
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   896
         * screen arguments to ensure they are exactly of type Object[]
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   897
         * so long as no methods write into the array or serialize it,
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   898
         * which we ensure here by defining this class as final.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   899
         */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   900
        private final Object[] array;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   901
        private int index;        // current index, modified on advance/split
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   902
        private final int fence;  // one past last index
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   903
        private final int characteristics;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   904
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   905
        /**
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   906
         * Creates a spliterator covering all of the given array.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   907
         * @param array the array, assumed to be unmodified during use
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   908
         * @param additionalCharacteristics Additional spliterator characteristics
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   909
         * of this spliterator's source or elements beyond {@code SIZED} and
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   910
         * {@code SUBSIZED} which are are always reported
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   911
         */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   912
        public ArraySpliterator(Object[] array, int additionalCharacteristics) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   913
            this(array, 0, array.length, additionalCharacteristics);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   914
        }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   915
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   916
        /**
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   917
         * Creates a spliterator covering the given array and range
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   918
         * @param array the array, assumed to be unmodified during use
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   919
         * @param origin the least index (inclusive) to cover
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   920
         * @param fence one past the greatest index to cover
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   921
         * @param additionalCharacteristics Additional spliterator characteristics
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   922
         * of this spliterator's source or elements beyond {@code SIZED} and
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   923
         * {@code SUBSIZED} which are are always reported
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   924
         */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   925
        public ArraySpliterator(Object[] array, int origin, int fence, int additionalCharacteristics) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   926
            this.array = array;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   927
            this.index = origin;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   928
            this.fence = fence;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   929
            this.characteristics = additionalCharacteristics | Spliterator.SIZED | Spliterator.SUBSIZED;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   930
        }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   931
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   932
        @Override
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   933
        public Spliterator<T> trySplit() {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   934
            int lo = index, mid = (lo + fence) >>> 1;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   935
            return (lo >= mid)
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   936
                   ? null
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   937
                   : new ArraySpliterator<>(array, lo, index = mid, characteristics);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   938
        }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   939
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   940
        @SuppressWarnings("unchecked")
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   941
        @Override
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   942
        public void forEachRemaining(Consumer<? super T> action) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   943
            Object[] a; int i, hi; // hoist accesses and checks from loop
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   944
            if (action == null)
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   945
                throw new NullPointerException();
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   946
            if ((a = array).length >= (hi = fence) &&
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   947
                (i = index) >= 0 && i < (index = hi)) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   948
                do { action.accept((T)a[i]); } while (++i < hi);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   949
            }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   950
        }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   951
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   952
        @Override
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   953
        public boolean tryAdvance(Consumer<? super T> action) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   954
            if (action == null)
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   955
                throw new NullPointerException();
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   956
            if (index >= 0 && index < fence) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   957
                @SuppressWarnings("unchecked") T e = (T) array[index++];
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   958
                action.accept(e);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   959
                return true;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   960
            }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   961
            return false;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   962
        }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   963
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   964
        @Override
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   965
        public long estimateSize() { return (long)(fence - index); }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   966
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   967
        @Override
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   968
        public int characteristics() {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   969
            return characteristics;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   970
        }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   971
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   972
        @Override
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   973
        public Comparator<? super T> getComparator() {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   974
            if (hasCharacteristics(Spliterator.SORTED))
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   975
                return null;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   976
            throw new IllegalStateException();
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   977
        }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   978
    }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   979
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   980
    /**
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   981
     * A Spliterator.OfInt designed for use by sources that traverse and split
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   982
     * elements maintained in an unmodifiable {@code int[]} array.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   983
     */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   984
    static final class IntArraySpliterator implements Spliterator.OfInt {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   985
        private final int[] array;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   986
        private int index;        // current index, modified on advance/split
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   987
        private final int fence;  // one past last index
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   988
        private final int characteristics;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   989
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   990
        /**
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   991
         * Creates a spliterator covering all of the given array.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   992
         * @param array the array, assumed to be unmodified during use
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   993
         * @param additionalCharacteristics Additional spliterator characteristics
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   994
         *        of this spliterator's source or elements beyond {@code SIZED} and
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   995
         *        {@code SUBSIZED} which are are always reported
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   996
         */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   997
        public IntArraySpliterator(int[] array, int additionalCharacteristics) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   998
            this(array, 0, array.length, additionalCharacteristics);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   999
        }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1000
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1001
        /**
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1002
         * Creates a spliterator covering the given array and range
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1003
         * @param array the array, assumed to be unmodified during use
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1004
         * @param origin the least index (inclusive) to cover
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1005
         * @param fence one past the greatest index to cover
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1006
         * @param additionalCharacteristics Additional spliterator characteristics
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1007
         *        of this spliterator's source or elements beyond {@code SIZED} and
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1008
         *        {@code SUBSIZED} which are are always reported
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1009
         */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1010
        public IntArraySpliterator(int[] array, int origin, int fence, int additionalCharacteristics) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1011
            this.array = array;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1012
            this.index = origin;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1013
            this.fence = fence;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1014
            this.characteristics = additionalCharacteristics | Spliterator.SIZED | Spliterator.SUBSIZED;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1015
        }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1016
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1017
        @Override
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1018
        public OfInt trySplit() {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1019
            int lo = index, mid = (lo + fence) >>> 1;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1020
            return (lo >= mid)
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1021
                   ? null
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1022
                   : new IntArraySpliterator(array, lo, index = mid, characteristics);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1023
        }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1024
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1025
        @Override
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1026
        public void forEachRemaining(IntConsumer action) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1027
            int[] a; int i, hi; // hoist accesses and checks from loop
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1028
            if (action == null)
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1029
                throw new NullPointerException();
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1030
            if ((a = array).length >= (hi = fence) &&
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1031
                (i = index) >= 0 && i < (index = hi)) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1032
                do { action.accept(a[i]); } while (++i < hi);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1033
            }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1034
        }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1035
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1036
        @Override
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1037
        public boolean tryAdvance(IntConsumer action) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1038
            if (action == null)
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1039
                throw new NullPointerException();
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1040
            if (index >= 0 && index < fence) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1041
                action.accept(array[index++]);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1042
                return true;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1043
            }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1044
            return false;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1045
        }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1046
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1047
        @Override
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1048
        public long estimateSize() { return (long)(fence - index); }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1049
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1050
        @Override
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1051
        public int characteristics() {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1052
            return characteristics;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1053
        }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1054
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1055
        @Override
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1056
        public Comparator<? super Integer> getComparator() {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1057
            if (hasCharacteristics(Spliterator.SORTED))
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1058
                return null;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1059
            throw new IllegalStateException();
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1060
        }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1061
    }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1062
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1063
    /**
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1064
     * A Spliterator.OfLong designed for use by sources that traverse and split
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1065
     * elements maintained in an unmodifiable {@code int[]} array.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1066
     */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1067
    static final class LongArraySpliterator implements Spliterator.OfLong {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1068
        private final long[] array;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1069
        private int index;        // current index, modified on advance/split
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1070
        private final int fence;  // one past last index
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1071
        private final int characteristics;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1072
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1073
        /**
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1074
         * Creates a spliterator covering all of the given array.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1075
         * @param array the array, assumed to be unmodified during use
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1076
         * @param additionalCharacteristics Additional spliterator characteristics
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1077
         *        of this spliterator's source or elements beyond {@code SIZED} and
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1078
         *        {@code SUBSIZED} which are are always reported
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1079
         */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1080
        public LongArraySpliterator(long[] array, int additionalCharacteristics) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1081
            this(array, 0, array.length, additionalCharacteristics);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1082
        }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1083
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1084
        /**
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1085
         * Creates a spliterator covering the given array and range
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1086
         * @param array the array, assumed to be unmodified during use
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1087
         * @param origin the least index (inclusive) to cover
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1088
         * @param fence one past the greatest index to cover
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1089
         * @param additionalCharacteristics Additional spliterator characteristics
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1090
         *        of this spliterator's source or elements beyond {@code SIZED} and
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1091
         *        {@code SUBSIZED} which are are always reported
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1092
         */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1093
        public LongArraySpliterator(long[] array, int origin, int fence, int additionalCharacteristics) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1094
            this.array = array;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1095
            this.index = origin;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1096
            this.fence = fence;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1097
            this.characteristics = additionalCharacteristics | Spliterator.SIZED | Spliterator.SUBSIZED;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1098
        }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1099
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1100
        @Override
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1101
        public OfLong trySplit() {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1102
            int lo = index, mid = (lo + fence) >>> 1;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1103
            return (lo >= mid)
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1104
                   ? null
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1105
                   : new LongArraySpliterator(array, lo, index = mid, characteristics);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1106
        }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1107
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1108
        @Override
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1109
        public void forEachRemaining(LongConsumer action) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1110
            long[] a; int i, hi; // hoist accesses and checks from loop
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1111
            if (action == null)
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1112
                throw new NullPointerException();
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1113
            if ((a = array).length >= (hi = fence) &&
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1114
                (i = index) >= 0 && i < (index = hi)) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1115
                do { action.accept(a[i]); } while (++i < hi);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1116
            }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1117
        }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1118
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1119
        @Override
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1120
        public boolean tryAdvance(LongConsumer action) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1121
            if (action == null)
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1122
                throw new NullPointerException();
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1123
            if (index >= 0 && index < fence) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1124
                action.accept(array[index++]);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1125
                return true;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1126
            }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1127
            return false;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1128
        }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1129
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1130
        @Override
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1131
        public long estimateSize() { return (long)(fence - index); }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1132
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1133
        @Override
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1134
        public int characteristics() {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1135
            return characteristics;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1136
        }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1137
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1138
        @Override
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1139
        public Comparator<? super Long> getComparator() {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1140
            if (hasCharacteristics(Spliterator.SORTED))
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1141
                return null;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1142
            throw new IllegalStateException();
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1143
        }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1144
    }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1145
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1146
    /**
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1147
     * A Spliterator.OfDouble designed for use by sources that traverse and split
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1148
     * elements maintained in an unmodifiable {@code int[]} array.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1149
     */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1150
    static final class DoubleArraySpliterator implements Spliterator.OfDouble {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1151
        private final double[] array;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1152
        private int index;        // current index, modified on advance/split
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1153
        private final int fence;  // one past last index
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1154
        private final int characteristics;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1155
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1156
        /**
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1157
         * Creates a spliterator covering all of the given array.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1158
         * @param array the array, assumed to be unmodified during use
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1159
         * @param additionalCharacteristics Additional spliterator characteristics
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1160
         *        of this spliterator's source or elements beyond {@code SIZED} and
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1161
         *        {@code SUBSIZED} which are are always reported
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1162
         */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1163
        public DoubleArraySpliterator(double[] array, int additionalCharacteristics) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1164
            this(array, 0, array.length, additionalCharacteristics);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1165
        }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1166
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1167
        /**
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1168
         * Creates a spliterator covering the given array and range
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1169
         * @param array the array, assumed to be unmodified during use
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1170
         * @param origin the least index (inclusive) to cover
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1171
         * @param fence one past the greatest index to cover
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1172
         * @param additionalCharacteristics Additional spliterator characteristics
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1173
         *        of this spliterator's source or elements beyond {@code SIZED} and
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1174
         *        {@code SUBSIZED} which are are always reported
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1175
         */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1176
        public DoubleArraySpliterator(double[] array, int origin, int fence, int additionalCharacteristics) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1177
            this.array = array;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1178
            this.index = origin;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1179
            this.fence = fence;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1180
            this.characteristics = additionalCharacteristics | Spliterator.SIZED | Spliterator.SUBSIZED;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1181
        }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1182
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1183
        @Override
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1184
        public OfDouble trySplit() {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1185
            int lo = index, mid = (lo + fence) >>> 1;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1186
            return (lo >= mid)
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1187
                   ? null
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1188
                   : new DoubleArraySpliterator(array, lo, index = mid, characteristics);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1189
        }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1190
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1191
        @Override
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1192
        public void forEachRemaining(DoubleConsumer action) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1193
            double[] a; int i, hi; // hoist accesses and checks from loop
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1194
            if (action == null)
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1195
                throw new NullPointerException();
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1196
            if ((a = array).length >= (hi = fence) &&
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1197
                (i = index) >= 0 && i < (index = hi)) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1198
                do { action.accept(a[i]); } while (++i < hi);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1199
            }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1200
        }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1201
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1202
        @Override
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1203
        public boolean tryAdvance(DoubleConsumer action) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1204
            if (action == null)
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1205
                throw new NullPointerException();
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1206
            if (index >= 0 && index < fence) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1207
                action.accept(array[index++]);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1208
                return true;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1209
            }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1210
            return false;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1211
        }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1212
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1213
        @Override
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1214
        public long estimateSize() { return (long)(fence - index); }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1215
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1216
        @Override
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1217
        public int characteristics() {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1218
            return characteristics;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1219
        }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1220
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1221
        @Override
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1222
        public Comparator<? super Double> getComparator() {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1223
            if (hasCharacteristics(Spliterator.SORTED))
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1224
                return null;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1225
            throw new IllegalStateException();
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1226
        }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1227
    }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1228
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1229
    //
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1230
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1231
    /**
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1232
     * An abstract {@code Spliterator} that implements {@code trySplit} to
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1233
     * permit limited parallelism.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1234
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1235
     * <p>An extending class need only
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1236
     * implement {@link #tryAdvance(java.util.function.Consumer) tryAdvance}.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1237
     * The extending class should override
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1238
     * {@link #forEachRemaining(java.util.function.Consumer) forEach} if it can
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1239
     * provide a more performant implementation.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1240
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1241
     * @apiNote
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1242
     * This class is a useful aid for creating a spliterator when it is not
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1243
     * possible or difficult to efficiently partition elements in a manner
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1244
     * allowing balanced parallel computation.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1245
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1246
     * <p>An alternative to using this class, that also permits limited
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1247
     * parallelism, is to create a spliterator from an iterator
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1248
     * (see {@link #spliterator(Iterator, long, int)}.  Depending on the
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1249
     * circumstances using an iterator may be easier or more convenient than
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1250
     * extending this class, such as when there is already an iterator
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1251
     * available to use.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1252
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1253
     * @see #spliterator(Iterator, long, int)
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1254
     * @since 1.8
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1255
     */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1256
    public static abstract class AbstractSpliterator<T> implements Spliterator<T> {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1257
        static final int BATCH_UNIT = 1 << 10;  // batch array size increment
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1258
        static final int MAX_BATCH = 1 << 25;  // max batch array size;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1259
        private final int characteristics;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1260
        private long est;             // size estimate
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1261
        private int batch;            // batch size for splits
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1262
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1263
        /**
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1264
         * Creates a spliterator reporting the given estimated size and
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1265
         * additionalCharacteristics.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1266
         *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1267
         * @param est the estimated size of this spliterator if known, otherwise
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1268
         *        {@code Long.MAX_VALUE}.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1269
         * @param additionalCharacteristics properties of this spliterator's
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1270
         *        source or elements.  If {@code SIZED} is reported then this
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1271
         *        spliterator will additionally report {@code SUBSIZED}.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1272
         */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1273
        protected AbstractSpliterator(long est, int additionalCharacteristics) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1274
            this.est = est;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1275
            this.characteristics = ((additionalCharacteristics & Spliterator.SIZED) != 0)
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1276
                                   ? additionalCharacteristics | Spliterator.SUBSIZED
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1277
                                   : additionalCharacteristics;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1278
        }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1279
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1280
        static final class HoldingConsumer<T> implements Consumer<T> {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1281
            Object value;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1282
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1283
            @Override
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1284
            public void accept(T value) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1285
                this.value = value;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1286
            }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1287
        }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1288
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1289
        /**
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1290
         * {@inheritDoc}
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1291
         *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1292
         * This implementation permits limited parallelism.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1293
         */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1294
        @Override
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1295
        public Spliterator<T> trySplit() {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1296
            /*
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1297
             * Split into arrays of arithmetically increasing batch
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1298
             * sizes.  This will only improve parallel performance if
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1299
             * per-element Consumer actions are more costly than
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1300
             * transferring them into an array.  The use of an
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1301
             * arithmetic progression in split sizes provides overhead
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1302
             * vs parallelism bounds that do not particularly favor or
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1303
             * penalize cases of lightweight vs heavyweight element
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1304
             * operations, across combinations of #elements vs #cores,
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1305
             * whether or not either are known.  We generate
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1306
             * O(sqrt(#elements)) splits, allowing O(sqrt(#cores))
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1307
             * potential speedup.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1308
             */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1309
            HoldingConsumer<T> holder = new HoldingConsumer<>();
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1310
            long s = est;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1311
            if (s > 1 && tryAdvance(holder)) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1312
                int n = batch + BATCH_UNIT;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1313
                if (n > s)
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1314
                    n = (int) s;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1315
                if (n > MAX_BATCH)
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1316
                    n = MAX_BATCH;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1317
                Object[] a;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1318
                try {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1319
                    a = new Object[n];
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1320
                } catch (OutOfMemoryError oome) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1321
                    return null;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1322
                }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1323
                int j = 0;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1324
                do { a[j] = holder.value; } while (++j < n && tryAdvance(holder));
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1325
                batch = j;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1326
                if (est != Long.MAX_VALUE)
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1327
                    est -= j;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1328
                return new ArraySpliterator<>(a, 0, j, characteristics());
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1329
            }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1330
            return null;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1331
        }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1332
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1333
        /**
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1334
         * {@inheritDoc}
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1335
         *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1336
         * @implSpec
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1337
         * This implementation returns the estimated size as reported when
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1338
         * created and, if the estimate size is known, decreases in size when
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1339
         * split.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1340
         */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1341
        @Override
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1342
        public long estimateSize() {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1343
            return est;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1344
        }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1345
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1346
        /**
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1347
         * {@inheritDoc}
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1348
         *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1349
         * @implSpec
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1350
         * This implementation returns the characteristics as reported when
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1351
         * created.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1352
         */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1353
        @Override
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1354
        public int characteristics() {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1355
            return characteristics;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1356
        }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1357
    }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1358
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1359
    /**
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1360
     * An abstract {@code Spliterator.OfInt} that implements {@code trySplit} to
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1361
     * permit limited parallelism.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1362
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1363
     * <p>To implement a spliterator an extending class need only
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1364
     * implement {@link #tryAdvance(java.util.function.IntConsumer)}
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1365
     * tryAdvance}.  The extending class should override
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1366
     * {@link #forEachRemaining(java.util.function.IntConsumer)} forEach} if it
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1367
     * can provide a more performant implementation.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1368
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1369
     * @apiNote
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1370
     * This class is a useful aid for creating a spliterator when it is not
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1371
     * possible or difficult to efficiently partition elements in a manner
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1372
     * allowing balanced parallel computation.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1373
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1374
     * <p>An alternative to using this class, that also permits limited
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1375
     * parallelism, is to create a spliterator from an iterator
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1376
     * (see {@link #spliterator(java.util.PrimitiveIterator.OfInt, long, int)}.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1377
     * Depending on the circumstances using an iterator may be easier or more
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1378
     * convenient than extending this class. For example, if there is already an
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1379
     * iterator available to use then there is no need to extend this class.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1380
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1381
     * @see #spliterator(java.util.PrimitiveIterator.OfInt, long, int)
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1382
     * @since 1.8
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1383
     */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1384
    public static abstract class AbstractIntSpliterator implements Spliterator.OfInt {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1385
        static final int MAX_BATCH = AbstractSpliterator.MAX_BATCH;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1386
        static final int BATCH_UNIT = AbstractSpliterator.BATCH_UNIT;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1387
        private final int characteristics;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1388
        private long est;             // size estimate
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1389
        private int batch;            // batch size for splits
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1390
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1391
        /**
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1392
         * Creates a spliterator reporting the given estimated size and
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1393
         * characteristics.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1394
         *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1395
         * @param est the estimated size of this spliterator if known, otherwise
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1396
         *        {@code Long.MAX_VALUE}.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1397
         * @param additionalCharacteristics properties of this spliterator's
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1398
         *        source or elements.  If {@code SIZED} is reported then this
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1399
         *        spliterator will additionally report {@code SUBSIZED}.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1400
         */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1401
        protected AbstractIntSpliterator(long est, int additionalCharacteristics) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1402
            this.est = est;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1403
            this.characteristics = ((additionalCharacteristics & Spliterator.SIZED) != 0)
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1404
                                   ? additionalCharacteristics | Spliterator.SUBSIZED
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1405
                                   : additionalCharacteristics;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1406
        }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1407
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1408
        static final class HoldingIntConsumer implements IntConsumer {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1409
            int value;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1410
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1411
            @Override
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1412
            public void accept(int value) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1413
                this.value = value;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1414
            }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1415
        }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1416
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1417
        /**
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1418
         * {@inheritDoc}
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1419
         *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1420
         * This implementation permits limited parallelism.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1421
         */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1422
        @Override
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1423
        public Spliterator.OfInt trySplit() {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1424
            HoldingIntConsumer holder = new HoldingIntConsumer();
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1425
            long s = est;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1426
            if (s > 1 && tryAdvance(holder)) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1427
                int n = batch + BATCH_UNIT;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1428
                if (n > s)
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1429
                    n = (int) s;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1430
                if (n > MAX_BATCH)
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1431
                    n = MAX_BATCH;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1432
                int[] a;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1433
                try {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1434
                    a = new int[n];
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1435
                } catch (OutOfMemoryError oome) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1436
                    return null;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1437
                }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1438
                int j = 0;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1439
                do { a[j] = holder.value; } while (++j < n && tryAdvance(holder));
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1440
                batch = j;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1441
                if (est != Long.MAX_VALUE)
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1442
                    est -= j;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1443
                return new IntArraySpliterator(a, 0, j, characteristics());
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1444
            }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1445
            return null;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1446
        }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1447
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1448
        /**
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1449
         * {@inheritDoc}
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1450
         *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1451
         * @implSpec
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1452
         * This implementation returns the estimated size as reported when
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1453
         * created and, if the estimate size is known, decreases in size when
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1454
         * split.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1455
         */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1456
        @Override
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1457
        public long estimateSize() {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1458
            return est;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1459
        }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1460
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1461
        /**
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1462
         * {@inheritDoc}
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1463
         *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1464
         * @implSpec
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1465
         * This implementation returns the characteristics as reported when
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1466
         * created.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1467
         */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1468
        @Override
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1469
        public int characteristics() {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1470
            return characteristics;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1471
        }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1472
    }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1473
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1474
    /**
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1475
     * An abstract {@code Spliterator.OfLong} that implements {@code trySplit}
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1476
     * to permit limited parallelism.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1477
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1478
     * <p>To implement a spliterator an extending class need only
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1479
     * implement {@link #tryAdvance(java.util.function.LongConsumer)}
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1480
     * tryAdvance}.  The extending class should override
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1481
     * {@link #forEachRemaining(java.util.function.LongConsumer)} forEach} if it
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1482
     * can provide a more performant implementation.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1483
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1484
     * @apiNote
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1485
     * This class is a useful aid for creating a spliterator when it is not
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1486
     * possible or difficult to efficiently partition elements in a manner
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1487
     * allowing balanced parallel computation.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1488
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1489
     * <p>An alternative to using this class, that also permits limited
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1490
     * parallelism, is to create a spliterator from an iterator
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1491
     * (see {@link #spliterator(java.util.PrimitiveIterator.OfLong, long, int)}.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1492
     * Depending on the circumstances using an iterator may be easier or more
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1493
     * convenient than extending this class. For example, if there is already an
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1494
     * iterator available to use then there is no need to extend this class.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1495
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1496
     * @see #spliterator(java.util.PrimitiveIterator.OfLong, long, int)
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1497
     * @since 1.8
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1498
     */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1499
    public static abstract class AbstractLongSpliterator implements Spliterator.OfLong {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1500
        static final int MAX_BATCH = AbstractSpliterator.MAX_BATCH;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1501
        static final int BATCH_UNIT = AbstractSpliterator.BATCH_UNIT;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1502
        private final int characteristics;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1503
        private long est;             // size estimate
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1504
        private int batch;            // batch size for splits
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1505
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1506
        /**
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1507
         * Creates a spliterator reporting the given estimated size and
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1508
         * characteristics.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1509
         *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1510
         * @param est the estimated size of this spliterator if known, otherwise
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1511
         *        {@code Long.MAX_VALUE}.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1512
         * @param additionalCharacteristics properties of this spliterator's
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1513
         *        source or elements.  If {@code SIZED} is reported then this
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1514
         *        spliterator will additionally report {@code SUBSIZED}.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1515
         */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1516
        protected AbstractLongSpliterator(long est, int additionalCharacteristics) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1517
            this.est = est;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1518
            this.characteristics = ((additionalCharacteristics & Spliterator.SIZED) != 0)
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1519
                                   ? additionalCharacteristics | Spliterator.SUBSIZED
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1520
                                   : additionalCharacteristics;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1521
        }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1522
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1523
        static final class HoldingLongConsumer implements LongConsumer {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1524
            long value;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1525
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1526
            @Override
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1527
            public void accept(long value) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1528
                this.value = value;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1529
            }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1530
        }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1531
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1532
        /**
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1533
         * {@inheritDoc}
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1534
         *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1535
         * This implementation permits limited parallelism.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1536
         */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1537
        @Override
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1538
        public Spliterator.OfLong trySplit() {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1539
            HoldingLongConsumer holder = new HoldingLongConsumer();
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1540
            long s = est;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1541
            if (s > 1 && tryAdvance(holder)) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1542
                int n = batch + BATCH_UNIT;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1543
                if (n > s)
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1544
                    n = (int) s;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1545
                if (n > MAX_BATCH)
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1546
                    n = MAX_BATCH;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1547
                long[] a;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1548
                try {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1549
                    a = new long[n];
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1550
                } catch (OutOfMemoryError oome) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1551
                    return null;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1552
                }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1553
                int j = 0;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1554
                do { a[j] = holder.value; } while (++j < n && tryAdvance(holder));
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1555
                batch = j;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1556
                if (est != Long.MAX_VALUE)
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1557
                    est -= j;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1558
                return new LongArraySpliterator(a, 0, j, characteristics());
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1559
            }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1560
            return null;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1561
        }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1562
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1563
        /**
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1564
         * {@inheritDoc}
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1565
         *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1566
         * @implSpec
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1567
         * This implementation returns the estimated size as reported when
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1568
         * created and, if the estimate size is known, decreases in size when
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1569
         * split.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1570
         */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1571
        @Override
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1572
        public long estimateSize() {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1573
            return est;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1574
        }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1575
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1576
        /**
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1577
         * {@inheritDoc}
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1578
         *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1579
         * @implSpec
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1580
         * This implementation returns the characteristics as reported when
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1581
         * created.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1582
         */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1583
        @Override
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1584
        public int characteristics() {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1585
            return characteristics;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1586
        }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1587
    }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1588
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1589
    /**
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1590
     * An abstract {@code Spliterator.OfDouble} that implements
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1591
     * {@code trySplit} to permit limited parallelism.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1592
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1593
     * <p>To implement a spliterator an extending class need only
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1594
     * implement {@link #tryAdvance(java.util.function.DoubleConsumer)}
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1595
     * tryAdvance}.  The extending class should override
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1596
     * {@link #forEachRemaining(java.util.function.DoubleConsumer)} forEach} if
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1597
     * it can provide a more performant implementation.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1598
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1599
     * @apiNote
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1600
     * This class is a useful aid for creating a spliterator when it is not
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1601
     * possible or difficult to efficiently partition elements in a manner
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1602
     * allowing balanced parallel computation.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1603
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1604
     * <p>An alternative to using this class, that also permits limited
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1605
     * parallelism, is to create a spliterator from an iterator
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1606
     * (see {@link #spliterator(java.util.PrimitiveIterator.OfDouble, long, int)}.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1607
     * Depending on the circumstances using an iterator may be easier or more
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1608
     * convenient than extending this class. For example, if there is already an
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1609
     * iterator available to use then there is no need to extend this class.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1610
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1611
     * @see #spliterator(java.util.PrimitiveIterator.OfDouble, long, int)
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1612
     * @since 1.8
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1613
     */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1614
    public static abstract class AbstractDoubleSpliterator implements Spliterator.OfDouble {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1615
        static final int MAX_BATCH = AbstractSpliterator.MAX_BATCH;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1616
        static final int BATCH_UNIT = AbstractSpliterator.BATCH_UNIT;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1617
        private final int characteristics;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1618
        private long est;             // size estimate
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1619
        private int batch;            // batch size for splits
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1620
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1621
        /**
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1622
         * Creates a spliterator reporting the given estimated size and
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1623
         * characteristics.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1624
         *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1625
         * @param est the estimated size of this spliterator if known, otherwise
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1626
         *        {@code Long.MAX_VALUE}.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1627
         * @param additionalCharacteristics properties of this spliterator's
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1628
         *        source or elements.  If {@code SIZED} is reported then this
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1629
         *        spliterator will additionally report {@code SUBSIZED}.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1630
         */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1631
        protected AbstractDoubleSpliterator(long est, int additionalCharacteristics) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1632
            this.est = est;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1633
            this.characteristics = ((additionalCharacteristics & Spliterator.SIZED) != 0)
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1634
                                   ? additionalCharacteristics | Spliterator.SUBSIZED
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1635
                                   : additionalCharacteristics;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1636
        }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1637
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1638
        static final class HoldingDoubleConsumer implements DoubleConsumer {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1639
            double value;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1640
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1641
            @Override
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1642
            public void accept(double value) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1643
                this.value = value;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1644
            }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1645
        }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1646
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1647
        /**
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1648
         * {@inheritDoc}
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1649
         *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1650
         * This implementation permits limited parallelism.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1651
         */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1652
        @Override
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1653
        public Spliterator.OfDouble trySplit() {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1654
            HoldingDoubleConsumer holder = new HoldingDoubleConsumer();
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1655
            long s = est;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1656
            if (s > 1 && tryAdvance(holder)) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1657
                int n = batch + BATCH_UNIT;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1658
                if (n > s)
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1659
                    n = (int) s;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1660
                if (n > MAX_BATCH)
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1661
                    n = MAX_BATCH;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1662
                double[] a;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1663
                try {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1664
                    a = new double[n];
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1665
                } catch (OutOfMemoryError oome) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1666
                    return null;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1667
                }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1668
                int j = 0;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1669
                do { a[j] = holder.value; } while (++j < n && tryAdvance(holder));
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1670
                batch = j;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1671
                if (est != Long.MAX_VALUE)
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1672
                    est -= j;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1673
                return new DoubleArraySpliterator(a, 0, j, characteristics());
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1674
            }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1675
            return null;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1676
        }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1677
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1678
        /**
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1679
         * {@inheritDoc}
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1680
         *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1681
         * @implSpec
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1682
         * This implementation returns the estimated size as reported when
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1683
         * created and, if the estimate size is known, decreases in size when
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1684
         * split.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1685
         */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1686
        @Override
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1687
        public long estimateSize() {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1688
            return est;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1689
        }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1690
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1691
        /**
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1692
         * {@inheritDoc}
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1693
         *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1694
         * @implSpec
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1695
         * This implementation returns the characteristics as reported when
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1696
         * created.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1697
         */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1698
        @Override
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1699
        public int characteristics() {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1700
            return characteristics;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1701
        }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1702
    }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1703
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1704
    // Iterator-based Spliterators
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1705
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1706
    /**
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1707
     * A Spliterator using a given Iterator for element
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1708
     * operations. The spliterator implements {@code trySplit} to
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1709
     * permit limited parallelism.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1710
     */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1711
    static class IteratorSpliterator<T> implements Spliterator<T> {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1712
        static final int BATCH_UNIT = 1 << 10;  // batch array size increment
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1713
        static final int MAX_BATCH = 1 << 25;  // max batch array size;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1714
        private final Collection<? extends T> collection; // null OK
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1715
        private Iterator<? extends T> it;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1716
        private final int characteristics;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1717
        private long est;             // size estimate
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1718
        private int batch;            // batch size for splits
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1719
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1720
        /**
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1721
         * Creates a spliterator using the given given
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1722
         * collection's {@link java.util.Collection#iterator()) for traversal,
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1723
         * and reporting its {@link java.util.Collection#size()) as its initial
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1724
         * size.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1725
         *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1726
         * @param c the collection
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1727
         * @param characteristics properties of this spliterator's
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1728
         *        source or elements.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1729
         */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1730
        public IteratorSpliterator(Collection<? extends T> collection, int characteristics) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1731
            this.collection = collection;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1732
            this.it = null;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1733
            this.characteristics = characteristics | Spliterator.SIZED | Spliterator.SUBSIZED;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1734
        }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1735
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1736
        /**
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1737
         * Creates a spliterator using the given iterator
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1738
         * for traversal, and reporting the given initial size
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1739
         * and characteristics.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1740
         *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1741
         * @param iterator the iterator for the source
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1742
         * @param size the number of elements in the source
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1743
         * @param characteristics properties of this spliterator's
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1744
         * source or elements.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1745
         */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1746
        public IteratorSpliterator(Iterator<? extends T> iterator, long size, int characteristics) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1747
            this.collection = null;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1748
            this.it = iterator;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1749
            this.est = size;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1750
            this.characteristics = characteristics | Spliterator.SIZED | Spliterator.SUBSIZED;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1751
        }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1752
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1753
        /**
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1754
         * Creates a spliterator using the given iterator
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1755
         * for traversal, and reporting the given initial size
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1756
         * and characteristics.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1757
         *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1758
         * @param iterator the iterator for the source
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1759
         * @param characteristics properties of this spliterator's
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1760
         * source or elements.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1761
         */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1762
        public IteratorSpliterator(Iterator<? extends T> iterator, int characteristics) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1763
            this.collection = null;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1764
            this.it = iterator;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1765
            this.est = Long.MAX_VALUE;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1766
            this.characteristics = characteristics & ~(Spliterator.SIZED | Spliterator.SUBSIZED);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1767
        }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1768
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1769
        @Override
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1770
        public Spliterator<T> trySplit() {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1771
            /*
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1772
             * Split into arrays of arithmetically increasing batch
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1773
             * sizes.  This will only improve parallel performance if
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1774
             * per-element Consumer actions are more costly than
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1775
             * transferring them into an array.  The use of an
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1776
             * arithmetic progression in split sizes provides overhead
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1777
             * vs parallelism bounds that do not particularly favor or
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1778
             * penalize cases of lightweight vs heavyweight element
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1779
             * operations, across combinations of #elements vs #cores,
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1780
             * whether or not either are known.  We generate
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1781
             * O(sqrt(#elements)) splits, allowing O(sqrt(#cores))
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1782
             * potential speedup.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1783
             */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1784
            Iterator<? extends T> i;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1785
            long s;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1786
            if ((i = it) == null) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1787
                i = it = collection.iterator();
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1788
                s = est = (long) collection.size();
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1789
            }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1790
            else
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1791
                s = est;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1792
            if (s > 1 && i.hasNext()) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1793
                int n = batch + BATCH_UNIT;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1794
                if (n > s)
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1795
                    n = (int) s;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1796
                if (n > MAX_BATCH)
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1797
                    n = MAX_BATCH;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1798
                Object[] a;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1799
                try {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1800
                    a = new Object[n];
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1801
                } catch (OutOfMemoryError oome) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1802
                    return null;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1803
                }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1804
                int j = 0;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1805
                do { a[j] = i.next(); } while (++j < n && i.hasNext());
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1806
                batch = j;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1807
                if (est != Long.MAX_VALUE)
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1808
                    est -= j;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1809
                return new ArraySpliterator<>(a, 0, j, characteristics);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1810
            }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1811
            return null;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1812
        }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1813
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1814
        @Override
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1815
        public void forEachRemaining(Consumer<? super T> action) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1816
            if (action == null) throw new NullPointerException();
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1817
            Iterator<? extends T> i;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1818
            if ((i = it) == null) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1819
                i = it = collection.iterator();
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1820
                est = (long)collection.size();
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1821
            }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1822
            i.forEachRemaining(action);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1823
        }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1824
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1825
        @Override
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1826
        public boolean tryAdvance(Consumer<? super T> action) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1827
            if (action == null) throw new NullPointerException();
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1828
            if (it == null) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1829
                it = collection.iterator();
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1830
                est = (long) collection.size();
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1831
            }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1832
            if (it.hasNext()) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1833
                action.accept(it.next());
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1834
                return true;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1835
            }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1836
            return false;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1837
        }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1838
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1839
        @Override
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1840
        public long estimateSize() {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1841
            if (it == null) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1842
                it = collection.iterator();
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1843
                return est = (long)collection.size();
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1844
            }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1845
            return est;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1846
        }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1847
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1848
        @Override
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1849
        public int characteristics() { return characteristics; }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1850
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1851
        @Override
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1852
        public Comparator<? super T> getComparator() {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1853
            if (hasCharacteristics(Spliterator.SORTED))
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1854
                return null;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1855
            throw new IllegalStateException();
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1856
        }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1857
    }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1858
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1859
    /**
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1860
     * A Spliterator.OfInt using a given IntStream.IntIterator for element
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1861
     * operations. The spliterator implements {@code trySplit} to
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1862
     * permit limited parallelism.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1863
     */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1864
    static final class IntIteratorSpliterator implements Spliterator.OfInt {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1865
        static final int BATCH_UNIT = IteratorSpliterator.BATCH_UNIT;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1866
        static final int MAX_BATCH = IteratorSpliterator.MAX_BATCH;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1867
        private PrimitiveIterator.OfInt it;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1868
        private final int characteristics;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1869
        private long est;             // size estimate
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1870
        private int batch;            // batch size for splits
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1871
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1872
        /**
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1873
         * Creates a spliterator using the given iterator
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1874
         * for traversal, and reporting the given initial size
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1875
         * and characteristics.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1876
         *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1877
         * @param iterator the iterator for the source
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1878
         * @param size the number of elements in the source
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1879
         * @param characteristics properties of this spliterator's
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1880
         * source or elements.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1881
         */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1882
        public IntIteratorSpliterator(PrimitiveIterator.OfInt iterator, long size, int characteristics) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1883
            this.it = iterator;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1884
            this.est = size;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1885
            this.characteristics = characteristics | Spliterator.SIZED | Spliterator.SUBSIZED;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1886
        }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1887
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1888
        /**
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1889
         * Creates a spliterator using the given iterator for a
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1890
         * source of unknown size, reporting the given
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1891
         * characteristics.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1892
         *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1893
         * @param iterator the iterator for the source
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1894
         * @param characteristics properties of this spliterator's
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1895
         * source or elements.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1896
         */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1897
        public IntIteratorSpliterator(PrimitiveIterator.OfInt iterator, int characteristics) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1898
            this.it = iterator;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1899
            this.est = Long.MAX_VALUE;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1900
            this.characteristics = characteristics & ~(Spliterator.SIZED | Spliterator.SUBSIZED);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1901
        }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1902
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1903
        @Override
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1904
        public OfInt trySplit() {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1905
            PrimitiveIterator.OfInt i = it;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1906
            long s = est;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1907
            if (s > 1 && i.hasNext()) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1908
                int n = batch + BATCH_UNIT;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1909
                if (n > s)
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1910
                    n = (int) s;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1911
                if (n > MAX_BATCH)
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1912
                    n = MAX_BATCH;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1913
                int[] a;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1914
                try {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1915
                    a = new int[n];
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1916
                } catch (OutOfMemoryError oome) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1917
                    return null;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1918
                }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1919
                int j = 0;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1920
                do { a[j] = i.nextInt(); } while (++j < n && i.hasNext());
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1921
                batch = j;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1922
                if (est != Long.MAX_VALUE)
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1923
                    est -= j;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1924
                return new IntArraySpliterator(a, 0, j, characteristics);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1925
            }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1926
            return null;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1927
        }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1928
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1929
        @Override
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1930
        public void forEachRemaining(IntConsumer action) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1931
            if (action == null) throw new NullPointerException();
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1932
            it.forEachRemaining(action);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1933
        }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1934
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1935
        @Override
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1936
        public boolean tryAdvance(IntConsumer action) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1937
            if (action == null) throw new NullPointerException();
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1938
            if (it.hasNext()) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1939
                action.accept(it.nextInt());
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1940
                return true;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1941
            }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1942
            return false;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1943
        }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1944
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1945
        @Override
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1946
        public long estimateSize() {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1947
            return est;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1948
        }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1949
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1950
        @Override
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1951
        public int characteristics() { return characteristics; }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1952
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1953
        @Override
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1954
        public Comparator<? super Integer> getComparator() {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1955
            if (hasCharacteristics(Spliterator.SORTED))
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1956
                return null;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1957
            throw new IllegalStateException();
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1958
        }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1959
    }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1960
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1961
    static final class LongIteratorSpliterator implements Spliterator.OfLong {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1962
        static final int BATCH_UNIT = IteratorSpliterator.BATCH_UNIT;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1963
        static final int MAX_BATCH = IteratorSpliterator.MAX_BATCH;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1964
        private PrimitiveIterator.OfLong it;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1965
        private final int characteristics;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1966
        private long est;             // size estimate
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1967
        private int batch;            // batch size for splits
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1968
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1969
        /**
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1970
         * Creates a spliterator using the given iterator
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1971
         * for traversal, and reporting the given initial size
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1972
         * and characteristics.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1973
         *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1974
         * @param iterator the iterator for the source
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1975
         * @param size the number of elements in the source
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1976
         * @param characteristics properties of this spliterator's
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1977
         * source or elements.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1978
         */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1979
        public LongIteratorSpliterator(PrimitiveIterator.OfLong iterator, long size, int characteristics) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1980
            this.it = iterator;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1981
            this.est = size;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1982
            this.characteristics = characteristics | Spliterator.SIZED | Spliterator.SUBSIZED;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1983
        }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1984
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1985
        /**
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1986
         * Creates a spliterator using the given iterator for a
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1987
         * source of unknown size, reporting the given
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1988
         * characteristics.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1989
         *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1990
         * @param iterator the iterator for the source
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1991
         * @param characteristics properties of this spliterator's
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1992
         * source or elements.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1993
         */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1994
        public LongIteratorSpliterator(PrimitiveIterator.OfLong iterator, int characteristics) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1995
            this.it = iterator;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1996
            this.est = Long.MAX_VALUE;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1997
            this.characteristics = characteristics & ~(Spliterator.SIZED | Spliterator.SUBSIZED);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1998
        }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  1999
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2000
        @Override
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2001
        public OfLong trySplit() {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2002
            PrimitiveIterator.OfLong i = it;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2003
            long s = est;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2004
            if (s > 1 && i.hasNext()) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2005
                int n = batch + BATCH_UNIT;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2006
                if (n > s)
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2007
                    n = (int) s;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2008
                if (n > MAX_BATCH)
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2009
                    n = MAX_BATCH;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2010
                long[] a;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2011
                try {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2012
                    a = new long[n];
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2013
                } catch (OutOfMemoryError oome) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2014
                    return null;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2015
                }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2016
                int j = 0;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2017
                do { a[j] = i.nextLong(); } while (++j < n && i.hasNext());
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2018
                batch = j;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2019
                if (est != Long.MAX_VALUE)
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2020
                    est -= j;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2021
                return new LongArraySpliterator(a, 0, j, characteristics);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2022
            }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2023
            return null;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2024
        }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2025
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2026
        @Override
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2027
        public void forEachRemaining(LongConsumer action) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2028
            if (action == null) throw new NullPointerException();
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2029
            it.forEachRemaining(action);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2030
        }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2031
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2032
        @Override
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2033
        public boolean tryAdvance(LongConsumer action) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2034
            if (action == null) throw new NullPointerException();
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2035
            if (it.hasNext()) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2036
                action.accept(it.nextLong());
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2037
                return true;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2038
            }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2039
            return false;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2040
        }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2041
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2042
        @Override
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2043
        public long estimateSize() {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2044
            return est;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2045
        }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2046
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2047
        @Override
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2048
        public int characteristics() { return characteristics; }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2049
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2050
        @Override
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2051
        public Comparator<? super Long> getComparator() {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2052
            if (hasCharacteristics(Spliterator.SORTED))
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2053
                return null;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2054
            throw new IllegalStateException();
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2055
        }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2056
    }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2057
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2058
    static final class DoubleIteratorSpliterator implements Spliterator.OfDouble {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2059
        static final int BATCH_UNIT = IteratorSpliterator.BATCH_UNIT;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2060
        static final int MAX_BATCH = IteratorSpliterator.MAX_BATCH;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2061
        private PrimitiveIterator.OfDouble it;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2062
        private final int characteristics;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2063
        private long est;             // size estimate
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2064
        private int batch;            // batch size for splits
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2065
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2066
        /**
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2067
         * Creates a spliterator using the given iterator
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2068
         * for traversal, and reporting the given initial size
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2069
         * and characteristics.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2070
         *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2071
         * @param iterator the iterator for the source
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2072
         * @param size the number of elements in the source
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2073
         * @param characteristics properties of this spliterator's
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2074
         * source or elements.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2075
         */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2076
        public DoubleIteratorSpliterator(PrimitiveIterator.OfDouble iterator, long size, int characteristics) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2077
            this.it = iterator;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2078
            this.est = size;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2079
            this.characteristics = characteristics | Spliterator.SIZED | Spliterator.SUBSIZED;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2080
        }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2081
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2082
        /**
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2083
         * Creates a spliterator using the given iterator for a
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2084
         * source of unknown size, reporting the given
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2085
         * characteristics.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2086
         *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2087
         * @param iterator the iterator for the source
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2088
         * @param characteristics properties of this spliterator's
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2089
         * source or elements.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2090
         */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2091
        public DoubleIteratorSpliterator(PrimitiveIterator.OfDouble iterator, int characteristics) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2092
            this.it = iterator;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2093
            this.est = Long.MAX_VALUE;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2094
            this.characteristics = characteristics & ~(Spliterator.SIZED | Spliterator.SUBSIZED);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2095
        }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2096
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2097
        @Override
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2098
        public OfDouble trySplit() {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2099
            PrimitiveIterator.OfDouble i = it;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2100
            long s = est;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2101
            if (s > 1 && i.hasNext()) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2102
                int n = batch + BATCH_UNIT;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2103
                if (n > s)
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2104
                    n = (int) s;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2105
                if (n > MAX_BATCH)
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2106
                    n = MAX_BATCH;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2107
                double[] a;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2108
                try {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2109
                    a = new double[n];
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2110
                } catch (OutOfMemoryError oome) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2111
                    return null;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2112
                }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2113
                int j = 0;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2114
                do { a[j] = i.nextDouble(); } while (++j < n && i.hasNext());
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2115
                batch = j;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2116
                if (est != Long.MAX_VALUE)
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2117
                    est -= j;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2118
                return new DoubleArraySpliterator(a, 0, j, characteristics);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2119
            }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2120
            return null;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2121
        }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2122
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2123
        @Override
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2124
        public void forEachRemaining(DoubleConsumer action) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2125
            if (action == null) throw new NullPointerException();
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2126
            it.forEachRemaining(action);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2127
        }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2128
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2129
        @Override
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2130
        public boolean tryAdvance(DoubleConsumer action) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2131
            if (action == null) throw new NullPointerException();
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2132
            if (it.hasNext()) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2133
                action.accept(it.nextDouble());
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2134
                return true;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2135
            }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2136
            return false;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2137
        }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2138
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2139
        @Override
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2140
        public long estimateSize() {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2141
            return est;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2142
        }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2143
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2144
        @Override
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2145
        public int characteristics() { return characteristics; }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2146
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2147
        @Override
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2148
        public Comparator<? super Double> getComparator() {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2149
            if (hasCharacteristics(Spliterator.SORTED))
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2150
                return null;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2151
            throw new IllegalStateException();
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2152
        }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2153
    }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
  2154
}