Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/observable/ObservableDoOnEach.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.*;
19 import io.reactivex.rxjava3.functions.*;
20 import io.reactivex.rxjava3.internal.disposables.DisposableHelper;
21 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
22
23 public final class ObservableDoOnEach<T> extends AbstractObservableWithUpstream<T, T> {
24 final Consumer<? super T> onNext;
25 final Consumer<? super Throwable> onError;
26 final Action onComplete;
27 final Action onAfterTerminate;
28
29 public ObservableDoOnEach(ObservableSource<T> source, Consumer<? super T> onNext,
30 Consumer<? super Throwable> onError,
31 Action onComplete,
32 Action onAfterTerminate) {
33 super(source);
34 this.onNext = onNext;
35 this.onError = onError;
36 this.onComplete = onComplete;
37 this.onAfterTerminate = onAfterTerminate;
38 }
39
40 @Override
41 public void subscribeActual(Observer<? super T> t) {
42 source.subscribe(new DoOnEachObserver<>(t, onNext, onError, onComplete, onAfterTerminate));
43 }
44
45 static final class DoOnEachObserver<T> implements Observer<T>, Disposable {
46 final Observer<? super T> downstream;
47 final Consumer<? super T> onNext;
48 final Consumer<? super Throwable> onError;
49 final Action onComplete;
50 final Action onAfterTerminate;
51
52 Disposable upstream;
53
54 boolean done;
55
56 DoOnEachObserver(
57 Observer<? super T> actual,
58 Consumer<? super T> onNext,
59 Consumer<? super Throwable> onError,
60 Action onComplete,
61 Action onAfterTerminate) {
62 this.downstream = actual;
63 this.onNext = onNext;
64 this.onError = onError;
65 this.onComplete = onComplete;
66 this.onAfterTerminate = onAfterTerminate;
67 }
68
69 @Override
70 public void onSubscribe(Disposable d) {
71 if (DisposableHelper.validate(this.upstream, d)) {
72 this.upstream = d;
73 downstream.onSubscribe(this);
74 }
75 }
76
77 @Override
78 public void dispose() {
79 upstream.dispose();
80 }
81
82 @Override
83 public boolean isDisposed() {
84 return upstream.isDisposed();
85 }
86
87 @Override
88 public void onNext(T t) {
89 if (done) {
90 return;
91 }
92 try {
93 onNext.accept(t);
94 } catch (Throwable e) {
95 Exceptions.throwIfFatal(e);
96 upstream.dispose();
97 onError(e);
98 return;
99 }
100
101 downstream.onNext(t);
102 }
103
104 @Override
105 public void onError(Throwable t) {
106 if (done) {
107 RxJavaPlugins.onError(t);
108 return;
109 }
110 done = true;
111 try {
112 onError.accept(t);
113 } catch (Throwable e) {
114 Exceptions.throwIfFatal(e);
115 t = new CompositeException(t, e);
116 }
117 downstream.onError(t);
118
119 try {
120 onAfterTerminate.run();
121 } catch (Throwable e) {
122 Exceptions.throwIfFatal(e);
123 RxJavaPlugins.onError(e);
124 }
125 }
126
127 @Override
128 public void onComplete() {
129 if (done) {
130 return;
131 }
132 try {
133 onComplete.run();
134 } catch (Throwable e) {
135 Exceptions.throwIfFatal(e);
136 onError(e);
137 return;
138 }
139
140 done = true;
141 downstream.onComplete();
142
143 try {
144 onAfterTerminate.run();
145 } catch (Throwable e) {
146 Exceptions.throwIfFatal(e);
147 RxJavaPlugins.onError(e);
148 }
149 }
150 }
151 }