jdk/src/share/classes/java/util/Spliterator.java
author darcy
Wed, 10 Jul 2013 11:05:39 -0700
changeset 18796 486b43748d9b
parent 17920 eef25bc37ccd
child 19435 9d7530ff42cb
permissions -rw-r--r--
8020294: Fix doclint issues in java.util.Spliterator Reviewed-by: psandoz
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
 * An object for traversing and partitioning elements of a source.  The source
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    34
 * of elements covered by a Spliterator could be, for example, an array, a
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    35
 * {@link Collection}, an IO channel, or a generator function.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    36
 *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    37
 * <p>A Spliterator may traverse elements individually ({@link
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    38
 * #tryAdvance tryAdvance()}) or sequentially in bulk
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    39
 * ({@link #forEachRemaining forEachRemaining()}).
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    40
 *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    41
 * <p>A Spliterator may also partition off some of its elements (using
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    42
 * {@link #trySplit}) as another Spliterator, to be used in
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    43
 * possibly-parallel operations.  Operations using a Spliterator that
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    44
 * cannot split, or does so in a highly imbalanced or inefficient
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    45
 * manner, are unlikely to benefit from parallelism.  Traversal
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    46
 * and splitting exhaust elements; each Spliterator is useful for only a single
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    47
 * bulk computation.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    48
 *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    49
 * <p>A Spliterator also reports a set of {@link #characteristics()} of its
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    50
 * structure, source, and elements from among {@link #ORDERED},
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    51
 * {@link #DISTINCT}, {@link #SORTED}, {@link #SIZED}, {@link #NONNULL},
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    52
 * {@link #IMMUTABLE}, {@link #CONCURRENT}, and {@link #SUBSIZED}. These may
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    53
 * be employed by Spliterator clients to control, specialize or simplify
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    54
 * computation.  For example, a Spliterator for a {@link Collection} would
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    55
 * report {@code SIZED}, a Spliterator for a {@link Set} would report
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    56
 * {@code DISTINCT}, and a Spliterator for a {@link SortedSet} would also
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    57
 * report {@code SORTED}.  Characteristics are reported as a simple unioned bit
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    58
 * set.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    59
 *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    60
 * Some characteristics additionally constrain method behavior; for example if
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    61
 * {@code ORDERED}, traversal methods must conform to their documented ordering.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    62
 * New characteristics may be defined in the future, so implementors should not
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    63
 * assign meanings to unlisted values.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    64
 *
18796
486b43748d9b 8020294: Fix doclint issues in java.util.Spliterator
darcy
parents: 17920
diff changeset
    65
 * <p><a name="binding">A Spliterator that does not report {@code IMMUTABLE} or
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    66
 * {@code CONCURRENT} is expected to have a documented policy concerning:
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    67
 * when the spliterator <em>binds</em> to the element source; and detection of
18796
486b43748d9b 8020294: Fix doclint issues in java.util.Spliterator
darcy
parents: 17920
diff changeset
    68
 * structural interference of the element source detected after binding.</a>  A
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    69
 * <em>late-binding</em> Spliterator binds to the source of elements at the
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    70
 * point of first traversal, first split, or first query for estimated size,
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    71
 * rather than at the time the Spliterator is created.  A Spliterator that is
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    72
 * not <em>late-binding</em> binds to the source of elements at the point of
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    73
 * construction or first invocation of any method.  Modifications made to the
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    74
 * source prior to binding are reflected when the Spliterator is traversed.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    75
 * After binding a Spliterator should, on a best-effort basis, throw
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    76
 * {@link ConcurrentModificationException} if structural interference is
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    77
 * detected.  Spliterators that do this are called <em>fail-fast</em>.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    78
 *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    79
 * <p>Spliterators can provide an estimate of the number of remaining elements
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    80
 * via the {@link #estimateSize} method.  Ideally, as reflected in characteristic
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    81
 * {@link #SIZED}, this value corresponds exactly to the number of elements
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    82
 * that would be encountered in a successful traversal.  However, even when not
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    83
 * exactly known, an estimated value value may still be useful to operations
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    84
 * being performed on the source, such as helping to determine whether it is
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    85
 * preferable to split further or traverse the remaining elements sequentially.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    86
 *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    87
 * <p>Despite their obvious utility in parallel algorithms, spliterators are not
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    88
 * expected to be thread-safe; instead, implementations of parallel algorithms
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    89
 * using spliterators should ensure that the spliterator is only used by one
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    90
 * thread at a time.  This is generally easy to attain via <em>serial
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    91
 * thread-confinement</em>, which often is a natural consequence of typical
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    92
 * parallel algorithms that work by recursive decomposition.  A thread calling
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    93
 * {@link #trySplit()} may hand over the returned Spliterator to another thread,
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    94
 * which in turn may traverse or further split that Spliterator.  The behaviour
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    95
 * of splitting and traversal is undefined if two or more threads operate
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    96
 * concurrently on the same spliterator.  If the original thread hands a
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    97
 * spliterator off to another thread for processing, it is best if that handoff
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    98
 * occurs before any elements are consumed with {@link #tryAdvance(Consumer)
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    99
 * tryAdvance()}, as certain guarantees (such as the accuracy of
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   100
 * {@link #estimateSize()} for {@code SIZED} spliterators) are only valid before
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   101
 * traversal has begun.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   102
 *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   103
 * <p>Primitive subtype specializations of {@code Spliterator} are provided for
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   104
 * {@link OfInt int}, {@link OfLong long}, and {@link OfDouble double} values.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   105
 * The subtype default implementations of
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   106
 * {@link Spliterator#tryAdvance(java.util.function.Consumer)}
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   107
 * and {@link Spliterator#forEachRemaining(java.util.function.Consumer)} box
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   108
 * primitive values to instances of their corresponding wrapper class.  Such
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   109
 * boxing may undermine any performance advantages gained by using the primitive
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   110
 * specializations.  To avoid boxing, the corresponding primitive-based methods
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   111
 * should be used.  For example,
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   112
 * {@link Spliterator.OfInt#tryAdvance(java.util.function.IntConsumer)}
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   113
 * and {@link Spliterator.OfInt#forEachRemaining(java.util.function.IntConsumer)}
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   114
 * should be used in preference to
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   115
 * {@link Spliterator.OfInt#tryAdvance(java.util.function.Consumer)} and
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   116
 * {@link Spliterator.OfInt#forEachRemaining(java.util.function.Consumer)}.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   117
 * Traversal of primitive values using boxing-based methods
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   118
 * {@link #tryAdvance tryAdvance()} and
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   119
 * {@link #forEachRemaining(java.util.function.Consumer) forEachRemaining()}
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   120
 * does not affect the order in which the values, transformed to boxed values,
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   121
 * are encountered.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   122
 *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   123
 * @apiNote
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   124
 * <p>Spliterators, like {@code Iterators}s, are for traversing the elements of
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   125
 * a source.  The {@code Spliterator} API was designed to support efficient
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   126
 * parallel traversal in addition to sequential traversal, by supporting
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   127
 * decomposition as well as single-element iteration.  In addition, the
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   128
 * protocol for accessing elements via a Spliterator is designed to impose
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   129
 * smaller per-element overhead than {@code Iterator}, and to avoid the inherent
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   130
 * race involved in having separate methods for {@code hasNext()} and
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   131
 * {@code next()}.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   132
 *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   133
 * <p>For mutable sources, arbitrary and non-deterministic behavior may occur if
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   134
 * the source is structurally interfered with (elements added, replaced, or
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   135
 * removed) between the time that the Spliterator binds to its data source and
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   136
 * the end of traversal.  For example, such interference will produce arbitrary,
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   137
 * non-deterministic results when using the {@code java.util.stream} framework.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   138
 *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   139
 * <p>Structural interference of a source can be managed in the following ways
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   140
 * (in approximate order of decreasing desirability):
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   141
 * <ul>
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   142
 * <li>The source cannot be structurally interfered with.
17693
2d7cbdb1bb53 8013334: Spliterator behavior for LinkedList contradicts Spliterator.trySplit
psandoz
parents: 17190
diff changeset
   143
 * <br>For example, an instance of
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   144
 * {@link java.util.concurrent.CopyOnWriteArrayList} is an immutable source.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   145
 * A Spliterator created from the source reports a characteristic of
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   146
 * {@code IMMUTABLE}.</li>
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   147
 * <li>The source manages concurrent modifications.
17693
2d7cbdb1bb53 8013334: Spliterator behavior for LinkedList contradicts Spliterator.trySplit
psandoz
parents: 17190
diff changeset
   148
 * <br>For example, a key set of a {@link java.util.concurrent.ConcurrentHashMap}
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   149
 * is a concurrent source.  A Spliterator created from the source reports a
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   150
 * characteristic of {@code CONCURRENT}.</li>
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   151
 * <li>The mutable source provides a late-binding and fail-fast Spliterator.
17693
2d7cbdb1bb53 8013334: Spliterator behavior for LinkedList contradicts Spliterator.trySplit
psandoz
parents: 17190
diff changeset
   152
 * <br>Late binding narrows the window during which interference can affect
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   153
 * the calculation; fail-fast detects, on a best-effort basis, that structural
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   154
 * interference has occurred after traversal has commenced and throws
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   155
 * {@link ConcurrentModificationException}.  For example, {@link ArrayList},
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   156
 * and many other non-concurrent {@code Collection} classes in the JDK, provide
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   157
 * a late-binding, fail-fast spliterator.</li>
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   158
 * <li>The mutable source provides a non-late-binding but fail-fast Spliterator.
17693
2d7cbdb1bb53 8013334: Spliterator behavior for LinkedList contradicts Spliterator.trySplit
psandoz
parents: 17190
diff changeset
   159
 * <br>The source increases the likelihood of throwing
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   160
 * {@code ConcurrentModificationException} since the window of potential
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   161
 * interference is larger.</li>
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   162
 * <li>The mutable source provides a late-binding and non-fail-fast Spliterator.
17693
2d7cbdb1bb53 8013334: Spliterator behavior for LinkedList contradicts Spliterator.trySplit
psandoz
parents: 17190
diff changeset
   163
 * <br>The source risks arbitrary, non-deterministic behavior after traversal
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   164
 * has commenced since interference is not detected.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   165
 * </li>
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   166
 * <li>The mutable source provides a non-late-binding and non-fail-fast
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   167
 * Spliterator.
17693
2d7cbdb1bb53 8013334: Spliterator behavior for LinkedList contradicts Spliterator.trySplit
psandoz
parents: 17190
diff changeset
   168
 * <br>The source increases the risk of arbitrary, non-deterministic behavior
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   169
 * since non-detected interference may occur after construction.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   170
 * </li>
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   171
 * </ul>
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   172
 *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   173
 * <p><b>Example.</b> Here is a class (not a very useful one, except
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   174
 * for illustration) that maintains an array in which the actual data
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   175
 * are held in even locations, and unrelated tag data are held in odd
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   176
 * locations. Its Spliterator ignores the tags.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   177
 *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   178
 * <pre> {@code
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   179
 * class TaggedArray<T> {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   180
 *   private final Object[] elements; // immutable after construction
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   181
 *   TaggedArray(T[] data, Object[] tags) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   182
 *     int size = data.length;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   183
 *     if (tags.length != size) throw new IllegalArgumentException();
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   184
 *     this.elements = new Object[2 * size];
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   185
 *     for (int i = 0, j = 0; i < size; ++i) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   186
 *       elements[j++] = data[i];
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   187
 *       elements[j++] = tags[i];
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   188
 *     }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   189
 *   }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   190
 *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   191
 *   public Spliterator<T> spliterator() {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   192
 *     return new TaggedArraySpliterator<>(elements, 0, elements.length);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   193
 *   }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   194
 *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   195
 *   static class TaggedArraySpliterator<T> implements Spliterator<T> {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   196
 *     private final Object[] array;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   197
 *     private int origin; // current index, advanced on split or traversal
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   198
 *     private final int fence; // one past the greatest index
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   199
 *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   200
 *     TaggedArraySpliterator(Object[] array, int origin, int fence) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   201
 *       this.array = array; this.origin = origin; this.fence = fence;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   202
 *     }
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 void forEachRemaining(Consumer<? super T> action) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   205
 *       for (; origin < fence; origin += 2)
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   206
 *         action.accept((T) array[origin]);
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
 *     public boolean tryAdvance(Consumer<? super T> action) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   210
 *       if (origin < fence) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   211
 *         action.accept((T) array[origin]);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   212
 *         origin += 2;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   213
 *         return true;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   214
 *       }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   215
 *       else // cannot advance
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   216
 *         return false;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   217
 *     }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   218
 *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   219
 *     public Spliterator<T> trySplit() {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   220
 *       int lo = origin; // divide range in half
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   221
 *       int mid = ((lo + fence) >>> 1) & ~1; // force midpoint to be even
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   222
 *       if (lo < mid) { // split out left half
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   223
 *         origin = mid; // reset this Spliterator's origin
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   224
 *         return new TaggedArraySpliterator<>(array, lo, mid);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   225
 *       }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   226
 *       else       // too small to split
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   227
 *         return null;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   228
 *     }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   229
 *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   230
 *     public long estimateSize() {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   231
 *       return (long)((fence - origin) / 2);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   232
 *     }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   233
 *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   234
 *     public int characteristics() {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   235
 *       return ORDERED | SIZED | IMMUTABLE | SUBSIZED;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   236
 *     }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   237
 *   }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   238
 * }}</pre>
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   239
 *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   240
 * <p>As an example how a parallel computation framework, such as the
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   241
 * {@code java.util.stream} package, would use Spliterator in a parallel
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   242
 * computation, here is one way to implement an associated parallel forEach,
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   243
 * that illustrates the primary usage idiom of splitting off subtasks until
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   244
 * the estimated amount of work is small enough to perform
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   245
 * sequentially. Here we assume that the order of processing across
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   246
 * subtasks doesn't matter; different (forked) tasks may further split
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   247
 * and process elements concurrently in undetermined order.  This
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   248
 * example uses a {@link java.util.concurrent.CountedCompleter};
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   249
 * similar usages apply to other parallel task constructions.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   250
 *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   251
 * <pre>{@code
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   252
 * static <T> void parEach(TaggedArray<T> a, Consumer<T> action) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   253
 *   Spliterator<T> s = a.spliterator();
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   254
 *   long targetBatchSize = s.estimateSize() / (ForkJoinPool.getCommonPoolParallelism() * 8);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   255
 *   new ParEach(null, s, action, targetBatchSize).invoke();
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   256
 * }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   257
 *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   258
 * static class ParEach<T> extends CountedCompleter<Void> {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   259
 *   final Spliterator<T> spliterator;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   260
 *   final Consumer<T> action;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   261
 *   final long targetBatchSize;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   262
 *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   263
 *   ParEach(ParEach<T> parent, Spliterator<T> spliterator,
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   264
 *           Consumer<T> action, long targetBatchSize) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   265
 *     super(parent);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   266
 *     this.spliterator = spliterator; this.action = action;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   267
 *     this.targetBatchSize = targetBatchSize;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   268
 *   }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   269
 *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   270
 *   public void compute() {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   271
 *     Spliterator<T> sub;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   272
 *     while (spliterator.estimateSize() > targetBatchSize &&
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   273
 *            (sub = spliterator.trySplit()) != null) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   274
 *       addToPendingCount(1);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   275
 *       new ParEach<>(this, sub, action, targetBatchSize).fork();
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   276
 *     }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   277
 *     spliterator.forEachRemaining(action);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   278
 *     propagateCompletion();
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   279
 *   }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   280
 * }}</pre>
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   281
 *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   282
 * @implNote
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   283
 * If the boolean system property {@code org.openjdk.java.util.stream.tripwire}
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   284
 * is set to {@code true} then diagnostic warnings are reported if boxing of
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   285
 * primitive values occur when operating on primitive subtype specializations.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   286
 *
17693
2d7cbdb1bb53 8013334: Spliterator behavior for LinkedList contradicts Spliterator.trySplit
psandoz
parents: 17190
diff changeset
   287
 * @param <T> the type of elements returned by this Spliterator
2d7cbdb1bb53 8013334: Spliterator behavior for LinkedList contradicts Spliterator.trySplit
psandoz
parents: 17190
diff changeset
   288
 *
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   289
 * @see Collection
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   290
 * @since 1.8
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   291
 */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   292
public interface Spliterator<T> {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   293
    /**
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   294
     * If a remaining element exists, performs the given action on it,
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   295
     * returning {@code true}; else returns {@code false}.  If this
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   296
     * Spliterator is {@link #ORDERED} the action is performed on the
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   297
     * next element in encounter order.  Exceptions thrown by the
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   298
     * action are relayed to the caller.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   299
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   300
     * @param action The action
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   301
     * @return {@code false} if no remaining elements existed
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   302
     * upon entry to this method, else {@code true}.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   303
     * @throws NullPointerException if the specified action is null
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   304
     */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   305
    boolean tryAdvance(Consumer<? super T> action);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   306
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   307
    /**
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   308
     * Performs the given action for each remaining element, sequentially in
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   309
     * the current thread, until all elements have been processed or the action
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   310
     * throws an exception.  If this Spliterator is {@link #ORDERED}, actions
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   311
     * are performed in encounter order.  Exceptions thrown by the action
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   312
     * are relayed to the caller.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   313
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   314
     * @implSpec
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   315
     * The default implementation repeatedly invokes {@link #tryAdvance} until
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   316
     * it returns {@code false}.  It should be overridden whenever possible.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   317
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   318
     * @param action The action
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   319
     * @throws NullPointerException if the specified action is null
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   320
     */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   321
    default void forEachRemaining(Consumer<? super T> action) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   322
        do { } while (tryAdvance(action));
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   323
    }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   324
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   325
    /**
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   326
     * If this spliterator can be partitioned, returns a Spliterator
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   327
     * covering elements, that will, upon return from this method, not
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   328
     * be covered by this Spliterator.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   329
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   330
     * <p>If this Spliterator is {@link #ORDERED}, the returned Spliterator
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   331
     * must cover a strict prefix of the elements.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   332
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   333
     * <p>Unless this Spliterator covers an infinite number of elements,
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   334
     * repeated calls to {@code trySplit()} must eventually return {@code null}.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   335
     * Upon non-null return:
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   336
     * <ul>
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   337
     * <li>the value reported for {@code estimateSize()} before splitting,
17693
2d7cbdb1bb53 8013334: Spliterator behavior for LinkedList contradicts Spliterator.trySplit
psandoz
parents: 17190
diff changeset
   338
     * must, after splitting, be greater than or equal to {@code estimateSize()}
2d7cbdb1bb53 8013334: Spliterator behavior for LinkedList contradicts Spliterator.trySplit
psandoz
parents: 17190
diff changeset
   339
     * for this and the returned Spliterator; and</li>
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   340
     * <li>if this Spliterator is {@code SUBSIZED}, then {@code estimateSize()}
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   341
     * for this spliterator before splitting must be equal to the sum of
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   342
     * {@code estimateSize()} for this and the returned Spliterator after
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   343
     * splitting.</li>
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   344
     * </ul>
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   345
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   346
     * <p>This method may return {@code null} for any reason,
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   347
     * including emptiness, inability to split after traversal has
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   348
     * commenced, data structure constraints, and efficiency
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   349
     * considerations.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   350
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   351
     * @apiNote
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   352
     * An ideal {@code trySplit} method efficiently (without
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   353
     * traversal) divides its elements exactly in half, allowing
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   354
     * balanced parallel computation.  Many departures from this ideal
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   355
     * remain highly effective; for example, only approximately
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   356
     * splitting an approximately balanced tree, or for a tree in
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   357
     * which leaf nodes may contain either one or two elements,
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   358
     * failing to further split these nodes.  However, large
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   359
     * deviations in balance and/or overly inefficient {@code
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   360
     * trySplit} mechanics typically result in poor parallel
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   361
     * performance.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   362
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   363
     * @return a {@code Spliterator} covering some portion of the
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   364
     * elements, or {@code null} if this spliterator cannot be split
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   365
     */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   366
    Spliterator<T> trySplit();
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   367
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   368
    /**
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   369
     * Returns an estimate of the number of elements that would be
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   370
     * encountered by a {@link #forEachRemaining} traversal, or returns {@link
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   371
     * Long#MAX_VALUE} if infinite, unknown, or too expensive to compute.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   372
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   373
     * <p>If this Spliterator is {@link #SIZED} and has not yet been partially
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   374
     * traversed or split, or this Spliterator is {@link #SUBSIZED} and has
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   375
     * not yet been partially traversed, this estimate must be an accurate
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   376
     * count of elements that would be encountered by a complete traversal.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   377
     * Otherwise, this estimate may be arbitrarily inaccurate, but must decrease
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   378
     * as specified across invocations of {@link #trySplit}.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   379
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   380
     * @apiNote
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   381
     * Even an inexact estimate is often useful and inexpensive to compute.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   382
     * For example, a sub-spliterator of an approximately balanced binary tree
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   383
     * may return a value that estimates the number of elements to be half of
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   384
     * that of its parent; if the root Spliterator does not maintain an
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   385
     * accurate count, it could estimate size to be the power of two
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   386
     * corresponding to its maximum depth.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   387
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   388
     * @return the estimated size, or {@code Long.MAX_VALUE} if infinite,
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   389
     *         unknown, or too expensive to compute.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   390
     */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   391
    long estimateSize();
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   392
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   393
    /**
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   394
     * Convenience method that returns {@link #estimateSize()} if this
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   395
     * Spliterator is {@link #SIZED}, else {@code -1}.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   396
     * @implSpec
17920
eef25bc37ccd 8014732: Minor spec issue: java.util.Spliterator.getExactSizeIfKnown
psandoz
parents: 17694
diff changeset
   397
     * The default implementation returns the result of {@code estimateSize()}
eef25bc37ccd 8014732: Minor spec issue: java.util.Spliterator.getExactSizeIfKnown
psandoz
parents: 17694
diff changeset
   398
     * if the Spliterator reports a characteristic of {@code SIZED}, and
eef25bc37ccd 8014732: Minor spec issue: java.util.Spliterator.getExactSizeIfKnown
psandoz
parents: 17694
diff changeset
   399
     * {@code -1} otherwise.
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   400
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   401
     * @return the exact size, if known, else {@code -1}.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   402
     */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   403
    default long getExactSizeIfKnown() {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   404
        return (characteristics() & SIZED) == 0 ? -1L : estimateSize();
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   405
    }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   406
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   407
    /**
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   408
     * Returns a set of characteristics of this Spliterator and its
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   409
     * elements. The result is represented as ORed values from {@link
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   410
     * #ORDERED}, {@link #DISTINCT}, {@link #SORTED}, {@link #SIZED},
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   411
     * {@link #NONNULL}, {@link #IMMUTABLE}, {@link #CONCURRENT},
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   412
     * {@link #SUBSIZED}.  Repeated calls to {@code characteristics()} on
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   413
     * a given spliterator should always return the same result.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   414
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   415
     * <p>If a Spliterator reports an inconsistent set of
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   416
     * characteristics (either those returned from a single invocation
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   417
     * or across multiple invocations), no guarantees can be made
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   418
     * about any computation using this Spliterator.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   419
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   420
     * @return a representation of characteristics
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   421
     */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   422
    int characteristics();
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
     * Returns {@code true} if this Spliterator's {@link
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   426
     * #characteristics} contain all of the given characteristics.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   427
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   428
     * @implSpec
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   429
     * The default implementation returns true if the corresponding bits
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   430
     * of the given characteristics are set.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   431
     *
18796
486b43748d9b 8020294: Fix doclint issues in java.util.Spliterator
darcy
parents: 17920
diff changeset
   432
     * @param characteristics the characteristics to check for
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   433
     * @return {@code true} if all the specified characteristics are present,
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   434
     * else {@code false}
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   435
     */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   436
    default boolean hasCharacteristics(int characteristics) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   437
        return (characteristics() & characteristics) == characteristics;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   438
    }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   439
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   440
    /**
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   441
     * If this Spliterator's source is {@link #SORTED} by a {@link Comparator},
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   442
     * returns that {@code Comparator}. If the source is {@code SORTED} in
17190
7e650321026c 8013413: javadoc warnings
alanb
parents: 16929
diff changeset
   443
     * {@linkplain Comparable natural order}, returns {@code null}.  Otherwise,
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   444
     * if the source is not {@code SORTED}, throws {@link IllegalStateException}.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   445
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   446
     * @implSpec
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   447
     * The default implementation always throws {@link IllegalStateException}.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   448
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   449
     * @return a Comparator, or {@code null} if the elements are sorted in the
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   450
     * natural order.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   451
     * @throws IllegalStateException if the spliterator does not report
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   452
     *         a characteristic of {@code SORTED}.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   453
     */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   454
    default Comparator<? super T> getComparator() {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   455
        throw new IllegalStateException();
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   456
    }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   457
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   458
    /**
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   459
     * Characteristic value signifying that an encounter order is defined for
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   460
     * elements. If so, this Spliterator guarantees that method
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   461
     * {@link #trySplit} splits a strict prefix of elements, that method
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   462
     * {@link #tryAdvance} steps by one element in prefix order, and that
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   463
     * {@link #forEachRemaining} performs actions in encounter order.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   464
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   465
     * <p>A {@link Collection} has an encounter order if the corresponding
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   466
     * {@link Collection#iterator} documents an order. If so, the encounter
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   467
     * order is the same as the documented order. Otherwise, a collection does
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   468
     * not have an encounter order.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   469
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   470
     * @apiNote Encounter order is guaranteed to be ascending index order for
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   471
     * any {@link List}. But no order is guaranteed for hash-based collections
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   472
     * such as {@link HashSet}. Clients of a Spliterator that reports
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   473
     * {@code ORDERED} are expected to preserve ordering constraints in
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   474
     * non-commutative parallel computations.
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 final int ORDERED    = 0x00000010;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   477
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   478
    /**
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   479
     * Characteristic value signifying that, for each pair of
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   480
     * encountered elements {@code x, y}, {@code !x.equals(y)}. This
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   481
     * applies for example, to a Spliterator based on a {@link Set}.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   482
     */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   483
    public static final int DISTINCT   = 0x00000001;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   484
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   485
    /**
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   486
     * Characteristic value signifying that encounter order follows a defined
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   487
     * sort order. If so, method {@link #getComparator()} returns the associated
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   488
     * Comparator, or {@code null} if all elements are {@link Comparable} and
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   489
     * are sorted by their natural ordering.
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>A Spliterator that reports {@code SORTED} must also report
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   492
     * {@code ORDERED}.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   493
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   494
     * @apiNote The spliterators for {@code Collection} classes in the JDK that
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   495
     * implement {@link NavigableSet} or {@link SortedSet} report {@code SORTED}.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   496
     */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   497
    public static final int SORTED     = 0x00000004;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   498
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   499
    /**
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   500
     * Characteristic value signifying that the value returned from
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   501
     * {@code estimateSize()} prior to traversal or splitting represents a
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   502
     * finite size that, in the absence of structural source modification,
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   503
     * represents an exact count of the number of elements that would be
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   504
     * encountered by a complete traversal.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   505
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   506
     * @apiNote Most Spliterators for Collections, that cover all elements of a
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   507
     * {@code Collection} report this characteristic. Sub-spliterators, such as
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   508
     * those for {@link HashSet}, that cover a sub-set of elements and
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   509
     * approximate their reported size do not.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   510
     */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   511
    public static final int SIZED      = 0x00000040;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   512
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   513
    /**
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   514
     * Characteristic value signifying that the source guarantees that
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   515
     * encountered elements will not be {@code null}. (This applies,
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   516
     * for example, to most concurrent collections, queues, and maps.)
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   517
     */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   518
    public static final int NONNULL    = 0x00000100;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   519
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   520
    /**
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   521
     * Characteristic value signifying that the element source cannot be
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   522
     * structurally modified; that is, elements cannot be added, replaced, or
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   523
     * removed, so such changes cannot occur during traversal. A Spliterator
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   524
     * that does not report {@code IMMUTABLE} or {@code CONCURRENT} is expected
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   525
     * to have a documented policy (for example throwing
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   526
     * {@link ConcurrentModificationException}) concerning structural
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   527
     * interference detected during traversal.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   528
     */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   529
    public static final int IMMUTABLE  = 0x00000400;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   530
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   531
    /**
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   532
     * Characteristic value signifying that the element source may be safely
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   533
     * concurrently modified (allowing additions, replacements, and/or removals)
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   534
     * by multiple threads without external synchronization. If so, the
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   535
     * Spliterator is expected to have a documented policy concerning the impact
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   536
     * of modifications during traversal.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   537
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   538
     * <p>A top-level Spliterator should not report {@code CONCURRENT} and
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   539
     * {@code SIZED}, since the finite size, if known, may change if the source
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   540
     * is concurrently modified during traversal. Such a Spliterator is
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   541
     * inconsistent and no guarantees can be made about any computation using
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   542
     * that Spliterator. Sub-spliterators may report {@code SIZED} if the
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   543
     * sub-split size is known and additions or removals to the source are not
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   544
     * reflected when traversing.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   545
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   546
     * @apiNote Most concurrent collections maintain a consistency policy
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   547
     * guaranteeing accuracy with respect to elements present at the point of
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   548
     * Spliterator construction, but possibly not reflecting subsequent
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   549
     * additions or removals.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   550
     */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   551
    public static final int CONCURRENT = 0x00001000;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   552
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   553
    /**
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   554
     * Characteristic value signifying that all Spliterators resulting from
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   555
     * {@code trySplit()} will be both {@link #SIZED} and {@link #SUBSIZED}.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   556
     * (This means that all child Spliterators, whether direct or indirect, will
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   557
     * be {@code SIZED}.)
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   558
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   559
     * <p>A Spliterator that does not report {@code SIZED} as required by
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   560
     * {@code SUBSIZED} is inconsistent and no guarantees can be made about any
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   561
     * computation using that Spliterator.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   562
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   563
     * @apiNote Some spliterators, such as the top-level spliterator for an
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   564
     * approximately balanced binary tree, will report {@code SIZED} but not
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   565
     * {@code SUBSIZED}, since it is common to know the size of the entire tree
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   566
     * but not the exact sizes of subtrees.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   567
     */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   568
    public static final int SUBSIZED = 0x00004000;
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   569
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   570
    /**
17694
31d23e077ab3 8014133: Spliterator.OfPrimitive
psandoz
parents: 17693
diff changeset
   571
     * A Spliterator specialized for primitive values.
31d23e077ab3 8014133: Spliterator.OfPrimitive
psandoz
parents: 17693
diff changeset
   572
     *
31d23e077ab3 8014133: Spliterator.OfPrimitive
psandoz
parents: 17693
diff changeset
   573
     * @param <T> the type of elements returned by this Spliterator.  The
31d23e077ab3 8014133: Spliterator.OfPrimitive
psandoz
parents: 17693
diff changeset
   574
     * type must be a wrapper type for a primitive type, such as {@code Integer}
31d23e077ab3 8014133: Spliterator.OfPrimitive
psandoz
parents: 17693
diff changeset
   575
     * for the primitive {@code int} type.
31d23e077ab3 8014133: Spliterator.OfPrimitive
psandoz
parents: 17693
diff changeset
   576
     * @param <T_CONS> the type of primitive consumer.  The type must be a
31d23e077ab3 8014133: Spliterator.OfPrimitive
psandoz
parents: 17693
diff changeset
   577
     * primitive specialization of {@link java.util.function.Consumer} for
31d23e077ab3 8014133: Spliterator.OfPrimitive
psandoz
parents: 17693
diff changeset
   578
     * {@code T}, such as {@link java.util.function.IntConsumer} for
31d23e077ab3 8014133: Spliterator.OfPrimitive
psandoz
parents: 17693
diff changeset
   579
     * {@code Integer}.
31d23e077ab3 8014133: Spliterator.OfPrimitive
psandoz
parents: 17693
diff changeset
   580
     * @param <T_SPLITR> the type of primitive Spliterator.  The type must be
31d23e077ab3 8014133: Spliterator.OfPrimitive
psandoz
parents: 17693
diff changeset
   581
     * a primitive specialization of Spliterator for {@code T}, such as
31d23e077ab3 8014133: Spliterator.OfPrimitive
psandoz
parents: 17693
diff changeset
   582
     * {@link Spliterator.OfInt} for {@code Integer}.
31d23e077ab3 8014133: Spliterator.OfPrimitive
psandoz
parents: 17693
diff changeset
   583
     *
31d23e077ab3 8014133: Spliterator.OfPrimitive
psandoz
parents: 17693
diff changeset
   584
     * @see Spliterator.OfInt
31d23e077ab3 8014133: Spliterator.OfPrimitive
psandoz
parents: 17693
diff changeset
   585
     * @see Spliterator.OfLong
31d23e077ab3 8014133: Spliterator.OfPrimitive
psandoz
parents: 17693
diff changeset
   586
     * @see Spliterator.OfDouble
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   587
     * @since 1.8
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   588
     */
17694
31d23e077ab3 8014133: Spliterator.OfPrimitive
psandoz
parents: 17693
diff changeset
   589
    public interface OfPrimitive<T, T_CONS, T_SPLITR extends Spliterator.OfPrimitive<T, T_CONS, T_SPLITR>>
31d23e077ab3 8014133: Spliterator.OfPrimitive
psandoz
parents: 17693
diff changeset
   590
            extends Spliterator<T> {
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   591
        @Override
17694
31d23e077ab3 8014133: Spliterator.OfPrimitive
psandoz
parents: 17693
diff changeset
   592
        T_SPLITR trySplit();
16929
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
         * If a remaining element exists, performs the given action on it,
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   596
         * returning {@code true}; else returns {@code false}.  If this
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   597
         * Spliterator is {@link #ORDERED} the action is performed on the
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   598
         * next element in encounter order.  Exceptions thrown by the
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   599
         * action are relayed to the caller.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   600
         *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   601
         * @param action The action
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   602
         * @return {@code false} if no remaining elements existed
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   603
         * upon entry to this method, else {@code true}.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   604
         * @throws NullPointerException if the specified action is null
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   605
         */
17694
31d23e077ab3 8014133: Spliterator.OfPrimitive
psandoz
parents: 17693
diff changeset
   606
        boolean tryAdvance(T_CONS action);
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   607
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   608
        /**
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   609
         * Performs the given action for each remaining element, sequentially in
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   610
         * the current thread, until all elements have been processed or the
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   611
         * action throws an exception.  If this Spliterator is {@link #ORDERED},
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   612
         * actions are performed in encounter order.  Exceptions thrown by the
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   613
         * action are relayed to the caller.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   614
         *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   615
         * @implSpec
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   616
         * The default implementation repeatedly invokes {@link #tryAdvance}
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   617
         * until it returns {@code false}.  It should be overridden whenever
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   618
         * possible.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   619
         *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   620
         * @param action The action
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   621
         * @throws NullPointerException if the specified action is null
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   622
         */
17694
31d23e077ab3 8014133: Spliterator.OfPrimitive
psandoz
parents: 17693
diff changeset
   623
        default void forEachRemaining(T_CONS action) {
31d23e077ab3 8014133: Spliterator.OfPrimitive
psandoz
parents: 17693
diff changeset
   624
            do { } while (tryAdvance(action));
31d23e077ab3 8014133: Spliterator.OfPrimitive
psandoz
parents: 17693
diff changeset
   625
        }
31d23e077ab3 8014133: Spliterator.OfPrimitive
psandoz
parents: 17693
diff changeset
   626
    }
31d23e077ab3 8014133: Spliterator.OfPrimitive
psandoz
parents: 17693
diff changeset
   627
31d23e077ab3 8014133: Spliterator.OfPrimitive
psandoz
parents: 17693
diff changeset
   628
    /**
31d23e077ab3 8014133: Spliterator.OfPrimitive
psandoz
parents: 17693
diff changeset
   629
     * A Spliterator specialized for {@code int} values.
31d23e077ab3 8014133: Spliterator.OfPrimitive
psandoz
parents: 17693
diff changeset
   630
     * @since 1.8
31d23e077ab3 8014133: Spliterator.OfPrimitive
psandoz
parents: 17693
diff changeset
   631
     */
31d23e077ab3 8014133: Spliterator.OfPrimitive
psandoz
parents: 17693
diff changeset
   632
    public interface OfInt extends OfPrimitive<Integer, IntConsumer, OfInt> {
31d23e077ab3 8014133: Spliterator.OfPrimitive
psandoz
parents: 17693
diff changeset
   633
31d23e077ab3 8014133: Spliterator.OfPrimitive
psandoz
parents: 17693
diff changeset
   634
        @Override
31d23e077ab3 8014133: Spliterator.OfPrimitive
psandoz
parents: 17693
diff changeset
   635
        OfInt trySplit();
31d23e077ab3 8014133: Spliterator.OfPrimitive
psandoz
parents: 17693
diff changeset
   636
31d23e077ab3 8014133: Spliterator.OfPrimitive
psandoz
parents: 17693
diff changeset
   637
        @Override
31d23e077ab3 8014133: Spliterator.OfPrimitive
psandoz
parents: 17693
diff changeset
   638
        boolean tryAdvance(IntConsumer action);
31d23e077ab3 8014133: Spliterator.OfPrimitive
psandoz
parents: 17693
diff changeset
   639
31d23e077ab3 8014133: Spliterator.OfPrimitive
psandoz
parents: 17693
diff changeset
   640
        @Override
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   641
        default void forEachRemaining(IntConsumer action) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   642
            do { } while (tryAdvance(action));
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   643
        }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   644
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   645
        /**
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   646
         * {@inheritDoc}
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   647
         * @implSpec
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   648
         * If the action is an instance of {@code IntConsumer} then it is cast
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   649
         * to {@code IntConsumer} and passed to
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   650
         * {@link #tryAdvance(java.util.function.IntConsumer)}; otherwise
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   651
         * the action is adapted to an instance of {@code IntConsumer}, by
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   652
         * boxing the argument of {@code IntConsumer}, and then passed to
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   653
         * {@link #tryAdvance(java.util.function.IntConsumer)}.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   654
         */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   655
        @Override
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   656
        default boolean tryAdvance(Consumer<? super Integer> action) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   657
            if (action instanceof IntConsumer) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   658
                return tryAdvance((IntConsumer) action);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   659
            }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   660
            else {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   661
                if (Tripwire.ENABLED)
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   662
                    Tripwire.trip(getClass(),
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   663
                                  "{0} calling Spliterator.OfInt.tryAdvance((IntConsumer) action::accept)");
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   664
                return tryAdvance((IntConsumer) action::accept);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   665
            }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   666
        }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   667
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   668
        /**
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   669
         * {@inheritDoc}
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   670
         * @implSpec
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   671
         * If the action is an instance of {@code IntConsumer} then it is cast
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   672
         * to {@code IntConsumer} and passed to
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   673
         * {@link #forEachRemaining(java.util.function.IntConsumer)}; otherwise
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   674
         * the action is adapted to an instance of {@code IntConsumer}, by
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   675
         * boxing the argument of {@code IntConsumer}, and then passed to
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   676
         * {@link #forEachRemaining(java.util.function.IntConsumer)}.
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
        default void forEachRemaining(Consumer<? super Integer> action) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   680
            if (action instanceof IntConsumer) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   681
                forEachRemaining((IntConsumer) action);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   682
            }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   683
            else {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   684
                if (Tripwire.ENABLED)
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   685
                    Tripwire.trip(getClass(),
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   686
                                  "{0} calling Spliterator.OfInt.forEachRemaining((IntConsumer) action::accept)");
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   687
                forEachRemaining((IntConsumer) action::accept);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   688
            }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   689
        }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   690
    }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   691
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   692
    /**
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   693
     * A Spliterator specialized for {@code long} values.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   694
     * @since 1.8
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   695
     */
17694
31d23e077ab3 8014133: Spliterator.OfPrimitive
psandoz
parents: 17693
diff changeset
   696
    public interface OfLong extends OfPrimitive<Long, LongConsumer, OfLong> {
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   697
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   698
        @Override
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   699
        OfLong trySplit();
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   700
17694
31d23e077ab3 8014133: Spliterator.OfPrimitive
psandoz
parents: 17693
diff changeset
   701
        @Override
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   702
        boolean tryAdvance(LongConsumer action);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   703
17694
31d23e077ab3 8014133: Spliterator.OfPrimitive
psandoz
parents: 17693
diff changeset
   704
        @Override
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   705
        default void forEachRemaining(LongConsumer action) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   706
            do { } while (tryAdvance(action));
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   707
        }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   708
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   709
        /**
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   710
         * {@inheritDoc}
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   711
         * @implSpec
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   712
         * If the action is an instance of {@code LongConsumer} then it is cast
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   713
         * to {@code LongConsumer} and passed to
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   714
         * {@link #tryAdvance(java.util.function.LongConsumer)}; otherwise
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   715
         * the action is adapted to an instance of {@code LongConsumer}, by
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   716
         * boxing the argument of {@code LongConsumer}, and then passed to
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   717
         * {@link #tryAdvance(java.util.function.LongConsumer)}.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   718
         */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   719
        @Override
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   720
        default boolean tryAdvance(Consumer<? super Long> action) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   721
            if (action instanceof LongConsumer) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   722
                return tryAdvance((LongConsumer) action);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   723
            }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   724
            else {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   725
                if (Tripwire.ENABLED)
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   726
                    Tripwire.trip(getClass(),
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   727
                                  "{0} calling Spliterator.OfLong.tryAdvance((LongConsumer) action::accept)");
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   728
                return tryAdvance((LongConsumer) action::accept);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   729
            }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   730
        }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   731
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   732
        /**
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   733
         * {@inheritDoc}
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   734
         * @implSpec
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   735
         * If the action is an instance of {@code LongConsumer} then it is cast
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   736
         * to {@code LongConsumer} and passed to
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   737
         * {@link #forEachRemaining(java.util.function.LongConsumer)}; otherwise
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   738
         * the action is adapted to an instance of {@code LongConsumer}, by
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   739
         * boxing the argument of {@code LongConsumer}, and then passed to
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   740
         * {@link #forEachRemaining(java.util.function.LongConsumer)}.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   741
         */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   742
        @Override
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   743
        default void forEachRemaining(Consumer<? super Long> action) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   744
            if (action instanceof LongConsumer) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   745
                forEachRemaining((LongConsumer) action);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   746
            }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   747
            else {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   748
                if (Tripwire.ENABLED)
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   749
                    Tripwire.trip(getClass(),
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   750
                                  "{0} calling Spliterator.OfLong.forEachRemaining((LongConsumer) action::accept)");
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   751
                forEachRemaining((LongConsumer) action::accept);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   752
            }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   753
        }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   754
    }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   755
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   756
    /**
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   757
     * A Spliterator specialized for {@code double} values.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   758
     * @since 1.8
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   759
     */
17694
31d23e077ab3 8014133: Spliterator.OfPrimitive
psandoz
parents: 17693
diff changeset
   760
    public interface OfDouble extends OfPrimitive<Double, DoubleConsumer, OfDouble> {
16929
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
        OfDouble trySplit();
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   764
17694
31d23e077ab3 8014133: Spliterator.OfPrimitive
psandoz
parents: 17693
diff changeset
   765
        @Override
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   766
        boolean tryAdvance(DoubleConsumer action);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   767
17694
31d23e077ab3 8014133: Spliterator.OfPrimitive
psandoz
parents: 17693
diff changeset
   768
        @Override
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   769
        default void forEachRemaining(DoubleConsumer action) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   770
            do { } while (tryAdvance(action));
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   771
        }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   772
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   773
        /**
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   774
         * {@inheritDoc}
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   775
         * @implSpec
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   776
         * If the action is an instance of {@code DoubleConsumer} then it is
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   777
         * cast to {@code DoubleConsumer} and passed to
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   778
         * {@link #tryAdvance(java.util.function.DoubleConsumer)}; otherwise
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   779
         * the action is adapted to an instance of {@code DoubleConsumer}, by
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   780
         * boxing the argument of {@code DoubleConsumer}, and then passed to
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   781
         * {@link #tryAdvance(java.util.function.DoubleConsumer)}.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   782
         */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   783
        @Override
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   784
        default boolean tryAdvance(Consumer<? super Double> action) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   785
            if (action instanceof DoubleConsumer) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   786
                return tryAdvance((DoubleConsumer) action);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   787
            }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   788
            else {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   789
                if (Tripwire.ENABLED)
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   790
                    Tripwire.trip(getClass(),
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   791
                                  "{0} calling Spliterator.OfDouble.tryAdvance((DoubleConsumer) action::accept)");
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   792
                return tryAdvance((DoubleConsumer) action::accept);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   793
            }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   794
        }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   795
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   796
        /**
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   797
         * {@inheritDoc}
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   798
         * @implSpec
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   799
         * If the action is an instance of {@code DoubleConsumer} then it is
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   800
         * cast to {@code DoubleConsumer} and passed to
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   801
         * {@link #forEachRemaining(java.util.function.DoubleConsumer)};
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   802
         * otherwise the action is adapted to an instance of
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   803
         * {@code DoubleConsumer}, by boxing the argument of
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   804
         * {@code DoubleConsumer}, and then passed to
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   805
         * {@link #forEachRemaining(java.util.function.DoubleConsumer)}.
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
        default void forEachRemaining(Consumer<? super Double> action) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   809
            if (action instanceof DoubleConsumer) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   810
                forEachRemaining((DoubleConsumer) action);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   811
            }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   812
            else {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   813
                if (Tripwire.ENABLED)
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   814
                    Tripwire.trip(getClass(),
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   815
                                  "{0} calling Spliterator.OfDouble.forEachRemaining((DoubleConsumer) action::accept)");
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   816
                forEachRemaining((DoubleConsumer) action::accept);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   817
            }
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
}