author | darcy |
Wed, 15 Aug 2018 10:44:56 -0700 | |
changeset 51413 | 43e41800d579 |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
2 | 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 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
package java.lang; |
|
26 |
||
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 | 32 |
|
4977
81902a400bbf
6929382: Various core classes in util and elsewhere are missing @param <T> tags
darcy
parents:
2
diff
changeset
|
33 |
/** |
45141 | 34 |
* Implementing this interface allows an object to be the target of the enhanced |
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 | 39 |
* @since 1.5 |
45141 | 40 |
* @jls 14.14.2 The enhanced {@code for} statement |
2 | 41 |
*/ |
42 |
public interface Iterable<T> { |
|
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 | 45 |
* |
46 |
* @return an Iterator. |
|
47 |
*/ |
|
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 | 51 |
* Performs the given action for each element of the {@code Iterable} |
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 | 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 | 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 | 103 |
} |