-
Notifications
You must be signed in to change notification settings - Fork 668
/
PrimitiveIterator.java
337 lines (303 loc) · 13.1 KB
/
PrimitiveIterator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/*
* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package java.util;
import java.util.function.Consumer;
import java.util.function.DoubleConsumer;
import java.util.function.IntConsumer;
import java.util.function.LongConsumer;
/**
* A base type for primitive specializations of {@code Iterator}.
* Specialized subtypes are provided for {@link OfInt int}, {@link OfLong long}, and {@link OfDouble double} values.
*
* <p>The specialized subtype default implementations of {@link Iterator#next}
* and {@link Iterator#forEachRemaining(java.util.function.Consumer)} box primitive values to instances of their corresponding wrapper class.
* Such boxing may offset any advantages gained when using the primitive specializations.
* To avoid boxing, the corresponding primitive-based methods should be used.
* For example, {@link PrimitiveIterator.OfInt#nextInt()} and {@link PrimitiveIterator.OfInt#forEachRemaining(java.util.function.IntConsumer)}
* should be used in preference to {@link PrimitiveIterator.OfInt#next()} and {@link PrimitiveIterator.OfInt#forEachRemaining(java.util.function.Consumer)}.
*
* <p>Iteration of primitive values using boxing-based methods {@link Iterator#next next()} and {@link Iterator#forEachRemaining(java.util.function.Consumer) forEachRemaining()},
* does not affect the order in which the values, transformed to boxed values, are encountered.
*
* @param <T> the type of elements returned by this PrimitiveIterator.
* The type must be a wrapper type for a primitive type, such as {@code Integer} for the primitive {@code int} type.
* @param <T_CONS> the type of primitive consumer.
* The type must be a primitive specialization of {@link java.util.function.Consumer} for {@code T},
* such as {@link java.util.function.IntConsumer} for {@code Integer}.
*
* @implNote If the boolean system property {@code org.openjdk.java.util.stream.tripwire} is set to {@code true}
* then diagnostic warnings are reported if boxing of primitive values occur when operating on primitive subtype specializations.
*
* @since 1.8
*/
/*
* 元素类型是基本数值类型的Iterator,用于将Spliterator适配为Iterator
*
* 参见:Spliterators
*/
public interface PrimitiveIterator<T, T_CONS> extends Iterator<T> {
/**
* Performs the given action for each remaining element, in the order
* elements occur when iterating, until all elements have been processed
* or the action throws an exception. Errors or runtime exceptions
* thrown by the action are relayed to the caller.
*
* @param action The action to be performed for each element
*
* @throws NullPointerException if the specified action is null
*/
// 尝试用action消费当前迭代器中所有元素
@SuppressWarnings("overloads")
void forEachRemaining(T_CONS action);
/**
* An Iterator specialized for {@code int} values.
*
* @since 1.8
*/
// 元素类型是int类型(包括byte,short,char)的Iterator
interface OfInt extends PrimitiveIterator<Integer, IntConsumer> {
/**
* Returns the next {@code int} element in the iteration.
*
* @return the next {@code int} element in the iteration
*
* @throws NoSuchElementException if the iteration has no more elements
*/
// 返回下一个元素
int nextInt();
/**
* {@inheritDoc}
*
* @implSpec The default implementation boxes the result of calling
* {@link #nextInt()}, and returns that boxed result.
*/
// 返回下一个元素
@Override
default Integer next() {
if(Tripwire.ENABLED) {
Tripwire.trip(getClass(), "{0} calling PrimitiveIterator.OfInt.nextInt()");
}
return nextInt();
}
/**
* Performs the given action for each remaining element until all elements
* have been processed or the action throws an exception. Actions are
* performed in the order of iteration, if that order is specified.
* Exceptions thrown by the action are relayed to the caller.
*
* @param action The action to be performed for each element
*
* @throws NullPointerException if the specified action is null
* @implSpec <p>The default implementation behaves as if:
* <pre>{@code
* while (hasNext())
* action.accept(nextInt());
* }</pre>
*/
// 尝试用action消费当前Spliterator中所有元素
default void forEachRemaining(IntConsumer action) {
Objects.requireNonNull(action);
while(hasNext()) {
action.accept(nextInt());
}
}
/**
* {@inheritDoc}
*
* @implSpec If the action is an instance of {@code IntConsumer} then it is cast
* to {@code IntConsumer} and passed to {@link #forEachRemaining};
* otherwise the action is adapted to an instance of
* {@code IntConsumer}, by boxing the argument of {@code IntConsumer},
* and then passed to {@link #forEachRemaining}.
*/
// 尝试用action消费当前迭代器中所有元素
@Override
default void forEachRemaining(Consumer<? super Integer> action) {
if(action instanceof IntConsumer) {
forEachRemaining((IntConsumer) action);
return;
}
// The method reference action::accept is never null
Objects.requireNonNull(action);
if(Tripwire.ENABLED) {
Tripwire.trip(getClass(), "{0} calling PrimitiveIterator.OfInt.forEachRemainingInt(action::accept)");
}
forEachRemaining((IntConsumer) action::accept);
}
}
/**
* An Iterator specialized for {@code long} values.
*
* @since 1.8
*/
// 元素类型是long类型的Iterator
interface OfLong extends PrimitiveIterator<Long, LongConsumer> {
/**
* Returns the next {@code long} element in the iteration.
*
* @return the next {@code long} element in the iteration
*
* @throws NoSuchElementException if the iteration has no more elements
*/
// 返回下一个元素
long nextLong();
/**
* {@inheritDoc}
*
* @implSpec The default implementation boxes the result of calling
* {@link #nextLong()}, and returns that boxed result.
*/
// 返回下一个元素
@Override
default Long next() {
if(Tripwire.ENABLED) {
Tripwire.trip(getClass(), "{0} calling PrimitiveIterator.OfLong.nextLong()");
}
return nextLong();
}
/**
* Performs the given action for each remaining element until all elements
* have been processed or the action throws an exception. Actions are
* performed in the order of iteration, if that order is specified.
* Exceptions thrown by the action are relayed to the caller.
*
* @param action The action to be performed for each element
*
* @throws NullPointerException if the specified action is null
* @implSpec <p>The default implementation behaves as if:
* <pre>{@code
* while (hasNext())
* action.accept(nextLong());
* }</pre>
*/
// 尝试用action消费当前Spliterator中所有元素
default void forEachRemaining(LongConsumer action) {
Objects.requireNonNull(action);
while(hasNext()) {
action.accept(nextLong());
}
}
/**
* {@inheritDoc}
*
* @implSpec If the action is an instance of {@code LongConsumer} then it is cast
* to {@code LongConsumer} and passed to {@link #forEachRemaining};
* otherwise the action is adapted to an instance of
* {@code LongConsumer}, by boxing the argument of {@code LongConsumer},
* and then passed to {@link #forEachRemaining}.
*/
// 尝试用action消费当前Spliterator中所有元素
@Override
default void forEachRemaining(Consumer<? super Long> action) {
if(action instanceof LongConsumer) {
forEachRemaining((LongConsumer) action);
return;
}
// The method reference action::accept is never null
Objects.requireNonNull(action);
if(Tripwire.ENABLED) {
Tripwire.trip(getClass(), "{0} calling PrimitiveIterator.OfLong.forEachRemainingLong(action::accept)");
}
forEachRemaining((LongConsumer) action::accept);
}
}
/**
* An Iterator specialized for {@code double} values.
*
* @since 1.8
*/
// 元素类型是double类型(包括float)的Iterator
interface OfDouble extends PrimitiveIterator<Double, DoubleConsumer> {
/**
* Returns the next {@code double} element in the iteration.
*
* @return the next {@code double} element in the iteration
*
* @throws NoSuchElementException if the iteration has no more elements
*/
// 返回下一个元素
double nextDouble();
/**
* {@inheritDoc}
*
* @implSpec The default implementation boxes the result of calling
* {@link #nextDouble()}, and returns that boxed result.
*/
// 返回下一个元素
@Override
default Double next() {
if(Tripwire.ENABLED) {
Tripwire.trip(getClass(), "{0} calling PrimitiveIterator.OfDouble.nextLong()");
}
return nextDouble();
}
/**
* Performs the given action for each remaining element until all elements
* have been processed or the action throws an exception. Actions are
* performed in the order of iteration, if that order is specified.
* Exceptions thrown by the action are relayed to the caller.
*
* @param action The action to be performed for each element
*
* @throws NullPointerException if the specified action is null
* @implSpec <p>The default implementation behaves as if:
* <pre>{@code
* while (hasNext())
* action.accept(nextDouble());
* }</pre>
*/
// 尝试用action消费当前Spliterator中所有元素
default void forEachRemaining(DoubleConsumer action) {
Objects.requireNonNull(action);
while(hasNext()) {
action.accept(nextDouble());
}
}
/**
* {@inheritDoc}
*
* @implSpec If the action is an instance of {@code DoubleConsumer} then it is
* cast to {@code DoubleConsumer} and passed to
* {@link #forEachRemaining}; otherwise the action is adapted to
* an instance of {@code DoubleConsumer}, by boxing the argument of
* {@code DoubleConsumer}, and then passed to
* {@link #forEachRemaining}.
*/
// 尝试用action消费当前Spliterator中所有元素
@Override
default void forEachRemaining(Consumer<? super Double> action) {
if(action instanceof DoubleConsumer) {
forEachRemaining((DoubleConsumer) action);
return;
}
// The method reference action::accept is never null
Objects.requireNonNull(action);
if(Tripwire.ENABLED) {
Tripwire.trip(getClass(), "{0} calling PrimitiveIterator.OfDouble.forEachRemainingDouble(action::accept)");
}
forEachRemaining((DoubleConsumer) action::accept);
}
}
}