src/java.base/share/classes/java/lang/Iterable.java
author darcy
Wed, 15 Aug 2018 10:44:56 -0700
changeset 51413 43e41800d579
parent 47216 71c04702a3d5
permissions -rw-r--r--
5075463: (enum) Serialized Form javadoc for java.lang.Enum is misleading Reviewed-by: lancea, rriggs, smarks
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
15647
314007859004 8005623: Retrofit FunctionalInterface annotations to core platform interfaces
darcy
parents: 7668
diff changeset
     2
 * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4977
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4977
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4977
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4977
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4977
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package java.lang;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.util.Iterator;
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 15647
diff changeset
    28
import java.util.Objects;
18824
9fa4af2af63d 8015320: Pull spliterator() up from Collection to Iterable
henryjen
parents: 16929
diff changeset
    29
import java.util.Spliterator;
9fa4af2af63d 8015320: Pull spliterator() up from Collection to Iterable
henryjen
parents: 16929
diff changeset
    30
import java.util.Spliterators;
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 15647
diff changeset
    31
import java.util.function.Consumer;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
4977
81902a400bbf 6929382: Various core classes in util and elsewhere are missing @param <T> tags
darcy
parents: 2
diff changeset
    33
/**
45141
99a3dd59f831 8180137: fix broken link in java.lang.Iterable
smarks
parents: 42335
diff changeset
    34
 * Implementing this interface allows an object to be the target of the enhanced
99a3dd59f831 8180137: fix broken link in java.lang.Iterable
smarks
parents: 42335
diff changeset
    35
 * {@code for} statement (sometimes called the "for-each loop" statement).
4977
81902a400bbf 6929382: Various core classes in util and elsewhere are missing @param <T> tags
darcy
parents: 2
diff changeset
    36
 *
81902a400bbf 6929382: Various core classes in util and elsewhere are missing @param <T> tags
darcy
parents: 2
diff changeset
    37
 * @param <T> the type of elements returned by the iterator
81902a400bbf 6929382: Various core classes in util and elsewhere are missing @param <T> tags
darcy
parents: 2
diff changeset
    38
 *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * @since 1.5
45141
99a3dd59f831 8180137: fix broken link in java.lang.Iterable
smarks
parents: 42335
diff changeset
    40
 * @jls 14.14.2 The enhanced {@code for} statement
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
public interface Iterable<T> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    /**
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 15647
diff changeset
    44
     * Returns an iterator over elements of type {@code T}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
     * @return an Iterator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    Iterator<T> iterator();
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 15647
diff changeset
    49
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 15647
diff changeset
    50
    /**
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 21334
diff changeset
    51
     * Performs the given action for each element of the {@code Iterable}
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 21334
diff changeset
    52
     * until all elements have been processed or the action throws an
42335
60a4da1ba918 8168745: Iterator.forEachRemaining vs. Iterator.remove
psandoz
parents: 25859
diff changeset
    53
     * exception.  Actions are performed in the order of iteration, if that
60a4da1ba918 8168745: Iterator.forEachRemaining vs. Iterator.remove
psandoz
parents: 25859
diff changeset
    54
     * order is specified.  Exceptions thrown by the action are relayed to the
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 21334
diff changeset
    55
     * caller.
42335
60a4da1ba918 8168745: Iterator.forEachRemaining vs. Iterator.remove
psandoz
parents: 25859
diff changeset
    56
     * <p>
60a4da1ba918 8168745: Iterator.forEachRemaining vs. Iterator.remove
psandoz
parents: 25859
diff changeset
    57
     * The behavior of this method is unspecified if the action performs
60a4da1ba918 8168745: Iterator.forEachRemaining vs. Iterator.remove
psandoz
parents: 25859
diff changeset
    58
     * side-effects that modify the underlying source of elements, unless an
60a4da1ba918 8168745: Iterator.forEachRemaining vs. Iterator.remove
psandoz
parents: 25859
diff changeset
    59
     * overriding class has specified a concurrent modification policy.
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 15647
diff changeset
    60
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 15647
diff changeset
    61
     * @implSpec
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 15647
diff changeset
    62
     * <p>The default implementation behaves as if:
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 15647
diff changeset
    63
     * <pre>{@code
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 15647
diff changeset
    64
     *     for (T t : this)
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 15647
diff changeset
    65
     *         action.accept(t);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 15647
diff changeset
    66
     * }</pre>
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 15647
diff changeset
    67
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 15647
diff changeset
    68
     * @param action The action to be performed for each element
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 15647
diff changeset
    69
     * @throws NullPointerException if the specified action is null
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 15647
diff changeset
    70
     * @since 1.8
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 15647
diff changeset
    71
     */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 15647
