jdk/src/java.base/share/classes/java/util/PrimitiveIterator.java
author mchung
Fri, 22 May 2015 16:43:39 -0700
changeset 30789 9eca83469588
parent 25859 3317bb8137f4
permissions -rw-r--r--
8074431: Remove native2ascii tool Reviewed-by: erikj, alanb, okutsu, mfang, naoto
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
 *
18530
f28682666cf7 8016139: PrimitiveIterator.forEachRemaining
psandoz
parents: 17930
diff changeset
    58
 * @param <T> the type of elements returned by this PrimitiveIterator.  The
f28682666cf7 8016139: PrimitiveIterator.forEachRemaining
psandoz
parents: 17930
diff changeset
    59
 *        type must be a wrapper type for a primitive type, such as
f28682666cf7 8016139: PrimitiveIterator.forEachRemaining
psandoz
parents: 17930
diff changeset
    60
 *        {@code Integer} for the primitive {@code int} type.
f28682666cf7 8016139: PrimitiveIterator.forEachRemaining
psandoz
parents: 17930
diff changeset
    61
 * @param <T_CONS> the type of primitive consumer.  The type must be a
f28682666cf7 8016139: PrimitiveIterator.forEachRemaining
psandoz
parents: 17930
diff changeset
    62
 *        primitive specialization of {@link java.util.function.Consumer} for
f28682666cf7 8016139: PrimitiveIterator.forEachRemaining
psandoz
parents: 17930
diff changeset
    63
 *        {@code T}, such as {@link java.util.function.IntConsumer} for
f28682666cf7 8016139: PrimitiveIterator.forEachRemaining
psandoz
parents: 17930
diff changeset
    64
 *        {@code Integer}.
f28682666cf7 8016139: PrimitiveIterator.forEachRemaining
psandoz
parents: 17930
diff changeset
    65
 *
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    66
 * @since 1.8
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    67
 */
18530
f28682666cf7 8016139: PrimitiveIterator.forEachRemaining
psandoz
parents: 17930
diff changeset
    68
