Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/observable/ObservableJoin.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.ObservableSource;
20 import io.reactivex.rxjava3.core.Observer;
21 import io.reactivex.rxjava3.disposables.*;
22 import io.reactivex.rxjava3.exceptions.Exceptions;
23 import io.reactivex.rxjava3.functions.*;
24 import io.reactivex.rxjava3.internal.operators.observable.ObservableGroupJoin.*;
25 import io.reactivex.rxjava3.internal.util.ExceptionHelper;
26 import io.reactivex.rxjava3.operators.SpscLinkedArrayQueue;
27 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
28
29 public final class ObservableJoin<TLeft, TRight, TLeftEnd, TRightEnd, R> extends AbstractObservableWithUpstream<TLeft, R> {
30
31 final ObservableSource<? extends TRight> other;
32
33 final Function<? super TLeft, ? extends ObservableSource<TLeftEnd>> leftEnd;
34
35 final Function<? super TRight, ? extends ObservableSource<TRightEnd>> rightEnd;
36
37 final BiFunction<? super TLeft, ? super TRight, ? extends R> resultSelector;
38
39 public ObservableJoin(
40 ObservableSource<TLeft> source,
41 ObservableSource<? extends TRight> other,
42 Function<? super TLeft, ? extends ObservableSource<TLeftEnd>> leftEnd,
43 Function<? super TRight, ? extends ObservableSource<TRightEnd>> rightEnd,
44 BiFunction<? super TLeft, ? super TRight, ? extends R> resultSelector) {
45 super(source);
46 this.other = other;
47 this.leftEnd = leftEnd;
48 this.rightEnd = rightEnd;
49 this.resultSelector = resultSelector;
50 }
51
52 @Override
53 protected void subscribeActual(Observer<? super R> observer) {
54
55 JoinDisposable<TLeft, TRight, TLeftEnd, TRightEnd, R> parent =
56 new JoinDisposable<>(
57 observer, leftEnd, rightEnd, resultSelector);
58
59 observer.onSubscribe(parent);
60
61 LeftRightObserver left = new LeftRightObserver(parent, true);
62 parent.disposables.add(left);
63 LeftRightObserver right = new LeftRightObserver(parent, false);
64 parent.disposables.add(right);
65
66 source.subscribe(left);
67 other.subscribe(right);
68 }
69
70 static final class JoinDisposable<TLeft, TRight, TLeftEnd, TRightEnd, R>
71 extends AtomicInteger implements Disposable, JoinSupport {
72
73 private static final long serialVersionUID = -6071216598687999801L;
74
75 final Observer<? super R> downstream;
76
77 final SpscLinkedArrayQueue<Object> queue;
78
79 final CompositeDisposable disposables;
80
81 final Map<Integer, TLeft> lefts;
82
83 final Map<Integer, TRight> rights;
84
85 final AtomicReference<Throwable> error;
86
87 final Function<? super TLeft, ? extends ObservableSource<TLeftEnd>> leftEnd;
88
89 final Function<? super TRight, ? extends ObservableSource<TRightEnd>> rightEnd;
90
91 final BiFunction<? super TLeft, ? super TRight, ? extends R> resultSelector;
92
93 final AtomicInteger active;
94
95 int leftIndex;
96
97 int rightIndex;
98
99 volatile boolean cancelled;
100
101 static final Integer LEFT_VALUE = 1;
102
103 static final Integer RIGHT_VALUE = 2;
104
105 static final Integer LEFT_CLOSE = 3;
106
107 static final Integer RIGHT_CLOSE = 4;
108
109 JoinDisposable(Observer<? super R> actual,
110 Function<? super TLeft, ? extends ObservableSource<TLeftEnd>> leftEnd,
111 Function<? super TRight, ? extends ObservableSource<TRightEnd>> rightEnd,
112 BiFunction<? super TLeft, ? super TRight, ? extends R> resultSelector) {
113 this.downstream = actual;
114 this.disposables = new CompositeDisposable();
115 this.queue = new SpscLinkedArrayQueue<>(bufferSize());
116 this.lefts = new LinkedHashMap<>();
117 this.rights = new LinkedHashMap<>();
118 this.error = new AtomicReference<>();
119 this.leftEnd = leftEnd;
120 this.rightEnd = rightEnd;
121 this.resultSelector = resultSelector;
122 this.active = new AtomicInteger(2);
123 }
124
125 @Override
126 public void dispose() {
127 if (!cancelled) {
128 cancelled = true;
129 cancelAll();
130 if (getAndIncrement() == 0) {
131 queue.clear();
132 }
133 }
134 }
135
136 @Override
137 public boolean isDisposed() {
138 return cancelled;
139 }
140
141 void cancelAll() {
142 disposables.dispose();
143 }
144
145 void errorAll(Observer<?> a) {
146 Throwable ex = ExceptionHelper.terminate(error);
147
148 lefts.clear();
149 rights.clear();
150
151 a.onError(ex);
152 }
153
154 void fail(Throwable exc, Observer<?> a, SpscLinkedArrayQueue<?> q) {
155 Exceptions.throwIfFatal(exc);
156 ExceptionHelper.addThrowable(error, exc);
157 q.clear();
158 cancelAll();
159 errorAll(a);
160 }
161
162 void drain() {
163 if (getAndIncrement() != 0) {
164 return;
165 }
166
167 int missed = 1;
168 SpscLinkedArrayQueue<Object> q = queue;
169 Observer<? super R> a = downstream;
170
171 for (;;) {
172 for (;;) {
173 if (cancelled) {
174 q.clear();
175 return;
176 }
177
178 Throwable ex = error.get();
179 if (ex != null) {
180 q.clear();
181 cancelAll();
182 errorAll(a);
183 return;
184 }
185
186 boolean d = active.get() == 0;
187
188 Integer mode = (Integer)q.poll();
189
190 boolean empty = mode == null;
191
192 if (d && empty) {
193
194 lefts.clear();
195 rights.clear();
196 disposables.dispose();
197
198 a.onComplete();
199 return;
200 }
201
202 if (empty) {
203 break;
204 }
205
206 Object val = q.poll();
207
208 if (mode == LEFT_VALUE) {
209 @SuppressWarnings("unchecked")
210 TLeft left = (TLeft)val;
211
212 int idx = leftIndex++;
213 lefts.put(idx, left);
214
215 ObservableSource<TLeftEnd> p;
216
217 try {
218 p = Objects.requireNonNull(leftEnd.apply(left), "The leftEnd returned a null ObservableSource");
219 } catch (Throwable exc) {
220 fail(exc, a, q);
221 return;
222 }
223
224 LeftRightEndObserver end = new LeftRightEndObserver(this, true, idx);
225 disposables.add(end);
226
227 p.subscribe(end);
228
229 ex = error.get();
230 if (ex != null) {
231 q.clear();
232 cancelAll();
233 errorAll(a);
234 return;
235 }
236
237 for (TRight right : rights.values()) {
238
239 R w;
240
241 try {
242 w = Objects.requireNonNull(resultSelector.apply(left, right), "The resultSelector returned a null value");
243 } catch (Throwable exc) {
244 fail(exc, a, q);
245 return;
246 }
247
248 a.onNext(w);
249 }
250 }
251 else if (mode == RIGHT_VALUE) {
252 @SuppressWarnings("unchecked")
253 TRight right = (TRight)val;
254
255 int idx = rightIndex++;
256
257 rights.put(idx, right);
258
259 ObservableSource<TRightEnd> p;
260
261 try {
262 p = Objects.requireNonNull(rightEnd.apply(right), "The rightEnd returned a null ObservableSource");
263 } catch (Throwable exc) {
264 fail(exc, a, q);
265 return;
266 }
267
268 LeftRightEndObserver end = new LeftRightEndObserver(this, false, idx);
269 disposables.add(end);
270
271 p.subscribe(end);
272
273 ex = error.get();
274 if (ex != null) {
275 q.clear();
276 cancelAll();
277 errorAll(a);
278 return;
279 }
280
281 for (TLeft left : lefts.values()) {
282
283 R w;
284
285 try {
286 w = Objects.requireNonNull(resultSelector.apply(left, right), "The resultSelector returned a null value");
287 } catch (Throwable exc) {
288 fail(exc, a, q);
289 return;
290 }
291
292 a.onNext(w);
293 }
294 }
295 else if (mode == LEFT_CLOSE) {
296 LeftRightEndObserver end = (LeftRightEndObserver)val;
297
298 lefts.remove(end.index);
299 disposables.remove(end);
300 } else {
301 LeftRightEndObserver end = (LeftRightEndObserver)val;
302
303 rights.remove(end.index);
304 disposables.remove(end);
305 }
306 }
307
308 missed = addAndGet(-missed);
309 if (missed == 0) {
310 break;
311 }
312 }
313 }
314
315 @Override
316 public void innerError(Throwable ex) {
317 if (ExceptionHelper.addThrowable(error, ex)) {
318 active.decrementAndGet();
319 drain();
320 } else {
321 RxJavaPlugins.onError(ex);
322 }
323 }
324
325 @Override
326 public void innerComplete(LeftRightObserver sender) {
327 disposables.delete(sender);
328 active.decrementAndGet();
329 drain();
330 }
331
332 @Override
333 public void innerValue(boolean isLeft, Object o) {
334 synchronized (this) {
335 queue.offer(isLeft ? LEFT_VALUE : RIGHT_VALUE, o);
336 }
337 drain();
338 }
339
340 @Override
341 public void innerClose(boolean isLeft, LeftRightEndObserver index) {
342 synchronized (this) {
343 queue.offer(isLeft ? LEFT_CLOSE : RIGHT_CLOSE, index);
344 }
345 drain();
346 }
347
348 @Override
349 public void innerCloseError(Throwable ex) {
350 if (ExceptionHelper.addThrowable(error, ex)) {
351 drain();
352 } else {
353 RxJavaPlugins.onError(ex);
354 }
355 }
356 }
357 }