Debian Codegraph @

src/main/java/io/reactivex/rxjava3/observers/SafeObserver.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.observers;
15
16 import io.reactivex.rxjava3.annotations.NonNull;
17 import io.reactivex.rxjava3.core.Observer;
18 import io.reactivex.rxjava3.disposables.Disposable;
19 import io.reactivex.rxjava3.exceptions.*;
20 import io.reactivex.rxjava3.internal.disposables.*;
21 import io.reactivex.rxjava3.internal.util.ExceptionHelper;
22 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
23
24 /**
25 * Wraps another {@link Observer} and ensures all {@code onXXX} methods conform the protocol
26 * (except the requirement for serialized access).
27 *
28 * @param <T> the value type
29 */
30 public final class SafeObserver<T> implements Observer<T>, Disposable {
31 /** The actual Subscriber. */
32 final Observer<? super T> downstream;
33 /** The subscription. */
34 Disposable upstream;
35 /** Indicates a terminal state. */
36 boolean done;
37
38 /**
39 * Constructs a {@code SafeObserver} by wrapping the given actual {@link Observer}.
40 * @param downstream the actual {@code Observer} to wrap, not {@code null} (not validated)
41 */
42 public SafeObserver(@NonNull Observer<? super T> downstream) {
43 this.downstream = downstream;
44 }
45
46 @Override
47 public void onSubscribe(@NonNull Disposable d) {
48 if (DisposableHelper.validate(this.upstream, d)) {
49 this.upstream = d;
50 try {
51 downstream.onSubscribe(this);
52 } catch (Throwable e) {
53 Exceptions.throwIfFatal(e);
54 done = true;
55 // can't call onError because the actual's state may be corrupt at this point
56 try {
57 d.dispose();
58 } catch (Throwable e1) {
59 Exceptions.throwIfFatal(e1);
60 RxJavaPlugins.onError(new CompositeException(e, e1));
61 return;
62 }
63 RxJavaPlugins.onError(e);
64 }
65 }
66 }
67
68 @Override
69 public void dispose() {
70 upstream.dispose();
71 }
72
73 @Override
74 public boolean isDisposed() {
75 return upstream.isDisposed();
76 }
77
78 @Override
79 public void onNext(@NonNull T t) {
80 if (done) {
81 return;
82 }
83 if (upstream == null) {
84 onNextNoSubscription();
85 return;
86 }
87
88 if (t == null) {
89 Throwable ex = ExceptionHelper.createNullPointerException("onNext called with a null value.");
90 try {
91 upstream.dispose();
92 } catch (Throwable e1) {
93 Exceptions.throwIfFatal(e1);
94 onError(new CompositeException(ex, e1));
95 return;
96 }
97 onError(ex);
98 return;
99 }
100
101 try {
102 downstream.onNext(t);
103 } catch (Throwable e) {
104 Exceptions.throwIfFatal(e);
105 try {
106 upstream.dispose();
107 } catch (Throwable e1) {
108 Exceptions.throwIfFatal(e1);
109 onError(new CompositeException(e, e1));
110 return;
111 }
112 onError(e);
113 }
114 }
115
116 void onNextNoSubscription() {
117 done = true;
118
119 Throwable ex = new NullPointerException("Subscription not set!");
120
121 try {
122 downstream.onSubscribe(EmptyDisposable.INSTANCE);
123 } catch (Throwable e) {
124 Exceptions.throwIfFatal(e);
125 // can't call onError because the actual's state may be corrupt at this point
126 RxJavaPlugins.onError(new CompositeException(ex, e));
127 return;
128 }
129 try {
130 downstream.onError(ex);
131 } catch (Throwable e) {
132 Exceptions.throwIfFatal(e);
133 // if onError failed, all that's left is to report the error to plugins
134 RxJavaPlugins.onError(new CompositeException(ex, e));
135 }
136 }
137
138 @Override
139 public void onError(@NonNull Throwable t) {
140 if (done) {
141 RxJavaPlugins.onError(t);
142 return;
143 }
144 done = true;
145
146 if (upstream == null) {
147 Throwable npe = new NullPointerException("Subscription not set!");
148
149 try {
150 downstream.onSubscribe(EmptyDisposable.INSTANCE);
151 } catch (Throwable e) {
152 Exceptions.throwIfFatal(e);
153 // can't call onError because the actual's state may be corrupt at this point
154 RxJavaPlugins.onError(new CompositeException(t, npe, e));
155 return;
156 }
157 try {
158 downstream.onError(new CompositeException(t, npe));
159 } catch (Throwable e) {
160 Exceptions.throwIfFatal(e);
161 // if onError failed, all that's left is to report the error to plugins
162 RxJavaPlugins.onError(new CompositeException(t, npe, e));
163 }
164 return;
165 }
166
167 if (t == null) {
168 t = ExceptionHelper.createNullPointerException("onError called with a null Throwable.");
169 }
170
171 try {
172 downstream.onError(t);
173 } catch (Throwable ex) {
174 Exceptions.throwIfFatal(ex);
175
176 RxJavaPlugins.onError(new CompositeException(t, ex));
177 }
178 }
179
180 @Override
181 public void onComplete() {
182 if (done) {
183 return;
184 }
185
186 done = true;
187
188 if (upstream == null) {
189 onCompleteNoSubscription();
190 return;
191 }
192
193 try {
194 downstream.onComplete();
195 } catch (Throwable e) {
196 Exceptions.throwIfFatal(e);
197 RxJavaPlugins.onError(e);
198 }
199 }
200
201 void onCompleteNoSubscription() {
202
203 Throwable ex = new NullPointerException("Subscription not set!");
204
205 try {
206 downstream.onSubscribe(EmptyDisposable.INSTANCE);
207 } catch (Throwable e) {
208 Exceptions.throwIfFatal(e);
209 // can't call onError because the actual's state may be corrupt at this point
210 RxJavaPlugins.onError(new CompositeException(ex, e));
211 return;
212 }
213 try {
214 downstream.onError(ex);
215 } catch (Throwable e) {
216 Exceptions.throwIfFatal(e);
217 // if onError failed, all that's left is to report the error to plugins
218 RxJavaPlugins.onError(new CompositeException(ex, e));
219 }
220 }
221
222 }