jdk/src/share/classes/java/util/PrimitiveIterator.java
author alanb
Mon, 10 Jun 2013 12:58:32 +0100
changeset 18156 edb590d448c5
parent 17930 8e80bd9fda30
child 18530 f28682666cf7
permissions -rw-r--r--
8016217: More javadoc warnings Reviewed-by: lancea, chegar, 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
 * A base type for primitive specializations of {@code Iterator}.  Specialized
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    34
 * subtypes are provided for {@link OfInt int}, {@link OfLong long}, and
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    35
 * {@link OfDouble double} values.
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>The specialized subtype default implementations of {@link Iterator#next}
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    38
 * and {@link Iterator#forEachRemaining(java.util.function.Consumer)} box
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    39
 * primitive values to instances of their corresponding wrapper class.  Such
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    40
 * boxing may offset any advantages gained when using the primitive
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    41
 * specializations.  To avoid boxing, the corresponding primitive-based methods
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    42
 * should be used.  For example, {@link PrimitiveIterator.OfInt#nextInt()} and
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    43
 * {@link PrimitiveIterator.OfInt#forEachRemaining(java.util.function.IntConsumer)}
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    44
 * should be used in preference to {@link PrimitiveIterator.OfInt#next()} and
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    45
 * {@link PrimitiveIterator.OfInt#forEachRemaining(java.util.function.Consumer)}.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    46
 *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    47
 * <p>Iteration of primitive values using boxing-based methods
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    48
 * {@link Iterator#next next()} and
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    49
 * {@link Iterator#forEachRemaining(java.util.function.Consumer) forEachRemaining()},
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    50
 * does not affect the order in which the values, transformed to boxed values,
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    51
 * are encountered.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    52
 *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    53
 * @implNote
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    54
 * If the boolean system property {@code org.openjdk.java.util.stream.tripwire}
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    55
 * is set to {@code true} then diagnostic warnings are reported if boxing of
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    56
 * primitive values occur when operating on primitive subtype specializations.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    57
 *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    58
 * @param <T> the boxed type of the primitive type
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    59
 * @since 1.8
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    60
 */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    61
public interface PrimitiveIterator<T> extends Iterator<T> {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    62
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    63
    /**
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    64
     * An Iterator specialized for {@code int} values.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    65
     * @since 1.8
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    66
     */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    67
    public static interface OfInt extends PrimitiveIterator<Integer> {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    68
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    69
        /**
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    70
         * Returns the next {@code int} element in the iteration.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    71
         *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    72
         * @return the next {@code int} element in the iteration
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    73
         * @throws NoSuchElementException if the iteration has no more elements
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    74
         */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    75
        int nextInt();
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    76
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    77
        /**
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    78
         * Performs the given action for each remaining element, in the order
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    79
         * elements occur when iterating, until all elements have been processed
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    80
         * or the action throws an exception.  Errors or runtime exceptions
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    81
         * thrown by the action are relayed to the caller.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    82
         *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    83
         * @implSpec
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    84
         * <p>The default implementation behaves as if:
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    85
         * <pre>{@code
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    86
         *     while (hasNext())
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    87
         *         action.accept(nextInt());
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    88
         * }</pre>
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    89
         *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    90
         * @param action The action to be performed for each element
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    91
         * @throws NullPointerException if the specified action is null
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    92
         */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    93
        default void forEachRemaining(IntConsumer action) {
17930
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents: 16929
diff changeset
    94
            Objects.requireNonNull(action);
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    95
            while (hasNext())
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    96
                action.accept(nextInt());
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    97
        }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    98
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    99
        /**
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   100
         * {@inheritDoc}
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   101
         * @implSpec
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   102
         * The default implementation boxes the result of calling
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   103
         * {@link #nextInt()}, and returns that boxed result.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   104
         */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   105
        @Override
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   106
        default Integer next() {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   107
            if (Tripwire.ENABLED)
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   108
                Tripwire.trip(getClass(), "{0} calling PrimitiveIterator.OfInt.nextInt()");
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   109
            return nextInt();
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   110
        }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   111
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   112
        /**
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   113
         * {@inheritDoc}
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   114
         * @implSpec
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   115
         * If the action is an instance of {@code IntConsumer} then it is cast
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   116
         * to {@code IntConsumer} and passed to {@link #forEachRemaining};
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   117
         * otherwise the action is adapted to an instance of
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   118
         * {@code IntConsumer}, by boxing the argument of {@code IntConsumer},
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   119
         * and then passed to {@link #forEachRemaining}.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   120
         */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   121
        @Override
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   122
        default void forEachRemaining(Consumer<? super Integer> action) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   123
            if (action instanceof IntConsumer) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   124
                forEachRemaining((IntConsumer) action);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   125
            }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   126
            else {
17930
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents: 16929
diff changeset
   127
                // The method reference action::accept is never null
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents: 16929
diff changeset
   128
                Objects.requireNonNull(action);
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   129
                if (Tripwire.ENABLED)
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   130
                    Tripwire.trip(getClass(), "{0} calling PrimitiveIterator.OfInt.forEachRemainingInt(action::accept)");
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   131
                forEachRemaining((IntConsumer) action::accept);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   132
            }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   133
        }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   134
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   135
    }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   136
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   137
    /**
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   138
     * An Iterator specialized for {@code long} values.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   139
     * @since 1.8
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   140
     */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   141
    public static interface OfLong extends PrimitiveIterator<Long> {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   142
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   143
        /**
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   144
         * Returns the next {@code long} element in the iteration.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   145
         *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   146
         * @return the next {@code long} element in the iteration
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   147
         * @throws NoSuchElementException if the iteration has no more elements
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   148
         */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   149
        long nextLong();
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   150
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   151
        /**
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   152
         * Performs the given action for each remaining element, in the order
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   153
         * elements occur when iterating, until all elements have been processed
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   154
         * or the action throws an exception.  Errors or runtime exceptions
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   155
         * thrown by the action are relayed to the caller.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   156
         *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   157
         * @implSpec
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   158
         * <p>The default implementation behaves as if:
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   159
         * <pre>{@code
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   160
         *     while (hasNext())
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   161
         *         action.accept(nextLong());
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   162
         * }</pre>
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   163
         *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   164
         * @param action The action to be performed for each element
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   165
         * @throws NullPointerException if the specified action is null
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   166
         */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   167
        default void forEachRemaining(LongConsumer action) {
17930
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents: 16929
diff changeset
   168
            Objects.requireNonNull(action);
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   169
            while (hasNext())
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   170
                action.accept(nextLong());
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   171
        }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   172
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   173
        /**
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   174
         * {@inheritDoc}
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   175
         * @implSpec
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   176
         * The default implementation boxes the result of calling
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   177
         * {@link #nextLong()}, and returns that boxed result.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   178
         */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   179
        @Override
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   180
        default Long next() {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   181
            if (Tripwire.ENABLED)
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   182
                Tripwire.trip(getClass(), "{0} calling PrimitiveIterator.OfLong.nextLong()");
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   183
            return nextLong();
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   184
        }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   185
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   186
        /**
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   187
         * {@inheritDoc}
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   188
         * @implSpec
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   189
         * If the action is an instance of {@code LongConsumer} then it is cast
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   190
         * to {@code LongConsumer} and passed to {@link #forEachRemaining};
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   191
         * otherwise the action is adapted to an instance of
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   192
         * {@code LongConsumer}, by boxing the argument of {@code LongConsumer},
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   193
         * and then passed to {@link #forEachRemaining}.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   194
         */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   195
        @Override
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   196
        default void forEachRemaining(Consumer<? super Long> action) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   197
            if (action instanceof LongConsumer) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   198
                forEachRemaining((LongConsumer) action);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   199
            }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   200
            else {
17930
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents: 16929
diff changeset
   201
                // The method reference action::accept is never null
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents: 16929
diff changeset
   202
                Objects.requireNonNull(action);
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   203
                if (Tripwire.ENABLED)
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   204
                    Tripwire.trip(getClass(), "{0} calling PrimitiveIterator.OfLong.forEachRemainingLong(action::accept)");
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   205
                forEachRemaining((LongConsumer) action::accept);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   206
            }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   207
        }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   208
    }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   209
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   210
    /**
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   211
     * An Iterator specialized for {@code double} values.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   212
     * @since 1.8
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   213
     */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   214
    public static interface OfDouble extends PrimitiveIterator<Double> {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   215
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   216
        /**
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   217
         * Returns the next {@code double} element in the iteration.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   218
         *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   219
         * @return the next {@code double} element in the iteration
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   220
         * @throws NoSuchElementException if the iteration has no more elements
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   221
         */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   222
        double nextDouble();
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   223
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   224
        /**
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   225
         * Performs the given action for each remaining element, in the order
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   226
         * elements occur when iterating, until all elements have been processed
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   227
         * or the action throws an exception.  Errors or runtime exceptions
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   228
         * thrown by the action are relayed to the caller.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   229
         *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   230
         * @implSpec
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   231
         * <p>The default implementation behaves as if:
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   232
         * <pre>{@code
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   233
         *     while (hasNext())
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   234
         *         action.accept(nextDouble());
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   235
         * }</pre>
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   236
         *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   237
         * @param action The action to be performed for each element
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   238
         * @throws NullPointerException if the specified action is null
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   239
         */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   240
        default void forEachRemaining(DoubleConsumer action) {
17930
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents: 16929
diff changeset
   241
            Objects.requireNonNull(action);
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   242
            while (hasNext())
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   243
                action.accept(nextDouble());
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   244
        }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   245
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   246
        /**
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   247
         * {@inheritDoc}
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   248
         * @implSpec
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   249
         * The default implementation boxes the result of calling
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   250
         * {@link #nextDouble()}, and returns that boxed result.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   251
         */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   252
        @Override
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   253
        default Double next() {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   254
            if (Tripwire.ENABLED)
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   255
                Tripwire.trip(getClass(), "{0} calling PrimitiveIterator.OfDouble.nextLong()");
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   256
            return nextDouble();
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   257
        }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   258
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   259
        /**
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   260
         * {@inheritDoc}
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   261
         * @implSpec
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   262
         * If the action is an instance of {@code DoubleConsumer} then it is
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   263
         * cast to {@code DoubleConsumer} and passed to
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   264
         * {@link #forEachRemaining}; otherwise the action is adapted to
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   265
         * an instance of {@code DoubleConsumer}, by boxing the argument of
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   266
         * {@code DoubleConsumer}, and then passed to
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   267
         * {@link #forEachRemaining}.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   268
         */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   269
        @Override
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   270
        default void forEachRemaining(Consumer<? super Double> action) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   271
            if (action instanceof DoubleConsumer) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   272
                forEachRemaining((DoubleConsumer) action);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   273
            }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   274
            else {
17930
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents: 16929
diff changeset
   275
                // The method reference action::accept is never null
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents: 16929
diff changeset
   276
                Objects.requireNonNull(action);
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   277
                if (Tripwire.ENABLED)
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   278
                    Tripwire.trip(getClass(), "{0} calling PrimitiveIterator.OfDouble.forEachRemainingDouble(action::accept)");
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   279
                forEachRemaining((DoubleConsumer) action::accept);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   280
            }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   281
        }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   282
    }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   283
}