Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/observable/ObservableScan.java

raw
1 /*
2 * Copyright (c) 2016-present, RxJava Contributors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
5 * compliance with the License. You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the License is
10 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See
11 * the License for the specific language governing permissions and limitations under the License.
12 */
13
14 package io.reactivex.rxjava3.internal.operators.observable;
15
16 import io.reactivex.rxjava3.core.*;
17 import io.reactivex.rxjava3.disposables.Disposable;
18 import io.reactivex.rxjava3.exceptions.Exceptions;
19 import io.reactivex.rxjava3.functions.BiFunction;
20 import io.reactivex.rxjava3.internal.disposables.DisposableHelper;
21 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
22
23 import java.util.Objects;
24
25 public final class ObservableScan<T> extends AbstractObservableWithUpstream<T, T> {
26 final BiFunction<T, T, T> accumulator;
27 public ObservableScan(ObservableSource<T> source, BiFunction<T, T, T> accumulator) {
28 super(source);
29 this.accumulator = accumulator;
30 }
31
32 @Override
33 public void subscribeActual(Observer<? super T> t) {
34 source.subscribe(new ScanObserver<>(t, accumulator));
35 }
36
37 static final class ScanObserver<T> implements Observer<T>, Disposable {
38 final Observer<? super T> downstream;
39 final BiFunction<T, T, T> accumulator;
40
41 Disposable upstream;
42
43 T value;
44
45 boolean done;
46
47 ScanObserver(Observer<? super T> actual, BiFunction<T, T, T> accumulator) {
48 this.downstream = actual;
49 this.accumulator = accumulator;
50 }
51
52 @Override
53 public void onSubscribe(Disposable d) {
54 if (DisposableHelper.validate(this.upstream, d)) {
55 this.upstream = d;
56 downstream.onSubscribe(this);
57 }
58 }
59
60 @Override
61 public void dispose() {
62 upstream.dispose();
63 }
64
65 @Override
66 public boolean isDisposed() {
67 return upstream.isDisposed();
68 }
69
70 @Override
71 public void onNext(T t) {
72 if (done) {
73 return;
74 }
75 final Observer<? super T> a = downstream;
76 T v = value;
77 if (v == null) {
78 value = t;
79 a.onNext(t);
80 } else {
81 T u;
82
83 try {
84 u = Objects.requireNonNull(accumulator.apply(v, t), "The value returned by the accumulator is null");
85 } catch (Throwable e) {
86 Exceptions.throwIfFatal(e);
87 upstream.dispose();
88 onError(e);
89 return;
90 }
91
92 value = u;
93 a.onNext(u);
94 }
95 }
96
97 @Override
98 public void onError(Throwable t) {
99 if (done) {
100 RxJavaPlugins.onError(t);
101 return;
102 }
103 done = true;
104 downstream.onError(t);
105 }
106
107 @Override
108 public void onComplete() {
109 if (done) {
110 return;
111 }
112 done = true;
113 downstream.onComplete();
114 }
115 }
116 }