public interface PrimitiveIterator<T, T_CONS> extends Iterator<T> {
f28682666cf7 8016139: PrimitiveIterator.forEachRemaining
psandoz
parents: 17930
diff changeset
    69
f28682666cf7 8016139: PrimitiveIterator.forEachRemaining
psandoz
parents: 17930
diff changeset
    70
    /**
f28682666cf7 8016139: PrimitiveIterator.forEachRemaining
psandoz
parents: 17930
diff changeset
    71
     * Performs the given action for each remaining element, in the order
f28682666cf7 8016139: PrimitiveIterator.forEachRemaining
psandoz
parents: 17930
diff changeset
    72
     * elements occur when iterating, until all elements have been processed
f28682666cf7 8016139: PrimitiveIterator.forEachRemaining
psandoz
parents: 17930
diff changeset
    73
     * or the action throws an exception.  Errors or runtime exceptions
f28682666cf7 8016139: PrimitiveIterator.forEachRemaining
psandoz
parents: 17930
diff changeset
    74
     * thrown by the action are relayed to the caller.
f28682666cf7 8016139: PrimitiveIterator.forEachRemaining
psandoz
parents: 17930
diff changeset
    75
     *
f28682666cf7 8016139: PrimitiveIterator.forEachRemaining
psandoz
parents: 17930
diff changeset
    76
     * @param action The action to be performed for each element
f28682666cf7 8016139: PrimitiveIterator.forEachRemaining
psandoz
parents: 17930
diff changeset
    77
     * @throws NullPointerException if the specified action is null
f28682666cf7 8016139: PrimitiveIterator.forEachRemaining
psandoz
parents: 17930
diff changeset
    78
     */
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 18530
diff changeset
    79
    @SuppressWarnings("overloads")
18530
f28682666cf7 8016139: PrimitiveIterator.forEachRemaining
psandoz
parents: 17930
diff changeset
    80
    void forEachRemaining(T_CONS action);
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    81
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    82
    /**
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    83
     * An Iterator specialized for {@code int} values.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    84
     * @since 1.8
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    85
     */
18530
f28682666cf7 8016139: PrimitiveIterator.forEachRemaining
psandoz
parents: 17930
diff changeset
    86
    public static interface OfInt extends PrimitiveIterator<Integer, IntConsumer> {
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    87
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    88
        /**
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    89
         * Returns the next {@code int} element in the iteration.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    90
         *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    91
         * @return the next {@code int} element in the iteration
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    92
         * @throws NoSuchElementException if the iteration has no more elements
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    93
         */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    94
        int nextInt();
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    95
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
    96
        /**
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 18530
diff changeset
    97
         * Performs the given action for each remaining element until all elements
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 18530
diff changeset
    98
         * have been processed or the action throws an exception.  Actions are
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 18530
diff changeset
    99
         * performed in the order of iteration, if that order is specified.
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 18530
diff changeset
   100
         * Exceptions thrown by the action are relayed to the caller.
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   101
         *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   102
         * @implSpec
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   103
         * <p>The default implementation behaves as if:
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   104
         * <pre>{@code
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   105
         *     while (hasNext())
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   106
         *         action.accept(nextInt());
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   107
         * }</pre>
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   108
         *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   109
         * @param action The action to be performed for each element
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   110
         * @throws NullPointerException if the specified action is null
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   111
         */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   112
        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
   113
            Objects.requireNonNull(action);
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   114
            while (hasNext())
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   115
                action.accept(nextInt());
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   116
        }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   117
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   118
        /**
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   119
         * {@inheritDoc}
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   120
         * @implSpec
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   121
         * The default implementation boxes the result of calling
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   122
         * {@link #nextInt()}, and returns that boxed result.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   123
         */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   124
        @Override
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   125
        default Integer next() {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   126
            if (Tripwire.ENABLED)
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   127
                Tripwire.trip(getClass(), "{0} calling PrimitiveIterator.OfInt.nextInt()");
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   128
            return nextInt();
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   129
        }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   130
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   131
        /**
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   132
         * {@inheritDoc}
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   133
         * @implSpec
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   134
         * If the action is an instance of {@code IntConsumer} then it is cast
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   135
         * to {@code IntConsumer} and passed to {@link #forEachRemaining};
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   136
         * otherwise the action is adapted to an instance of
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   137
         * {@code IntConsumer}, by boxing the argument of {@code IntConsumer},
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   138
         * and then passed to {@link #forEachRemaining}.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   139
         */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   140
        @Override
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   141
        default void forEachRemaining(Consumer<? super Integer> action) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   142
            if (action instanceof IntConsumer) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   143
                forEachRemaining((IntConsumer) action);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   144
            }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   145
            else {
17930
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents: 16929
diff changeset
   146
                // 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
   147
                Objects.requireNonNull(action);
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   148
                if (Tripwire.ENABLED)
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   149
                    Tripwire.trip(getClass(), "{0} calling PrimitiveIterator.OfInt.forEachRemainingInt(action::accept)");
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   150
                forEachRemaining((IntConsumer) action::accept);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   151
            }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   152
        }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   153
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   154
    }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   155
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   156
    /**
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   157
     * An Iterator specialized for {@code long} values.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   158
     * @since 1.8
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   159
     */
18530
f28682666cf7 8016139: PrimitiveIterator.forEachRemaining
psandoz
parents: 17930
diff changeset
   160
    public static interface OfLong extends PrimitiveIterator<Long, LongConsumer> {
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   161
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   162
        /**
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   163
         * Returns the next {@code long} element in the iteration.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   164
         *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   165
         * @return the next {@code long} element in the iteration
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   166
         * @throws NoSuchElementException if the iteration has no more elements
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   167
         */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   168
        long nextLong();
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   169
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   170
        /**
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 18530
diff changeset
   171
         * Performs the given action for each remaining element until all elements
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 18530
diff changeset
   172
         * have been processed or the action throws an exception.  Actions are
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 18530
diff changeset
   173
         * performed in the order of iteration, if that order is specified.
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 18530
diff changeset
   174
         * Exceptions thrown by the action are relayed to the caller.
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   175
         *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   176
         * @implSpec
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   177
         * <p>The default implementation behaves as if:
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
         *     while (hasNext())
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   180
         *         action.accept(nextLong());
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   181
         * }</pre>
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   182
         *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   183
         * @param action The action to be performed for each element
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   184
         * @throws NullPointerException if the specified action is null
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   185
         */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   186
        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
   187
            Objects.requireNonNull(action);
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   188
            while (hasNext())
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   189
                action.accept(nextLong());
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   190
        }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   191
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   192
        /**
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   193
         * {@inheritDoc}
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   194
         * @implSpec
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   195
         * The default implementation boxes the result of calling
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   196
         * {@link #nextLong()}, and returns that boxed result.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   197
         */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   198
        @Override
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   199
        default Long next() {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   200
            if (Tripwire.ENABLED)
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   201
                Tripwire.trip(getClass(), "{0} calling PrimitiveIterator.OfLong.nextLong()");
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   202
            return nextLong();
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   203
        }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   204
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   205
        /**
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   206
         * {@inheritDoc}
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   207
         * @implSpec
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   208
         * If the action is an instance of {@code LongConsumer} then it is cast
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   209
         * to {@code LongConsumer} and passed to {@link #forEachRemaining};
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   210
         * otherwise the action is adapted to an instance of
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   211
         * {@code LongConsumer}, by boxing the argument of {@code LongConsumer},
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   212
         * and then passed to {@link #forEachRemaining}.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   213
         */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   214
        @Override
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   215
        default void forEachRemaining(Consumer<? super Long> action) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   216
            if (action instanceof LongConsumer) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   217
                forEachRemaining((LongConsumer) action);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   218
            }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   219
            else {
17930
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents: 16929
diff changeset
   220
                // 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
   221
                Objects.requireNonNull(action);
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   222
                if (Tripwire.ENABLED)
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   223
                    Tripwire.trip(getClass(), "{0} calling PrimitiveIterator.OfLong.forEachRemainingLong(action::accept)");
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   224
                forEachRemaining((LongConsumer) action::accept);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   225
            }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   226
        }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   227
    }
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
     * An Iterator specialized for {@code double} values.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   231
     * @since 1.8
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   232
     */
