Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/flowable/FlowableMergeWithSingle.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.flowable;
15
16 import java.util.concurrent.atomic.*;
17
18 import org.reactivestreams.*;
19
20 import io.reactivex.rxjava3.core.*;
21 import io.reactivex.rxjava3.disposables.Disposable;
22 import io.reactivex.rxjava3.internal.disposables.DisposableHelper;
23 import io.reactivex.rxjava3.internal.subscriptions.SubscriptionHelper;
24 import io.reactivex.rxjava3.internal.util.*;
25 import io.reactivex.rxjava3.operators.SimplePlainQueue;
26 import io.reactivex.rxjava3.operators.SpscArrayQueue;
27
28 /**
29 * Merges an Observable and a Maybe by emitting the items of the Observable and the success
30 * value of the Maybe and waiting until both the Observable and Maybe terminate normally.
31 * <p>History: 2.1.10 - experimental
32 * @param <T> the element type of the Observable
33 * @since 2.2
34 */
35 public final class FlowableMergeWithSingle<T> extends AbstractFlowableWithUpstream<T, T> {
36
37 final SingleSource<? extends T> other;
38
39 public FlowableMergeWithSingle(Flowable<T> source, SingleSource<? extends T> other) {
40 super(source);
41 this.other = other;
42 }
43
44 @Override
45 protected void subscribeActual(Subscriber<? super T> subscriber) {
46 MergeWithObserver<T> parent = new MergeWithObserver<>(subscriber);
47 subscriber.onSubscribe(parent);
48 source.subscribe(parent);
49 other.subscribe(parent.otherObserver);
50 }
51
52 static final class MergeWithObserver<T> extends AtomicInteger
53 implements FlowableSubscriber<T>, Subscription {
54
55 private static final long serialVersionUID = -4592979584110982903L;
56
57 final Subscriber<? super T> downstream;
58
59 final AtomicReference<Subscription> mainSubscription;
60
61 final OtherObserver<T> otherObserver;
62
63 final AtomicThrowable errors;
64
65 final AtomicLong requested;
66
67 final int prefetch;
68
69 final int limit;
70
71 volatile SimplePlainQueue<T> queue;
72
73 T singleItem;
74
75 volatile boolean cancelled;
76
77 volatile boolean mainDone;
78
79 volatile int otherState;
80
81 long emitted;
82
83 int consumed;
84
85 static final int OTHER_STATE_HAS_VALUE = 1;
86
87 static final int OTHER_STATE_CONSUMED_OR_EMPTY = 2;
88
89 MergeWithObserver(Subscriber<? super T> downstream) {
90 this.downstream = downstream;
91 this.mainSubscription = new AtomicReference<>();
92 this.otherObserver = new OtherObserver<>(this);
93 this.errors = new AtomicThrowable();
94 this.requested = new AtomicLong();
95 this.prefetch = bufferSize();
96 this.limit = prefetch - (prefetch >> 2);
97 }
98
99 @Override
100 public void onSubscribe(Subscription s) {
101 SubscriptionHelper.setOnce(mainSubscription, s, prefetch);
102 }
103
104 @Override
105 public void onNext(T t) {
106 if (compareAndSet(0, 1)) {
107 long e = emitted;
108 if (requested.get() != e) {
109 SimplePlainQueue<T> q = queue;
110 if (q == null || q.isEmpty()) {
111
112 emitted = e + 1;
113 downstream.onNext(t);
114
115 int c = consumed + 1;
116 if (c == limit) {
117 consumed = 0;
118 mainSubscription.get().request(c);
119 } else {
120 consumed = c;
121 }
122 } else {
123 q.offer(t);
124 }
125 } else {
126 SimplePlainQueue<T> q = getOrCreateQueue();
127 q.offer(t);
128 }
129 if (decrementAndGet() == 0) {
130 return;
131 }
132 } else {
133 SimplePlainQueue<T> q = getOrCreateQueue();
134 q.offer(t);
135 if (getAndIncrement() != 0) {
136 return;
137 }
138 }
139 drainLoop();
140 }
141
142 @Override
143 public void onError(Throwable ex) {
144 if (errors.tryAddThrowableOrReport(ex)) {
145 DisposableHelper.dispose(otherObserver);
146 drain();
147 }
148 }
149
150 @Override
151 public void onComplete() {
152 mainDone = true;
153 drain();
154 }
155
156 @Override
157 public void request(long n) {
158 BackpressureHelper.add(requested, n);
159 drain();
160 }
161
162 @Override
163 public void cancel() {
164 cancelled = true;
165 SubscriptionHelper.cancel(mainSubscription);
166 DisposableHelper.dispose(otherObserver);
167 errors.tryTerminateAndReport();
168 if (getAndIncrement() == 0) {
169 queue = null;
170 singleItem = null;
171 }
172 }
173
174 void otherSuccess(T value) {
175 if (compareAndSet(0, 1)) {
176 long e = emitted;
177 if (requested.get() != e) {
178
179 emitted = e + 1;
180 downstream.onNext(value);
181 otherState = OTHER_STATE_CONSUMED_OR_EMPTY;
182 } else {
183 singleItem = value;
184 otherState = OTHER_STATE_HAS_VALUE;
185 if (decrementAndGet() == 0) {
186 return;
187 }
188 }
189 } else {
190 singleItem = value;
191 otherState = OTHER_STATE_HAS_VALUE;
192 if (getAndIncrement() != 0) {
193 return;
194 }
195 }
196 drainLoop();
197 }
198
199 void otherError(Throwable ex) {
200 if (errors.tryAddThrowableOrReport(ex)) {
201 SubscriptionHelper.cancel(mainSubscription);
202 drain();
203 }
204 }
205
206 SimplePlainQueue<T> getOrCreateQueue() {
207 SimplePlainQueue<T> q = queue;
208 if (q == null) {
209 q = new SpscArrayQueue<>(bufferSize());
210 queue = q;
211 }
212 return q;
213 }
214
215 void drain() {
216 if (getAndIncrement() == 0) {
217 drainLoop();
218 }
219 }
220
221 void drainLoop() {
222 Subscriber<? super T> actual = this.downstream;
223 int missed = 1;
224 long e = emitted;
225 int c = consumed;
226 int lim = limit;
227 for (;;) {
228
229 long r = requested.get();
230
231 while (e != r) {
232 if (cancelled) {
233 singleItem = null;
234 queue = null;
235 return;
236 }
237
238 if (errors.get() != null) {
239 singleItem = null;
240 queue = null;
241 errors.tryTerminateConsumer(downstream);
242 return;
243 }
244
245 int os = otherState;
246 if (os == OTHER_STATE_HAS_VALUE) {
247 T v = singleItem;
248 singleItem = null;
249 otherState = OTHER_STATE_CONSUMED_OR_EMPTY;
250 os = OTHER_STATE_CONSUMED_OR_EMPTY;
251 actual.onNext(v);
252
253 e++;
254 continue;
255 }
256
257 boolean d = mainDone;
258 SimplePlainQueue<T> q = queue;
259 T v = q != null ? q.poll() : null;
260 boolean empty = v == null;
261
262 if (d && empty && os == OTHER_STATE_CONSUMED_OR_EMPTY) {
263 queue = null;
264 actual.onComplete();
265 return;
266 }
267
268 if (empty) {
269 break;
270 }
271
272 actual.onNext(v);
273
274 e++;
275
276 if (++c == lim) {
277 c = 0;
278 mainSubscription.get().request(lim);
279 }
280 }
281
282 if (e == r) {
283 if (cancelled) {
284 singleItem = null;
285 queue = null;
286 return;
287 }
288
289 if (errors.get() != null) {
290 singleItem = null;
291 queue = null;
292 errors.tryTerminateConsumer(downstream);
293 return;
294 }
295
296 boolean d = mainDone;
297 SimplePlainQueue<T> q = queue;
298 boolean empty = q == null || q.isEmpty();
299
300 if (d && empty && otherState == 2) {
301 queue = null;
302 actual.onComplete();
303 return;
304 }
305 }
306
307 emitted = e;
308 consumed = c;
309 missed = addAndGet(-missed);
310 if (missed == 0) {
311 break;
312 }
313 }
314 }
315
316 static final class OtherObserver<T> extends AtomicReference<Disposable>
317 implements SingleObserver<T> {
318
319 private static final long serialVersionUID = -2935427570954647017L;
320
321 final MergeWithObserver<T> parent;
322
323 OtherObserver(MergeWithObserver<T> parent) {
324 this.parent = parent;
325 }
326
327 @Override
328 public void onSubscribe(Disposable d) {
329 DisposableHelper.setOnce(this, d);
330 }
331
332 @Override
333 public void onSuccess(T t) {
334 parent.otherSuccess(t);
335 }
336
337 @Override
338 public void onError(Throwable e) {
339 parent.otherError(e);
340 }
341 }
342 }
343 }