-
Notifications
You must be signed in to change notification settings - Fork 669
/
Copy pathSpliterators.java
2862 lines (2478 loc) · 121 KB
/
Spliterators.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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* 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;
/**
* Static classes and methods for operating on or creating instances of {@link Spliterator}
* and its primitive specializations {@link Spliterator.OfInt}, {@link Spliterator.OfLong}, and {@link Spliterator.OfDouble}.
*
* @see Spliterator
* @since 1.8
*/
/*
* 流迭代器工厂,提供一些简单的流迭代器的实现
*
* 主要包含以下4类流迭代器:
* [1] "空"Spliterator
* [2] "数组"Spliterator
* [3] "支持有限并行"的Spliterator
* [4] "适配Iterator"的Spliterator
*
* 在方法层面,除了提供构造各类Spliterator的工厂方法,还提供了将Spliterator适配为Iterator的方法实现。
*/
public final class Spliterators {
// Suppresses default constructor, ensuring non-instantiability.
private Spliterators() {
}
/*▼ (1) 构造"空"Spliterator ████████████████████████████████████████████████████████████████████████████████┓ */
// 空的Spliterator,不包含任何待处理元素
private static final Spliterator<Object> EMPTY_SPLITERATOR = new EmptySpliterator.OfRef<>();
private static final Spliterator.OfInt EMPTY_INT_SPLITERATOR = new EmptySpliterator.OfInt();
private static final Spliterator.OfLong EMPTY_LONG_SPLITERATOR = new EmptySpliterator.OfLong();
private static final Spliterator.OfDouble EMPTY_DOUBLE_SPLITERATOR = new EmptySpliterator.OfDouble();
/**
* Creates an empty {@code Spliterator}
*
* <p>The empty spliterator reports {@link Spliterator#SIZED} and
* {@link Spliterator#SUBSIZED}. Calls to
* {@link java.util.Spliterator#trySplit()} always return {@code null}.
*
* @param <T> Type of elements
*
* @return An empty spliterator
*/
// 构造"空"Spliterator(引用类型版本)
@SuppressWarnings("unchecked")
public static <T> Spliterator<T> emptySpliterator() {
return (Spliterator<T>) EMPTY_SPLITERATOR;
}
/**
* Creates an empty {@code Spliterator.OfInt}
*
* <p>The empty spliterator reports {@link Spliterator#SIZED} and
* {@link Spliterator#SUBSIZED}. Calls to
* {@link java.util.Spliterator#trySplit()} always return {@code null}.
*
* @return An empty spliterator
*/
// 构造"空"Spliterator(int类型版本)
public static Spliterator.OfInt emptyIntSpliterator() {
return EMPTY_INT_SPLITERATOR;
}
/**
* Creates an empty {@code Spliterator.OfLong}
*
* <p>The empty spliterator reports {@link Spliterator#SIZED} and
* {@link Spliterator#SUBSIZED}. Calls to
* {@link java.util.Spliterator#trySplit()} always return {@code null}.
*
* @return An empty spliterator
*/
// 构造"空"Spliterator(long类型版本)
public static Spliterator.OfLong emptyLongSpliterator() {
return EMPTY_LONG_SPLITERATOR;
}
/**
* Creates an empty {@code Spliterator.OfDouble}
*
* <p>The empty spliterator reports {@link Spliterator#SIZED} and
* {@link Spliterator#SUBSIZED}. Calls to
* {@link java.util.Spliterator#trySplit()} always return {@code null}.
*
* @return An empty spliterator
*/
// 构造"空"Spliterator(double类型版本)
public static Spliterator.OfDouble emptyDoubleSpliterator() {
return EMPTY_DOUBLE_SPLITERATOR;
}
/*▲ (1) 构造"空"Spliterator ████████████████████████████████████████████████████████████████████████████████┛ */
/*▼ (2) 构造"数组"Spliterator ████████████████████████████████████████████████████████████████████████████████┓ */
/**
* Creates a {@code Spliterator} covering the elements of a given array,
* using a customized set of spliterator characteristics.
*
* <p>This method is provided as an implementation convenience for
* Spliterators which store portions of their elements in arrays, and need
* fine control over Spliterator characteristics. Most other situations in
* which a Spliterator for an array is needed should use
* {@link Arrays#spliterator(Object[])}.
*
* <p>The returned spliterator always reports the characteristics
* {@code SIZED} and {@code SUBSIZED}. The caller may provide additional
* characteristics for the spliterator to report; it is common to
* additionally specify {@code IMMUTABLE} and {@code ORDERED}.
*
* @param <T> Type of elements
* @param array The array, assumed to be unmodified during use
* @param additionalCharacteristics Additional spliterator characteristics
* of this spliterator's source or elements beyond {@code SIZED} and
* {@code SUBSIZED} which are always reported
*
* @return A spliterator for an array
*
* @throws NullPointerException if the given array is {@code null}
* @see Arrays#spliterator(Object[])
*/
// 构造"数组"Spliterator(引用类型版本),数据源是array
public static <T> Spliterator<T> spliterator(Object[] array, int additionalCharacteristics) {
return new ArraySpliterator<>(Objects.requireNonNull(array), additionalCharacteristics);
}
/**
* Creates a {@code Spliterator} covering a range of elements of a given
* array, using a customized set of spliterator characteristics.
*
* <p>This method is provided as an implementation convenience for
* Spliterators which store portions of their elements in arrays, and need
* fine control over Spliterator characteristics. Most other situations in
* which a Spliterator for an array is needed should use
* {@link Arrays#spliterator(Object[])}.
*
* <p>The returned spliterator always reports the characteristics
* {@code SIZED} and {@code SUBSIZED}. The caller may provide additional
* characteristics for the spliterator to report; it is common to
* additionally specify {@code IMMUTABLE} and {@code ORDERED}.
*
* @param <T> Type of elements
* @param array The array, assumed to be unmodified during use
* @param fromIndex The least index (inclusive) to cover
* @param toIndex One past the greatest index to cover
* @param additionalCharacteristics Additional spliterator characteristics
* of this spliterator's source or elements beyond {@code SIZED} and
* {@code SUBSIZED} which are always reported
*
* @return A spliterator for an array
*
* @throws NullPointerException if the given array is {@code null}
* @throws ArrayIndexOutOfBoundsException if {@code fromIndex} is negative,
* {@code toIndex} is less than {@code fromIndex}, or
* {@code toIndex} is greater than the array size
* @see Arrays#spliterator(Object[], int, int)
*/
// 构造"数组"Spliterator(引用类型版本),数据源是array[fromIndex, toIndex)
public static <T> Spliterator<T> spliterator(Object[] array, int fromIndex, int toIndex, int additionalCharacteristics) {
checkFromToBounds(Objects.requireNonNull(array).length, fromIndex, toIndex);
return new ArraySpliterator<>(array, fromIndex, toIndex, additionalCharacteristics);
}
/**
* Creates a {@code Spliterator.OfInt} covering the elements of a given array,
* using a customized set of spliterator characteristics.
*
* <p>This method is provided as an implementation convenience for
* Spliterators which store portions of their elements in arrays, and need
* fine control over Spliterator characteristics. Most other situations in
* which a Spliterator for an array is needed should use
* {@link Arrays#spliterator(int[])}.
*
* <p>The returned spliterator always reports the characteristics
* {@code SIZED} and {@code SUBSIZED}. The caller may provide additional
* characteristics for the spliterator to report; it is common to
* additionally specify {@code IMMUTABLE} and {@code ORDERED}.
*
* @param array The array, assumed to be unmodified during use
* @param additionalCharacteristics Additional spliterator characteristics
* of this spliterator's source or elements beyond {@code SIZED} and
* {@code SUBSIZED} which are always reported
*
* @return A spliterator for an array
*
* @throws NullPointerException if the given array is {@code null}
* @see Arrays#spliterator(int[])
*/
// 构造"数组"Spliterator(int类型版本),数据源是array
public static Spliterator.OfInt spliterator(int[] array, int additionalCharacteristics) {
return new IntArraySpliterator(Objects.requireNonNull(array), additionalCharacteristics);
}
/**
* Creates a {@code Spliterator.OfInt} covering a range of elements of a
* given array, using a customized set of spliterator characteristics.
*
* <p>This method is provided as an implementation convenience for
* Spliterators which store portions of their elements in arrays, and need
* fine control over Spliterator characteristics. Most other situations in
* which a Spliterator for an array is needed should use
* {@link Arrays#spliterator(int[], int, int)}.
*
* <p>The returned spliterator always reports the characteristics
* {@code SIZED} and {@code SUBSIZED}. The caller may provide additional
* characteristics for the spliterator to report; it is common to
* additionally specify {@code IMMUTABLE} and {@code ORDERED}.
*
* @param array The array, assumed to be unmodified during use
* @param fromIndex The least index (inclusive) to cover
* @param toIndex One past the greatest index to cover
* @param additionalCharacteristics Additional spliterator characteristics
* of this spliterator's source or elements beyond {@code SIZED} and
* {@code SUBSIZED} which are always reported
*
* @return A spliterator for an array
*
* @throws NullPointerException if the given array is {@code null}
* @throws ArrayIndexOutOfBoundsException if {@code fromIndex} is negative,
* {@code toIndex} is less than {@code fromIndex}, or
* {@code toIndex} is greater than the array size
* @see Arrays#spliterator(int[], int, int)
*/
// 构造"数组"Spliterator(int类型版本),数据源是array[fromIndex, toIndex)
public static Spliterator.OfInt spliterator(int[] array, int fromIndex, int toIndex, int additionalCharacteristics) {
checkFromToBounds(Objects.requireNonNull(array).length, fromIndex, toIndex);
return new IntArraySpliterator(array, fromIndex, toIndex, additionalCharacteristics);
}
/**
* Creates a {@code Spliterator.OfLong} covering the elements of a given array,
* using a customized set of spliterator characteristics.
*
* <p>This method is provided as an implementation convenience for
* Spliterators which store portions of their elements in arrays, and need
* fine control over Spliterator characteristics. Most other situations in
* which a Spliterator for an array is needed should use
* {@link Arrays#spliterator(long[])}.
*
* <p>The returned spliterator always reports the characteristics
* {@code SIZED} and {@code SUBSIZED}. The caller may provide additional
* characteristics for the spliterator to report; it is common to
* additionally specify {@code IMMUTABLE} and {@code ORDERED}.
*
* @param array The array, assumed to be unmodified during use
* @param additionalCharacteristics Additional spliterator characteristics
* of this spliterator's source or elements beyond {@code SIZED} and
* {@code SUBSIZED} which are always reported
*
* @return A spliterator for an array
*
* @throws NullPointerException if the given array is {@code null}
* @see Arrays#spliterator(long[])
*/
// 构造"数组"Spliterator(long类型版本),数据源是array
public static Spliterator.OfLong spliterator(long[] array, int additionalCharacteristics) {
return new LongArraySpliterator(Objects.requireNonNull(array), additionalCharacteristics);
}
/**
* Creates a {@code Spliterator.OfLong} covering a range of elements of a
* given array, using a customized set of spliterator characteristics.
*
* <p>This method is provided as an implementation convenience for
* Spliterators which store portions of their elements in arrays, and need
* fine control over Spliterator characteristics. Most other situations in
* which a Spliterator for an array is needed should use
* {@link Arrays#spliterator(long[], int, int)}.
*
* <p>The returned spliterator always reports the characteristics
* {@code SIZED} and {@code SUBSIZED}. The caller may provide additional
* characteristics for the spliterator to report. (For example, if it is
* known the array will not be further modified, specify {@code IMMUTABLE};
* if the array data is considered to have an encounter order, specify
* {@code ORDERED}). The method {@link Arrays#spliterator(long[], int, int)} can
* often be used instead, which returns a spliterator that reports
* {@code SIZED}, {@code SUBSIZED}, {@code IMMUTABLE}, and {@code ORDERED}.
*
* @param array The array, assumed to be unmodified during use
* @param fromIndex The least index (inclusive) to cover
* @param toIndex One past the greatest index to cover
* @param additionalCharacteristics Additional spliterator characteristics
* of this spliterator's source or elements beyond {@code SIZED} and
* {@code SUBSIZED} which are always reported
*
* @return A spliterator for an array
*
* @throws NullPointerException if the given array is {@code null}
* @throws ArrayIndexOutOfBoundsException if {@code fromIndex} is negative,
* {@code toIndex} is less than {@code fromIndex}, or
* {@code toIndex} is greater than the array size
* @see Arrays#spliterator(long[], int, int)
*/
// 构造"数组"Spliterator(long类型版本),数据源是array[fromIndex, toIndex)
public static Spliterator.OfLong spliterator(long[] array, int fromIndex, int toIndex, int additionalCharacteristics) {
checkFromToBounds(Objects.requireNonNull(array).length, fromIndex, toIndex);
return new LongArraySpliterator(array, fromIndex, toIndex, additionalCharacteristics);
}
/**
* Creates a {@code Spliterator.OfDouble} covering the elements of a given array,
* using a customized set of spliterator characteristics.
*
* <p>This method is provided as an implementation convenience for
* Spliterators which store portions of their elements in arrays, and need
* fine control over Spliterator characteristics. Most other situations in
* which a Spliterator for an array is needed should use
* {@link Arrays#spliterator(double[])}.
*
* <p>The returned spliterator always reports the characteristics
* {@code SIZED} and {@code SUBSIZED}. The caller may provide additional
* characteristics for the spliterator to report; it is common to
* additionally specify {@code IMMUTABLE} and {@code ORDERED}.
*
* @param array The array, assumed to be unmodified during use
* @param additionalCharacteristics Additional spliterator characteristics
* of this spliterator's source or elements beyond {@code SIZED} and
* {@code SUBSIZED} which are always reported
*
* @return A spliterator for an array
*
* @throws NullPointerException if the given array is {@code null}
* @see Arrays#spliterator(double[])
*/
// 构造"数组"Spliterator(double类型版本),数据源是array
public static Spliterator.OfDouble spliterator(double[] array, int additionalCharacteristics) {
return new DoubleArraySpliterator(Objects.requireNonNull(array), additionalCharacteristics);
}
/**
* Creates a {@code Spliterator.OfDouble} covering a range of elements of a
* given array, using a customized set of spliterator characteristics.
*
* <p>This method is provided as an implementation convenience for
* Spliterators which store portions of their elements in arrays, and need
* fine control over Spliterator characteristics. Most other situations in
* which a Spliterator for an array is needed should use
* {@link Arrays#spliterator(double[], int, int)}.
*
* <p>The returned spliterator always reports the characteristics
* {@code SIZED} and {@code SUBSIZED}. The caller may provide additional
* characteristics for the spliterator to report. (For example, if it is
* known the array will not be further modified, specify {@code IMMUTABLE};
* if the array data is considered to have an encounter order, specify
* {@code ORDERED}). The method {@link Arrays#spliterator(long[], int, int)} can
* often be used instead, which returns a spliterator that reports
* {@code SIZED}, {@code SUBSIZED}, {@code IMMUTABLE}, and {@code ORDERED}.
*
* @param array The array, assumed to be unmodified during use
* @param fromIndex The least index (inclusive) to cover
* @param toIndex One past the greatest index to cover
* @param additionalCharacteristics Additional spliterator characteristics
* of this spliterator's source or elements beyond {@code SIZED} and
* {@code SUBSIZED} which are always reported
*
* @return A spliterator for an array
*
* @throws NullPointerException if the given array is {@code null}
* @throws ArrayIndexOutOfBoundsException if {@code fromIndex} is negative,
* {@code toIndex} is less than {@code fromIndex}, or
* {@code toIndex} is greater than the array size
* @see Arrays#spliterator(double[], int, int)
*/
// 构造"数组"Spliterator(double类型版本),数据源是array[fromIndex, toIndex)
public static Spliterator.OfDouble spliterator(double[] array, int fromIndex, int toIndex, int additionalCharacteristics) {
checkFromToBounds(Objects.requireNonNull(array).length, fromIndex, toIndex);
return new DoubleArraySpliterator(array, fromIndex, toIndex, additionalCharacteristics);
}
/*▲ (2) 构造"数组"Spliterator ████████████████████████████████████████████████████████████████████████████████┛ */
/*▼ (3) 构造"支持有限并行"的Spliterator ████████████████████████████████████████████████████████████████████████████████┓ */
// 需要子类重写该抽象类,并实现tryAdvance()方法;必要时,还需要重写forEachRemaining()方法
/*▲ (3) 构造"支持有限并行"的Spliterator ████████████████████████████████████████████████████████████████████████████████┛ */
/*▼ (4) 构造"适配Iterator"的Spliterator ████████████████████████████████████████████████████████████████████████████████┓ */
/**
* Creates a {@code Spliterator} using the given collection's
* {@link java.util.Collection#iterator()} as the source of elements, and
* reporting its {@link java.util.Collection#size()} as its initial size.
*
* <p>The spliterator is
* <em><a href="Spliterator.html#binding">late-binding</a></em>, inherits
* the <em>fail-fast</em> properties of the collection's iterator, and
* implements {@code trySplit} to permit limited parallelism.
*
* @param <T> Type of elements
* @param c The collection
* @param characteristics Characteristics of this spliterator's source or
* elements. The characteristics {@code SIZED} and {@code SUBSIZED}
* are additionally reported unless {@code CONCURRENT} is supplied.
*
* @return A spliterator from an iterator
*
* @throws NullPointerException if the given collection is {@code null}
*/
// 构造"适配Iterator"的Spliterator(引用类型版本)
public static <T> Spliterator<T> spliterator(Collection<? extends T> collection, int characteristics) {
return new IteratorSpliterator<>(Objects.requireNonNull(collection), characteristics);
}
/**
* Creates a {@code Spliterator} using a given {@code Iterator}
* as the source of elements, with no initial size estimate.
*
* <p>The spliterator is not
* <em><a href="Spliterator.html#binding">late-binding</a></em>, inherits
* the <em>fail-fast</em> properties of the iterator, and implements
* {@code trySplit} to permit limited parallelism.
*
* <p>Traversal of elements should be accomplished through the spliterator.
* The behaviour of splitting and traversal is undefined if the iterator is
* operated on after the spliterator is returned.
*
* @param <T> Type of elements
* @param iterator The iterator for the source
* @param characteristics Characteristics of this spliterator's source
* or elements ({@code SIZED} and {@code SUBSIZED}, if supplied, are
* ignored and are not reported.)
*
* @return A spliterator from an iterator
*
* @throws NullPointerException if the given iterator is {@code null}
*/
// 构造"适配Iterator"的Spliterator(引用类型版本)
public static <T> Spliterator<T> spliteratorUnknownSize(Iterator<? extends T> iterator, int characteristics) {
return new IteratorSpliterator<>(Objects.requireNonNull(iterator), characteristics);
}
/**
* Creates a {@code Spliterator} using a given {@code Iterator}
* as the source of elements, and with a given initially reported size.
*
* <p>The spliterator is not
* <em><a href="Spliterator.html#binding">late-binding</a></em>, inherits
* the <em>fail-fast</em> properties of the iterator, and implements
* {@code trySplit} to permit limited parallelism.
*
* <p>Traversal of elements should be accomplished through the spliterator.
* The behaviour of splitting and traversal is undefined if the iterator is
* operated on after the spliterator is returned, or the initially reported
* size is not equal to the actual number of elements in the source.
*
* @param <T> Type of elements
* @param iterator The iterator for the source
* @param size The number of elements in the source, to be reported as
* initial {@code estimateSize}
* @param characteristics Characteristics of this spliterator's source or
* elements. The characteristics {@code SIZED} and {@code SUBSIZED}
* are additionally reported unless {@code CONCURRENT} is supplied.
*
* @return A spliterator from an iterator
*
* @throws NullPointerException if the given iterator is {@code null}
*/
// 构造"适配Iterator"的Spliterator(引用类型版本)
public static <T> Spliterator<T> spliterator(Iterator<? extends T> iterator, long size, int characteristics) {
return new IteratorSpliterator<>(Objects.requireNonNull(iterator), size, characteristics);
}
/**
* Creates a {@code Spliterator.OfInt} using a given
* {@code IntStream.IntIterator} as the source of elements, with no initial
* size estimate.
*
* <p>The spliterator is not
* <em><a href="Spliterator.html#binding">late-binding</a></em>, inherits
* the <em>fail-fast</em> properties of the iterator, and implements
* {@code trySplit} to permit limited parallelism.
*
* <p>Traversal of elements should be accomplished through the spliterator.
* The behaviour of splitting and traversal is undefined if the iterator is
* operated on after the spliterator is returned.
*
* @param iterator The iterator for the source
* @param characteristics Characteristics of this spliterator's source
* or elements ({@code SIZED} and {@code SUBSIZED}, if supplied, are
* ignored and are not reported.)
*
* @return A spliterator from an iterator
*
* @throws NullPointerException if the given iterator is {@code null}
*/
// 构造"适配Iterator"的Spliterator(int类型版本)
public static Spliterator.OfInt spliteratorUnknownSize(PrimitiveIterator.OfInt iterator, int characteristics) {
return new IntIteratorSpliterator(Objects.requireNonNull(iterator), characteristics);
}
/**
* Creates a {@code Spliterator.OfInt} using a given
* {@code IntStream.IntIterator} as the source of elements, and with a given
* initially reported size.
*
* <p>The spliterator is not
* <em><a href="Spliterator.html#binding">late-binding</a></em>, inherits
* the <em>fail-fast</em> properties of the iterator, and implements
* {@code trySplit} to permit limited parallelism.
*
* <p>Traversal of elements should be accomplished through the spliterator.
* The behaviour of splitting and traversal is undefined if the iterator is
* operated on after the spliterator is returned, or the initially reported
* size is not equal to the actual number of elements in the source.
*
* @param iterator The iterator for the source
* @param size The number of elements in the source, to be reported as
* initial {@code estimateSize}.
* @param characteristics Characteristics of this spliterator's source or
* elements. The characteristics {@code SIZED} and {@code SUBSIZED}
* are additionally reported unless {@code CONCURRENT} is supplied.
*
* @return A spliterator from an iterator
*
* @throws NullPointerException if the given iterator is {@code null}
*/
// 构造"适配Iterator"的Spliterator(int类型版本)
public static Spliterator.OfInt spliterator(PrimitiveIterator.OfInt iterator, long size, int characteristics) {
return new IntIteratorSpliterator(Objects.requireNonNull(iterator), size, characteristics);
}
/**
* Creates a {@code Spliterator.OfLong} using a given
* {@code LongStream.LongIterator} as the source of elements, with no
* initial size estimate.
*
* <p>The spliterator is not
* <em><a href="Spliterator.html#binding">late-binding</a></em>, inherits
* the <em>fail-fast</em> properties of the iterator, and implements
* {@code trySplit} to permit limited parallelism.
*
* <p>Traversal of elements should be accomplished through the spliterator.
* The behaviour of splitting and traversal is undefined if the iterator is
* operated on after the spliterator is returned.
*
* @param iterator The iterator for the source
* @param characteristics Characteristics of this spliterator's source
* or elements ({@code SIZED} and {@code SUBSIZED}, if supplied, are
* ignored and are not reported.)
*
* @return A spliterator from an iterator
*
* @throws NullPointerException if the given iterator is {@code null}
*/
// 构造"适配Iterator"的Spliterator(long类型版本)
public static Spliterator.OfLong spliteratorUnknownSize(PrimitiveIterator.OfLong iterator, int characteristics) {
return new LongIteratorSpliterator(Objects.requireNonNull(iterator), characteristics);
}
/**
* Creates a {@code Spliterator.OfLong} using a given
* {@code LongStream.LongIterator} as the source of elements, and with a
* given initially reported size.
*
* <p>The spliterator is not
* <em><a href="Spliterator.html#binding">late-binding</a></em>, inherits
* the <em>fail-fast</em> properties of the iterator, and implements
* {@code trySplit} to permit limited parallelism.
*
* <p>Traversal of elements should be accomplished through the spliterator.
* The behaviour of splitting and traversal is undefined if the iterator is
* operated on after the spliterator is returned, or the initially reported
* size is not equal to the actual number of elements in the source.
*
* @param iterator The iterator for the source
* @param size The number of elements in the source, to be reported as
* initial {@code estimateSize}.
* @param characteristics Characteristics of this spliterator's source or
* elements. The characteristics {@code SIZED} and {@code SUBSIZED}
* are additionally reported unless {@code CONCURRENT} is supplied.
*
* @return A spliterator from an iterator
*
* @throws NullPointerException if the given iterator is {@code null}
*/
// 构造"适配Iterator"的Spliterator(long类型版本)
public static Spliterator.OfLong spliterator(PrimitiveIterator.OfLong iterator, long size, int characteristics) {
return new LongIteratorSpliterator(Objects.requireNonNull(iterator), size, characteristics);
}
/**
* Creates a {@code Spliterator.OfDouble} using a given
* {@code DoubleStream.DoubleIterator} as the source of elements, with no
* initial size estimate.
*
* <p>The spliterator is not
* <em><a href="Spliterator.html#binding">late-binding</a></em>, inherits
* the <em>fail-fast</em> properties of the iterator, and implements
* {@code trySplit} to permit limited parallelism.
*
* <p>Traversal of elements should be accomplished through the spliterator.
* The behaviour of splitting and traversal is undefined if the iterator is
* operated on after the spliterator is returned.
*
* @param iterator The iterator for the source
* @param characteristics Characteristics of this spliterator's source
* or elements ({@code SIZED} and {@code SUBSIZED}, if supplied, are
* ignored and are not reported.)
*
* @return A spliterator from an iterator
*
* @throws NullPointerException if the given iterator is {@code null}
*/
// 构造"适配Iterator"的Spliterator(double类型版本)
public static Spliterator.OfDouble spliteratorUnknownSize(PrimitiveIterator.OfDouble iterator, int characteristics) {
return new DoubleIteratorSpliterator(Objects.requireNonNull(iterator), characteristics);
}
/**
* Creates a {@code Spliterator.OfDouble} using a given
* {@code DoubleStream.DoubleIterator} as the source of elements, and with a
* given initially reported size.
*
* <p>The spliterator is not
* <em><a href="Spliterator.html#binding">late-binding</a></em>, inherits
* the <em>fail-fast</em> properties of the iterator, and implements
* {@code trySplit} to permit limited parallelism.
*
* <p>Traversal of elements should be accomplished through the spliterator.
* The behaviour of splitting and traversal is undefined if the iterator is
* operated on after the spliterator is returned, or the initially reported
* size is not equal to the actual number of elements in the source.
*
* @param iterator The iterator for the source
* @param size The number of elements in the source, to be reported as
* initial {@code estimateSize}
* @param characteristics Characteristics of this spliterator's source or
* elements. The characteristics {@code SIZED} and {@code SUBSIZED}
* are additionally reported unless {@code CONCURRENT} is supplied.
*
* @return A spliterator from an iterator
*
* @throws NullPointerException if the given iterator is {@code null}
*/
// 构造"适配Iterator"的Spliterator(double类型版本)
public static Spliterator.OfDouble spliterator(PrimitiveIterator.OfDouble iterator, long size, int characteristics) {
return new DoubleIteratorSpliterator(Objects.requireNonNull(iterator), size, characteristics);
}
/*▲ (4) 构造"适配Iterator"的Spliterator ████████████████████████████████████████████████████████████████████████████████┛ */
/*▼ 将Spliterator适配为Iterator ████████████████████████████████████████████████████████████████████████████████┓ */
/**
* Creates an {@code Iterator} from a {@code Spliterator}.
*
* <p>Traversal of elements should be accomplished through the iterator.
* The behaviour of traversal is undefined if the spliterator is operated
* after the iterator is returned.
*
* @param <T> Type of elements
* @param spliterator The spliterator
*
* @return An iterator
*
* @throws NullPointerException if the given spliterator is {@code null}
*/
// 将Spliterator适配为Iterator(引用类型版本)
public static <T> Iterator<T> iterator(Spliterator<? extends T> spliterator) {
Objects.requireNonNull(spliterator);
// 适配器,包装Spliterator,使其发挥Iterator的功能
class Adapter implements Iterator<T>, Consumer<T> {
boolean valueReady = false; // 是否存在下一个元素
T nextElement; // 记录下一个元素
// 重写了accept,被tryAdvance回调,用于记录下一个元素
@Override
public void accept(T e) {
valueReady = true;
nextElement = e;
}
// 是否存在下一个未遍历元素
@Override
public boolean hasNext() {
if(!valueReady) {
// 尝试用accept()消费spliterator中下一个元素
spliterator.tryAdvance(this);
}
return valueReady;
}
// 返回下一个元素
@Override
public T next() {
if(!valueReady && !hasNext()) {
throw new NoSuchElementException();
}
valueReady = false;
return nextElement;
}
}
return new Adapter();
}
/**
* Creates an {@code PrimitiveIterator.OfInt} from a
* {@code Spliterator.OfInt}.
*
* <p>Traversal of elements should be accomplished through the iterator.
* The behaviour of traversal is undefined if the spliterator is operated
* after the iterator is returned.
*
* @param spliterator The spliterator
*
* @return An iterator
*
* @throws NullPointerException if the given spliterator is {@code null}
*/
// 将Spliterator适配为Iterator(int类型版本)
public static PrimitiveIterator.OfInt iterator(Spliterator.OfInt spliterator) {
Objects.requireNonNull(spliterator);
// 适配器,包装Spliterator,使其发挥Iterator的功能
class Adapter implements PrimitiveIterator.OfInt, IntConsumer {
boolean valueReady = false; // 是否存在下一个元素
int nextElement; // 记录下一个元素
// 重写了accept,被tryAdvance回调,用于记录下一个元素
@Override
public void accept(int e) {
valueReady = true;
nextElement = e;
}
// 是否存在下一个未遍历元素
@Override
public boolean hasNext() {
if(!valueReady) {
// 尝试用accept()消费spliterator中下一个元素
spliterator.tryAdvance(this);
}
return valueReady;
}
// 返回下一个元素
@Override
public int nextInt() {
if(!valueReady && !hasNext()) {
throw new NoSuchElementException();
}
valueReady = false;
return nextElement;
}
}
return new Adapter();
}
/**
* Creates an {@code PrimitiveIterator.OfLong} from a
* {@code Spliterator.OfLong}.
*
* <p>Traversal of elements should be accomplished through the iterator.
* The behaviour of traversal is undefined if the spliterator is operated
* after the iterator is returned.
*
* @param spliterator The spliterator
*
* @return An iterator
*
* @throws NullPointerException if the given spliterator is {@code null}
*/
// 将Spliterator适配为Iterator(long类型版本)
public static PrimitiveIterator.OfLong iterator(Spliterator.OfLong spliterator) {
Objects.requireNonNull(spliterator);
// 适配器,包装Spliterator,使其发挥Iterator的功能
class Adapter implements PrimitiveIterator.OfLong, LongConsumer {
boolean valueReady = false; // 是否存在下一个元素
long nextElement; // 记录下一个元素
// 重写了accept,被tryAdvance回调,用于记录下一个元素
@Override
public void accept(long e) {
valueReady = true;
nextElement = e;
}
// 是否存在下一个未遍历元素
@Override
public boolean hasNext() {
if(!valueReady) {
// 尝试用accept()消费spliterator中下一个元素
spliterator.tryAdvance(this);
}
return valueReady;
}
// 返回下一个元素
@Override
public long nextLong() {
if(!valueReady && !hasNext()) {
throw new NoSuchElementException();
}
valueReady = false;
return nextElement;
}
}
return new Adapter();
}
/**
* Creates an {@code PrimitiveIterator.OfDouble} from a
* {@code Spliterator.OfDouble}.
*
* <p>Traversal of elements should be accomplished through the iterator.
* The behaviour of traversal is undefined if the spliterator is operated
* after the iterator is returned.
*
* @param spliterator The spliterator
*
* @return An iterator
*
* @throws NullPointerException if the given spliterator is {@code null}
*/
// 将Spliterator适配为Iterator(double类型版本)
public static PrimitiveIterator.OfDouble iterator(Spliterator.OfDouble spliterator) {
Objects.requireNonNull(spliterator);
// 适配器,包装Spliterator,使其发挥Iterator的功能
class Adapter implements PrimitiveIterator.OfDouble, DoubleConsumer {
boolean valueReady = false; // 是否存在下一个元素
double nextElement; // 记录下一个元素
// 重写了accept,被tryAdvance回调,用于记录下一个元素
@Override
public void accept(double e) {
valueReady = true;
nextElement = e;
}
// 是否存在下一个未遍历元素
@Override
public boolean hasNext() {
if(!valueReady) {
// 尝试用accept()消费spliterator中下一个元素
spliterator.tryAdvance(this);
}
return valueReady;
}
// 返回下一个元素
@Override
public double nextDouble() {
if(!valueReady && !hasNext()) {
throw new NoSuchElementException();
}
valueReady = false;
return nextElement;
}
}
return new Adapter();
}
/*▲ 将Spliterator适配为Iterator ████████████████████████████████████████████████████████████████████████████████┛ */
/*▼ [1] "空"Spliterator ████████████████████████████████████████████████████████████████████████████████┓ */
// 空的Spliterator,不包含任何有效元素
private abstract static class EmptySpliterator<T, S extends Spliterator<T>, C> {
EmptySpliterator() {
}
public S trySplit() {
return null;
}
public boolean tryAdvance(C consumer) {
Objects.requireNonNull(consumer);
return false;
}
public void forEachRemaining(C consumer) {
Objects.requireNonNull(consumer);
}
public long estimateSize() {
return 0;
}
public int characteristics() {
return Spliterator.SIZED | Spliterator.SUBSIZED;
}
// 空的Spliterator(引用类型版本),不包含任何有效元素
private static final class OfRef<T> extends EmptySpliterator<T, Spliterator<T>, Consumer<? super T>> implements Spliterator<T> {
OfRef() {
}
}
// 空的Spliterator(int类型版本),不包含任何有效元素
private static final class OfInt extends EmptySpliterator<Integer, Spliterator.OfInt, IntConsumer> implements Spliterator.OfInt {
OfInt() {
}
}
// 空的Spliterator(long类型版本),不包含任何有效元素
private static final class OfLong extends EmptySpliterator<Long, Spliterator.OfLong, LongConsumer> implements Spliterator.OfLong {
OfLong() {
}
}
// 空的Spliterator(double类型版本),不包含任何有效元素
private static final class OfDouble extends EmptySpliterator<Double, Spliterator.OfDouble, DoubleConsumer> implements Spliterator.OfDouble {
OfDouble() {
}
}
}
/*▲ [1] "空"Spliterator ████████████████████████████████████████████████████████████████████████████████┛ */
/*▼ [2] "数组"Spliterator ████████████████████████████████████████████████████████████████████████████████┓ */
/**
* A Spliterator designed for use by sources that traverse and split elements maintained in an unmodifiable {@code Object[]} array.
*/
// "数组"Spliterator(引用类型版本)
static final class ArraySpliterator<T> implements Spliterator<T> {
/**