Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/mixed/ObservableConcatMapMaybe.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.AtomicReference;
18
19 import io.reactivex.rxjava3.core.*;
20 import io.reactivex.rxjava3.disposables.Disposable;
21 import io.reactivex.rxjava3.exceptions.Exceptions;
22 import io.reactivex.rxjava3.functions.Function;
23 import io.reactivex.rxjava3.internal.disposables.DisposableHelper;
24 import io.reactivex.rxjava3.internal.util.*;
25 import io.reactivex.rxjava3.operators.SimpleQueue;
26
27 /**
28 * Maps each upstream item into a {@link MaybeSource}, subscribes to them one after the other terminates
29 * and relays their success values, optionally delaying any errors till the main and inner sources
30 * terminate.
31 * <p>History: 2.1.11 - experimental
32 * @param <T> the upstream element type
33 * @param <R> the output element type
34 * @since 2.2
35 */
36 public final class ObservableConcatMapMaybe<T, R> extends Observable<R> {
37
38 final Observable<T> source;
39
40 final Function<? super T, ? extends MaybeSource<? extends R>> mapper;
41
42 final ErrorMode errorMode;
43
44 final int prefetch;
45
46 public ObservableConcatMapMaybe(Observable<T> source,
47 Function<? super T, ? extends MaybeSource<? extends R>> mapper,
48 ErrorMode errorMode, int prefetch) {
49 this.source = source;
50 this.mapper = mapper;
51 this.errorMode = errorMode;
52 this.prefetch = prefetch;
53 }
54
55 @Override
56 protected void subscribeActual(Observer<? super R> observer) {
57 if (!ScalarXMapZHelper.tryAsMaybe(source, mapper, observer)) {
58 source.subscribe(new ConcatMapMaybeMainObserver<>(observer, mapper, prefetch, errorMode));
59 }
60 }
61
62 static final class ConcatMapMaybeMainObserver<T, R>
63 extends ConcatMapXMainObserver<T> {
64
65 private static final long serialVersionUID = -9140123220065488293L;
66
67 final Observer<? super R> downstream;
68
69 final Function<? super T, ? extends MaybeSource<? extends R>> mapper;
70
71 final ConcatMapMaybeObserver<R> inner;
72
73 R item;
74
75 volatile int state;
76
77 /** No inner MaybeSource is running. */
78 static final int STATE_INACTIVE = 0;
79 /** An inner MaybeSource is running but there are no results yet. */
80 static final int STATE_ACTIVE = 1;
81 /** The inner MaybeSource succeeded with a value in {@link #item}. */
82 static final int STATE_RESULT_VALUE = 2;
83
84 ConcatMapMaybeMainObserver(Observer<? super R> downstream,
85 Function<? super T, ? extends MaybeSource<? extends R>> mapper,
86 int prefetch, ErrorMode errorMode) {
87 super(prefetch, errorMode);
88 this.downstream = downstream;
89 this.mapper = mapper;
90 this.inner = new ConcatMapMaybeObserver<>(this);
91 }
92
93 @Override
94 void onSubscribeDownstream() {
95 downstream.onSubscribe(this);
96 }
97
98 @Override
99 void clearValue() {
100 item = null;
101 }
102
103 void innerSuccess(R item) {
104 this.item = item;
105 this.state = STATE_RESULT_VALUE;
106 drain();
107 }
108
109 void innerComplete() {
110 this.state = STATE_INACTIVE;
111 drain();
112 }
113
114 void innerError(Throwable ex) {
115 if (errors.tryAddThrowableOrReport(ex)) {
116 if (errorMode != ErrorMode.END) {
117 upstream.dispose();
118 }
119 this.state = STATE_INACTIVE;
120 drain();
121 }
122 }
123
124 @Override
125 void disposeInner() {
126 inner.dispose();
127 }
128
129 @Override
130 void drain() {
131 if (getAndIncrement() != 0) {
132 return;
133 }
134
135 int missed = 1;
136 Observer<? super R> downstream = this.downstream;
137 ErrorMode errorMode = this.errorMode;
138 SimpleQueue<T> queue = this.queue;
139 AtomicThrowable errors = this.errors;
140
141 for (;;) {
142
143 for (;;) {
144 if (disposed) {
145 queue.clear();
146 item = null;
147 break;
148 }
149
150 int s = state;
151
152 if (errors.get() != null) {
153 if (errorMode == ErrorMode.IMMEDIATE
154 || (errorMode == ErrorMode.BOUNDARY && s == STATE_INACTIVE)) {
155 queue.clear();
156 item = null;
157 errors.tryTerminateConsumer(downstream);
158 return;
159 }
160 }
161
162 if (s == STATE_INACTIVE) {
163 boolean d = done;
164 T v;
165
166 try {
167 v = queue.poll();
168 } catch (Throwable ex) {
169 Exceptions.throwIfFatal(ex);
170 disposed = true;
171 upstream.dispose();
172 errors.tryAddThrowableOrReport(ex);
173 errors.tryTerminateConsumer(downstream);
174 return;
175 }
176 boolean empty = v == null;
177
178 if (d && empty) {
179 errors.tryTerminateConsumer(downstream);
180 return;
181 }
182
183 if (empty) {
184 break;
185 }
186
187 MaybeSource<? extends R> ms;
188
189 try {
190 ms = Objects.requireNonNull(mapper.apply(v), "The mapper returned a null MaybeSource");
191 } catch (Throwable ex) {
192 Exceptions.throwIfFatal(ex);
193 upstream.dispose();
194 queue.clear();
195 errors.tryAddThrowableOrReport(ex);
196 errors.tryTerminateConsumer(downstream);
197 return;
198 }
199
200 state = STATE_ACTIVE;
201 ms.subscribe(inner);
202 break;
203 } else if (s == STATE_RESULT_VALUE) {
204 R w = item;
205 item = null;
206 downstream.onNext(w);
207
208 state = STATE_INACTIVE;
209 } else {
210 break;
211 }
212 }
213
214 missed = addAndGet(-missed);
215 if (missed == 0) {
216 break;
217 }
218 }
219 }
220
221 static final class ConcatMapMaybeObserver<R>
222 extends AtomicReference<Disposable>
223 implements MaybeObserver<R> {
224
225 private static final long serialVersionUID = -3051469169682093892L;
226
227 final ConcatMapMaybeMainObserver<?, R> parent;
228
229 ConcatMapMaybeObserver(ConcatMapMaybeMainObserver<?, R> parent) {
230 this.parent = parent;
231 }
232
233 @Override
234 public void onSubscribe(Disposable d) {
235 DisposableHelper.replace(this, d);
236 }
237
238 @Override
239 public void onSuccess(R t) {
240 parent.innerSuccess(t);
241 }
242
243 @Override
244 public void onError(Throwable e) {
245 parent.innerError(e);
246 }
247
248 @Override
249 public void onComplete() {
250 parent.innerComplete();
251 }
252
253 void dispose() {
254 DisposableHelper.dispose(this);
255 }
256 }
257 }
258 }