Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/observable/ObservableGroupJoin.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 java.util.*;
17 import java.util.concurrent.atomic.*;
18
19 import io.reactivex.rxjava3.core.Observable;
20 import io.reactivex.rxjava3.core.ObservableSource;
21 import io.reactivex.rxjava3.core.Observer;
22 import io.reactivex.rxjava3.disposables.*;
23 import io.reactivex.rxjava3.exceptions.Exceptions;
24 import io.reactivex.rxjava3.functions.*;
25 import io.reactivex.rxjava3.internal.disposables.DisposableHelper;
26 import io.reactivex.rxjava3.internal.util.ExceptionHelper;
27 import io.reactivex.rxjava3.operators.SpscLinkedArrayQueue;
28 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
29 import io.reactivex.rxjava3.subjects.UnicastSubject;
30
31 public final class ObservableGroupJoin<TLeft, TRight, TLeftEnd, TRightEnd, R> extends AbstractObservableWithUpstream<TLeft, R> {
32
33 final ObservableSource<? extends TRight> other;
34
35 final Function<? super TLeft, ? extends ObservableSource<TLeftEnd>> leftEnd;
36
37 final Function<? super TRight, ? extends ObservableSource<TRightEnd>> rightEnd;
38
39 final BiFunction<? super TLeft, ? super Observable<TRight>, ? extends R> resultSelector;
40
41 public ObservableGroupJoin(
42 ObservableSource<TLeft> source,
43 ObservableSource<? extends TRight> other,
44 Function<? super TLeft, ? extends ObservableSource<TLeftEnd>> leftEnd,
45 Function<? super TRight, ? extends ObservableSource<TRightEnd>> rightEnd,
46 BiFunction<? super TLeft, ? super Observable<TRight>, ? extends R> resultSelector) {
47 super(source);
48 this.other = other;
49 this.leftEnd = leftEnd;
50 this.rightEnd = rightEnd;
51 this.resultSelector = resultSelector;
52 }
53
54 @Override
55 protected void subscribeActual(Observer<? super R> observer) {
56
57 GroupJoinDisposable<TLeft, TRight, TLeftEnd, TRightEnd, R> parent =
58 new GroupJoinDisposable<>(observer, leftEnd, rightEnd, resultSelector);
59
60 observer.onSubscribe(parent);
61
62 LeftRightObserver left = new LeftRightObserver(parent, true);
63 parent.disposables.add(left);
64 LeftRightObserver right = new LeftRightObserver(parent, false);
65 parent.disposables.add(right);
66
67 source.subscribe(left);
68 other.subscribe(right);
69 }
70
71 interface JoinSupport {
72
73 void innerError(Throwable ex);
74
75 void innerComplete(LeftRightObserver sender);
76
77 void innerValue(boolean isLeft, Object o);
78
79 void innerClose(boolean isLeft, LeftRightEndObserver index);
80
81 void innerCloseError(Throwable ex);
82 }
83
84 static final class GroupJoinDisposable<TLeft, TRight, TLeftEnd, TRightEnd, R>
85 extends AtomicInteger implements Disposable, JoinSupport {
86
87 private static final long serialVersionUID = -6071216598687999801L;
88
89 final Observer<? super R> downstream;
90
91 final SpscLinkedArrayQueue<Object> queue;
92
93 final CompositeDisposable disposables;
94
95 final Map<Integer, UnicastSubject<TRight>> lefts;
96
97 final Map<Integer, TRight> rights;
98
99 final AtomicReference<Throwable> error;
100
101 final Function<? super TLeft, ? extends ObservableSource<TLeftEnd>> leftEnd;
102
103 final Function<? super TRight, ? extends ObservableSource<TRightEnd>> rightEnd;
104
105 final BiFunction<? super TLeft, ? super Observable<TRight>, ? extends R> resultSelector;
106
107 final AtomicInteger active;
108
109 int leftIndex;
110
111 int rightIndex;
112
113 volatile boolean cancelled;
114
115 static final Integer LEFT_VALUE = 1;
116
117 static final Integer RIGHT_VALUE = 2;
118
119 static final Integer LEFT_CLOSE = 3;
120
121 static final Integer RIGHT_CLOSE = 4;
122
123 GroupJoinDisposable(
124 Observer<? super R> actual,
125 Function<? super TLeft, ? extends ObservableSource<TLeftEnd>> leftEnd,
126 Function<? super TRight, ? extends ObservableSource<TRightEnd>> rightEnd,
127 BiFunction<? super TLeft, ? super Observable<TRight>, ? extends R> resultSelector) {
128 this.downstream = actual;
129 this.disposables = new CompositeDisposable();
130 this.queue = new SpscLinkedArrayQueue<>(bufferSize());
131 this.lefts = new LinkedHashMap<>();
132 this.rights = new LinkedHashMap<>();
133 this.error = new AtomicReference<>();
134 this.leftEnd = leftEnd;
135 this.rightEnd = rightEnd;
136 this.resultSelector = resultSelector;
137 this.active = new AtomicInteger(2);
138 }
139
140 @Override
141 public void dispose() {
142 if (cancelled) {
143 return;
144 }
145 cancelled = true;
146 cancelAll();
147 if (getAndIncrement() == 0) {
148 queue.clear();
149 }
150 }
151
152 @Override
153 public boolean isDisposed() {
154 return cancelled;
155 }
156
157 void cancelAll() {
158 disposables.dispose();
159 }
160
161 void errorAll(Observer<?> a) {
162 Throwable ex = ExceptionHelper.terminate(error);
163
164 for (UnicastSubject<TRight> up : lefts.values()) {
165 up.onError(ex);
166 }
167
168 lefts.clear();
169 rights.clear();
170
171 a.onError(ex);
172 }
173
174 void fail(Throwable exc, Observer<?> a, SpscLinkedArrayQueue<?> q) {
175 Exceptions.throwIfFatal(exc);
176 ExceptionHelper.addThrowable(error, exc);
177 q.clear();
178 cancelAll();
179 errorAll(a);
180 }
181
182 void drain() {
183 if (getAndIncrement() != 0) {
184 return;
185 }
186
187 int missed = 1;
188 SpscLinkedArrayQueue<Object> q = queue;
189 Observer<? super R> a = downstream;
190
191 for (;;) {
192 for (;;) {
193 if (cancelled) {
194 q.clear();
195 return;
196 }
197
198 Throwable ex = error.get();
199 if (ex != null) {
200 q.clear();
201 cancelAll();
202 errorAll(a);
203 return;
204 }
205
206 boolean d = active.get() == 0;
207
208 Integer mode = (Integer)q.poll();
209
210 boolean empty = mode == null;
211
212 if (d && empty) {
213 for (UnicastSubject<?> up : lefts.values()) {
214 up.onComplete();
215 }
216
217 lefts.clear();
218 rights.clear();
219 disposables.dispose();
220
221 a.onComplete();
222 return;
223 }
224
225 if (empty) {
226 break;
227 }
228
229 Object val = q.poll();
230
231 if (mode == LEFT_VALUE) {
232 @SuppressWarnings("unchecked")
233 TLeft left = (TLeft)val;
234
235 UnicastSubject<TRight> up = UnicastSubject.create();
236 int idx = leftIndex++;
237 lefts.put(idx, up);
238
239 ObservableSource<TLeftEnd> p;
240
241 try {
242 p = Objects.requireNonNull(leftEnd.apply(left), "The leftEnd returned a null ObservableSource");
243 } catch (Throwable exc) {
244 fail(exc, a, q);
245 return;
246 }
247
248 LeftRightEndObserver end = new LeftRightEndObserver(this, true, idx);
249 disposables.add(end);
250
251 p.subscribe(end);
252
253 ex = error.get();
254 if (ex != null) {
255 q.clear();
256 cancelAll();
257 errorAll(a);
258 return;
259 }
260
261 R w;
262
263 try {
264 w = Objects.requireNonNull(resultSelector.apply(left, up), "The resultSelector returned a null value");
265 } catch (Throwable exc) {
266 fail(exc, a, q);
267 return;
268 }
269
270 a.onNext(w);
271
272 for (TRight right : rights.values()) {
273 up.onNext(right);
274 }
275 }
276 else if (mode == RIGHT_VALUE) {
277 @SuppressWarnings("unchecked")
278 TRight right = (TRight)val;
279
280 int idx = rightIndex++;
281
282 rights.put(idx, right);
283
284 ObservableSource<TRightEnd> p;
285
286 try {
287 p = Objects.requireNonNull(rightEnd.apply(right), "The rightEnd returned a null ObservableSource");
288 } catch (Throwable exc) {
289 fail(exc, a, q);
290 return;
291 }
292
293 LeftRightEndObserver end = new LeftRightEndObserver(this, false, idx);
294 disposables.add(end);
295
296 p.subscribe(end);
297
298 ex = error.get();
299 if (ex != null) {
300 q.clear();
301 cancelAll();
302 errorAll(a);
303 return;
304 }
305
306 for (UnicastSubject<TRight> up : lefts.values()) {
307 up.onNext(right);
308 }
309 }
310 else if (mode == LEFT_CLOSE) {
311 LeftRightEndObserver end = (LeftRightEndObserver)val;
312
313 UnicastSubject<TRight> up = lefts.remove(end.index);
314 disposables.remove(end);
315 if (up != null) {
316 up.onComplete();
317 }
318 }
319 else {
320 LeftRightEndObserver end = (LeftRightEndObserver)val;
321
322 rights.remove(end.index);
323 disposables.remove(end);
324 }
325 }
326
327 missed = addAndGet(-missed);
328 if (missed == 0) {
329 break;
330 }
331 }
332 }
333
334 @Override
335 public void innerError(Throwable ex) {
336 if (ExceptionHelper.addThrowable(error, ex)) {
337 active.decrementAndGet();
338 drain();
339 } else {
340 RxJavaPlugins.onError(ex);
341 }
342 }
343
344 @Override
345 public void innerComplete(LeftRightObserver sender) {
346 disposables.delete(sender);
347 active.decrementAndGet();
348 drain();
349 }
350
351 @Override
352 public void innerValue(boolean isLeft, Object o) {
353 synchronized (this) {
354 queue.offer(isLeft ? LEFT_VALUE : RIGHT_VALUE, o);
355 }
356 drain();
357 }
358
359 @Override
360 public void innerClose(boolean isLeft, LeftRightEndObserver index) {
361 synchronized (this) {
362 queue.offer(isLeft ? LEFT_CLOSE : RIGHT_CLOSE, index);
363 }
364 drain();
365 }
366
367 @Override
368 public void innerCloseError(Throwable ex) {
369 if (ExceptionHelper.addThrowable(error, ex)) {
370 drain();
371 } else {
372 RxJavaPlugins.onError(ex);
373 }
374 }
375 }
376
377 static final class LeftRightObserver
378 extends AtomicReference<Disposable>
379 implements Observer<Object>, Disposable {
380
381 private static final long serialVersionUID = 1883890389173668373L;
382
383 final JoinSupport parent;
384
385 final boolean isLeft;
386
387 LeftRightObserver(JoinSupport parent, boolean isLeft) {
388 this.parent = parent;
389 this.isLeft = isLeft;
390 }
391
392 @Override
393 public void dispose() {
394 DisposableHelper.dispose(this);
395 }
396
397 @Override
398 public boolean isDisposed() {
399 return DisposableHelper.isDisposed(get());
400 }
401
402 @Override
403 public void onSubscribe(Disposable d) {
404 DisposableHelper.setOnce(this, d);
405 }
406
407 @Override
408 public void onNext(Object t) {
409 parent.innerValue(isLeft, t);
410 }
411
412 @Override
413 public void onError(Throwable t) {
414 parent.innerError(t);
415 }
416
417 @Override
418 public void onComplete() {
419 parent.innerComplete(this);
420 }
421
422 }
423
424 static final class LeftRightEndObserver
425 extends AtomicReference<Disposable>
426 implements Observer<Object>, Disposable {
427
428 private static final long serialVersionUID = 1883890389173668373L;
429
430 final JoinSupport parent;
431
432 final boolean isLeft;
433
434 final int index;
435
436 LeftRightEndObserver(JoinSupport parent,
437 boolean isLeft, int index) {
438 this.parent = parent;
439 this.isLeft = isLeft;
440 this.index = index;
441 }
442
443 @Override
444 public void dispose() {
445 DisposableHelper.dispose(this);
446 }
447
448 @Override
449 public boolean isDisposed() {
450 return DisposableHelper.isDisposed(get());
451 }
452
453 @Override
454 public void onSubscribe(Disposable d) {
455 DisposableHelper.setOnce(this, d);
456 }
457
458 @Override
459 public void onNext(Object t) {
460 if (DisposableHelper.dispose(this)) {
461 parent.innerClose(isLeft, this);
462 }
463 }
464
465 @Override
466 public void onError(Throwable t) {
467 parent.innerCloseError(t);
468 }
469
470 @Override
471 public void onComplete() {
472 parent.innerClose(isLeft, this);
473 }
474
475 }
476
477 }