Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/mixed/FlowableSwitchMapMaybe.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.mixed;
15
16 import java.util.Objects;
17 import java.util.concurrent.atomic.*;
18
19 import org.reactivestreams.*;
20
21 import io.reactivex.rxjava3.core.*;
22 import io.reactivex.rxjava3.disposables.Disposable;
23 import io.reactivex.rxjava3.exceptions.Exceptions;
24 import io.reactivex.rxjava3.functions.Function;
25 import io.reactivex.rxjava3.internal.disposables.DisposableHelper;
26 import io.reactivex.rxjava3.internal.subscriptions.SubscriptionHelper;
27 import io.reactivex.rxjava3.internal.util.*;
28 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
29
30 /**
31 * Maps the upstream items into {@link MaybeSource}s and switches (subscribes) to the newer ones
32 * while disposing the older ones and emits the latest success value if available, optionally delaying
33 * errors from the main source or the inner sources.
34 * <p>History: 2.1.11 - experimental
35 * @param <T> the upstream value type
36 * @param <R> the downstream value type
37 * @since 2.2
38 */
39 public final class FlowableSwitchMapMaybe<T, R> extends Flowable<R> {
40
41 final Flowable<T> source;
42
43 final Function<? super T, ? extends MaybeSource<? extends R>> mapper;
44
45 final boolean delayErrors;
46
47 public FlowableSwitchMapMaybe(Flowable<T> source,
48 Function<? super T, ? extends MaybeSource<? extends R>> mapper,
49 boolean delayErrors) {
50 this.source = source;
51 this.mapper = mapper;
52 this.delayErrors = delayErrors;
53 }
54
55 @Override
56 protected void subscribeActual(Subscriber<? super R> s) {
57 source.subscribe(new SwitchMapMaybeSubscriber<>(s, mapper, delayErrors));
58 }
59
60 static final class SwitchMapMaybeSubscriber<T, R> extends AtomicInteger
61 implements FlowableSubscriber<T>, Subscription {
62
63 private static final long serialVersionUID = -5402190102429853762L;
64
65 final Subscriber<? super R> downstream;
66
67 final Function<? super T, ? extends MaybeSource<? extends R>> mapper;
68
69 final boolean delayErrors;
70
71 final AtomicThrowable errors;
72
73 final AtomicLong requested;
74
75 final AtomicReference<SwitchMapMaybeObserver<R>> inner;
76
77 static final SwitchMapMaybeObserver<Object> INNER_DISPOSED =
78 new SwitchMapMaybeObserver<>(null);
79
80 Subscription upstream;
81
82 volatile boolean done;
83
84 volatile boolean cancelled;
85
86 long emitted;
87
88 SwitchMapMaybeSubscriber(Subscriber<? super R> downstream,
89 Function<? super T, ? extends MaybeSource<? extends R>> mapper,
90 boolean delayErrors) {
91 this.downstream = downstream;
92 this.mapper = mapper;
93 this.delayErrors = delayErrors;
94 this.errors = new AtomicThrowable();
95 this.requested = new AtomicLong();
96 this.inner = new AtomicReference<>();
97 }
98
99 @Override
100 public void onSubscribe(Subscription s) {
101 if (SubscriptionHelper.validate(upstream, s)) {
102 upstream = s;
103 downstream.onSubscribe(this);
104 s.request(Long.MAX_VALUE);
105 }
106 }
107
108 @Override
109 @SuppressWarnings({ "unchecked", "rawtypes" })
110 public void onNext(T t) {
111 SwitchMapMaybeObserver<R> current = inner.get();
112 if (current != null) {
113 current.dispose();
114 }
115
116 MaybeSource<? extends R> ms;
117
118 try {
119 ms = Objects.requireNonNull(mapper.apply(t), "The mapper returned a null MaybeSource");
120 } catch (Throwable ex) {
121 Exceptions.throwIfFatal(ex);
122 upstream.cancel();
123 inner.getAndSet((SwitchMapMaybeObserver)INNER_DISPOSED);
124 onError(ex);
125 return;
126 }
127
128 SwitchMapMaybeObserver<R> observer = new SwitchMapMaybeObserver<>(this);
129
130 for (;;) {
131 current = inner.get();
132 if (current == INNER_DISPOSED) {
133 break;
134 }
135 if (inner.compareAndSet(current, observer)) {
136 ms.subscribe(observer);
137 break;
138 }
139 }
140 }
141
142 @Override
143 public void onError(Throwable t) {
144 if (errors.tryAddThrowableOrReport(t)) {
145 if (!delayErrors) {
146 disposeInner();
147 }
148 done = true;
149 drain();
150 }
151 }
152
153 @Override
154 public void onComplete() {
155 done = true;
156 drain();
157 }
158
159 @SuppressWarnings({ "unchecked", "rawtypes" })
160 void disposeInner() {
161 SwitchMapMaybeObserver<R> current = inner.getAndSet((SwitchMapMaybeObserver)INNER_DISPOSED);
162 if (current != null && current != INNER_DISPOSED) {
163 current.dispose();
164 }
165 }
166
167 @Override
168 public void request(long n) {
169 BackpressureHelper.add(requested, n);
170 drain();
171 }
172
173 @Override
174 public void cancel() {
175 cancelled = true;
176 upstream.cancel();
177 disposeInner();
178 errors.tryTerminateAndReport();
179 }
180
181 void innerError(SwitchMapMaybeObserver<R> sender, Throwable ex) {
182 if (inner.compareAndSet(sender, null)) {
183 if (errors.tryAddThrowableOrReport(ex)) {
184 if (!delayErrors) {
185 upstream.cancel();
186 disposeInner();
187 }
188 drain();
189 }
190 } else {
191 RxJavaPlugins.onError(ex);
192 }
193 }
194
195 void innerComplete(SwitchMapMaybeObserver<R> sender) {
196 if (inner.compareAndSet(sender, null)) {
197 drain();
198 }
199 }
200
201 void drain() {
202 if (getAndIncrement() != 0) {
203 return;
204 }
205
206 int missed = 1;
207 Subscriber<? super R> downstream = this.downstream;
208 AtomicThrowable errors = this.errors;
209 AtomicReference<SwitchMapMaybeObserver<R>> inner = this.inner;
210 AtomicLong requested = this.requested;
211 long emitted = this.emitted;
212
213 for (;;) {
214
215 for (;;) {
216 if (cancelled) {
217 return;
218 }
219
220 if (errors.get() != null) {
221 if (!delayErrors) {
222 errors.tryTerminateConsumer(downstream);
223 return;
224 }
225 }
226
227 boolean d = done;
228 SwitchMapMaybeObserver<R> current = inner.get();
229 boolean empty = current == null;
230
231 if (d && empty) {
232 errors.tryTerminateConsumer(downstream);
233 return;
234 }
235
236 if (empty || current.item == null || emitted == requested.get()) {
237 break;
238 }
239
240 inner.compareAndSet(current, null);
241
242 downstream.onNext(current.item);
243
244 emitted++;
245 }
246
247 this.emitted = emitted;
248 missed = addAndGet(-missed);
249 if (missed == 0) {
250 break;
251 }
252 }
253 }
254
255 static final class SwitchMapMaybeObserver<R>
256 extends AtomicReference<Disposable> implements MaybeObserver<R> {
257
258 private static final long serialVersionUID = 8042919737683345351L;
259
260 final SwitchMapMaybeSubscriber<?, R> parent;
261
262 volatile R item;
263
264 SwitchMapMaybeObserver(SwitchMapMaybeSubscriber<?, R> parent) {
265 this.parent = parent;
266 }
267
268 @Override
269 public void onSubscribe(Disposable d) {
270 DisposableHelper.setOnce(this, d);
271 }
272
273 @Override
274 public void onSuccess(R t) {
275 item = t;
276 parent.drain();
277 }
278
279 @Override
280 public void onError(Throwable e) {
281 parent.innerError(this, e);
282 }
283
284 @Override
285 public void onComplete() {
286 parent.innerComplete(this);
287 }
288
289 void dispose() {
290 DisposableHelper.dispose(this);
291 }
292 }
293 }
294 }