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 |
* Static classes and methods for operating on or creating instances of
|
|
34 |
* {@link Spliterator} and its primitive specializations
|
|
35 |
* {@link Spliterator.OfInt}, {@link Spliterator.OfLong}, and
|
|
36 |
* {@link Spliterator.OfDouble}.
|
|
37 |
*
|
|
38 |
* @see Spliterator
|
|
39 |
* @since 1.8
|
|
40 |
*/
|
|
41 |
public final class Spliterators {
|
|
42 |
|
|
43 |
// Suppresses default constructor, ensuring non-instantiability.
|
|
44 |
private Spliterators() {}
|
|
45 |
|
|
46 |
// Empty spliterators
|
|
47 |
|
|
48 |
/**
|
|
49 |
* Creates an empty {@code Spliterator}
|
|
50 |
*
|
|
51 |
* <p>The empty spliterator reports {@link Spliterator#SIZED} and
|
|
52 |
* {@link Spliterator#SUBSIZED}. Calls to
|
|
53 |
* {@link java.util.Spliterator#trySplit()} always return {@code null}.
|
|
54 |
*
|
|
55 |
* @param <T> Type of elements
|
|
56 |
* @return An empty spliterator
|
|
57 |
*/
|
|
58 |
@SuppressWarnings("unchecked")
|
|
59 |
public static <T> Spliterator<T> emptySpliterator() {
|
|
60 |
return (Spliterator<T>) EMPTY_SPLITERATOR;
|
|
61 |
}
|
|
62 |
|
|
63 |
private static final Spliterator<Object> EMPTY_SPLITERATOR =
|
|
64 |
new EmptySpliterator.OfRef<>();
|
|
65 |
|
|
66 |
/**
|
|
67 |
* Creates an empty {@code Spliterator.OfInt}
|
|
68 |
*
|
|
69 |
* <p>The empty spliterator reports {@link Spliterator#SIZED} and
|
|
70 |
* {@link Spliterator#SUBSIZED}. Calls to
|
|
71 |
* {@link java.util.Spliterator#trySplit()} always return {@code null}.
|
|
72 |
*
|
|
73 |
* @return An empty spliterator
|
|
74 |
*/
|
|
75 |
public static Spliterator.OfInt emptyIntSpliterator() {
|
|
76 |
return EMPTY_INT_SPLITERATOR;
|
|
77 |
}
|
|
78 |
|
|
79 |
private static final Spliterator.OfInt EMPTY_INT_SPLITERATOR =
|
|
80 |
new EmptySpliterator.OfInt();
|
|
81 |
|
|
82 |
/**
|
|
83 |
* Creates an empty {@code Spliterator.OfLong}
|
|
84 |
*
|
|
85 |
* <p>The empty spliterator reports {@link Spliterator#SIZED} and
|
|
86 |
* {@link Spliterator#SUBSIZED}. Calls to
|
|
87 |
* {@link java.util.Spliterator#trySplit()} always return {@code null}.
|
|
88 |
*
|
|
89 |
* @return An empty spliterator
|
|
90 |
*/
|
|
91 |
public static Spliterator.OfLong emptyLongSpliterator() {
|
|
92 |
return EMPTY_LONG_SPLITERATOR;
|
|
93 |
}
|
|
94 |
|
|
95 |
private static final Spliterator.OfLong EMPTY_LONG_SPLITERATOR =
|
|
96 |
new EmptySpliterator.OfLong();
|
|
97 |
|
|
98 |
/**
|
|
99 |
* Creates an empty {@code Spliterator.OfDouble}
|
|
100 |
*
|
|
101 |
* <p>The empty spliterator reports {@link Spliterator#SIZED} and
|
|
102 |
* {@link Spliterator#SUBSIZED}. Calls to
|
|
103 |
* {@link java.util.Spliterator#trySplit()} always return {@code null}.
|
|
104 |
*
|
|
105 |
* @return An empty spliterator
|
|
106 |
*/
|
|
107 |
public static Spliterator.OfDouble emptyDoubleSpliterator() {
|
|
108 |
return EMPTY_DOUBLE_SPLITERATOR;
|
|
109 |
}
|
|
110 |
|
|
111 |
private static final Spliterator.OfDouble EMPTY_DOUBLE_SPLITERATOR =
|
|
112 |
new EmptySpliterator.OfDouble();
|
|
113 |
|
|
114 |
// Array-based spliterators
|
|
115 |
|
|
116 |
/**
|
|
117 |
* Creates a {@code Spliterator} covering the elements of a given array,
|
|
118 |
* using a customized set of spliterator characteristics.
|
|
119 |
*
|
|
120 |
* <p>This method is provided as an implementation convenience for
|
|
121 |
* Spliterators which store portions of their elements in arrays, and need
|
|
122 |
* fine control over Spliterator characteristics. Most other situations in
|
|
123 |
* which a Spliterator for an array is needed should use
|
|
124 |
* {@link Arrays#spliterator(Object[])}.
|
|
125 |
*
|
|
126 |
* <p>The returned spliterator always reports the characteristics
|
|
127 |
* {@code SIZED} and {@code SUBSIZED}. The caller may provide additional
|
|
128 |
* characteristics for the spliterator to report; it is common to
|
|
129 |
* additionally specify {@code IMMUTABLE} and {@code ORDERED}.
|
|
130 |
*
|
|
131 |
* @param <T> Type of elements
|
|
132 |
* @param array The array, assumed to be unmodified during use
|
|
133 |
* @param additionalCharacteristics Additional spliterator characteristics
|
|
134 |
* of this spliterator's source or elements beyond {@code SIZED} and
|
|
135 |
* {@code SUBSIZED} which are are always reported
|
|
136 |
* @return A spliterator for an array
|
|
137 |
* @throws NullPointerException if the given array is {@code null}
|
|
138 |
* @see Arrays#spliterator(Object[])
|
|
139 |
*/
|
|
140 |
public static <T> Spliterator<T> spliterator(Object[] array,
|
|
141 |
int additionalCharacteristics) {
|
|
142 |
return new ArraySpliterator<>(Objects.requireNonNull(array),
|
|
143 |
additionalCharacteristics);
|
|
144 |
}
|
|
145 |
|
|
146 |
/**
|
|
147 |
* Creates a {@code Spliterator} covering a range of elements of a given
|
|
148 |
* array, using a customized set of spliterator characteristics.
|
|
149 |
*
|
|
150 |
* <p>This method is provided as an implementation convenience for
|
|
151 |
* Spliterators which store portions of their elements in arrays, and need
|
|
152 |
* fine control over Spliterator characteristics. Most other situations in
|
|
153 |
* which a Spliterator for an array is needed should use
|
|
154 |
* {@link Arrays#spliterator(Object[])}.
|
|
155 |
*
|
|
156 |
* <p>The returned spliterator always reports the characteristics
|
|
157 |
* {@code SIZED} and {@code SUBSIZED}. The caller may provide additional
|
|
158 |
* characteristics for the spliterator to report; it is common to
|
|
159 |
* additionally specify {@code IMMUTABLE} and {@code ORDERED}.
|
|
160 |
*
|
|
161 |
* @param <T> Type of elements
|
|
162 |
* @param array The array, assumed to be unmodified during use
|
|
163 |
* @param fromIndex The least index (inclusive) to cover
|
|
164 |
* @param toIndex One past the greatest index to cover
|
|
165 |
* @param additionalCharacteristics Additional spliterator characteristics
|
|
166 |
* of this spliterator's source or elements beyond {@code SIZED} and
|
|
167 |
* {@code SUBSIZED} which are are always reported
|
|
168 |
* @return A spliterator for an array
|
|
169 |
* @throws NullPointerException if the given array is {@code null}
|
|
170 |
* @throws ArrayIndexOutOfBoundsException if {@code fromIndex} is negative,
|
|
171 |
* {@code toIndex} is less than {@code fromIndex}, or
|
|
172 |
* {@code toIndex} is greater than the array size
|
|
173 |
* @see Arrays#spliterator(Object[], int, int)
|
|
174 |
*/
|
|
175 |
public static <T> Spliterator<T> spliterator(Object[] array, int fromIndex, int toIndex,
|
|
176 |
int additionalCharacteristics) {
|
|
177 |
checkFromToBounds(Objects.requireNonNull(array).length, fromIndex, toIndex);
|
|
178 |
return new ArraySpliterator<>(array, fromIndex, toIndex, additionalCharacteristics);
|
|
179 |
}
|
|
180 |
|
|
181 |
/**
|
|
182 |
* Creates a {@code Spliterator.OfInt} covering the elements of a given array,
|
|
183 |
* using a customized set of spliterator characteristics.
|
|
184 |
*
|
|
185 |
* <p>This method is provided as an implementation convenience for
|
|
186 |
* Spliterators which store portions of their elements in arrays, and need
|
|
187 |
* fine control over Spliterator characteristics. Most other situations in
|
|
188 |
* which a Spliterator for an array is needed should use
|
|
189 |
* {@link Arrays#spliterator(int[])}.
|
|
190 |
*
|
|
191 |
* <p>The returned spliterator always reports the characteristics
|
|
192 |
* {@code SIZED} and {@code SUBSIZED}. The caller may provide additional
|
|
193 |
* characteristics for the spliterator to report; it is common to
|
|
194 |
* additionally specify {@code IMMUTABLE} and {@code ORDERED}.
|
|
195 |
*
|
|
196 |
* @param array The array, assumed to be unmodified during use
|
|
197 |
* @param additionalCharacteristics Additional spliterator characteristics
|
|
198 |
* of this spliterator's source or elements beyond {@code SIZED} and
|
|
199 |
* {@code SUBSIZED} which are are always reported
|
|
200 |
* @return A spliterator for an array
|
|
201 |
* @throws NullPointerException if the given array is {@code null}
|
|
202 |
* @see Arrays#spliterator(int[])
|
|
203 |
*/
|
|
204 |
public static Spliterator.OfInt spliterator(int[] array,
|
|
205 |
int additionalCharacteristics) {
|
|
206 |
return new IntArraySpliterator(Objects.requireNonNull(array), additionalCharacteristics);
|
|
207 |
}
|
|
208 |
|
|
209 |
/**
|
|
210 |
* Creates a {@code Spliterator.OfInt} covering a range of elements of a
|
|
211 |
* given array, using a customized set of spliterator characteristics.
|
|
212 |
*
|
|
213 |
* <p>This method is provided as an implementation convenience for
|
|
214 |
* Spliterators which store portions of their elements in arrays, and need
|
|
215 |
* fine control over Spliterator characteristics. Most other situations in
|
|
216 |
* which a Spliterator for an array is needed should use
|
|
217 |
* {@link Arrays#spliterator(int[], int, int)}.
|
|
218 |
*
|
|
219 |
* <p>The returned spliterator always reports the characteristics
|
|
220 |
* {@code SIZED} and {@code SUBSIZED}. The caller may provide additional
|
|
221 |
* characteristics for the spliterator to report; it is common to
|
|
222 |
* additionally specify {@code IMMUTABLE} and {@code ORDERED}.
|
|
223 |
*
|
|
224 |
* @param array The array, assumed to be unmodified during use
|
|
225 |
* @param fromIndex The least index (inclusive) to cover
|
|
226 |
* @param toIndex One past the greatest index to cover
|
|
227 |
* @param additionalCharacteristics Additional spliterator characteristics
|
|
228 |
* of this spliterator's source or elements beyond {@code SIZED} and
|
|
229 |
* {@code SUBSIZED} which are are always reported
|
|
230 |
* @return A spliterator for an array
|
|
231 |
* @throws NullPointerException if the given array is {@code null}
|
|
232 |
* @throws ArrayIndexOutOfBoundsException if {@code fromIndex} is negative,
|
|
233 |
* {@code toIndex} is less than {@code fromIndex}, or
|
|
234 |
* {@code toIndex} is greater than the array size
|
|
235 |
* @see Arrays#spliterator(int[], int, int)
|
|
236 |
*/
|
|
237 |
public static Spliterator.OfInt spliterator(int[] array, int fromIndex, int toIndex,
|
|
238 |
int additionalCharacteristics) {
|
|
239 |
checkFromToBounds(Objects.requireNonNull(array).length, fromIndex, toIndex);
|
|
240 |
return new IntArraySpliterator(array, fromIndex, toIndex, additionalCharacteristics);
|
|
241 |
}
|
|
242 |
|
|
243 |
/**
|
|
244 |
* Creates a {@code Spliterator.OfLong} covering the elements of a given array,
|
|
245 |
* using a customized set of spliterator characteristics.
|
|
246 |
*
|
|
247 |
* <p>This method is provided as an implementation convenience for
|
|
248 |
* Spliterators which store portions of their elements in arrays, and need
|
|
249 |
* fine control over Spliterator characteristics. Most other situations in
|
|
250 |
* which a Spliterator for an array is needed should use
|
|
251 |
* {@link Arrays#spliterator(long[])}.
|
|
252 |
*
|
|
253 |
* <p>The returned spliterator always reports the characteristics
|
|
254 |
* {@code SIZED} and {@code SUBSIZED}. The caller may provide additional
|
|
255 |
* characteristics for the spliterator to report; it is common to
|
|
256 |
* additionally specify {@code IMMUTABLE} and {@code ORDERED}.
|
|
257 |
*
|
|
258 |
* @param array The array, assumed to be unmodified during use
|
|
259 |
* @param additionalCharacteristics Additional spliterator characteristics
|
|
260 |
* of this spliterator's source or elements beyond {@code SIZED} and
|
|
261 |
* {@code SUBSIZED} which are are always reported
|
|
262 |
* @return A spliterator for an array
|
|
263 |
* @throws NullPointerException if the given array is {@code null}
|
|
264 |
* @see Arrays#spliterator(long[])
|
|
265 |
*/
|
|
266 |
public static Spliterator.OfLong spliterator(long[] array,
|
|
267 |
int additionalCharacteristics) {
|
|
268 |
return new LongArraySpliterator(Objects.requireNonNull(array), additionalCharacteristics);
|
|
269 |
}
|
|
270 |
|
|
271 |
/**
|
|
272 |
* Creates a {@code Spliterator.OfLong} covering a range of elements of a
|
|
273 |
* given array, using a customized set of spliterator characteristics.
|
|
274 |
*
|
|
275 |
* <p>This method is provided as an implementation convenience for
|
|
276 |
* Spliterators which store portions of their elements in arrays, and need
|
|
277 |
* fine control over Spliterator characteristics. Most other situations in
|
|
278 |
* which a Spliterator for an array is needed should use
|
|
279 |
* {@link Arrays#spliterator(long[], int, int)}.
|
|
280 |
*
|
|
281 |
* <p>The returned spliterator always reports the characteristics
|
|
282 |
* {@code SIZED} and {@code SUBSIZED}. The caller may provide additional
|
|
283 |
* characteristics for the spliterator to report. (For example, if it is
|
|
284 |
* known the array will not be further modified, specify {@code IMMUTABLE};
|
|
285 |
* if the array data is considered to have an an encounter order, specify
|
|
286 |
* {@code ORDERED}). The method {@link Arrays#spliterator(long[], int, int)} can
|
|
287 |
* often be used instead, which returns a spliterator that reports
|
|
288 |
* {@code SIZED}, {@code SUBSIZED}, {@code IMMUTABLE}, and {@code ORDERED}.
|
|
289 |
*
|
|
290 |
* @param array The array, assumed to be unmodified during use
|
|
291 |
* @param fromIndex The least index (inclusive) to cover
|
|
292 |
* @param toIndex One past the greatest index to cover
|
|
293 |
* @param additionalCharacteristics Additional spliterator characteristics
|
|
294 |
* of this spliterator's source or elements beyond {@code SIZED} and
|
|
295 |
* {@code SUBSIZED} which are are always reported
|
|
296 |
* @return A spliterator for an array
|
|
297 |
* @throws NullPointerException if the given array is {@code null}
|
|
298 |
* @throws ArrayIndexOutOfBoundsException if {@code fromIndex} is negative,
|
|
299 |
* {@code toIndex} is less than {@code fromIndex}, or
|
|
300 |
* {@code toIndex} is greater than the array size
|
|
301 |
* @see Arrays#spliterator(long[], int, int)
|
|
302 |
*/
|
|
303 |
public static Spliterator.OfLong spliterator(long[] array, int fromIndex, int toIndex,
|
|
304 |
int additionalCharacteristics) {
|
|
305 |
checkFromToBounds(Objects.requireNonNull(array).length, fromIndex, toIndex);
|
|
306 |
return new LongArraySpliterator(array, fromIndex, toIndex, additionalCharacteristics);
|
|
307 |
}
|
|
308 |
|
|
309 |
/**
|
|
310 |
* Creates a {@code Spliterator.OfDouble} covering the elements of a given array,
|
|
311 |
* using a customized set of spliterator characteristics.
|
|
312 |
*
|
|
313 |
* <p>This method is provided as an implementation convenience for
|
|
314 |
* Spliterators which store portions of their elements in arrays, and need
|
|
315 |
* fine control over Spliterator characteristics. Most other situations in
|
|
316 |
* which a Spliterator for an array is needed should use
|
|
317 |
* {@link Arrays#spliterator(double[])}.
|
|
318 |
*
|
|
319 |
* <p>The returned spliterator always reports the characteristics
|
|
320 |
* {@code SIZED} and {@code SUBSIZED}. The caller may provide additional
|
|
321 |
* characteristics for the spliterator to report; it is common to
|
|
322 |
* additionally specify {@code IMMUTABLE} and {@code ORDERED}.
|
|
323 |
*
|
|
324 |
* @param array The array, assumed to be unmodified during use
|
|
325 |
* @param additionalCharacteristics Additional spliterator characteristics
|
|
326 |
* of this spliterator's source or elements beyond {@code SIZED} and
|
|
327 |
* {@code SUBSIZED} which are are always reported
|
|
328 |
* @return A spliterator for an array
|
|
329 |
* @throws NullPointerException if the given array is {@code null}
|
|
330 |
* @see Arrays#spliterator(double[])
|
|
331 |
*/
|
|
332 |
public static Spliterator.OfDouble spliterator(double[] array,
|
|
333 |
int additionalCharacteristics) {
|
|
334 |
return new DoubleArraySpliterator(Objects.requireNonNull(array), additionalCharacteristics);
|
|
335 |
}
|
|
336 |
|
|
337 |
/**
|
|
338 |
* Creates a {@code Spliterator.OfDouble} covering a range of elements of a
|
|
339 |
* given array, using a customized set of spliterator characteristics.
|
|
340 |
*
|
|
341 |
* <p>This method is provided as an implementation convenience for
|
|
342 |
* Spliterators which store portions of their elements in arrays, and need
|
|
343 |
* fine control over Spliterator characteristics. Most other situations in
|
|
344 |
* which a Spliterator for an array is needed should use
|
|
345 |
* {@link Arrays#spliterator(double[], int, int)}.
|
|
346 |
*
|
|
347 |
* <p>The returned spliterator always reports the characteristics
|
|
348 |
* {@code SIZED} and {@code SUBSIZED}. The caller may provide additional
|
|
349 |
* characteristics for the spliterator to report. (For example, if it is
|
|
350 |
* known the array will not be further modified, specify {@code IMMUTABLE};
|
|
351 |
* if the array data is considered to have an an encounter order, specify
|
|
352 |
* {@code ORDERED}). The method {@link Arrays#spliterator(long[], int, int)} can
|
|
353 |
* often be used instead, which returns a spliterator that reports
|
|
354 |
* {@code SIZED}, {@code SUBSIZED}, {@code IMMUTABLE}, and {@code ORDERED}.
|
|
355 |
*
|
|
356 |
* @param array The array, assumed to be unmodified during use
|
|
357 |
* @param fromIndex The least index (inclusive) to cover
|
|
358 |
* @param toIndex One past the greatest index to cover
|
|
359 |
* @param additionalCharacteristics Additional spliterator characteristics
|
|
360 |
* of this spliterator's source or elements beyond {@code SIZED} and
|
|
361 |
* {@code SUBSIZED} which are are always reported
|
|
362 |
* @return A spliterator for an array
|
|
363 |
* @throws NullPointerException if the given array is {@code null}
|
|
364 |
* @throws ArrayIndexOutOfBoundsException if {@code fromIndex} is negative,
|
|
365 |
* {@code toIndex} is less than {@code fromIndex}, or
|
|
366 |
* {@code toIndex} is greater than the array size
|
|
367 |
* @see Arrays#spliterator(double[], int, int)
|
|
368 |
*/
|
|
369 |
public static Spliterator.OfDouble spliterator(double[] array, int fromIndex, int toIndex,
|
|
370 |
int additionalCharacteristics) {
|
|
371 |
checkFromToBounds(Objects.requireNonNull(array).length, fromIndex, toIndex);
|
|
372 |
return new DoubleArraySpliterator(array, fromIndex, toIndex, additionalCharacteristics);
|
|
373 |
}
|
|
374 |
|
|
375 |
/**
|
|
376 |
* Validate inclusive start index and exclusive end index against the length
|
|
377 |
* of an array.
|
|
378 |
* @param arrayLength The length of the array
|
|
379 |
* @param origin The inclusive start index
|
|
380 |
* @param fence The exclusive end index
|
|
381 |
* @throws ArrayIndexOutOfBoundsException if the start index is greater than
|
|
382 |
* the end index, if the start index is negative, or the end index is
|
|
383 |
* greater than the array length
|
|
384 |
*/
|
|
385 |
private static void checkFromToBounds(int arrayLength, int origin, int fence) {
|
|
386 |
if (origin > fence) {
|
|
387 |
throw new IllegalArgumentException(
|
|
388 |
"origin(" + origin + ") > fence(" + fence + ")");
|
|
389 |
}
|
|
390 |
if (origin < 0) {
|
|
391 |
throw new ArrayIndexOutOfBoundsException(origin);
|
|
392 |
}
|
|
393 |
if (fence > arrayLength) {
|
|
394 |
throw new ArrayIndexOutOfBoundsException(fence);
|
|
395 |
}
|
|
396 |
}
|
|
397 |
|
|
398 |
// Iterator-based spliterators
|
|
399 |
|
|
400 |
/**
|
|
401 |
* Creates a {@code Spliterator} using the given collection's
|
|
402 |
* {@link java.util.Collection#iterator()} as the source of elements, and
|
|
403 |
* reporting its {@link java.util.Collection#size()} as its initial size.
|
|
404 |
*
|
|
405 |
* <p>The spliterator is
|
|
406 |
* <em><a href="Spliterator.html#binding">late-binding</a></em>, inherits
|
|
407 |
* the <em>fail-fast</em> properties of the collection's iterator, and
|
|
408 |
* implements {@code trySplit} to permit limited parallelism.
|
|
409 |
*
|
|
410 |
* @param <T> Type of elements
|
|
411 |
* @param c The collection
|
|
412 |
* @param additionalCharacteristics Additional spliterator characteristics
|
|
413 |
* of this spliterator's source or elements beyond {@code SIZED} and
|
|
414 |
* {@code SUBSIZED} which are are always reported
|
|
415 |
* @return A spliterator from an iterator
|
|
416 |
* @throws NullPointerException if the given collection is {@code null}
|
|
417 |
*/
|
|
418 |
public static <T> Spliterator<T> spliterator(Collection<? extends T> c,
|
|
419 |
int additionalCharacteristics) {
|
|
420 |
return new IteratorSpliterator<>(Objects.requireNonNull(c),
|
|
421 |
additionalCharacteristics);
|
|
422 |
}
|
|
423 |
|
|
424 |
/**
|
|
425 |
* Creates a {@code Spliterator} using a given {@code Iterator}
|
|
426 |
* as the source of elements, and with a given initially reported size.
|
|
427 |
*
|
|
428 |
* <p>The spliterator is not
|
|
429 |
* <em><a href="Spliterator.html#binding">late-binding</a></em>, inherits
|
|
430 |
* the <em>fail-fast</em> properties of the iterator, and implements
|
|
431 |
* {@code trySplit} to permit limited parallelism.
|
|
432 |
*
|
|
433 |
* <p>Traversal of elements should be accomplished through the spliterator.
|
|
434 |
* The behaviour of splitting and traversal is undefined if the iterator is
|
|
435 |
* operated on after the spliterator is returned, or the initially reported
|
|
436 |
* size is not equal to the actual number of elements in the source.
|
|
437 |
*
|
|
438 |
* @param <T> Type of elements
|
|
439 |
* @param iterator The iterator for the source
|
|
440 |
* @param size The number of elements in the source, to be reported as
|
|
441 |
* initial {@code estimateSize}
|
|
442 |
* @param additionalCharacteristics Additional spliterator characteristics
|
|
443 |
* of this spliterator's source or elements beyond {@code SIZED} and
|
|
444 |
* {@code SUBSIZED} which are are always reported
|
|
445 |
* @return A spliterator from an iterator
|
|
446 |
* @throws NullPointerException if the given iterator is {@code null}
|
|
447 |
*/
|
|
448 |
public static <T> Spliterator<T> spliterator(Iterator<? extends T> iterator,
|
|
449 |
long size,
|
|
450 |
int additionalCharacteristics) {
|
|
451 |
return new IteratorSpliterator<>(Objects.requireNonNull(iterator), size,
|
|
452 |
additionalCharacteristics);
|
|
453 |
}
|
|
454 |
|
|
455 |
/**
|
|
456 |
* Creates a {@code Spliterator} using a given {@code Iterator}
|
|
457 |
* as the source of elements, with no initial size estimate.
|
|
458 |
*
|
|
459 |
* <p>The spliterator is not
|
|
460 |
* <em><a href="Spliterator.html#binding">late-binding</a></em>, inherits
|
|
461 |
* the <em>fail-fast</em> properties of the iterator, and implements
|
|
462 |
* {@code trySplit} to permit limited parallelism.
|
|
463 |
*
|
|
464 |
* <p>Traversal of elements should be accomplished through the spliterator.
|
|
465 |
* The behaviour of splitting and traversal is undefined if the iterator is
|
|
466 |
* operated on after the spliterator is returned.
|
|
467 |
*
|
|
468 |
* @param <T> Type of elements
|
|
469 |
* @param iterator The iterator for the source
|
|
470 |
* @param characteristics Properties of this spliterator's source
|
|
471 |
* or elements ({@code SIZED} and {@code SUBSIZED}, if supplied, are
|
|
472 |
* ignored and are not reported.)
|
|
473 |
* @return A spliterator from an iterator
|
|
474 |
* @throws NullPointerException if the given iterator is {@code null}
|
|
475 |
*/
|
|
476 |
public static <T> Spliterator<T> spliteratorUnknownSize(Iterator<? extends T> iterator,
|
|
477 |
int characteristics) {
|
|
478 |
return new IteratorSpliterator<>(Objects.requireNonNull(iterator), characteristics);
|
|
479 |
}
|
|
480 |
|
|
481 |
/**
|
|
482 |
* Creates a {@code Spliterator.OfInt} using a given
|
|
483 |
* {@code IntStream.IntIterator} as the source of elements, and with a given
|
|
484 |
* initially reported size.
|
|
485 |
*
|
|
486 |
* <p>The spliterator is not
|
|
487 |
* <em><a href="Spliterator.html#binding">late-binding</a></em>, inherits
|
|
488 |
* the <em>fail-fast</em> properties of the iterator, and implements
|
|
489 |
* {@code trySplit} to permit limited parallelism.
|
|
490 |
*
|
|
491 |
* <p>Traversal of elements should be accomplished through the spliterator.
|
|
492 |
* The behaviour of splitting and traversal is undefined if the iterator is
|
|
493 |
* operated on after the spliterator is returned, or the initially reported
|
|
494 |
* size is not equal to the actual number of elements in the source.
|
|
495 |
*
|
|
496 |
* @param iterator The iterator for the source
|
|
497 |
* @param size The number of elements in the source, to be reported as
|
|
498 |
* initial {@code estimateSize}.
|
|
499 |
* @param additionalCharacteristics Additional spliterator characteristics
|
|
500 |
* of this spliterator's source or elements beyond {@code SIZED} and
|
|
501 |
* {@code SUBSIZED} which are are always reported
|
|
502 |
* @return A spliterator from an iterator
|
|
503 |
* @throws NullPointerException if the given iterator is {@code null}
|
|
504 |
*/
|
|
505 |
public static Spliterator.OfInt spliterator(PrimitiveIterator.OfInt iterator,
|
|
506 |
long size,
|
|
507 |
int additionalCharacteristics) {
|
|
508 |
return new IntIteratorSpliterator(Objects.requireNonNull(iterator),
|
|
509 |
size, additionalCharacteristics);
|
|
510 |
}
|
|
511 |
|
|
512 |
/**
|
|
513 |
* Creates a {@code Spliterator.OfInt} using a given
|
|
514 |
* {@code IntStream.IntIterator} as the source of elements, with no initial
|
|
515 |
* size estimate.
|
|
516 |
*
|
|
517 |
* <p>The spliterator is not
|
|
518 |
* <em><a href="Spliterator.html#binding">late-binding</a></em>, inherits
|
|
519 |
* the <em>fail-fast</em> properties of the iterator, and implements
|
|
520 |
* {@code trySplit} to permit limited parallelism.
|
|
521 |
*
|
|
522 |
* <p>Traversal of elements should be accomplished through the spliterator.
|
|
523 |
* The behaviour of splitting and traversal is undefined if the iterator is
|
|
524 |
* operated on after the spliterator is returned.
|
|
525 |
*
|
|
526 |
* @param iterator The iterator for the source
|
|
527 |
* @param characteristics Properties of this spliterator's source
|
|
528 |
* or elements ({@code SIZED} and {@code SUBSIZED}, if supplied, are
|
|
529 |
* ignored and are not reported.)
|
|
530 |
* @return A spliterator from an iterator
|
|
531 |
* @throws NullPointerException if the given iterator is {@code null}
|
|
532 |
*/
|
|
533 |
public static Spliterator.OfInt spliteratorUnknownSize(PrimitiveIterator.OfInt iterator,
|
|
534 |
int characteristics) {
|
|
535 |
return new IntIteratorSpliterator(Objects.requireNonNull(iterator), characteristics);
|
|
536 |
}
|
|
537 |
|
|
538 |
/**
|
|
539 |
* Creates a {@code Spliterator.OfLong} using a given
|
|
540 |
* {@code LongStream.LongIterator} as the source of elements, and with a
|
|
541 |
* given initially reported size.
|
|
542 |
*
|
|
543 |
* <p>The spliterator is not
|
|
544 |
* <em><a href="Spliterator.html#binding">late-binding</a></em>, inherits
|
|
545 |
* the <em>fail-fast</em> properties of the iterator, and implements
|
|
546 |
* {@code trySplit} to permit limited parallelism.
|
|
547 |
*
|
|
548 |
* <p>Traversal of elements should be accomplished through the spliterator.
|
|
549 |
* The behaviour of splitting and traversal is undefined if the iterator is
|
|
550 |
* operated on after the spliterator is returned, or the initially reported
|
|
551 |
* size is not equal to the actual number of elements in the source.
|
|
552 |
*
|
|
553 |
* @param iterator The iterator for the source
|
|
554 |
* @param size The number of elements in the source, to be reported as
|
|
555 |
* initial {@code estimateSize}.
|
|
556 |
* @param additionalCharacteristics Additional spliterator characteristics
|
|
557 |
* of this spliterator's source or elements beyond {@code SIZED} and
|
|
558 |
* {@code SUBSIZED} which are are always reported
|
|
559 |
* @return A spliterator from an iterator
|
|
560 |
* @throws NullPointerException if the given iterator is {@code null}
|
|
561 |
*/
|
|
562 |
public static Spliterator.OfLong spliterator(PrimitiveIterator.OfLong iterator,
|
|
563 |
long size,
|
|
564 |
int additionalCharacteristics) {
|
|
565 |
return new LongIteratorSpliterator(Objects.requireNonNull(iterator),
|
|
566 |
size, additionalCharacteristics);
|
|
567 |
}
|
|
568 |
|
|
569 |
/**
|
|
570 |
* Creates a {@code Spliterator.OfLong} using a given
|
|
571 |
* {@code LongStream.LongIterator} as the source of elements, with no
|
|
572 |
* initial size estimate.
|
|
573 |
*
|
|
574 |
* <p>The spliterator is not
|
|
575 |
* <em><a href="Spliterator.html#binding">late-binding</a></em>, inherits
|
|
576 |
* the <em>fail-fast</em> properties of the iterator, and implements
|
|
577 |
* {@code trySplit} to permit limited parallelism.
|
|
578 |
*
|
|
579 |
* <p>Traversal of elements should be accomplished through the spliterator.
|
|
580 |
* The behaviour of splitting and traversal is undefined if the iterator is
|
|
581 |
* operated on after the spliterator is returned.
|
|
582 |
*
|
|
583 |
* @param iterator The iterator for the source
|
|
584 |
* @param characteristics Properties of this spliterator's source
|
|
585 |
* or elements ({@code SIZED} and {@code SUBSIZED}, if supplied, are
|
|
586 |
* ignored and are not reported.)
|
|
587 |
* @return A spliterator from an iterator
|
|
588 |
* @throws NullPointerException if the given iterator is {@code null}
|
|
589 |
*/
|
|
590 |
public static Spliterator.OfLong spliteratorUnknownSize(PrimitiveIterator.OfLong iterator,
|
|
591 |
int characteristics) {
|
|
592 |
return new LongIteratorSpliterator(Objects.requireNonNull(iterator), characteristics);
|
|
593 |
}
|
|
594 |
|
|
595 |
/**
|
|
596 |
* Creates a {@code Spliterator.OfDouble} using a given
|
|
597 |
* {@code DoubleStream.DoubleIterator} as the source of elements, and with a
|
|
598 |
* given initially reported size.
|
|
599 |
*
|
|
600 |
* <p>The spliterator is not
|
|
601 |
* <em><a href="Spliterator.html#binding">late-binding</a></em>, inherits
|
|
602 |
* the <em>fail-fast</em> properties of the iterator, and implements
|
|
603 |
* {@code trySplit} to permit limited parallelism.
|
|
604 |
*
|
|
605 |
* <p>Traversal of elements should be accomplished through the spliterator.
|
|
606 |
* The behaviour of splitting and traversal is undefined if the iterator is
|
|
607 |
* operated on after the spliterator is returned, or the initially reported
|
|
608 |
* size is not equal to the actual number of elements in the source.
|
|
609 |
*
|
|
610 |
* @param iterator The iterator for the source
|
|
611 |
* @param size The number of elements in the source, to be reported as
|
|
612 |
* initial {@code estimateSize}
|
|
613 |
* @param additionalCharacteristics Additional spliterator characteristics
|
|
614 |
* of this spliterator's source or elements beyond {@code SIZED} and
|
|
615 |
* {@code SUBSIZED} which are are always reported
|
|
616 |
* @return A spliterator from an iterator
|
|
617 |
* @throws NullPointerException if the given iterator is {@code null}
|
|
618 |
*/
|
|
619 |
public static Spliterator.OfDouble spliterator(PrimitiveIterator.OfDouble iterator,
|
|
620 |
long size,
|
|
621 |
int additionalCharacteristics) {
|
|
622 |
return new DoubleIteratorSpliterator(Objects.requireNonNull(iterator),
|
|
623 |
size, additionalCharacteristics);
|
|
624 |
}
|
|
625 |
|
|
626 |
/**
|
|
627 |
* Creates a {@code Spliterator.OfDouble} using a given
|
|
628 |
* {@code DoubleStream.DoubleIterator} as the source of elements, with no
|
|
629 |
* initial size estimate.
|
|
630 |
*
|
|
631 |
* <p>The spliterator is not
|
|
632 |
* <em><a href="Spliterator.html#binding">late-binding</a></em>, inherits
|
|
633 |
* the <em>fail-fast</em> properties of the iterator, and implements
|
|
634 |
* {@code trySplit} to permit limited parallelism.
|
|
635 |
*
|
|
636 |
* <p>Traversal of elements should be accomplished through the spliterator.
|
|
637 |
* The behaviour of splitting and traversal is undefined if the iterator is
|
|
638 |
* operated on after the spliterator is returned.
|
|
639 |
*
|
|
640 |
* @param iterator The iterator for the source
|
|
641 |
* @param characteristics Properties of this spliterator's source
|
|
642 |
* or elements ({@code SIZED} and {@code SUBSIZED}, if supplied, are
|
|
643 |
* ignored and are not reported.)
|
|
644 |
* @return A spliterator from an iterator
|
|
645 |
* @throws NullPointerException if the given iterator is {@code null}
|
|
646 |
*/
|
|
647 |
public static Spliterator.OfDouble spliteratorUnknownSize(PrimitiveIterator.OfDouble iterator,
|
|
648 |
int characteristics) {
|
|
649 |
return new DoubleIteratorSpliterator(Objects.requireNonNull(iterator), characteristics);
|
|
650 |
}
|
|
651 |
|
|
652 |
// Iterators from Spliterators
|
|
653 |
|
|
654 |
/**
|
|
655 |
* Creates an {@code Iterator} from a {@code Spliterator}.
|
|
656 |
*
|
|
657 |
* <p>Traversal of elements should be accomplished through the iterator.
|
|
658 |
* The behaviour of traversal is undefined if the spliterator is operated
|
|
659 |
* after the iterator is returned.
|
|
660 |
*
|
|
661 |
* @param <T> Type of elements
|
|
662 |
* @param spliterator The spliterator
|
|
663 |
* @return An iterator
|
|
664 |
* @throws NullPointerException if the given spliterator is {@code null}
|
|
665 |
*/
|
|
666 |
public static<T> Iterator<T> iteratorFromSpliterator(Spliterator<? extends T> spliterator) {
|
|
667 |
Objects.requireNonNull(spliterator);
|
|
668 |
class Adapter implements Iterator<T>, Consumer<T> {
|
|
669 |
boolean valueReady = false;
|
|
670 |
T nextElement;
|
|
671 |
|
|
672 |
@Override
|
|
673 |
public void accept(T t) {
|
|
674 |
valueReady = true;
|
|
675 |
nextElement = t;
|
|
676 |
}
|
|
677 |
|
|
678 |
@Override
|
|
679 |
public boolean hasNext() {
|
|
680 |
if (!valueReady)
|
|
681 |
spliterator.tryAdvance(this);
|
|
682 |
return valueReady;
|
|
683 |
}
|
|
684 |
|
|
685 |
@Override
|
|
686 |
public T next() {
|
|
687 |
if (!valueReady && !hasNext())
|
|
688 |
throw new NoSuchElementException();
|
|
689 |
else {
|
|
690 |
valueReady = false;
|
|
691 |
return nextElement;
|
|
692 |
}
|
|
693 |
}
|
|
694 |
}
|
|
695 |
|
|
696 |
return new Adapter();
|
|
697 |
}
|
|
698 |
|
|
699 |
/**
|
|
700 |
* Creates an {@code PrimitiveIterator.OfInt} from a
|
|
701 |
* {@code Spliterator.OfInt}.
|
|
702 |
*
|
|
703 |
* <p>Traversal of elements should be accomplished through the iterator.
|
|
704 |
* The behaviour of traversal is undefined if the spliterator is operated
|
|
705 |
* after the iterator is returned.
|
|
706 |
*
|
|
707 |
* @param spliterator The spliterator
|
|
708 |
* @return An iterator
|
|
709 |
* @throws NullPointerException if the given spliterator is {@code null}
|
|
710 |
*/
|
|
711 |
public static PrimitiveIterator.OfInt iteratorFromSpliterator(Spliterator.OfInt spliterator) {
|
|
712 |
Objects.requireNonNull(spliterator);
|
|
713 |
class Adapter implements PrimitiveIterator.OfInt, IntConsumer {
|
|
714 |
boolean valueReady = false;
|
|
715 |
int nextElement;
|
|
716 |
|
|
717 |
@Override
|
|
718 |
public void accept(int t) {
|
|
719 |
valueReady = true;
|
|
720 |
nextElement = t;
|
|
721 |
}
|
|
722 |
|
|
723 |
@Override
|
|
724 |
public boolean hasNext() {
|
|
725 |
if (!valueReady)
|
|
726 |
spliterator.tryAdvance(this);
|
|
727 |
return valueReady;
|
|
728 |
}
|
|
729 |
|
|
730 |
@Override
|
|
731 |
public int nextInt() {
|
|
732 |
if (!valueReady && !hasNext())
|
|
733 |
throw new NoSuchElementException();
|
|
734 |
else {
|
|
735 |
valueReady = false;
|
|
736 |
return nextElement;
|
|
737 |
}
|
|
738 |
}
|
|
739 |
}
|
|
740 |
|
|
741 |
return new Adapter();
|
|
742 |
}
|
|
743 |
|
|
744 |
/**
|
|
745 |
* Creates an {@code PrimitiveIterator.OfLong} from a
|
|
746 |
* {@code Spliterator.OfLong}.
|
|
747 |
*
|
|
748 |
* <p>Traversal of elements should be accomplished through the iterator.
|
|
749 |
* The behaviour of traversal is undefined if the spliterator is operated
|
|
750 |
* after the iterator is returned.
|
|
751 |
*
|
|
752 |
* @param spliterator The spliterator
|
|
753 |
* @return An iterator
|
|
754 |
* @throws NullPointerException if the given spliterator is {@code null}
|
|
755 |
*/
|
|
756 |
public static PrimitiveIterator.OfLong iteratorFromSpliterator(Spliterator.OfLong spliterator) {
|
|
757 |
Objects.requireNonNull(spliterator);
|
|
758 |
class Adapter implements PrimitiveIterator.OfLong, LongConsumer {
|
|
759 |
boolean valueReady = false;
|
|
760 |
long nextElement;
|
|
761 |
|
|
762 |
@Override
|
|
763 |
public void accept(long t) {
|
|
764 |
valueReady = true;
|
|
765 |
nextElement = t;
|
|
766 |
}
|
|
767 |
|
|
768 |
@Override
|
|
769 |
public boolean hasNext() {
|
|
770 |
if (!valueReady)
|
|
771 |
spliterator.tryAdvance(this);
|
|
772 |
return valueReady;
|
|
773 |
}
|
|
774 |
|
|
775 |
@Override
|
|
776 |
public long nextLong() {
|
|
777 |
if (!valueReady && !hasNext())
|
|
778 |
throw new NoSuchElementException();
|
|
779 |
else {
|
|
780 |
valueReady = false;
|
|
781 |
return nextElement;
|
|
782 |
}
|
|
783 |
}
|
|
784 |
}
|
|
785 |
|
|
786 |
return new Adapter();
|
|
787 |
}
|
|
788 |
|
|
789 |
/**
|
|
790 |
* Creates an {@code PrimitiveIterator.OfDouble} from a
|
|
791 |
* {@code Spliterator.OfDouble}.
|
|
792 |
*
|
|
793 |
* <p>Traversal of elements should be accomplished through the iterator.
|
|
794 |
* The behaviour of traversal is undefined if the spliterator is operated
|
|
795 |
* after the iterator is returned.
|
|
796 |
*
|
|
797 |
* @param spliterator The spliterator
|
|
798 |
* @return An iterator
|
|
799 |
* @throws NullPointerException if the given spliterator is {@code null}
|
|
800 |
*/
|
|
801 |
public static PrimitiveIterator.OfDouble iteratorFromSpliterator(Spliterator.OfDouble spliterator) {
|
|
802 |
Objects.requireNonNull(spliterator);
|
|
803 |
class Adapter implements PrimitiveIterator.OfDouble, DoubleConsumer {
|
|
804 |
boolean valueReady = false;
|
|
805 |
double nextElement;
|
|
806 |
|
|
807 |
@Override
|
|
808 |
public void accept(double t) {
|
|
809 |
valueReady = true;
|
|
810 |
nextElement = t;
|
|
811 |
}
|
|
812 |
|
|
813 |
@Override
|
|
814 |
public boolean hasNext() {
|
|
815 |
if (!valueReady)
|
|
816 |
spliterator.tryAdvance(this);
|
|
817 |
return valueReady;
|
|
818 |
}
|
|
819 |
|
|
820 |
@Override
|
|
821 |
public double nextDouble() {
|
|
822 |
if (!valueReady && !hasNext())
|
|
823 |
throw new NoSuchElementException();
|
|
824 |
else {
|
|
825 |
valueReady = false;
|
|
826 |
return nextElement;
|
|
827 |
}
|
|
828 |
}
|
|
829 |
}
|
|
830 |
|
|
831 |
return new Adapter();
|
|
832 |
}
|
|
833 |
|
|
834 |
// Implementations
|
|
835 |
|
|
836 |
private static abstract class EmptySpliterator<T, S extends Spliterator<T>, C> {
|
|
837 |
|
|
838 |
EmptySpliterator() { }
|
|
839 |
|
|
840 |
public S trySplit() {
|
|
841 |
return null;
|
|
842 |
}
|
|
843 |
|
|
844 |
public boolean tryAdvance(C consumer) {
|
|
845 |
Objects.requireNonNull(consumer);
|
|
846 |
return false;
|
|
847 |
}
|
|
848 |
|
|
849 |
public void forEachRemaining(C consumer) {
|
|
850 |
Objects.requireNonNull(consumer);
|
|
851 |
}
|
|
852 |
|
|
853 |
public long estimateSize() {
|
|
854 |
return 0;
|
|
855 |
}
|
|
856 |
|
|
857 |
public int characteristics() {
|
|
858 |
return Spliterator.SIZED | Spliterator.SUBSIZED;
|
|
859 |
}
|
|
860 |
|
|
861 |
private static final class OfRef<T>
|
|
862 |
extends EmptySpliterator<T, Spliterator<T>, Consumer<? super T>>
|
|
863 |
implements Spliterator<T> {
|
|
864 |
OfRef() { }
|
|
865 |
}
|
|
866 |
|
|
867 |
private static final class OfInt
|
|
868 |
extends EmptySpliterator<Integer, Spliterator.OfInt, IntConsumer>
|
|
869 |
implements Spliterator.OfInt {
|
|
870 |
OfInt() { }
|
|
871 |
}
|
|
872 |
|
|
873 |
private static final class OfLong
|
|
874 |
extends EmptySpliterator<Long, Spliterator.OfLong, LongConsumer>
|
|
875 |
implements Spliterator.OfLong {
|
|
876 |
OfLong() { }
|
|
877 |
}
|
|
878 |
|
|
879 |
private static final class OfDouble
|
|
880 |
extends EmptySpliterator<Double, Spliterator.OfDouble, DoubleConsumer>
|
|
881 |
implements Spliterator.OfDouble {
|
|
882 |
OfDouble() { }
|
|
883 |
}
|
|
884 |
}
|
|
885 |
|
|
886 |
// Array-based spliterators
|
|
887 |
|
|
888 |
/**
|
|
889 |
* A Spliterator designed for use by sources that traverse and split
|
|
890 |
* elements maintained in an unmodifiable {@code Object[]} array.
|
|
891 |
*/
|
|
892 |
static final class ArraySpliterator<T> implements Spliterator<T> {
|
|
893 |
/**
|
|
894 |
* The array, explicitly typed as Object[]. Unlike in some other
|
|
895 |
* classes (see for example CR 6260652), we do not need to
|
|
896 |
* screen arguments to ensure they are exactly of type Object[]
|
|
897 |
* so long as no methods write into the array or serialize it,
|
|
898 |
* which we ensure here by defining this class as final.
|
|
899 |
*/
|
|
900 |
private final Object[] array;
|
|
901 |
private int index; // current index, modified on advance/split
|
|
902 |
private final int fence; // one past last index
|
|
903 |
private final int characteristics;
|
|
904 |
|
|
905 |
/**
|
|
906 |
* Creates a spliterator covering all of the given array.
|
|
907 |
* @param array the array, assumed to be unmodified during use
|
|
908 |
* @param additionalCharacteristics Additional spliterator characteristics
|
|
909 |
* of this spliterator's source or elements beyond {@code SIZED} and
|
|
910 |
* {@code SUBSIZED} which are are always reported
|
|
911 |
*/
|
|
912 |
public ArraySpliterator(Object[] array, int additionalCharacteristics) {
|
|
913 |
this(array, 0, array.length, additionalCharacteristics);
|
|
914 |
}
|
|
915 |
|
|
916 |
/**
|
|
917 |
* Creates a spliterator covering the given array and range
|
|
918 |
* @param array the array, assumed to be unmodified during use
|
|
919 |
* @param origin the least index (inclusive) to cover
|
|
920 |
* @param fence one past the greatest index to cover
|
|
921 |
* @param additionalCharacteristics Additional spliterator characteristics
|
|
922 |
* of this spliterator's source or elements beyond {@code SIZED} and
|
|
923 |
* {@code SUBSIZED} which are are always reported
|
|
924 |
*/
|
|
925 |
public ArraySpliterator(Object[] array, int origin, int fence, int additionalCharacteristics) {
|
|
926 |
this.array = array;
|
|
927 |
this.index = origin;
|
|
928 |
this.fence = fence;
|
|
929 |
this.characteristics = additionalCharacteristics | Spliterator.SIZED | Spliterator.SUBSIZED;
|
|
930 |
}
|
|
931 |
|
|
932 |
@Override
|
|
933 |
public Spliterator<T> trySplit() {
|
|
934 |
int lo = index, mid = (lo + fence) >>> 1;
|
|
935 |
return (lo >= mid)
|
|
936 |
? null
|
|
937 |
: new ArraySpliterator<>(array, lo, index = mid, characteristics);
|
|
938 |
}
|
|
939 |
|
|
940 |
@SuppressWarnings("unchecked")
|
|
941 |
@Override
|
|
942 |
public void forEachRemaining(Consumer<? super T> action) {
|
|
943 |
Object[] a; int i, hi; // hoist accesses and checks from loop
|
|
944 |
if (action == null)
|
|
945 |
throw new NullPointerException();
|
|
946 |
if ((a = array).length >= (hi = fence) &&
|
|
947 |
(i = index) >= 0 && i < (index = hi)) {
|
|
948 |
do { action.accept((T)a[i]); } while (++i < hi);
|
|
949 |
}
|
|
950 |
}
|
|
951 |
|
|
952 |
@Override
|
|
953 |
public boolean tryAdvance(Consumer<? super T> action) {
|
|
954 |
if (action == null)
|
|
955 |
throw new NullPointerException();
|
|
956 |
if (index >= 0 && index < fence) {
|
|
957 |
@SuppressWarnings("unchecked") T e = (T) array[index++];
|
|
958 |
action.accept(e);
|
|
959 |
return true;
|
|
960 |
}
|
|
961 |
return false;
|
|
962 |
}
|
|
963 |
|
|
964 |
@Override
|
|
965 |
public long estimateSize() { return (long)(fence - index); }
|
|
966 |
|
|
967 |
@Override
|
|
968 |
public int characteristics() {
|
|
969 |
return characteristics;
|
|
970 |
}
|
|
971 |
|
|
972 |
@Override
|
|
973 |
public Comparator<? super T> getComparator() {
|
|
974 |
if (hasCharacteristics(Spliterator.SORTED))
|
|
975 |
return null;
|
|
976 |
throw new IllegalStateException();
|
|
977 |
}
|
|
978 |
}
|
|
979 |
|
|
980 |
/**
|
|
981 |
* A Spliterator.OfInt designed for use by sources that traverse and split
|
|
982 |
* elements maintained in an unmodifiable {@code int[]} array.
|
|
983 |
*/
|
|
984 |
static final class IntArraySpliterator implements Spliterator.OfInt {
|
|
985 |
private final int[] array;
|
|
986 |
private int index; // current index, modified on advance/split
|
|
987 |
private final int fence; // one past last index
|
|
988 |
private final int characteristics;
|
|
989 |
|
|
990 |
/**
|
|
991 |
* Creates a spliterator covering all of the given array.
|
|
992 |
* @param array the array, assumed to be unmodified during use
|
|
993 |
* @param additionalCharacteristics Additional spliterator characteristics
|
|
994 |
* of this spliterator's source or elements beyond {@code SIZED} and
|
|
995 |
* {@code SUBSIZED} which are are always reported
|
|
996 |
*/
|
|
997 |
public IntArraySpliterator(int[] array, int additionalCharacteristics) {
|
|
998 |
this(array, 0, array.length, additionalCharacteristics);
|
|
999 |
}
|
|
1000 |
|
|
1001 |
/**
|
|
1002 |
* Creates a spliterator covering the given array and range
|
|
1003 |
* @param array the array, assumed to be unmodified during use
|
|
1004 |
* @param origin the least index (inclusive) to cover
|
|
1005 |
* @param fence one past the greatest index to cover
|
|
1006 |
* @param additionalCharacteristics Additional spliterator characteristics
|
|
1007 |
* of this spliterator's source or elements beyond {@code SIZED} and
|
|
1008 |
* {@code SUBSIZED} which are are always reported
|
|
1009 |
*/
|
|
1010 |
public IntArraySpliterator(int[] array, int origin, int fence, int additionalCharacteristics) {
|
|
1011 |
this.array = array;
|
|
1012 |
this.index = origin;
|
|
1013 |
this.fence = fence;
|
|
1014 |
this.characteristics = additionalCharacteristics | Spliterator.SIZED | Spliterator.SUBSIZED;
|
|
1015 |
}
|
|
1016 |
|
|
1017 |
@Override
|
|
1018 |
public OfInt trySplit() {
|
|
1019 |
int lo = index, mid = (lo + fence) >>> 1;
|
|
1020 |
return (lo >= mid)
|
|
1021 |
? null
|
|
1022 |
: new IntArraySpliterator(array, lo, index = mid, characteristics);
|
|
1023 |
}
|
|
1024 |
|
|
1025 |
@Override
|
|
1026 |
public void forEachRemaining(IntConsumer action) {
|
|
1027 |
int[] a; int i, hi; // hoist accesses and checks from loop
|
|
1028 |
if (action == null)
|
|
1029 |
throw new NullPointerException();
|
|
1030 |
if ((a = array).length >= (hi = fence) &&
|
|
1031 |
(i = index) >= 0 && i < (index = hi)) {
|
|
1032 |
do { action.accept(a[i]); } while (++i < hi);
|
|
1033 |
}
|
|
1034 |
}
|
|
1035 |
|
|
1036 |
@Override
|
|
1037 |
public boolean tryAdvance(IntConsumer action) {
|
|
1038 |
if (action == null)
|
|
1039 |
throw new NullPointerException();
|
|
1040 |
if (index >= 0 && index < fence) {
|
|
1041 |
action.accept(array[index++]);
|
|
1042 |
return true;
|
|
1043 |
}
|
|
1044 |
return false;
|
|
1045 |
}
|
|
1046 |
|
|
1047 |
@Override
|
|
1048 |
public long estimateSize() { return (long)(fence - index); }
|
|
1049 |
|
|
1050 |
@Override
|
|
1051 |
public int characteristics() {
|
|
1052 |
return characteristics;
|
|
1053 |
}
|
|
1054 |
|
|
1055 |
@Override
|
|
1056 |
public Comparator<? super Integer> getComparator() {
|
|
1057 |
if (hasCharacteristics(Spliterator.SORTED))
|
|
1058 |
return null;
|
|
1059 |
throw new IllegalStateException();
|
|
1060 |
}
|
|
1061 |
}
|
|
1062 |
|
|
1063 |
/**
|
|
1064 |
* A Spliterator.OfLong designed for use by sources that traverse and split
|
|
1065 |
* elements maintained in an unmodifiable {@code int[]} array.
|
|
1066 |
*/
|
|
1067 |
static final class LongArraySpliterator implements Spliterator.OfLong {
|
|
1068 |
private final long[] array;
|
|
1069 |
private int index; // current index, modified on advance/split
|
|
1070 |
private final int fence; // one past last index
|
|
1071 |
private final int characteristics;
|
|
1072 |
|
|
1073 |
/**
|
|
1074 |
* Creates a spliterator covering all of the given array.
|
|
1075 |
* @param array the array, assumed to be unmodified during use
|
|
1076 |
* @param additionalCharacteristics Additional spliterator characteristics
|
|
1077 |
* of this spliterator's source or elements beyond {@code SIZED} and
|
|
1078 |
* {@code SUBSIZED} which are are always reported
|
|
1079 |
*/
|
|
1080 |
public LongArraySpliterator(long[] array, int additionalCharacteristics) {
|
|
1081 |
this(array, 0, array.length, additionalCharacteristics);
|
|
1082 |
}
|
|
1083 |
|
|
1084 |
/**
|
|
1085 |
* Creates a spliterator covering the given array and range
|
|
1086 |
* @param array the array, assumed to be unmodified during use
|
|
1087 |
* @param origin the least index (inclusive) to cover
|
|
1088 |
* @param fence one past the greatest index to cover
|
|
1089 |
* @param additionalCharacteristics Additional spliterator characteristics
|
|
1090 |
* of this spliterator's source or elements beyond {@code SIZED} and
|
|
1091 |
* {@code SUBSIZED} which are are always reported
|
|
1092 |
*/
|
|
1093 |
public LongArraySpliterator(long[] array, int origin, int fence, int additionalCharacteristics) {
|
|
1094 |
this.array = array;
|
|
1095 |
this.index = origin;
|
|
1096 |
this.fence = fence;
|
|
1097 |
this.characteristics = additionalCharacteristics | Spliterator.SIZED | Spliterator.SUBSIZED;
|
|
1098 |
}
|
|
1099 |
|
|
1100 |
@Override
|
|
1101 |
public OfLong trySplit() {
|
|
1102 |
int lo = index, mid = (lo + fence) >>> 1;
|
|
1103 |
return (lo >= mid)
|
|
1104 |
? null
|
|
1105 |
: new LongArraySpliterator(array, lo, index = mid, characteristics);
|
|
1106 |
}
|
|
1107 |
|
|
1108 |
@Override
|
|
1109 |
public void forEachRemaining(LongConsumer action) {
|
|
1110 |
long[] a; int i, hi; // hoist accesses and checks from loop
|
|
1111 |
if (action == null)
|
|
1112 |
throw new NullPointerException();
|
|
1113 |
if ((a = array).length >= (hi = fence) &&
|
|
1114 |
(i = index) >= 0 && i < (index = hi)) {
|
|
1115 |
do { action.accept(a[i]); } while (++i < hi);
|
|
1116 |
}
|
|
1117 |
}
|
|
1118 |
|
|
1119 |
@Override
|
|
1120 |
public boolean tryAdvance(LongConsumer action) {
|
|
1121 |
if (action == null)
|
|
1122 |
throw new NullPointerException();
|
|
1123 |
if (index >= 0 && index < fence) {
|
|
1124 |
action.accept(array[index++]);
|
|
1125 |
return true;
|
|
1126 |
}
|
|
1127 |
return false;
|
|
1128 |
}
|
|
1129 |
|
|
1130 |
@Override
|
|
1131 |
public long estimateSize() { return (long)(fence - index); }
|
|
1132 |
|
|
1133 |
@Override
|
|
1134 |
public int characteristics() {
|
|
1135 |
return characteristics;
|
|
1136 |
}
|
|
1137 |
|
|
1138 |
@Override
|
|
1139 |
public Comparator<? super Long> getComparator() {
|
|
1140 |
if (hasCharacteristics(Spliterator.SORTED))
|
|
1141 |
return null;
|
|
1142 |
throw new IllegalStateException();
|
|
1143 |
}
|
|
1144 |
}
|
|
1145 |
|
|
1146 |
/**
|
|
1147 |
* A Spliterator.OfDouble designed for use by sources that traverse and split
|
|
1148 |
* elements maintained in an unmodifiable {@code int[]} array.
|
|
1149 |
*/
|
|
1150 |
static final class DoubleArraySpliterator implements Spliterator.OfDouble {
|
|
1151 |
private final double[] array;
|
|
1152 |
private int index; // current index, modified on advance/split
|
|
1153 |
private final int fence; // one past last index
|
|
1154 |
private final int characteristics;
|
|
1155 |
|
|
1156 |
/**
|
|
1157 |
* Creates a spliterator covering all of the given array.
|
|
1158 |
* @param array the array, assumed to be unmodified during use
|
|
1159 |
* @param additionalCharacteristics Additional spliterator characteristics
|
|
1160 |
* of this spliterator's source or elements beyond {@code SIZED} and
|
|
1161 |
* {@code SUBSIZED} which are are always reported
|
|
1162 |
*/
|
|
1163 |
public DoubleArraySpliterator(double[] array, int additionalCharacteristics) {
|
|
1164 |
this(array, 0, array.length, additionalCharacteristics);
|
|
1165 |
}
|
|
1166 |
|
|
1167 |
/**
|
|
1168 |
* Creates a spliterator covering the given array and range
|
|
1169 |
* @param array the array, assumed to be unmodified during use
|
|
1170 |
* @param origin the least index (inclusive) to cover
|
|
1171 |
* @param fence one past the greatest index to cover
|
|
1172 |
* @param additionalCharacteristics Additional spliterator characteristics
|
|
1173 |
* of this spliterator's source or elements beyond {@code SIZED} and
|
|
1174 |
* {@code SUBSIZED} which are are always reported
|
|
1175 |
*/
|
|
1176 |
public DoubleArraySpliterator(double[] array, int origin, int fence, int additionalCharacteristics) {
|
|
1177 |
this.array = array;
|
|
1178 |
this.index = origin;
|
|
1179 |
this.fence = fence;
|
|
1180 |
this.characteristics = additionalCharacteristics | Spliterator.SIZED | Spliterator.SUBSIZED;
|
|
1181 |
}
|
|
1182 |
|
|
1183 |
@Override
|
|
1184 |
public OfDouble trySplit() {
|
|
1185 |
int lo = index, mid = (lo + fence) >>> 1;
|
|
1186 |
return (lo >= mid)
|
|
1187 |
? null
|
|
1188 |
: new DoubleArraySpliterator(array, lo, index = mid, characteristics);
|
|
1189 |
}
|
|
1190 |
|
|
1191 |
@Override
|
|
1192 |
public void forEachRemaining(DoubleConsumer action) {
|
|
1193 |
double[] a; int i, hi; // hoist accesses and checks from loop
|
|
1194 |
if (action == null)
|
|
1195 |
throw new NullPointerException();
|
|
1196 |
if ((a = array).length >= (hi = fence) &&
|
|
1197 |
(i = index) >= 0 && i < (index = hi)) {
|
|
1198 |
do { action.accept(a[i]); } while (++i < hi);
|
|
1199 |
}
|
|
1200 |
}
|
|
1201 |
|
|
1202 |
@Override
|
|
1203 |
public boolean tryAdvance(DoubleConsumer action) {
|
|
1204 |
if (action == null)
|
|
1205 |
throw new NullPointerException();
|
|
1206 |
if (index >= 0 && index < fence) {
|
|
1207 |
action.accept(array[index++]);
|
|
1208 |
return true;
|
|
1209 |
}
|
|
1210 |
return false;
|
|
1211 |
}
|
|
1212 |
|
|
1213 |
@Override
|
|
1214 |
public long estimateSize() { return (long)(fence - index); }
|
|
1215 |
|
|
1216 |
@Override
|
|
1217 |
public int characteristics() {
|
|
1218 |
return characteristics;
|
|
1219 |
}
|
|
1220 |
|
|
1221 |
@Override
|
|
1222 |
public Comparator<? super Double> getComparator() {
|
|
1223 |
if (hasCharacteristics(Spliterator.SORTED))
|
|
1224 |
return null;
|
|
1225 |
throw new IllegalStateException();
|
|
1226 |
}
|
|
1227 |
}
|
|
1228 |
|
|
1229 |
//
|
|
1230 |
|
|
1231 |
/**
|
|
1232 |
* An abstract {@code Spliterator} that implements {@code trySplit} to
|
|
1233 |
* permit limited parallelism.
|
|
1234 |
*
|
|
1235 |
* <p>An extending class need only
|
|
1236 |
* implement {@link #tryAdvance(java.util.function.Consumer) tryAdvance}.
|
|
1237 |
* The extending class should override
|
|
1238 |
* {@link #forEachRemaining(java.util.function.Consumer) forEach} if it can
|
|
1239 |
* provide a more performant implementation.
|
|
1240 |
*
|
|
1241 |
* @apiNote
|
|
1242 |
* This class is a useful aid for creating a spliterator when it is not
|
|
1243 |
* possible or difficult to efficiently partition elements in a manner
|
|
1244 |
* allowing balanced parallel computation.
|
|
1245 |
*
|
|
1246 |
* <p>An alternative to using this class, that also permits limited
|
|
1247 |
* parallelism, is to create a spliterator from an iterator
|
|
1248 |
* (see {@link #spliterator(Iterator, long, int)}. Depending on the
|
|
1249 |
* circumstances using an iterator may be easier or more convenient than
|
|
1250 |
* extending this class, such as when there is already an iterator
|
|
1251 |
* available to use.
|
|
1252 |
*
|
|
1253 |
* @see #spliterator(Iterator, long, int)
|
|
1254 |
* @since 1.8
|
|
1255 |
*/
|
|
1256 |
public static abstract class AbstractSpliterator<T> implements Spliterator<T> {
|
|
1257 |
static final int BATCH_UNIT = 1 << 10; // batch array size increment
|
|
1258 |
static final int MAX_BATCH = 1 << 25; // max batch array size;
|
|
1259 |
private final int characteristics;
|
|
1260 |
private long est; // size estimate
|
|
1261 |
private int batch; // batch size for splits
|
|
1262 |
|
|
1263 |
/**
|
|
1264 |
* Creates a spliterator reporting the given estimated size and
|
|
1265 |
* additionalCharacteristics.
|
|
1266 |
*
|
|
1267 |
* @param est the estimated size of this spliterator if known, otherwise
|
|
1268 |
* {@code Long.MAX_VALUE}.
|
|
1269 |
* @param additionalCharacteristics properties of this spliterator's
|
|
1270 |
* source or elements. If {@code SIZED} is reported then this
|
|
1271 |
* spliterator will additionally report {@code SUBSIZED}.
|
|
1272 |
*/
|
|
1273 |
protected AbstractSpliterator(long est, int additionalCharacteristics) {
|
|
1274 |
this.est = est;
|
|
1275 |
this.characteristics = ((additionalCharacteristics & Spliterator.SIZED) != 0)
|
|
1276 |
? additionalCharacteristics | Spliterator.SUBSIZED
|
|
1277 |
: additionalCharacteristics;
|
|
1278 |
}
|
|
1279 |
|
|
1280 |
static final class HoldingConsumer<T> implements Consumer<T> {
|
|
1281 |
Object value;
|
|
1282 |
|
|
1283 |
@Override
|
|
1284 |
public void accept(T value) {
|
|
1285 |
this.value = value;
|
|
1286 |
}
|
|
1287 |
}
|
|
1288 |
|
|
1289 |
/**
|
|
1290 |
* {@inheritDoc}
|
|
1291 |
*
|
|
1292 |
* This implementation permits limited parallelism.
|
|
1293 |
*/
|
|
1294 |
@Override
|
|
1295 |
public Spliterator<T> trySplit() {
|
|
1296 |
/*
|
|
1297 |
* Split into arrays of arithmetically increasing batch
|
|
1298 |
* sizes. This will only improve parallel performance if
|
|
1299 |
* per-element Consumer actions are more costly than
|
|
1300 |
* transferring them into an array. The use of an
|
|
1301 |
* arithmetic progression in split sizes provides overhead
|
|
1302 |
* vs parallelism bounds that do not particularly favor or
|
|
1303 |
* penalize cases of lightweight vs heavyweight element
|
|
1304 |
* operations, across combinations of #elements vs #cores,
|
|
1305 |
* whether or not either are known. We generate
|
|
1306 |
* O(sqrt(#elements)) splits, allowing O(sqrt(#cores))
|
|
1307 |
* potential speedup.
|
|
1308 |
*/
|
|
1309 |
HoldingConsumer<T> holder = new HoldingConsumer<>();
|
|
1310 |
long s = est;
|
|
1311 |
if (s > 1 && tryAdvance(holder)) {
|
|
1312 |
int n = batch + BATCH_UNIT;
|
|
1313 |
if (n > s)
|
|
1314 |
n = (int) s;
|
|
1315 |
if (n > MAX_BATCH)
|
|
1316 |
n = MAX_BATCH;
|
|
1317 |
Object[] a;
|
|
1318 |
try {
|
|
1319 |
a = new Object[n];
|
|
1320 |
} catch (OutOfMemoryError oome) {
|
|
1321 |
return null;
|
|
1322 |
}
|
|
1323 |
int j = 0;
|
|
1324 |
do { a[j] = holder.value; } while (++j < n && tryAdvance(holder));
|
|
1325 |
batch = j;
|
|
1326 |
if (est != Long.MAX_VALUE)
|
|
1327 |
est -= j;
|
|
1328 |
return new ArraySpliterator<>(a, 0, j, characteristics());
|
|
1329 |
}
|
|
1330 |
return null;
|
|
1331 |
}
|
|
1332 |
|
|
1333 |
/**
|
|
1334 |
* {@inheritDoc}
|
|
1335 |
*
|
|
1336 |
* @implSpec
|
|
1337 |
* This implementation returns the estimated size as reported when
|
|
1338 |
* created and, if the estimate size is known, decreases in size when
|
|
1339 |
* split.
|
|
1340 |
*/
|
|
1341 |
@Override
|
|
1342 |
public long estimateSize() {
|
|
1343 |
return est;
|
|
1344 |
}
|
|
1345 |
|
|
1346 |
/**
|
|
1347 |
* {@inheritDoc}
|
|
1348 |
*
|
|
1349 |
* @implSpec
|
|
1350 |
* This implementation returns the characteristics as reported when
|
|
1351 |
* created.
|
|
1352 |
*/
|
|
1353 |
@Override
|
|
1354 |
public int characteristics() {
|
|
1355 |
return characteristics;
|
|
1356 |
}
|
|
1357 |
}
|
|
1358 |
|
|
1359 |
/**
|
|
1360 |
* An abstract {@code Spliterator.OfInt} that implements {@code trySplit} to
|
|
1361 |
* permit limited parallelism.
|
|
1362 |
*
|
|
1363 |
* <p>To implement a spliterator an extending class need only
|
|
1364 |
* implement {@link #tryAdvance(java.util.function.IntConsumer)}
|
|
1365 |
* tryAdvance}. The extending class should override
|
|
1366 |
* {@link #forEachRemaining(java.util.function.IntConsumer)} forEach} if it
|
|
1367 |
* can provide a more performant implementation.
|
|
1368 |
*
|
|
1369 |
* @apiNote
|
|
1370 |
* This class is a useful aid for creating a spliterator when it is not
|
|
1371 |
* possible or difficult to efficiently partition elements in a manner
|
|
1372 |
* allowing balanced parallel computation.
|
|
1373 |
*
|
|
1374 |
* <p>An alternative to using this class, that also permits limited
|
|
1375 |
* parallelism, is to create a spliterator from an iterator
|
|
1376 |
* (see {@link #spliterator(java.util.PrimitiveIterator.OfInt, long, int)}.
|
|
1377 |
* Depending on the circumstances using an iterator may be easier or more
|
|
1378 |
* convenient than extending this class. For example, if there is already an
|
|
1379 |
* iterator available to use then there is no need to extend this class.
|
|
1380 |
*
|
|
1381 |
* @see #spliterator(java.util.PrimitiveIterator.OfInt, long, int)
|
|
1382 |
* @since 1.8
|
|
1383 |
*/
|
|
1384 |
public static abstract class AbstractIntSpliterator implements Spliterator.OfInt {
|
|
1385 |
static final int MAX_BATCH = AbstractSpliterator.MAX_BATCH;
|
|
1386 |
static final int BATCH_UNIT = AbstractSpliterator.BATCH_UNIT;
|
|
1387 |
private final int characteristics;
|
|
1388 |
private long est; // size estimate
|
|
1389 |
private int batch; // batch size for splits
|
|
1390 |
|
|
1391 |
/**
|
|
1392 |
* Creates a spliterator reporting the given estimated size and
|
|
1393 |
* characteristics.
|
|
1394 |
*
|
|
1395 |
* @param est the estimated size of this spliterator if known, otherwise
|
|
1396 |
* {@code Long.MAX_VALUE}.
|
|
1397 |
* @param additionalCharacteristics properties of this spliterator's
|
|
1398 |
* source or elements. If {@code SIZED} is reported then this
|
|
1399 |
* spliterator will additionally report {@code SUBSIZED}.
|
|
1400 |
*/
|
|
1401 |
protected AbstractIntSpliterator(long est, int additionalCharacteristics) {
|
|
1402 |
this.est = est;
|
|
1403 |
this.characteristics = ((additionalCharacteristics & Spliterator.SIZED) != 0)
|
|
1404 |
? additionalCharacteristics | Spliterator.SUBSIZED
|
|
1405 |
: additionalCharacteristics;
|
|
1406 |
}
|
|
1407 |
|
|
1408 |
static final class HoldingIntConsumer implements IntConsumer {
|
|
1409 |
int value;
|
|
1410 |
|
|
1411 |
@Override
|
|
1412 |
public void accept(int value) {
|
|
1413 |
this.value = value;
|
|
1414 |
}
|
|
1415 |
}
|
|
1416 |
|
|
1417 |
/**
|
|
1418 |
* {@inheritDoc}
|
|
1419 |
*
|
|
1420 |
* This implementation permits limited parallelism.
|
|
1421 |
*/
|
|
1422 |
@Override
|
|
1423 |
public Spliterator.OfInt trySplit() {
|
|
1424 |
HoldingIntConsumer holder = new HoldingIntConsumer();
|
|
1425 |
long s = est;
|
|
1426 |
if (s > 1 && tryAdvance(holder)) {
|
|
1427 |
int n = batch + BATCH_UNIT;
|
|
1428 |
if (n > s)
|
|
1429 |
n = (int) s;
|
|
1430 |
if (n > MAX_BATCH)
|
|
1431 |
n = MAX_BATCH;
|
|
1432 |
int[] a;
|
|
1433 |
try {
|
|
1434 |
a = new int[n];
|
|
1435 |
} catch (OutOfMemoryError oome) {
|
|
1436 |
return null;
|
|
1437 |
}
|
|
1438 |
int j = 0;
|
|
1439 |
do { a[j] = holder.value; } while (++j < n && tryAdvance(holder));
|
|
1440 |
batch = j;
|
|
1441 |
if (est != Long.MAX_VALUE)
|
|
1442 |
est -= j;
|
|
1443 |
return new IntArraySpliterator(a, 0, j, characteristics());
|
|
1444 |
}
|
|
1445 |
return null;
|
|
1446 |
}
|
|
1447 |
|
|
1448 |
/**
|
|
1449 |
* {@inheritDoc}
|
|
1450 |
*
|
|
1451 |
* @implSpec
|
|
1452 |
* This implementation returns the estimated size as reported when
|
|
1453 |
* created and, if the estimate size is known, decreases in size when
|
|
1454 |
* split.
|
|
1455 |
*/
|
|
1456 |
@Override
|
|
1457 |
public long estimateSize() {
|
|
1458 |
return est;
|
|
1459 |
}
|
|
1460 |
|
|
1461 |
/**
|
|
1462 |
* {@inheritDoc}
|
|
1463 |
*
|
|
1464 |
* @implSpec
|
|
1465 |
* This implementation returns the characteristics as reported when
|
|
1466 |
* created.
|
|
1467 |
*/
|
|
1468 |
@Override
|
|
1469 |
public int characteristics() {
|
|
1470 |
return characteristics;
|
|
1471 |
}
|
|
1472 |
}
|
|
1473 |
|
|
1474 |
/**
|
|
1475 |
* An abstract {@code Spliterator.OfLong} that implements {@code trySplit}
|
|
1476 |
* to permit limited parallelism.
|
|
1477 |
*
|
|
1478 |
* <p>To implement a spliterator an extending class need only
|
|
1479 |
* implement {@link #tryAdvance(java.util.function.LongConsumer)}
|
|
1480 |
* tryAdvance}. The extending class should override
|
|
1481 |
* {@link #forEachRemaining(java.util.function.LongConsumer)} forEach} if it
|
|
1482 |
* can provide a more performant implementation.
|
|
1483 |
*
|
|
1484 |
* @apiNote
|
|
1485 |
* This class is a useful aid for creating a spliterator when it is not
|
|
1486 |
* possible or difficult to efficiently partition elements in a manner
|
|
1487 |
* allowing balanced parallel computation.
|
|
1488 |
*
|
|
1489 |
* <p>An alternative to using this class, that also permits limited
|
|
1490 |
* parallelism, is to create a spliterator from an iterator
|
|
1491 |
* (see {@link #spliterator(java.util.PrimitiveIterator.OfLong, long, int)}.
|
|
1492 |
* Depending on the circumstances using an iterator may be easier or more
|
|
1493 |
* convenient than extending this class. For example, if there is already an
|
|
1494 |
* iterator available to use then there is no need to extend this class.
|
|
1495 |
*
|
|
1496 |
* @see #spliterator(java.util.PrimitiveIterator.OfLong, long, int)
|
|
1497 |
* @since 1.8
|
|
1498 |
*/
|
|
1499 |
public static abstract class AbstractLongSpliterator implements Spliterator.OfLong {
|
|
1500 |
static final int MAX_BATCH = AbstractSpliterator.MAX_BATCH;
|
|
1501 |
static final int BATCH_UNIT = AbstractSpliterator.BATCH_UNIT;
|
|
1502 |
private final int characteristics;
|
|
1503 |
private long est; // size estimate
|
|
1504 |
private int batch; // batch size for splits
|
|
1505 |
|
|
1506 |
/**
|
|
1507 |
* Creates a spliterator reporting the given estimated size and
|
|
1508 |
* characteristics.
|
|
1509 |
*
|
|
1510 |
* @param est the estimated size of this spliterator if known, otherwise
|
|
1511 |
* {@code Long.MAX_VALUE}.
|
|
1512 |
* @param additionalCharacteristics properties of this spliterator's
|
|
1513 |
* source or elements. If {@code SIZED} is reported then this
|
|
1514 |
* spliterator will additionally report {@code SUBSIZED}.
|
|
1515 |
*/
|
|
1516 |
protected AbstractLongSpliterator(long est, int additionalCharacteristics) {
|
|
1517 |
this.est = est;
|
|
1518 |
this.characteristics = ((additionalCharacteristics & Spliterator.SIZED) != 0)
|
|
1519 |
? additionalCharacteristics | Spliterator.SUBSIZED
|
|
1520 |
: additionalCharacteristics;
|
|
1521 |
}
|
|
1522 |
|
|
1523 |
static final class HoldingLongConsumer implements LongConsumer {
|
|
1524 |
long value;
|
|
1525 |
|
|
1526 |
@Override
|
|
1527 |
public void accept(long value) {
|
|
1528 |
this.value = value;
|
|
1529 |
}
|
|
1530 |
}
|
|
1531 |
|
|
1532 |
/**
|
|
1533 |
* {@inheritDoc}
|
|
1534 |
*
|
|
1535 |
* This implementation permits limited parallelism.
|
|
1536 |
*/
|
|
1537 |
@Override
|
|
1538 |
public Spliterator.OfLong trySplit() {
|
|
1539 |
HoldingLongConsumer holder = new HoldingLongConsumer();
|
|
1540 |
long s = est;
|
|
1541 |
if (s > 1 && tryAdvance(holder)) {
|
|
1542 |
int n = batch + BATCH_UNIT;
|
|
1543 |
if (n > s)
|
|
1544 |
n = (int) s;
|
|
1545 |
if (n > MAX_BATCH)
|
|
1546 |
n = MAX_BATCH;
|
|
1547 |
long[] a;
|
|
1548 |
try {
|
|
1549 |
a = new long[n];
|
|
1550 |
} catch (OutOfMemoryError oome) {
|
|
1551 |
return null;
|
|
1552 |
}
|
|
1553 |
int j = 0;
|
|
1554 |
do { a[j] = holder.value; } while (++j < n && tryAdvance(holder));
|
|
1555 |
batch = j;
|
|
1556 |
if (est != Long.MAX_VALUE)
|
|
1557 |
est -= j;
|
|
1558 |
return new LongArraySpliterator(a, 0, j, characteristics());
|
|
1559 |
}
|
|
1560 |
return null;
|
|
1561 |
}
|
|
1562 |
|
|
1563 |
/**
|
|
1564 |
* {@inheritDoc}
|
|
1565 |
*
|
|
1566 |
* @implSpec
|
|
1567 |
* This implementation returns the estimated size as reported when
|
|
1568 |
* created and, if the estimate size is known, decreases in size when
|
|
1569 |
* split.
|
|
1570 |
*/
|
|
1571 |
@Override
|
|
1572 |
public long estimateSize() {
|
|
1573 |
return est;
|
|
1574 |
}
|
|
1575 |
|
|
1576 |
/**
|
|
1577 |
* {@inheritDoc}
|
|
1578 |
*
|
|
1579 |
* @implSpec
|
|
1580 |
* This implementation returns the characteristics as reported when
|
|
1581 |
* created.
|
|
1582 |
*/
|
|
1583 |
@Override
|
|
1584 |
public int characteristics() {
|
|
1585 |
return characteristics;
|
|
1586 |
}
|
|
1587 |
}
|
|
1588 |
|
|
1589 |
/**
|
|
1590 |
* An abstract {@code Spliterator.OfDouble} that implements
|
|
1591 |
* {@code trySplit} to permit limited parallelism.
|
|
1592 |
*
|
|
1593 |
* <p>To implement a spliterator an extending class need only
|
|
1594 |
* implement {@link #tryAdvance(java.util.function.DoubleConsumer)}
|
|
1595 |
* tryAdvance}. The extending class should override
|
|
1596 |
* {@link #forEachRemaining(java.util.function.DoubleConsumer)} forEach} if
|
|
1597 |
* it can provide a more performant implementation.
|
|
1598 |
*
|
|
1599 |
* @apiNote
|
|
1600 |
* This class is a useful aid for creating a spliterator when it is not
|
|
1601 |
* possible or difficult to efficiently partition elements in a manner
|
|
1602 |
* allowing balanced parallel computation.
|
|
1603 |
*
|
|
1604 |
* <p>An alternative to using this class, that also permits limited
|
|
1605 |
* parallelism, is to create a spliterator from an iterator
|
|
1606 |
* (see {@link #spliterator(java.util.PrimitiveIterator.OfDouble, long, int)}.
|
|
1607 |
* Depending on the circumstances using an iterator may be easier or more
|
|
1608 |
* convenient than extending this class. For example, if there is already an
|
|
1609 |
* iterator available to use then there is no need to extend this class.
|
|
1610 |
*
|
|
1611 |
* @see #spliterator(java.util.PrimitiveIterator.OfDouble, long, int)
|
|
1612 |
* @since 1.8
|
|
1613 |
*/
|
|
1614 |
public static abstract class AbstractDoubleSpliterator implements Spliterator.OfDouble {
|
|
1615 |
static final int MAX_BATCH = AbstractSpliterator.MAX_BATCH;
|
|
1616 |
static final int BATCH_UNIT = AbstractSpliterator.BATCH_UNIT;
|
|
1617 |
private final int characteristics;
|
|
1618 |
private long est; // size estimate
|
|
1619 |
private int batch; // batch size for splits
|
|
1620 |
|
|
1621 |
/**
|
|
1622 |
* Creates a spliterator reporting the given estimated size and
|
|
1623 |
* characteristics.
|
|
1624 |
*
|
|
1625 |
* @param est the estimated size of this spliterator if known, otherwise
|
|
1626 |
* {@code Long.MAX_VALUE}.
|
|
1627 |
* @param additionalCharacteristics properties of this spliterator's
|
|
1628 |
* source or elements. If {@code SIZED} is reported then this
|
|
1629 |
* spliterator will additionally report {@code SUBSIZED}.
|
|
1630 |
*/
|
|
1631 |
protected AbstractDoubleSpliterator(long est, int additionalCharacteristics) {
|
|
1632 |
this.est = est;
|
|
1633 |
this.characteristics = ((additionalCharacteristics & Spliterator.SIZED) != 0)
|
|
1634 |
? additionalCharacteristics | Spliterator.SUBSIZED
|
|
1635 |
: additionalCharacteristics;
|
|
1636 |
}
|
|
1637 |
|
|
1638 |
static final class HoldingDoubleConsumer implements DoubleConsumer {
|
|
1639 |
double value;
|
|
1640 |
|
|
1641 |
@Override
|
|
1642 |
public void accept(double value) {
|
|
1643 |
this.value = value;
|
|
1644 |
}
|
|
1645 |
}
|
|
1646 |
|
|
1647 |
/**
|
|
1648 |
* {@inheritDoc}
|
|
1649 |
*
|
|
1650 |
* This implementation permits limited parallelism.
|
|
1651 |
*/
|
|
1652 |
@Override
|
|
1653 |
public Spliterator.OfDouble trySplit() {
|
|
1654 |
HoldingDoubleConsumer holder = new HoldingDoubleConsumer();
|
|
1655 |
long s = est;
|
|
1656 |
if (s > 1 && tryAdvance(holder)) {
|
|
1657 |
int n = batch + BATCH_UNIT;
|
|
1658 |
if (n > s)
|
|
1659 |
n = (int) s;
|
|
1660 |
if (n > MAX_BATCH)
|
|
1661 |
n = MAX_BATCH;
|
|
1662 |
double[] a;
|
|
1663 |
try {
|
|
1664 |
a = new double[n];
|
|
1665 |
} catch (OutOfMemoryError oome) {
|
|
1666 |
return null;
|
|
1667 |
}
|
|
1668 |
int j = 0;
|
|
1669 |
do { a[j] = holder.value; } while (++j < n && tryAdvance(holder));
|
|
1670 |
batch = j;
|
|
1671 |
if (est != Long.MAX_VALUE)
|
|
1672 |
est -= j;
|
|
1673 |
return new DoubleArraySpliterator(a, 0, j, characteristics());
|
|
1674 |
}
|
|
1675 |
return null;
|
|
1676 |
}
|
|
1677 |
|
|
1678 |
/**
|
|
1679 |
* {@inheritDoc}
|
|
1680 |
*
|
|
1681 |
* @implSpec
|
|
1682 |
* This implementation returns the estimated size as reported when
|
|
1683 |
* created and, if the estimate size is known, decreases in size when
|
|
1684 |
* split.
|
|
1685 |
*/
|
|
1686 |
@Override
|
|
1687 |
public long estimateSize() {
|
|
1688 |
return est;
|
|
1689 |
}
|
|
1690 |
|
|
1691 |
/**
|
|
1692 |
* {@inheritDoc}
|
|
1693 |
*
|
|
1694 |
* @implSpec
|
|
1695 |
* This implementation returns the characteristics as reported when
|
|
1696 |
* created.
|
|
1697 |
*/
|
|
1698 |
@Override
|
|
1699 |
public int characteristics() {
|
|
1700 |
return characteristics;
|
|
1701 |
}
|
|
1702 |
}
|
|
1703 |
|
|
1704 |
// Iterator-based Spliterators
|
|
1705 |
|
|
1706 |
/**
|
|
1707 |
* A Spliterator using a given Iterator for element
|
|
1708 |
* operations. The spliterator implements {@code trySplit} to
|
|
1709 |
* permit limited parallelism.
|
|
1710 |
*/
|
|
1711 |
static class IteratorSpliterator<T> implements Spliterator<T> {
|
|
1712 |
static final int BATCH_UNIT = 1 << 10; // batch array size increment
|
|
1713 |
static final int MAX_BATCH = 1 << 25; // max batch array size;
|
|
1714 |
private final Collection<? extends T> collection; // null OK
|
|
1715 |
private Iterator<? extends T> it;
|
|
1716 |
private final int characteristics;
|
|
1717 |
private long est; // size estimate
|
|
1718 |
private int batch; // batch size for splits
|
|
1719 |
|
|
1720 |
/**
|
|
1721 |
* Creates a spliterator using the given given
|
|
1722 |
* collection's {@link java.util.Collection#iterator()) for traversal,
|
|
1723 |
* and reporting its {@link java.util.Collection#size()) as its initial
|
|
1724 |
* size.
|
|
1725 |
*
|
|
1726 |
* @param c the collection
|
|
1727 |
* @param characteristics properties of this spliterator's
|
|
1728 |
* source or elements.
|
|
1729 |
*/
|
|
1730 |
public IteratorSpliterator(Collection<? extends T> collection, int characteristics) {
|
|
1731 |
this.collection = collection;
|
|
1732 |
this.it = null;
|
|
1733 |
this.characteristics = characteristics | Spliterator.SIZED | Spliterator.SUBSIZED;
|
|
1734 |
}
|
|
1735 |
|
|
1736 |
/**
|
|
1737 |
* Creates a spliterator using the given iterator
|
|
1738 |
* for traversal, and reporting the given initial size
|
|
1739 |
* and characteristics.
|
|
1740 |
*
|
|
1741 |
* @param iterator the iterator for the source
|
|
1742 |
* @param size the number of elements in the source
|
|
1743 |
* @param characteristics properties of this spliterator's
|
|
1744 |
* source or elements.
|
|
1745 |
*/
|
|
1746 |
public IteratorSpliterator(Iterator<? extends T> iterator, long size, int characteristics) {
|
|
1747 |
this.collection = null;
|
|
1748 |
this.it = iterator;
|
|
1749 |
this.est = size;
|
|
1750 |
this.characteristics = characteristics | Spliterator.SIZED | Spliterator.SUBSIZED;
|
|
1751 |
}
|
|
1752 |
|
|
1753 |
/**
|
|
1754 |
* Creates a spliterator using the given iterator
|
|
1755 |
* for traversal, and reporting the given initial size
|
|
1756 |
* and characteristics.
|
|
1757 |
*
|
|
1758 |
* @param iterator the iterator for the source
|
|
1759 |
* @param characteristics properties of this spliterator's
|
|
1760 |
* source or elements.
|
|
1761 |
*/
|
|
1762 |
public IteratorSpliterator(Iterator<? extends T> iterator, int characteristics) {
|
|
1763 |
this.collection = null;
|
|
1764 |
this.it = iterator;
|
|
1765 |
this.est = Long.MAX_VALUE;
|
|
1766 |
this.characteristics = characteristics & ~(Spliterator.SIZED | Spliterator.SUBSIZED);
|
|
1767 |
}
|
|
1768 |
|
|
1769 |
@Override
|
|
1770 |
public Spliterator<T> trySplit() {
|
|
1771 |
/*
|
|
1772 |
* Split into arrays of arithmetically increasing batch
|
|
1773 |
* sizes. This will only improve parallel performance if
|
|
1774 |
* per-element Consumer actions are more costly than
|
|
1775 |
* transferring them into an array. The use of an
|
|
1776 |
* arithmetic progression in split sizes provides overhead
|
|
1777 |
* vs parallelism bounds that do not particularly favor or
|
|
1778 |
* penalize cases of lightweight vs heavyweight element
|
|
1779 |
* operations, across combinations of #elements vs #cores,
|
|
1780 |
* whether or not either are known. We generate
|
|
1781 |
* O(sqrt(#elements)) splits, allowing O(sqrt(#cores))
|
|
1782 |
* potential speedup.
|
|
1783 |
*/
|
|
1784 |
Iterator<? extends T> i;
|
|
1785 |
long s;
|
|
1786 |
if ((i = it) == null) {
|
|
1787 |
i = it = collection.iterator();
|
|
1788 |
s = est = (long) collection.size();
|
|
1789 |
}
|
|
1790 |
else
|
|
1791 |
s = est;
|
|
1792 |
if (s > 1 && i.hasNext()) {
|
|
1793 |
int n = batch + BATCH_UNIT;
|
|
1794 |
if (n > s)
|
|
1795 |
n = (int) s;
|
|
1796 |
if (n > MAX_BATCH)
|
|
1797 |
n = MAX_BATCH;
|
|
1798 |
Object[] a;
|
|
1799 |
try {
|
|
1800 |
a = new Object[n];
|
|
1801 |
} catch (OutOfMemoryError oome) {
|
|
1802 |
return null;
|
|
1803 |
}
|
|
1804 |
int j = 0;
|
|
1805 |
do { a[j] = i.next(); } while (++j < n && i.hasNext());
|
|
1806 |
batch = j;
|
|
1807 |
if (est != Long.MAX_VALUE)
|
|
1808 |
est -= j;
|
|
1809 |
return new ArraySpliterator<>(a, 0, j, characteristics);
|
|
1810 |
}
|
|
1811 |
return null;
|
|
1812 |
}
|
|
1813 |
|
|
1814 |
@Override
|
|
1815 |
public void forEachRemaining(Consumer<? super T> action) {
|
|
1816 |
if (action == null) throw new NullPointerException();
|
|
1817 |
Iterator<? extends T> i;
|
|
1818 |
if ((i = it) == null) {
|
|
1819 |
i = it = collection.iterator();
|
|
1820 |
est = (long)collection.size();
|
|
1821 |
}
|
|
1822 |
i.forEachRemaining(action);
|
|
1823 |
}
|
|
1824 |
|
|
1825 |
@Override
|
|
1826 |
public boolean tryAdvance(Consumer<? super T> action) {
|
|
1827 |
if (action == null) throw new NullPointerException();
|
|
1828 |
if (it == null) {
|
|
1829 |
it = collection.iterator();
|
|
1830 |
est = (long) collection.size();
|
|
1831 |
}
|
|
1832 |
if (it.hasNext()) {
|
|
1833 |
action.accept(it.next());
|
|
1834 |
return true;
|
|
1835 |
}
|
|
1836 |
return false;
|
|
1837 |
}
|
|
1838 |
|
|
1839 |
@Override
|
|
1840 |
public long estimateSize() {
|
|
1841 |
if (it == null) {
|
|
1842 |
it = collection.iterator();
|
|
1843 |
return est = (long)collection.size();
|
|
1844 |
}
|
|
1845 |
return est;
|
|
1846 |
}
|
|
1847 |
|
|
1848 |
@Override
|
|
1849 |
public int characteristics() { return characteristics; }
|
|
1850 |
|
|
1851 |
@Override
|
|
1852 |
public Comparator<? super T> getComparator() {
|
|
1853 |
if (hasCharacteristics(Spliterator.SORTED))
|
|
1854 |
return null;
|
|
1855 |
throw new IllegalStateException();
|
|
1856 |
}
|
|
1857 |
}
|
|
1858 |
|
|
1859 |
/**
|
|
1860 |
* A Spliterator.OfInt using a given IntStream.IntIterator for element
|
|
1861 |
* operations. The spliterator implements {@code trySplit} to
|
|
1862 |
* permit limited parallelism.
|
|
1863 |
*/
|
|
1864 |
static final class IntIteratorSpliterator implements Spliterator.OfInt {
|
|
1865 |
static final int BATCH_UNIT = IteratorSpliterator.BATCH_UNIT;
|
|
1866 |
static final int MAX_BATCH = IteratorSpliterator.MAX_BATCH;
|
|
1867 |
private PrimitiveIterator.OfInt it;
|
|
1868 |
private final int characteristics;
|
|
1869 |
private long est; // size estimate
|
|
1870 |
private int batch; // batch size for splits
|
|
1871 |
|
|
1872 |
/**
|
|
1873 |
* Creates a spliterator using the given iterator
|
|
1874 |
* for traversal, and reporting the given initial size
|
|
1875 |
* and characteristics.
|
|
1876 |
*
|
|
1877 |
* @param iterator the iterator for the source
|
|
1878 |
* @param size the number of elements in the source
|
|
1879 |
* @param characteristics properties of this spliterator's
|
|
1880 |
* source or elements.
|
|
1881 |
*/
|
|
1882 |
public IntIteratorSpliterator(PrimitiveIterator.OfInt iterator, long size, int characteristics) {
|
|
1883 |
this.it = iterator;
|
|
1884 |
this.est = size;
|
|
1885 |
this.characteristics = characteristics | Spliterator.SIZED | Spliterator.SUBSIZED;
|
|
1886 |
}
|
|
1887 |
|
|
1888 |
/**
|
|
1889 |
* Creates a spliterator using the given iterator for a
|
|
1890 |
* source of unknown size, reporting the given
|
|
1891 |
* characteristics.
|
|
1892 |
*
|
|
1893 |
* @param iterator the iterator for the source
|
|
1894 |
* @param characteristics properties of this spliterator's
|
|
1895 |
* source or elements.
|
|
1896 |
*/
|
|
1897 |
public IntIteratorSpliterator(PrimitiveIterator.OfInt iterator, int characteristics) {
|
|
1898 |
this.it = iterator;
|
|
1899 |
this.est = Long.MAX_VALUE;
|
|
1900 |
this.characteristics = characteristics & ~(Spliterator.SIZED | Spliterator.SUBSIZED);
|
|
1901 |
}
|
|
1902 |
|
|
1903 |
@Override
|
|
1904 |
public OfInt trySplit() {
|
|
1905 |
PrimitiveIterator.OfInt i = it;
|
|
1906 |
long s = est;
|
|
1907 |
if (s > 1 && i.hasNext()) {
|
|
1908 |
int n = batch + BATCH_UNIT;
|
|
1909 |
if (n > s)
|
|
1910 |
n = (int) s;
|
|
1911 |
if (n > MAX_BATCH)
|
|
1912 |
n = MAX_BATCH;
|
|
1913 |
int[] a;
|
|
1914 |
try {
|
|
1915 |
a = new int[n];
|
|
1916 |
} catch (OutOfMemoryError oome) {
|
|
1917 |
return null;
|
|
1918 |
}
|
|
1919 |
int j = 0;
|
|
1920 |
do { a[j] = i.nextInt(); } while (++j < n && i.hasNext());
|
|
1921 |
batch = j;
|
|
1922 |
if (est != Long.MAX_VALUE)
|
|
1923 |
est -= j;
|
|
1924 |
return new IntArraySpliterator(a, 0, j, characteristics);
|
|
1925 |
}
|
|
1926 |
return null;
|
|
1927 |
}
|
|
1928 |
|
|
1929 |
@Override
|
|
1930 |
public void forEachRemaining(IntConsumer action) {
|
|
1931 |
if (action == null) throw new NullPointerException();
|
|
1932 |
it.forEachRemaining(action);
|
|
1933 |
}
|
|
1934 |
|
|
1935 |
@Override
|
|
1936 |
public boolean tryAdvance(IntConsumer action) {
|
|
1937 |
if (action == null) throw new NullPointerException();
|
|
1938 |
if (it.hasNext()) {
|
|
1939 |
action.accept(it.nextInt());
|
|
1940 |
return true;
|
|
1941 |
}
|
|
1942 |
return false;
|
|
1943 |
}
|
|
1944 |
|
|
1945 |
@Override
|
|
1946 |
public long estimateSize() {
|
|
1947 |
return est;
|
|
1948 |
}
|
|
1949 |
|
|
1950 |
@Override
|
|
1951 |
public int characteristics() { return characteristics; }
|
|
1952 |
|
|
1953 |
@Override
|
|
1954 |
public Comparator<? super Integer> getComparator() {
|
|
1955 |
if (hasCharacteristics(Spliterator.SORTED))
|
|
1956 |
return null;
|
|
1957 |
throw new IllegalStateException();
|
|
1958 |
}
|
|
1959 |
}
|
|
1960 |
|
|
1961 |
static final class LongIteratorSpliterator implements Spliterator.OfLong {
|
|
1962 |
static final int BATCH_UNIT = IteratorSpliterator.BATCH_UNIT;
|
|
1963 |
static final int MAX_BATCH = IteratorSpliterator.MAX_BATCH;
|
|
1964 |
private PrimitiveIterator.OfLong it;
|
|
1965 |
private final int characteristics;
|
|
1966 |
private long est; // size estimate
|
|
1967 |
private int batch; // batch size for splits
|
|
1968 |
|
|
1969 |
/**
|
|
1970 |
* Creates a spliterator using the given iterator
|
|
1971 |
* for traversal, and reporting the given initial size
|
|
1972 |
* and characteristics.
|
|
1973 |
*
|
|
1974 |
* @param iterator the iterator for the source
|
|
1975 |
* @param size the number of elements in the source
|
|
1976 |
* @param characteristics properties of this spliterator's
|
|
1977 |
* source or elements.
|
|
1978 |
*/
|
|
1979 |
public LongIteratorSpliterator(PrimitiveIterator.OfLong iterator, long size, int characteristics) {
|
|
1980 |
this.it = iterator;
|
|
1981 |
this.est = size;
|
|
1982 |
this.characteristics = characteristics | Spliterator.SIZED | Spliterator.SUBSIZED;
|
|
1983 |
}
|
|
1984 |
|
|
1985 |
/**
|
|
1986 |
* Creates a spliterator using the given iterator for a
|
|
1987 |
* source of unknown size, reporting the given
|
|
1988 |
* characteristics.
|
|
1989 |
*
|
|
1990 |
* @param iterator the iterator for the source
|
|
1991 |
* @param characteristics properties of this spliterator's
|
|
1992 |
* source or elements.
|
|
1993 |
*/
|
|
1994 |
public LongIteratorSpliterator(PrimitiveIterator.OfLong iterator, int characteristics) {
|
|
1995 |
this.it = iterator;
|
|
1996 |
this.est = Long.MAX_VALUE;
|
|
1997 |
this.characteristics = characteristics & ~(Spliterator.SIZED | Spliterator.SUBSIZED);
|
|
1998 |
}
|
|
1999 |
|
|
2000 |
@Override
|
|
2001 |
public OfLong trySplit() {
|
|
2002 |
PrimitiveIterator.OfLong i = it;
|
|
2003 |
long s = est;
|
|
2004 |
if (s > 1 && i.hasNext()) {
|
|
2005 |
int n = batch + BATCH_UNIT;
|
|
2006 |
if (n > s)
|
|
2007 |
n = (int) s;
|
|
2008 |
if (n > MAX_BATCH)
|
|
2009 |
n = MAX_BATCH;
|
|
2010 |
long[] a;
|
|
2011 |
try {
|
|
2012 |
a = new long[n];
|
|
2013 |
} catch (OutOfMemoryError oome) {
|
|
2014 |
return null;
|
|
2015 |
}
|
|
2016 |
int j = 0;
|
|
2017 |
do { a[j] = i.nextLong(); } while (++j < n && i.hasNext());
|
|
2018 |
batch = j;
|
|
2019 |
if (est != Long.MAX_VALUE)
|
|
2020 |
est -= j;
|
|
2021 |
return new LongArraySpliterator(a, 0, j, characteristics);
|
|
2022 |
}
|
|
2023 |
return null;
|
|
2024 |
}
|
|
2025 |
|
|
2026 |
@Override
|
|
2027 |
public void forEachRemaining(LongConsumer action) {
|
|
2028 |
if (action == null) throw new NullPointerException();
|
|
2029 |
it.forEachRemaining(action);
|
|
2030 |
}
|
|
2031 |
|
|
2032 |
@Override
|
|
2033 |
public boolean tryAdvance(LongConsumer action) {
|
|
2034 |
if (action == null) throw new NullPointerException();
|
|
2035 |
if (it.hasNext()) {
|
|
2036 |
action.accept(it.nextLong());
|
|
2037 |
return true;
|
|
2038 |
}
|
|
2039 |
return false;
|
|
2040 |
}
|
|
2041 |
|
|
2042 |
@Override
|
|
2043 |
public long estimateSize() {
|
|
2044 |
return est;
|
|
2045 |
}
|
|
2046 |
|
|
2047 |
@Override
|
|
2048 |
public int characteristics() { return characteristics; }
|
|
2049 |
|
|
2050 |
@Override
|
|
2051 |
public Comparator<? super Long> getComparator() {
|
|
2052 |
if (hasCharacteristics(Spliterator.SORTED))
|
|
2053 |
return null;
|
|
2054 |
throw new IllegalStateException();
|
|
2055 |
}
|
|
2056 |
}
|
|
2057 |
|
|
2058 |
static final class DoubleIteratorSpliterator implements Spliterator.OfDouble {
|
|
2059 |
static final int BATCH_UNIT = IteratorSpliterator.BATCH_UNIT;
|
|
2060 |
static final int MAX_BATCH = IteratorSpliterator.MAX_BATCH;
|
|
2061 |
private PrimitiveIterator.OfDouble it;
|
|
2062 |
private final int characteristics;
|
|
2063 |
private long est; // size estimate
|
|
2064 |
private int batch; // batch size for splits
|
|
2065 |
|
|
2066 |
/**
|
|
2067 |
* Creates a spliterator using the given iterator
|
|
2068 |
* for traversal, and reporting the given initial size
|
|
2069 |
* and characteristics.
|
|
2070 |
*
|
|
2071 |
* @param iterator the iterator for the source
|
|
2072 |
* @param size the number of elements in the source
|
|
2073 |
* @param characteristics properties of this spliterator's
|
|
2074 |
* source or elements.
|
|
2075 |
*/
|
|
2076 |
public DoubleIteratorSpliterator(PrimitiveIterator.OfDouble iterator, long size, int characteristics) {
|
|
2077 |
this.it = iterator;
|
|
2078 |
this.est = size;
|
|
2079 |
this.characteristics = characteristics | Spliterator.SIZED | Spliterator.SUBSIZED;
|
|
2080 |
}
|
|
2081 |
|
|
2082 |
/**
|
|
2083 |
* Creates a spliterator using the given iterator for a
|
|
2084 |
* source of unknown size, reporting the given
|
|
2085 |
* characteristics.
|
|
2086 |
*
|
|
2087 |
* @param iterator the iterator for the source
|
|
2088 |
* @param characteristics properties of this spliterator's
|
|
2089 |
* source or elements.
|
|
2090 |
*/
|
|
2091 |
public DoubleIteratorSpliterator(PrimitiveIterator.OfDouble iterator, int characteristics) {
|
|
2092 |
this.it = iterator;
|
|
2093 |
this.est = Long.MAX_VALUE;
|
|
2094 |
this.characteristics = characteristics & ~(Spliterator.SIZED | Spliterator.SUBSIZED);
|
|
2095 |
}
|
|
2096 |
|
|
2097 |
@Override
|
|
2098 |
public OfDouble trySplit() {
|
|
2099 |
PrimitiveIterator.OfDouble i = it;
|
|
2100 |
long s = est;
|
|
2101 |
if (s > 1 && i.hasNext()) {
|
|
2102 |
int n = batch + BATCH_UNIT;
|
|
2103 |
if (n > s)
|
|
2104 |
n = (int) s;
|
|
2105 |
if (n > MAX_BATCH)
|
|
2106 |
n = MAX_BATCH;
|
|
2107 |
double[] a;
|
|
2108 |
try {
|
|
2109 |
a = new double[n];
|
|
2110 |
} catch (OutOfMemoryError oome) {
|
|
2111 |
return null;
|
|
2112 |
}
|
|
2113 |
int j = 0;
|
|
2114 |
do { a[j] = i.nextDouble(); } while (++j < n && i.hasNext());
|
|
2115 |
batch = j;
|
|
2116 |
if (est != Long.MAX_VALUE)
|
|
2117 |
est -= j;
|
|
2118 |
return new DoubleArraySpliterator(a, 0, j, characteristics);
|
|
2119 |
}
|
|
2120 |
return null;
|
|
2121 |
}
|
|
2122 |
|
|
2123 |
@Override
|
|
2124 |
public void forEachRemaining(DoubleConsumer action) {
|
|
2125 |
if (action == null) throw new NullPointerException();
|
|
2126 |
it.forEachRemaining(action);
|
|
2127 |
}
|
|
2128 |
|
|
2129 |
@Override
|
|
2130 |
public boolean tryAdvance(DoubleConsumer action) {
|
|
2131 |
if (action == null) throw new NullPointerException();
|
|
2132 |
if (it.hasNext()) {
|
|
2133 |
action.accept(it.nextDouble());
|
|
2134 |
return true;
|
|
2135 |
}
|
|
2136 |
return false;
|
|
2137 |
}
|
|
2138 |
|
|
2139 |
@Override
|
|
2140 |
public long estimateSize() {
|
|
2141 |
return est;
|
|
2142 |
}
|
|
2143 |
|
|
2144 |
@Override
|
|
2145 |
public int characteristics() { return characteristics; }
|
|
2146 |
|
|
2147 |
@Override
|
|
2148 |
public Comparator<? super Double> getComparator() {
|
|
2149 |
if (hasCharacteristics(Spliterator.SORTED))
|
|
2150 |
return null;
|
|
2151 |
throw new IllegalStateException();
|
|
2152 |
}
|
|
2153 |
}
|
|
2154 |
}
|