forked from kangjianwei/LearningJDK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDoublePipeline.java
713 lines (618 loc) · 28 KB
/
DoublePipeline.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
/*
* Copyright (c) 2013, 2017, 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.DoubleSummaryStatistics;
import java.util.Objects;
import java.util.OptionalDouble;
import java.util.PrimitiveIterator;
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.function.BiConsumer;
import java.util.function.BinaryOperator;
import java.util.function.DoubleBinaryOperator;
import java.util.function.DoubleConsumer;
import java.util.function.DoubleFunction;
import java.util.function.DoublePredicate;
import java.util.function.DoubleToIntFunction;
import java.util.function.DoubleToLongFunction;
import java.util.function.DoubleUnaryOperator;
import java.util.function.IntFunction;
import java.util.function.ObjDoubleConsumer;
import java.util.function.Supplier;
/**
* Abstract base class for an intermediate pipeline stage or pipeline source
* stage implementing whose elements are of type {@code double}.
*
* @param <E_IN> type of elements in the upstream source
*
* @since 1.8
*/
// double类型的流水线
abstract class DoublePipeline<E_IN>
extends AbstractPipeline<E_IN, Double, DoubleStream>
implements DoubleStream {
/**
* Constructor for the head of a stream pipeline.
*
* @param source {@code Supplier<Spliterator>} describing the stream source
* @param sourceFlags the source flags for the stream source, described in
* {@link StreamOpFlag}
*/
// 构造Stream,常用于构建流的源头阶段(HEAD),需要从source中提取Spliterator
DoublePipeline(Supplier<? extends Spliterator<Double>> source, int sourceFlags, boolean parallel) {
super(source, sourceFlags, parallel);
}
/**
* Constructor for the head of a stream pipeline.
*
* @param source {@code Spliterator} describing the stream source
* @param sourceFlags the source flags for the stream source, described in
* {@link StreamOpFlag}
*/
// 构造Stream,常用于构建流的源头阶段(HEAD)
DoublePipeline(Spliterator<Double> source, int sourceFlags, boolean parallel) {
super(source, sourceFlags, parallel);
}
/**
* Constructor for appending an intermediate operation onto an existing
* pipeline.
*
* @param upstream the upstream element source.
* @param opFlags the operation flags
*/
// 构造Stream,常用于构建流的中间阶段,包括有状态流和无状态流。upstream是调用此构造方法的流
DoublePipeline(AbstractPipeline<?, E_IN, ?> upstream, int opFlags) {
super(upstream, opFlags);
}
/*▼ 创建流的源头阶段 ████████████████████████████████████████████████████████████████████████████████┓ */
@Override
public final DoubleStream takeWhile(DoublePredicate predicate) {
return WhileOps.makeTakeWhileDouble(this, predicate);
}
@Override
public final DoubleStream dropWhile(DoublePredicate predicate) {
return WhileOps.makeDropWhileDouble(this, predicate);
}
/*▲ 创建流的源头阶段 ████████████████████████████████████████████████████████████████████████████████┛ */
/*▼ 中间操作-无状态 ████████████████████████████████████████████████████████████████████████████████┓ */
// 筛选数据
@Override
public final DoubleStream filter(DoublePredicate predicate) {
Objects.requireNonNull(predicate);
return new StatelessOp<Double>(this, StreamShape.DOUBLE_VALUE, StreamOpFlag.NOT_SIZED) {
@Override
Sink<Double> opWrapSink(int flags, Sink<Double> sink) {
return new Sink.ChainedDouble<Double>(sink) {
@Override
public void begin(long size) {
downstream.begin(-1);
}
@Override
public void accept(double t) {
if(predicate.test(t))
downstream.accept(t);
}
};
}
};
}
// 映射数据
@Override
public final DoubleStream map(DoubleUnaryOperator mapper) {
Objects.requireNonNull(mapper);
return new StatelessOp<Double>(this, StreamShape.DOUBLE_VALUE, StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT) {
@Override
Sink<Double> opWrapSink(int flags, Sink<Double> sink) {
return new Sink.ChainedDouble<Double>(sink) {
@Override
public void accept(double t) {
downstream.accept(mapper.applyAsDouble(t));
}
};
}
};
}
// 映射数据
@Override
public final IntStream mapToInt(DoubleToIntFunction mapper) {
Objects.requireNonNull(mapper);
return new IntPipeline.StatelessOp<Double>(this, StreamShape.DOUBLE_VALUE, StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT) {
@Override
Sink<Double> opWrapSink(int flags, Sink<Integer> sink) {
return new Sink.ChainedDouble<Integer>(sink) {
@Override
public void accept(double t) {
downstream.accept(mapper.applyAsInt(t));
}
};
}
};
}
// 映射数据
@Override
public final LongStream mapToLong(DoubleToLongFunction mapper) {
Objects.requireNonNull(mapper);
return new LongPipeline.StatelessOp<Double>(this, StreamShape.DOUBLE_VALUE, StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT) {
@Override
Sink<Double> opWrapSink(int flags, Sink<Long> sink) {
return new Sink.ChainedDouble<Long>(sink) {
@Override
public void accept(double t) {
downstream.accept(mapper.applyAsLong(t));
}
};
}
};
}
// 映射数据
@Override
public final <U> Stream<U> mapToObj(DoubleFunction<? extends U> mapper) {
Objects.requireNonNull(mapper);
return mapToObj(mapper, StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT);
}
// 映射数据
private <U> Stream<U> mapToObj(DoubleFunction<? extends U> mapper, int opFlags) {
return new ReferencePipeline.StatelessOp<Double, U>(this, StreamShape.DOUBLE_VALUE, opFlags) {
@Override
Sink<Double> opWrapSink(int flags, Sink<U> sink) {
return new Sink.ChainedDouble<U>(sink) {
@Override
public void accept(double t) {
downstream.accept(mapper.apply(t));
}
};
}
};
}
// 数据降维
@Override
public final DoubleStream flatMap(DoubleFunction<? extends DoubleStream> mapper) {
Objects.requireNonNull(mapper);
return new StatelessOp<Double>(this, StreamShape.DOUBLE_VALUE, StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT | StreamOpFlag.NOT_SIZED) {
@Override
Sink<Double> opWrapSink(int flags, Sink<Double> sink) {
return new Sink.ChainedDouble<Double>(sink) {
// true if cancellationRequested() has been called
boolean cancellationRequestedCalled;
// cache the consumer to avoid creation on every accepted element
DoubleConsumer downstreamAsDouble = downstream::accept;
@Override
public void begin(long size) {
downstream.begin(-1);
}
@Override
public void accept(double t) {
try(DoubleStream result = mapper.apply(t)) {
if(result != null) {
if(!cancellationRequestedCalled) {
result.sequential().forEach(downstreamAsDouble);
} else {
var s = result.sequential().spliterator();
do {
} while(!downstream.cancellationRequested() && s.tryAdvance(downstreamAsDouble));
}
}
}
}
@Override
public boolean cancellationRequested() {
// If this method is called then an operation within the stream
// pipeline is short-circuiting (see AbstractPipeline.copyInto).
// Note that we cannot differentiate between an upstream or
// downstream operation
cancellationRequestedCalled = true;
return downstream.cancellationRequested();
}
};
}
};
}
// 用于查看流的内部结构,不会对流的结构产生影响
@Override
public final DoubleStream peek(DoubleConsumer action) {
Objects.requireNonNull(action);
return new StatelessOp<Double>(this, StreamShape.DOUBLE_VALUE, 0) {
@Override
Sink<Double> opWrapSink(int flags, Sink<Double> sink) {
return new Sink.ChainedDouble<Double>(sink) {
@Override
public void accept(double t) {
action.accept(t);
downstream.accept(t);
}
};
}
};
}
// 装箱
@Override
public final Stream<Double> boxed() {
return mapToObj(Double::valueOf, 0);
}
// 中间操作,返回等效的无序流
@Override
public DoubleStream unordered() {
if(!isOrdered())
return this;
return new StatelessOp<Double>(this, StreamShape.DOUBLE_VALUE, StreamOpFlag.NOT_ORDERED) {
@Override
Sink<Double> opWrapSink(int flags, Sink<Double> sink) {
return sink;
}
};
}
/*▲ 中间操作-无状态 ████████████████████████████████████████████████████████████████████████████████┛ */
/*▼ 中间操作-有状态 ████████████████████████████████████████████████████████████████████████████████┓ */
// 去重
@Override
public final DoubleStream distinct() {
// While functional and quick to implement, this approach is not very efficient.
// An efficient version requires a double-specific map/set implementation.
return boxed().distinct().mapToDouble(i -> (double) i);
}
// 排序(默认升序)
@Override
public final DoubleStream sorted() {
return SortedOps.makeDouble(this);
}
// 只显示前maxSize个元素
@Override
public final DoubleStream limit(long maxSize) {
if(maxSize<0)
throw new IllegalArgumentException(Long.toString(maxSize));
return SliceOps.makeDouble(this, (long) 0, maxSize);
}
// 跳过前n个元素
@Override
public final DoubleStream skip(long n) {
if(n<0)
throw new IllegalArgumentException(Long.toString(n));
if(n == 0)
return this;
else {
long limit = -1;
return SliceOps.makeDouble(this, n, limit);
}
}
/*▲ 中间操作-有状态 ████████████████████████████████████████████████████████████████████████████████┛ */
/*▼ 终端操作-短路操作 ████████████████████████████████████████████████████████████████████████████████┓ */
// 存在元素满足predicate条件
@Override
public final boolean anyMatch(DoublePredicate predicate) {
return evaluate(MatchOps.makeDouble(predicate, MatchOps.MatchKind.ANY));
}
// 所有元素满足predicate条件
@Override
public final boolean allMatch(DoublePredicate predicate) {
return evaluate(MatchOps.makeDouble(predicate, MatchOps.MatchKind.ALL));
}
// 没有元素满足predicate条件
@Override
public final boolean noneMatch(DoublePredicate predicate) {
return evaluate(MatchOps.makeDouble(predicate, MatchOps.MatchKind.NONE));
}
// 找出第一个元素,返回一个可选的操作
@Override
public final OptionalDouble findFirst() {
return evaluate(FindOps.makeDouble(true));
}
// 找到一个元素就返回,往往是第一个元素
@Override
public final OptionalDouble findAny() {
return evaluate(FindOps.makeDouble(false));
}
/*▲ 终端操作-短路操作 ████████████████████████████████████████████████████████████████████████████████┛ */
/*▼ 终端操作-非短路操作 ████████████████████████████████████████████████████████████████████████████████┓ */
// 将数据存入double数组返回
@Override
public final double[] toArray() {
return Nodes.flattenDouble((Node.OfDouble) evaluateToArrayNode(Double[]::new)).asPrimitiveArray();
}
// 遍历,并执行action操作
@Override
public void forEach(DoubleConsumer consumer) {
evaluate(ForEachOps.makeDouble(consumer, false));
}
// 按遭遇顺序遍历,并执行action操作
@Override
public void forEachOrdered(DoubleConsumer consumer) {
evaluate(ForEachOps.makeDouble(consumer, true));
}
// 收纳汇总,两两比对,完成指定动作
@Override
public final OptionalDouble reduce(DoubleBinaryOperator op) {
return evaluate(ReduceOps.makeDouble(op));
}
// 收纳汇总,两两比对,完成op动作。identity是初值,op中的输入类型应当一致
@Override
public final double reduce(double identity, DoubleBinaryOperator op) {
return evaluate(ReduceOps.makeDouble(identity, op));
}
// 收集输出的元素到某个容器
@Override
public final <R> R collect(Supplier<R> supplier, ObjDoubleConsumer<R> accumulator, BiConsumer<R, R> combiner) {
Objects.requireNonNull(combiner);
BinaryOperator<R> operator = (left, right) -> {
combiner.accept(left, right);
return left;
};
return evaluate(ReduceOps.makeDouble(supplier, accumulator, operator));
}
// 计数
@Override
public final long count() {
return evaluate(ReduceOps.makeDoubleCounting());
}
// 求最小值
@Override
public final OptionalDouble min() {
return reduce(Math::min);
}
// 求最大值
@Override
public final OptionalDouble max() {
return reduce(Math::max);
}
// 求和
@Override
public final double sum() {
/*
* In the arrays allocated for the collect operation, index 0
* holds the high-order bits of the running sum, index 1 holds
* the low-order bits of the sum computed via compensated
* summation, and index 2 holds the simple sum used to compute
* the proper result if the stream contains infinite values of
* the same sign.
*/
double[] summation = collect(() -> new double[3], (ll, d) -> {
Collectors.sumWithCompensation(ll, d);
ll[2] += d;
}, (ll, rr) -> {
Collectors.sumWithCompensation(ll, rr[0]);
Collectors.sumWithCompensation(ll, rr[1]);
ll[2] += rr[2];
});
return Collectors.computeFinalSum(summation);
}
/**
* {@inheritDoc}
*
* @implNote The {@code double} format can represent all
* consecutive integers in the range -2<sup>53</sup> to
* 2<sup>53</sup>. If the pipeline has more than 2<sup>53</sup>
* values, the divisor in the average computation will saturate at
* 2<sup>53</sup>, leading to additional numerical errors.
*/
// 求平均值
@Override
public final OptionalDouble average() {
/*
* In the arrays allocated for the collect operation, index 0
* holds the high-order bits of the running sum, index 1 holds
* the low-order bits of the sum computed via compensated
* summation, index 2 holds the number of values seen, index 3
* holds the simple sum.
*/
double[] avg = collect(() -> new double[4], (ll, d) -> {
ll[2]++;
Collectors.sumWithCompensation(ll, d);
ll[3] += d;
}, (ll, rr) -> {
Collectors.sumWithCompensation(ll, rr[0]);
Collectors.sumWithCompensation(ll, rr[1]);
ll[2] += rr[2];
ll[3] += rr[3];
});
return avg[2]>0 ? OptionalDouble.of(Collectors.computeFinalSum(avg) / avg[2]) : OptionalDouble.empty();
}
// 信息统计
@Override
public final DoubleSummaryStatistics summaryStatistics() {
return collect(DoubleSummaryStatistics::new, DoubleSummaryStatistics::accept, DoubleSummaryStatistics::combine);
}
/*▲ 终端操作-非短路操作 ████████████████████████████████████████████████████████████████████████████████┛ */
/*▼ 实现DoubleStream接口 ████████████████████████████████████████████████████████████████████████████████┓ */
@Override
public final PrimitiveIterator.OfDouble iterator() {
return Spliterators.iterator(spliterator());
}
@Override
public final Spliterator.OfDouble spliterator() {
return adapt(super.spliterator());
}
/*▲ 实现DoubleStream接口 ████████████████████████████████████████████████████████████████████████████████┛ */
// 返回流的形状
@Override
final StreamShape getOutputShape() {
return StreamShape.DOUBLE_VALUE;
}
// 逐个择取元素,每次择取之前都要先判断是否应当停止接收数据
@Override
final boolean forEachWithCancel(Spliterator<Double> spliterator, Sink<Double> sink) {
Spliterator.OfDouble spl = adapt(spliterator);
DoubleConsumer adaptedSink = adapt(sink);
boolean cancelled;
do {
} while(!(cancelled = sink.cancellationRequested()) && spl.tryAdvance(adaptedSink));
return cancelled;
}
// 返回第(3)、(4)类Node(固定长度Node和可变长度Node)
@Override
final Node.Builder<Double> makeNodeBuilder(long exactSizeIfKnown, IntFunction<Double[]> generator) {
return Nodes.doubleBuilder(exactSizeIfKnown);
}
// 并行收集元素。如果流水线上存在有状态的中间操作,则优化计算过程
@Override
final <P_IN> Node<Double> evaluateToNode(PipelineHelper<Double> helper, Spliterator<P_IN> spliterator, boolean flattenTree, IntFunction<Double[]> generator) {
return Nodes.collectDouble(helper, spliterator, flattenTree);
}
@Override
final <P_IN> Spliterator<Double> wrap(PipelineHelper<Double> ph, Supplier<Spliterator<P_IN>> supplier, boolean isParallel) {
return new StreamSpliterators.DoubleWrappingSpliterator<>(ph, supplier, isParallel);
}
@Override
@SuppressWarnings("unchecked")
final Spliterator.OfDouble lazySpliterator(Supplier<? extends Spliterator<Double>> supplier) {
return new StreamSpliterators.DelegatingSpliterator.OfDouble((Supplier<Spliterator.OfDouble>) supplier);
}
/**
* Adapt a {@code Sink<Double> to a {@code DoubleConsumer}, ideally simply
* by casting.
*/
private static DoubleConsumer adapt(Sink<Double> sink) {
if(sink instanceof DoubleConsumer) {
return (DoubleConsumer) sink;
} else {
if(Tripwire.ENABLED)
Tripwire.trip(AbstractPipeline.class, "using DoubleStream.adapt(Sink<Double> s)");
return sink::accept;
}
}
/**
* Adapt a {@code Spliterator<Double>} to a {@code Spliterator.OfDouble}.
*
* @implNote The implementation attempts to cast to a Spliterator.OfDouble, and throws
* an exception if this cast is not possible.
*/
private static Spliterator.OfDouble adapt(Spliterator<Double> s) {
if(s instanceof Spliterator.OfDouble) {
return (Spliterator.OfDouble) s;
} else {
if(Tripwire.ENABLED)
Tripwire.trip(AbstractPipeline.class, "using DoubleStream.adapt(Spliterator<Double> s)");
throw new UnsupportedOperationException("DoubleStream.adapt(Spliterator<Double> s)");
}
}
/**
* Source stage of a DoubleStream
*
* @param <E_IN> type of elements in the upstream source
*/
// 流的源头阶段,包含了数据源和可以对数据源执行的遍历、分割操作
static class Head<E_IN> extends DoublePipeline<E_IN> {
/**
* Constructor for the source stage of a DoubleStream.
*
* @param source {@code Supplier<Spliterator>} describing the stream
* source
* @param sourceFlags the source flags for the stream source, described
* in {@link StreamOpFlag}
* @param parallel {@code true} if the pipeline is parallel
*/
Head(Supplier<? extends Spliterator<Double>> source, int sourceFlags, boolean parallel) {
super(source, sourceFlags, parallel);
}
/**
* Constructor for the source stage of a DoubleStream.
*
* @param source {@code Spliterator} describing the stream source
* @param sourceFlags the source flags for the stream source, described
* in {@link StreamOpFlag}
* @param parallel {@code true} if the pipeline is parallel
*/
Head(Spliterator<Double> source, int sourceFlags, boolean parallel) {
super(source, sourceFlags, parallel);
}
@Override
final boolean opIsStateful() {
throw new UnsupportedOperationException();
}
@Override
final Sink<E_IN> opWrapSink(int flags, Sink<Double> sink) {
throw new UnsupportedOperationException();
}
// Optimized sequential terminal operations for the head of the pipeline
@Override
public void forEach(DoubleConsumer consumer) {
if(!isParallel()) {
adapt(sourceStageSpliterator()).forEachRemaining(consumer);
} else {
super.forEach(consumer);
}
}
@Override
public void forEachOrdered(DoubleConsumer consumer) {
if(!isParallel()) {
adapt(sourceStageSpliterator()).forEachRemaining(consumer);
} else {
super.forEachOrdered(consumer);
}
}
}
/**
* Base class for a stateless intermediate stage of a DoubleStream.
*
* @param <E_IN> type of elements in the upstream source
*
* @since 1.8
*/
// 流的中间阶段-无状态流
abstract static class StatelessOp<E_IN> extends DoublePipeline<E_IN> {
/**
* Construct a new DoubleStream by appending a stateless intermediate
* operation to an existing stream.
*
* @param upstream the upstream pipeline stage
* @param inputShape the stream shape for the upstream pipeline stage
* @param opFlags operation flags for the new stage
*/
StatelessOp(AbstractPipeline<?, E_IN, ?> upstream, StreamShape inputShape, int opFlags) {
super(upstream, opFlags);
assert upstream.getOutputShape() == inputShape;
}
@Override
final boolean opIsStateful() {
return false;
}
}
/**
* Base class for a stateful intermediate stage of a DoubleStream.
*
* @param <E_IN> type of elements in the upstream source
*
* @since 1.8
*/
// 流的中间阶段-有状态流
abstract static class StatefulOp<E_IN> extends DoublePipeline<E_IN> {
/**
* Construct a new DoubleStream by appending a stateful intermediate
* operation to an existing stream.
*
* @param upstream the upstream pipeline stage
* @param inputShape the stream shape for the upstream pipeline stage
* @param opFlags operation flags for the new stage
*/
StatefulOp(AbstractPipeline<?, E_IN, ?> upstream, StreamShape inputShape, int opFlags) {
super(upstream, opFlags);
assert upstream.getOutputShape() == inputShape;
}
@Override
abstract <P_IN> Node<Double> opEvaluateParallel(PipelineHelper<Double> helper, Spliterator<P_IN> spliterator, IntFunction<Double[]> generator);
@Override
final boolean opIsStateful() {
return true;
}
}
}