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