18530
f28682666cf7 8016139: PrimitiveIterator.forEachRemaining
psandoz
parents: 17930
diff changeset
   233
    public static interface OfDouble extends PrimitiveIterator<Double, DoubleConsumer> {
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   234
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   235
        /**
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   236
         * Returns the next {@code double} element in the iteration.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   237
         *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   238
         * @return the next {@code double} element in the iteration
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   239
         * @throws NoSuchElementException if the iteration has no more elements
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   240
         */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   241
        double nextDouble();
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   242
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   243
        /**
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 18530
diff changeset
   244
         * Performs the given action for each remaining element until all elements
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 18530
diff changeset
   245
         * have been processed or the action throws an exception.  Actions are
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 18530
diff changeset
   246
         * performed in the order of iteration, if that order is specified.
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 18530
diff changeset
   247
         * Exceptions thrown by the action are relayed to the caller.
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   248
         *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   249
         * @implSpec
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   250
         * <p>The default implementation behaves as if:
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
         *     while (hasNext())
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   253
         *         action.accept(nextDouble());
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   254
         * }</pre>
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   255
         *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   256
         * @param action The action to be performed for each element
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   257
         * @throws NullPointerException if the specified action is null
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   258
         */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   259
        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
   260
            Objects.requireNonNull(action);
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   261
            while (hasNext())
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   262
                action.accept(nextDouble());
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   263
        }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   264
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   265
        /**
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   266
         * {@inheritDoc}
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   267
         * @implSpec
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   268
         * The default implementation boxes the result of calling
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   269
         * {@link #nextDouble()}, and returns that boxed result.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   270
         */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   271
        @Override
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   272
        default Double next() {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   273
            if (Tripwire.ENABLED)
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   274
                Tripwire.trip(getClass(), "{0} calling PrimitiveIterator.OfDouble.nextLong()");
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   275
            return nextDouble();
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   276
        }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   277
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   278
        /**
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   279
         * {@inheritDoc}
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   280
         * @implSpec
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   281
         * If the action is an instance of {@code DoubleConsumer} then it is
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   282
         * cast to {@code DoubleConsumer} and passed to
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   283
         * {@link #forEachRemaining}; otherwise the action is adapted to
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   284
         * an instance of {@code DoubleConsumer}, by boxing the argument of
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   285
         * {@code DoubleConsumer}, and then passed to
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   286
         * {@link #forEachRemaining}.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   287
         */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   288
        @Override
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   289
        default void forEachRemaining(Consumer<? super Double> action) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   290
            if (action instanceof DoubleConsumer) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   291
                forEachRemaining((DoubleConsumer) action);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   292
            }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   293
            else {
17930
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents: 16929
diff changeset
   294
                // 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
   295
                Objects.requireNonNull(action);
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   296
                if (Tripwire.ENABLED)
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   297
                    Tripwire.trip(getClass(), "{0} calling PrimitiveIterator.OfDouble.forEachRemainingDouble(action::accept)");
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   298
                forEachRemaining((DoubleConsumer) action::accept);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   299
            }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   300
        }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   301
    }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents:
diff changeset
   302
}