diff changeset
    72
    default void forEach(Consumer<? super T> action) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 15647
diff changeset
    73
        Objects.requireNonNull(action);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 15647
diff changeset
    74
        for (T t : this) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 15647
diff changeset
    75
            action.accept(t);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 15647
diff changeset
    76
        }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 15647
diff changeset
    77
    }
18824
9fa4af2af63d 8015320: Pull spliterator() up from Collection to Iterable
henryjen
parents: 16929
diff changeset
    78
9fa4af2af63d 8015320: Pull spliterator() up from Collection to Iterable
henryjen
parents: 16929
diff changeset
    79
    /**
9fa4af2af63d 8015320: Pull spliterator() up from Collection to Iterable
henryjen
parents: 16929
diff changeset
    80
     * Creates a {@link Spliterator} over the elements described by this
9fa4af2af63d 8015320: Pull spliterator() up from Collection to Iterable
henryjen
parents: 16929
diff changeset
    81
     * {@code Iterable}.
9fa4af2af63d 8015320: Pull spliterator() up from Collection to Iterable
henryjen
parents: 16929
diff changeset
    82
     *
9fa4af2af63d 8015320: Pull spliterator() up from Collection to Iterable
henryjen
parents: 16929
diff changeset
    83
     * @implSpec
9fa4af2af63d 8015320: Pull spliterator() up from Collection to Iterable
henryjen
parents: 16929
diff changeset
    84
     * The default implementation creates an
23040
0cb50d5761df 8035813: Broken link in java.lang.Iterable
rriggs
parents: 21339
diff changeset
    85
     * <em><a href="../util/Spliterator.html#binding">early-binding</a></em>
18824
9fa4af2af63d 8015320: Pull spliterator() up from Collection to Iterable
henryjen
parents: 16929
diff changeset
    86
     * spliterator from the iterable's {@code Iterator}.  The spliterator
9fa4af2af63d 8015320: Pull spliterator() up from Collection to Iterable
henryjen
parents: 16929
diff changeset
    87
     * inherits the <em>fail-fast</em> properties of the iterable's iterator.
9fa4af2af63d 8015320: Pull spliterator() up from Collection to Iterable
henryjen
parents: 16929
diff changeset
    88
     *
9fa4af2af63d 8015320: Pull spliterator() up from Collection to Iterable
henryjen
parents: 16929
diff changeset
    89
     * @implNote
9fa4af2af63d 8015320: Pull spliterator() up from Collection to Iterable
henryjen
parents: 16929
diff changeset
    90
     * The default implementation should usually be overridden.  The
9fa4af2af63d 8015320: Pull spliterator() up from Collection to Iterable
henryjen
parents: 16929
diff changeset
    91
     * spliterator returned by the default implementation has poor splitting
9fa4af2af63d 8015320: Pull spliterator() up from Collection to Iterable
henryjen
parents: 16929
diff changeset
    92
     * capabilities, is unsized, and does not report any spliterator
9fa4af2af63d 8015320: Pull spliterator() up from Collection to Iterable
henryjen
parents: 16929
diff changeset
    93
     * characteristics. Implementing classes can nearly always provide a
9fa4af2af63d 8015320: Pull spliterator() up from Collection to Iterable
henryjen
parents: 16929
diff changeset
    94
     * better implementation.
9fa4af2af63d 8015320: Pull spliterator() up from Collection to Iterable
henryjen
parents: 16929
diff changeset
    95
     *
9fa4af2af63d 8015320: Pull spliterator() up from Collection to Iterable
henryjen
parents: 16929
diff changeset
    96
     * @return a {@code Spliterator} over the elements described by this
9fa4af2af63d 8015320: Pull spliterator() up from Collection to Iterable
henryjen
parents: 16929
diff changeset
    97
     * {@code Iterable}.
9fa4af2af63d 8015320: Pull spliterator() up from Collection to Iterable
henryjen
parents: 16929
diff changeset
    98
     * @since 1.8
9fa4af2af63d 8015320: Pull spliterator() up from Collection to Iterable
henryjen
parents: 16929
diff changeset
    99
     */
9fa4af2af63d 8015320: Pull spliterator() up from Collection to Iterable
henryjen
parents: 16929
diff changeset
   100
    default Spliterator<T> spliterator() {
9fa4af2af63d 8015320: Pull spliterator() up from Collection to Iterable
henryjen
parents: 16929
diff changeset
   101
        return Spliterators.spliteratorUnknownSize(iterator(), 0);
9fa4af2af63d 8015320: Pull spliterator() up from Collection to Iterable
henryjen
parents: 16929
diff changeset
   102
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
}