-
Notifications
You must be signed in to change notification settings - Fork 668
/
ReduceOps.java
1275 lines (1105 loc) · 51 KB
/
ReduceOps.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) 2012, 2015, 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.stream;
import java.util.Objects;
import java.util.Optional;
import java.util.OptionalDouble;
import java.util.OptionalInt;
import java.util.OptionalLong;
import java.util.Spliterator;
import java.util.concurrent.CountedCompleter;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.BinaryOperator;
import java.util.function.DoubleBinaryOperator;
import java.util.function.IntBinaryOperator;
import java.util.function.LongBinaryOperator;
import java.util.function.ObjDoubleConsumer;
import java.util.function.ObjIntConsumer;
import java.util.function.ObjLongConsumer;
import java.util.function.Supplier;
/**
* Factory for creating instances of {@code TerminalOp} that implement
* reductions.
*
* @since 1.8
*/
// 应用在终端阶段的辅助类,服务于汇总操作
final class ReduceOps {
private ReduceOps() {
}
/**
* Constructs a {@code TerminalOp} that implements a functional reduce on
* reference values producing an optional reference result.
*
* @param <T> The type of the input elements, and the type of the result
* @param operator The reducing function
*
* @return A {@code TerminalOp} implementing the reduction
*/
/*
* 无初始状态的汇总操作(引用类型版本)
*
* 尝试将遇到的每个数据与上一个状态做operator操作后,将汇总结果保存到上一次的状态值中。
* 未设置初始状态,所以每个(子)任务只是专注处理它自身遇到的数据源。
*
* 例如:
* Stream.of(1, 2, 3, 4, 5).reduce((a, b) -> a + b)
* 这会将1、2、3、4、5累加起来。
*
* operator: 两种用途:
* 1.用于择取操作,如果是并行流,则用在每个子任务中
* 2.用于并行流的合并操作
*/
public static <T> TerminalOp<T, Optional<T>> makeRef(BinaryOperator<T> operator) {
Objects.requireNonNull(operator);
class ReducingSink implements AccumulatingSink<T, Optional<T>, ReducingSink> {
private boolean empty;
private T state;
public void begin(long size) {
empty = true;
state = null;
}
@Override
public void accept(T t) {
if(empty) {
empty = false;
state = t;
} else {
state = operator.apply(state, t);
}
}
@Override
public void combine(ReducingSink other) {
if(!other.empty) {
accept(other.state);
}
}
@Override
public Optional<T> get() {
return empty ? Optional.empty() : Optional.of(state);
}
}
return new ReduceOp<T, Optional<T>, ReducingSink>(StreamShape.REFERENCE) {
@Override
public ReducingSink makeSink() {
return new ReducingSink();
}
};
}
/**
* Constructs a {@code TerminalOp} that implements a functional reduce on
* {@code int} values, producing an optional integer result.
*
* @param operator the combining function
*
* @return a {@code TerminalOp} implementing the reduction
*/
// 无初始状态的汇总操作(int类型版本)
public static TerminalOp<Integer, OptionalInt> makeInt(IntBinaryOperator operator) {
Objects.requireNonNull(operator);
class ReducingSink implements AccumulatingSink<Integer, OptionalInt, ReducingSink>, Sink.OfInt {
private boolean empty;
private int state;
public void begin(long size) {
empty = true;
state = 0;
}
@Override
public void accept(int t) {
if(empty) {
empty = false;
state = t;
} else {
state = operator.applyAsInt(state, t);
}
}
@Override
public OptionalInt get() {
return empty ? OptionalInt.empty() : OptionalInt.of(state);
}
@Override
public void combine(ReducingSink other) {
if(!other.empty)
accept(other.state);
}
}
return new ReduceOp<Integer, OptionalInt, ReducingSink>(StreamShape.INT_VALUE) {
@Override
public ReducingSink makeSink() {
return new ReducingSink();
}
};
}
/**
* Constructs a {@code TerminalOp} that implements a functional reduce on
* {@code long} values, producing an optional long result.
*
* @param operator the combining function
*
* @return a {@code TerminalOp} implementing the reduction
*/
// 无初始状态的汇总操作(long类型版本)
public static TerminalOp<Long, OptionalLong> makeLong(LongBinaryOperator operator) {
Objects.requireNonNull(operator);
class ReducingSink implements AccumulatingSink<Long, OptionalLong, ReducingSink>, Sink.OfLong {
private boolean empty;
private long state;
public void begin(long size) {
empty = true;
state = 0;
}
@Override
public void accept(long t) {
if(empty) {
empty = false;
state = t;
} else {
state = operator.applyAsLong(state, t);
}
}
@Override
public OptionalLong get() {
return empty ? OptionalLong.empty() : OptionalLong.of(state);
}
@Override
public void combine(ReducingSink other) {
if(!other.empty)
accept(other.state);
}
}
return new ReduceOp<Long, OptionalLong, ReducingSink>(StreamShape.LONG_VALUE) {
@Override
public ReducingSink makeSink() {
return new ReducingSink();
}
};
}
/**
* Constructs a {@code TerminalOp} that implements a functional reduce on
* {@code double} values, producing an optional double result.
*
* @param operator the combining function
*
* @return a {@code TerminalOp} implementing the reduction
*/
// 无初始状态的汇总操作(double类型版本)
public static TerminalOp<Double, OptionalDouble> makeDouble(DoubleBinaryOperator operator) {
Objects.requireNonNull(operator);
class ReducingSink implements AccumulatingSink<Double, OptionalDouble, ReducingSink>, Sink.OfDouble {
private boolean empty;
private double state;
public void begin(long size) {
empty = true;
state = 0;
}
@Override
public void accept(double t) {
if(empty) {
empty = false;
state = t;
} else {
state = operator.applyAsDouble(state, t);
}
}
@Override
public OptionalDouble get() {
return empty ? OptionalDouble.empty() : OptionalDouble.of(state);
}
@Override
public void combine(ReducingSink other) {
if(!other.empty)
accept(other.state);
}
}
return new ReduceOp<Double, OptionalDouble, ReducingSink>(StreamShape.DOUBLE_VALUE) {
@Override
public ReducingSink makeSink() {
return new ReducingSink();
}
};
}
/**
* Constructs a {@code TerminalOp} that implements a functional reduce on
* reference values.
*
* @param <T> the type of the input elements
* @param <U> the type of the result
* @param seed the identity element for the reduction
* @param reducer the accumulating function that incorporates an additional
* input element into the result
* @param combiner the combining function that combines two intermediate
* results
*
* @return a {@code TerminalOp} implementing the reduction
*/
/*
* 有初始状态的汇总操作(引用类型版本)
*
* 尝试将遇到的每个数据与上一个状态做汇总操作后,将汇总结果保存到上一次的状态值中。
* 这里提供了两个操作:reducer用于在单个任务中择取数据,而combiner用于在并行流中合并多个子任务。
* 这里需要设定一个初始状态seed,所以每个(子)任务在处理它自身遇到的数据源之前,首先要与该初始状态进行汇总。
*
* 例如:
* Stream.of(1, 2, 3, 4, 5).reduce(-1, (a, b) -> a + b, (a, b) -> a + b)
* 这是顺序流,操作结果是将-1、1、2、3、4、5累加起来,结果是14。
*
* Stream.of(1, 2, 3, 4, 5).parallel().reduce(-1, (a, b) -> a + b, (a, b) -> a + b)
* 这是并行流,虽然使用的择取方法与顺序流相同,但不同的是这里需要先将数据源拆分到各个子任务中。
* 根据默认的二分法拆分规则,上面的数据会被拆分为(1)、(2)、(3)、(4)、(5)这五组,
* 由于这五组数据位于五个子任务中,那么每个子任务择取数据之时都会先与那个初始值-1去做汇总,
* 即五个子任务的执行结果分别是:0、1、2、3、4,
* 最后,将这5个子任务用combiner合并起来,那就是0+1+2+3+4 = 10
*
* seed : 每个(子)任务需要使用的初始状态
* reducer : 用于择取操作,如果是并行流,则用在每个叶子任务中
* combiner: 用于并行流的合并子任务操作
*
* 注:通常来讲,要求reducer和combiner相呼应
*/
public static <T, U> TerminalOp<T, U> makeRef(U seed, BiFunction<U, ? super T, U> reducer, BinaryOperator<U> combiner) {
Objects.requireNonNull(reducer);
Objects.requireNonNull(combiner);
class ReducingSink extends Box<U> implements AccumulatingSink<T, U, ReducingSink> {
@Override
public void begin(long size) {
state = seed;
}
@Override
public void accept(T t) {
state = reducer.apply(state, t);
}
@Override
public void combine(ReducingSink other) {
state = combiner.apply(state, other.state);
}
}
return new ReduceOp<T, U, ReducingSink>(StreamShape.REFERENCE) {
@Override
public ReducingSink makeSink() {
return new ReducingSink();
}
};
}
/**
* Constructs a {@code TerminalOp} that implements a functional reduce on
* {@code int} values.
*
* @param identity the identity for the combining function
* @param operator the combining function
*
* @return a {@code TerminalOp} implementing the reduction
*/
// 有初始状态的汇总操作(int类型版本)
public static TerminalOp<Integer, Integer> makeInt(int identity, IntBinaryOperator operator) {
Objects.requireNonNull(operator);
class ReducingSink implements AccumulatingSink<Integer, Integer, ReducingSink>, Sink.OfInt {
private int state;
@Override
public void begin(long size) {
state = identity;
}
@Override
public void accept(int t) {
state = operator.applyAsInt(state, t);
}
@Override
public Integer get() {
return state;
}
@Override
public void combine(ReducingSink other) {
accept(other.state);
}
}
return new ReduceOp<Integer, Integer, ReducingSink>(StreamShape.INT_VALUE) {
@Override
public ReducingSink makeSink() {
return new ReducingSink();
}
};
}
/**
* Constructs a {@code TerminalOp} that implements a functional reduce on
* {@code long} values.
*
* @param identity the identity for the combining function
* @param operator the combining function
*
* @return a {@code TerminalOp} implementing the reduction
*/
// 有初始状态的汇总操作(long类型版本)
public static TerminalOp<Long, Long> makeLong(long identity, LongBinaryOperator operator) {
Objects.requireNonNull(operator);
class ReducingSink implements AccumulatingSink<Long, Long, ReducingSink>, Sink.OfLong {
private long state;
@Override
public void begin(long size) {
state = identity;
}
@Override
public void accept(long t) {
state = operator.applyAsLong(state, t);
}
@Override
public Long get() {
return state;
}
@Override
public void combine(ReducingSink other) {
accept(other.state);
}
}
return new ReduceOp<Long, Long, ReducingSink>(StreamShape.LONG_VALUE) {
@Override
public ReducingSink makeSink() {
return new ReducingSink();
}
};
}
/**
* Constructs a {@code TerminalOp} that implements a functional reduce on
* {@code double} values.
*
* @param identity the identity for the combining function
* @param operator the combining function
*
* @return a {@code TerminalOp} implementing the reduction
*/
// 有初始状态的汇总操作(double类型版本)
public static TerminalOp<Double, Double> makeDouble(double identity, DoubleBinaryOperator operator) {
Objects.requireNonNull(operator);
class ReducingSink implements AccumulatingSink<Double, Double, ReducingSink>, Sink.OfDouble {
private double state;
@Override
public void begin(long size) {
state = identity;
}
@Override
public void accept(double t) {
state = operator.applyAsDouble(state, t);
}
@Override
public Double get() {
return state;
}
@Override
public void combine(ReducingSink other) {
accept(other.state);
}
}
return new ReduceOp<Double, Double, ReducingSink>(StreamShape.DOUBLE_VALUE) {
@Override
public ReducingSink makeSink() {
return new ReducingSink();
}
};
}
/**
* Constructs a {@code TerminalOp} that implements a mutable reduce on reference values.
*
* @param <T> the type of the input elements
* @param <R> the type of the result
* @param seedFactory a factory to produce a new base accumulator
* @param accumulator a function to incorporate an element into an accumulator
* @param reducer a function to combine an accumulator into another
*
* @return a {@code TerminalOp} implementing the reduction
*/
/*
* 有初始状态的消费操作(引用类型版本)
*
* 注:这里的消费通常是将遇到的元素存储到某个容器中。
*
* 尝试将遇到的每个数据与上一个状态做汇总操作后,汇总过程是一个消费过程,在消费中如何处理状态值,由该方法的入参决定。
* 通常来说,我们会让seedFactory生成一个代表容器的"初始状态",然后在消费过程中,把遇到的元素收纳到该容器当中。
* 这里提供了两个操作:accumulator用于在单个任务中择取数据,而combiner用于在并行流中合并多个子任务。
* 这里需要设定一个初始状态的工厂seedFactory,所以每个(子)任务在处理它自身遇到的数据源之前,首先要与该初始状态进行汇总。
*
* 例如:
*
* 假设有如下两个操作:
* BiConsumer<ArrayList<Integer>, Integer> accumulator = (list, e) -> list.add(e);
*
* BiConsumer<ArrayList<Integer>, ArrayList<Integer>> combiner = (list1, list2) -> {
* for(Integer e : list2) {
* if(!list1.contains(e)) {
* list1.add(e);
* }
* }
* };
*
*
* Stream<Integer> stream = Stream.of(3, 2, 3, 1, 2);
* ArrayList<Integer> list = stream.collect(() -> new ArrayList<Integer>(), accumulator, combiner);
* 这是顺序流,操作结果是将3、2、3、1、2全部收集到list中。
*
* Stream<Integer> stream = Stream.of(3, 2, 3, 1, 2).parallel();
* ArrayList<Integer> list = stream.collect(() -> new ArrayList<Integer>(), accumulator, combiner);
* 这是并行流,虽然使用的择取方法与顺序流相同,但不同的是这里需要先将数据源拆分到各个子任务中
* 根据默认的二分法拆分规则,上面的数据会被拆分为(1)、(2)、(3)、(4)、(5)这五组
* 由于这五组数据位于五个子任务中,那么每个子任务择取数据之时都会先与那个初始状态做汇总。
* 此处给出的初始状态就是一个list,操作目标就是将遇到的元素添加到该list中。
* 因此在每个叶子任务完成后,其对应的元素就被添加到了list中。
* 接下来,使用combiner对子任务汇总。这里的操作是遍历list2中的元素,找出那些不在list1中的元素,并将其添加到list1中。
* 因此最终的汇总结果中只有3、2、1。
*
* seedFactory: 初始状态工厂
* accumulator: 用于择取操作,如果是并行流,则用在每个叶子任务中
* combiner : 用于并行流的合并子任务操作
*
* 注:通常来讲,要求accumulator和combiner相呼应
*/
public static <T, R> TerminalOp<T, R> makeRef(Supplier<R> seedFactory, BiConsumer<R, ? super T> accumulator, BiConsumer<R, R> combiner) {
Objects.requireNonNull(seedFactory);
Objects.requireNonNull(accumulator);
Objects.requireNonNull(combiner);
// 目标sink
class ReducingSink extends Box<R> implements AccumulatingSink<T, R, ReducingSink> {
@Override
public void begin(long size) {
state = seedFactory.get();
}
@Override
public void accept(T t) {
accumulator.accept(state, t);
}
@Override
public void combine(ReducingSink other) {
combiner.accept(state, other.state);
}
}
return new ReduceOp<T, R, ReducingSink>(StreamShape.REFERENCE) {
@Override
public ReducingSink makeSink() {
return new ReducingSink();
}
};
}
/**
* Constructs a {@code TerminalOp} that implements a mutable reduce on
* {@code int} values.
*
* @param <R> The type of the result
* @param supplier a factory to produce a new accumulator of the result type
* @param accumulator a function to incorporate an int into an
* accumulator
* @param combiner a function to combine an accumulator into another
*
* @return A {@code ReduceOp} implementing the reduction
*/
// 有初始状态的消费操作(int类型版本)
public static <R> TerminalOp<Integer, R> makeInt(Supplier<R> supplier, ObjIntConsumer<R> accumulator, BinaryOperator<R> combiner) {
Objects.requireNonNull(supplier);
Objects.requireNonNull(accumulator);
Objects.requireNonNull(combiner);
class ReducingSink extends Box<R> implements AccumulatingSink<Integer, R, ReducingSink>, Sink.OfInt {
@Override
public void begin(long size) {
state = supplier.get();
}
@Override
public void accept(int t) {
accumulator.accept(state, t);
}
@Override
public void combine(ReducingSink other) {
state = combiner.apply(state, other.state);
}
}
return new ReduceOp<Integer, R, ReducingSink>(StreamShape.INT_VALUE) {
@Override
public ReducingSink makeSink() {
return new ReducingSink();
}
};
}
/**
* Constructs a {@code TerminalOp} that implements a mutable reduce on
* {@code long} values.
*
* @param <R> the type of the result
* @param supplier a factory to produce a new accumulator of the result type
* @param accumulator a function to incorporate an int into an
* accumulator
* @param combiner a function to combine an accumulator into another
*
* @return a {@code TerminalOp} implementing the reduction
*/
// 有初始状态的消费操作(long类型版本)
public static <R> TerminalOp<Long, R> makeLong(Supplier<R> supplier, ObjLongConsumer<R> accumulator, BinaryOperator<R> combiner) {
Objects.requireNonNull(supplier);
Objects.requireNonNull(accumulator);
Objects.requireNonNull(combiner);
class ReducingSink extends Box<R> implements AccumulatingSink<Long, R, ReducingSink>, Sink.OfLong {
@Override
public void begin(long size) {
state = supplier.get();
}
@Override
public void accept(long t) {
accumulator.accept(state, t);
}
@Override
public void combine(ReducingSink other) {
state = combiner.apply(state, other.state);
}
}
return new ReduceOp<Long, R, ReducingSink>(StreamShape.LONG_VALUE) {
@Override
public ReducingSink makeSink() {
return new ReducingSink();
}
};
}
/**
* Constructs a {@code TerminalOp} that implements a mutable reduce on
* {@code double} values.
*
* @param <R> the type of the result
* @param supplier a factory to produce a new accumulator of the result type
* @param accumulator a function to incorporate an int into an
* accumulator
* @param combiner a function to combine an accumulator into another
*
* @return a {@code TerminalOp} implementing the reduction
*/
// 有初始状态的消费操作(double类型版本)
public static <R> TerminalOp<Double, R> makeDouble(Supplier<R> supplier, ObjDoubleConsumer<R> accumulator, BinaryOperator<R> combiner) {
Objects.requireNonNull(supplier);
Objects.requireNonNull(accumulator);
Objects.requireNonNull(combiner);
class ReducingSink extends Box<R> implements AccumulatingSink<Double, R, ReducingSink>, Sink.OfDouble {
@Override
public void begin(long size) {
state = supplier.get();
}
@Override
public void accept(double t) {
accumulator.accept(state, t);
}
@Override
public void combine(ReducingSink other) {
state = combiner.apply(state, other.state);
}
}
return new ReduceOp<Double, R, ReducingSink>(StreamShape.DOUBLE_VALUE) {
@Override
public ReducingSink makeSink() {
return new ReducingSink();
}
};
}
/**
* Constructs a {@code TerminalOp} that implements a mutable reduce on
* reference values.
*
* @param <T> the type of the input elements
* @param <I> the type of the intermediate reduction result
* @param collector a {@code Collector} defining the reduction
*
* @return a {@code ReduceOp} implementing the reduction
*/
// 依赖收集器的汇总操作(引用类型版本)
public static <T, I> TerminalOp<T, I> makeRef(Collector<? super T, I, ?> collector) {
// 收集器工厂
Supplier<I> supplier = Objects.requireNonNull(collector).supplier();
// 择取操作:(子)任务如何处理遇到的每个元素
BiConsumer<I, ? super T> accumulator = collector.accumulator();
// 合并操作:并行流中如何合并子任务
BinaryOperator<I> combiner = collector.combiner();
class ReducingSink extends Box<I> implements AccumulatingSink<T, I, ReducingSink> {
@Override
public void begin(long size) {
state = supplier.get();
}
@Override
public void accept(T t) {
accumulator.accept(state, t);
}
@Override
public void combine(ReducingSink other) {
state = combiner.apply(state, other.state);
}
}
return new ReduceOp<T, I, ReducingSink>(StreamShape.REFERENCE) {
@Override
public ReducingSink makeSink() {
return new ReducingSink();
}
@Override
public int getOpFlags() {
return collector.characteristics().contains(Collector.Characteristics.UNORDERED) ? StreamOpFlag.NOT_ORDERED : 0;
}
};
}
/**
* Constructs a {@code TerminalOp} that counts the number of stream
* elements. If the size of the pipeline is known then count is the size
* and there is no need to evaluate the pipeline. If the size of the
* pipeline is non known then count is produced, via reduction, using a
* {@link CountingSink}.
*
* @param <T> the type of the input elements
*
* @return a {@code TerminalOp} implementing the counting
*/
// 计数操作(引用类型版本)
public static <T> TerminalOp<T, Long> makeRefCounting() {
return new ReduceOp<T, Long, CountingSink<T>>(StreamShape.REFERENCE) {
// 返回一个"计数"sink,用来统计经过当前sink过滤后输出的元素数量
@Override
public CountingSink<T> makeSink() {
return new CountingSink.OfRef<>();
}
/*
* 同步处理helper流阶段输出的元素,返回处理后的结果
*
* 为helper构造一个终端sink,并使用该终端sink对spliterator中的数据进行择取,返回最后的处理结果。
* 返回值可能是收集到的元素,也可能只是对过滤后的元素的计数,还可能是其它定制化的结果。
*
* helper : 某个流阶段,通常需要在当前终端操作中处理从helper阶段输出的数据
* spliterator: 待处理的数据的源头,该流迭代器属于helper之前的(depth==0)的流阶段(包含helper阶段)
*/
@Override
public <P_IN> Long evaluateSequential(PipelineHelper<T> helper, Spliterator<P_IN> spliterator) {
// 获取helper流阶段的组合参数
int streamAndOpFlags = helper.getStreamAndOpFlags();
/*
* 如果helper流阶段的元素数量有限,则可以直接返回其元素数量,
* 因为当前终端操作的目的就是获取元素数量。
*/
if(StreamOpFlag.SIZED.isKnown(streamAndOpFlags)) {
return spliterator.getExactSizeIfKnown();
}
// 否则,执行上述makeSink()构造出的终端sink,来统计spliterator流阶段元素数量
return super.evaluateSequential(helper, spliterator);
}
/*
* 并行处理helper流阶段输出的元素,返回处理后的结果
*
* 为helper构造一个终端sink,并使用该终端sink对spliterator中的数据进行择取,返回最后的处理结果。
* 返回值可能是收集到的元素,也可能只是对过滤后的元素的计数,还可能是其它定制化的结果。
*
* helper : 某个流阶段,通常需要在当前终端操作中处理从helper阶段输出的数据
* spliterator: 待处理的数据的源头,该流迭代器属于helper之前的(depth==0)的流阶段(包含helper阶段)
*/
@Override
public <P_IN> Long evaluateParallel(PipelineHelper<T> helper, Spliterator<P_IN> spliterator) {
// 获取helper流阶段的组合参数
int streamAndOpFlags = helper.getStreamAndOpFlags();
// 如果helper流阶段的元素有限,则此处可以直接获取到固定的元素数量
if(StreamOpFlag.SIZED.isKnown(streamAndOpFlags)) {
return spliterator.getExactSizeIfKnown();
}
return super.evaluateParallel(helper, spliterator);
}
@Override
public int getOpFlags() {
return StreamOpFlag.NOT_ORDERED;
}
};
}
/**
* Constructs a {@code TerminalOp} that counts the number of stream
* elements. If the size of the pipeline is known then count is the size
* and there is no need to evaluate the pipeline. If the size of the
* pipeline is non known then count is produced, via reduction, using a
* {@link CountingSink}.
*
* @return a {@code TerminalOp} implementing the counting
*/
// 计数操作(int类型版本)
public static TerminalOp<Integer, Long> makeIntCounting() {
return new ReduceOp<Integer, Long, CountingSink<Integer>>(StreamShape.INT_VALUE) {
// 返回一个"计数"sink,用来统计经过当前sink过滤后输出的元素数量
@Override
public CountingSink<Integer> makeSink() {
return new CountingSink.OfInt();
}
/*
* 同步处理helper流阶段输出的元素,返回处理后的结果
*
* 为helper构造一个终端sink,并使用该终端sink对spliterator中的数据进行择取,返回最后的处理结果。
* 返回值可能是收集到的元素,也可能只是对过滤后的元素的计数,还可能是其它定制化的结果。
*
* helper : 某个流阶段,通常需要在当前终端操作中处理从helper阶段输出的数据
* spliterator: 待处理的数据的源头,该流迭代器属于helper之前的(depth==0)的流阶段(包含helper阶段)
*/
@Override
public <P_IN> Long evaluateSequential(PipelineHelper<Integer> helper, Spliterator<P_IN> spliterator) {
// 获取helper流阶段的组合参数
int streamAndOpFlags = helper.getStreamAndOpFlags();
if(StreamOpFlag.SIZED.isKnown(streamAndOpFlags)) {
return spliterator.getExactSizeIfKnown();
}
return super.evaluateSequential(helper, spliterator);
}
/*
* 并行处理helper流阶段输出的元素,返回处理后的结果
*
* 为helper构造一个终端sink,并使用该终端sink对spliterator中的数据进行择取,返回最后的处理结果。
* 返回值可能是收集到的元素,也可能只是对过滤后的元素的计数,还可能是其它定制化的结果。
*
* helper : 某个流阶段,通常需要在当前终端操作中处理从helper阶段输出的数据
* spliterator: 待处理的数据的源头,该流迭代器属于helper之前的(depth==0)的流阶段(包含helper阶段)
*/
@Override
public <P_IN> Long evaluateParallel(PipelineHelper<Integer> helper, Spliterator<P_IN> spliterator) {
// 获取helper流阶段的组合参数
int streamAndOpFlags = helper.getStreamAndOpFlags();
// 如果helper流阶段的元素有限,则此处可以直接获取到固定的元素数量
if(StreamOpFlag.SIZED.isKnown(streamAndOpFlags)) {
return spliterator.getExactSizeIfKnown();
}
return super.evaluateParallel(helper, spliterator);
}
@Override
public int getOpFlags() {
return StreamOpFlag.NOT_ORDERED;
}
};
}
/**
* Constructs a {@code TerminalOp} that counts the number of stream
* elements. If the size of the pipeline is known then count is the size
* and there is no need to evaluate the pipeline. If the size of the
* pipeline is non known then count is produced, via reduction, using a
* {@link CountingSink}.
*
* @return a {@code TerminalOp} implementing the counting
*/
// 计数操作(long类型版本)
public static TerminalOp<Long, Long> makeLongCounting() {
return new ReduceOp<Long, Long, CountingSink<Long>>(StreamShape.LONG_VALUE) {
// 返回一个"计数"sink,用来统计经过当前sink过滤后输出的元素数量
@Override
public CountingSink<Long> makeSink() {
return new CountingSink.OfLong();
}
/*
* 同步处理helper流阶段输出的元素,返回处理后的结果
*
* 为helper构造一个终端sink,并使用该终端sink对spliterator中的数据进行择取,返回最后的处理结果。
* 返回值可能是收集到的元素,也可能只是对过滤后的元素的计数,还可能是其它定制化的结果。
*
* helper : 某个流阶段,通常需要在当前终端操作中处理从helper阶段输出的数据
* spliterator: 待处理的数据的源头,该流迭代器属于helper之前的(depth==0)的流阶段(包含helper阶段)
*/
@Override
public <P_IN> Long evaluateSequential(PipelineHelper<Long> helper, Spliterator<P_IN> spliterator) {
// 获取helper流阶段的组合参数
int streamAndOpFlags = helper.getStreamAndOpFlags();
// 如果helper流阶段的元素有限,则此处可以直接获取到固定的元素数量
if(StreamOpFlag.SIZED.isKnown(streamAndOpFlags)) {
return spliterator.getExactSizeIfKnown();
}
return super.evaluateSequential(helper, spliterator);
}
/*
* 并行处理helper流阶段输出的元素,返回处理后的结果
*
* 为helper构造一个终端sink,并使用该终端sink对spliterator中的数据进行择取,返回最后的处理结果。
* 返回值可能是收集到的元素,也可能只是对过滤后的元素的计数,还可能是其它定制化的结果。
*
* helper : 某个流阶段,通常需要在当前终端操作中处理从helper阶段输出的数据
* spliterator: 待处理的数据的源头,该流迭代器属于helper之前的(depth==0)的流阶段(包含helper阶段)
*/
@Override
public <P_IN> Long evaluateParallel(PipelineHelper<Long> helper, Spliterator<P_IN> spliterator) {
// 获取helper流阶段的组合参数
int streamAndOpFlags = helper.getStreamAndOpFlags();
// 如果helper流阶段的元素有限,则此处可以直接获取到固定的元素数量
if(StreamOpFlag.SIZED.isKnown(streamAndOpFlags)) {
return spliterator.getExactSizeIfKnown();
}
return super.evaluateParallel(helper, spliterator);
}
@Override
public int getOpFlags() {
return StreamOpFlag.NOT_ORDERED;
}
};
}
/**
* Constructs a {@code TerminalOp} that counts the number of stream
* elements. If the size of the pipeline is known then count is the size
* and there is no need to evaluate the pipeline. If the size of the
* pipeline is non known then count is produced, via reduction, using a
* {@link CountingSink}.
*
* @return a {@code TerminalOp} implementing the counting
*/
// 计数操作(double类型版本)
public static TerminalOp<Double, Long> makeDoubleCounting() {
return new ReduceOp<Double, Long, CountingSink<Double>>(StreamShape.DOUBLE_VALUE) {
// 返回一个"计数"sink,用来统计经过当前sink过滤后输出的元素数量
@Override
public CountingSink<Double> makeSink() {
return new CountingSink.OfDouble();
}
/*
* 同步处理helper流阶段输出的元素,返回处理后的结果
*
* 为helper构造一个终端sink,并使用该终端sink对spliterator中的数据进行择取,返回最后的处理结果。
* 返回值可能是收集到的元素,也可能只是对过滤后的元素的计数,还可能是其它定制化的结果。
*
* helper : 某个流阶段,通常需要在当前终端操作中处理从helper阶段输出的数据
* spliterator: 待处理的数据的源头,该流迭代器属于helper之前的(depth==0)的流阶段(包含helper阶段)
*/
@Override
public <P_IN> Long evaluateSequential(PipelineHelper<Double> helper, Spliterator<P_IN> spliterator) {