Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/flowable/FlowableZip.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.Arrays;
17 import java.util.Objects;
18 import java.util.concurrent.atomic.*;
19
20 import org.reactivestreams.*;
21
22 import io.reactivex.rxjava3.core.*;
23 import io.reactivex.rxjava3.exceptions.Exceptions;
24 import io.reactivex.rxjava3.functions.Function;
25 import io.reactivex.rxjava3.internal.subscriptions.*;
26 import io.reactivex.rxjava3.internal.util.*;
27 import io.reactivex.rxjava3.operators.QueueSubscription;
28 import io.reactivex.rxjava3.operators.SimpleQueue;
29 import io.reactivex.rxjava3.operators.SpscArrayQueue;
30
31 public final class FlowableZip<T, R> extends Flowable<R> {
32
33 final Publisher<? extends T>[] sources;
34 final Iterable<? extends Publisher<? extends T>> sourcesIterable;
35 final Function<? super Object[], ? extends R> zipper;
36 final int bufferSize;
37 final boolean delayError;
38
39 public FlowableZip(Publisher<? extends T>[] sources,
40 Iterable<? extends Publisher<? extends T>> sourcesIterable,
41 Function<? super Object[], ? extends R> zipper,
42 int bufferSize,
43 boolean delayError) {
44 this.sources = sources;
45 this.sourcesIterable = sourcesIterable;
46 this.zipper = zipper;
47 this.bufferSize = bufferSize;
48 this.delayError = delayError;
49 }
50
51 @Override
52 @SuppressWarnings("unchecked")
53 public void subscribeActual(Subscriber<? super R> s) {
54 Publisher<? extends T>[] sources = this.sources;
55 int count = 0;
56 if (sources == null) {
57 sources = new Publisher[8];
58 for (Publisher<? extends T> p : sourcesIterable) {
59 if (count == sources.length) {
60 Publisher<? extends T>[] b = new Publisher[count + (count >> 2)];
61 System.arraycopy(sources, 0, b, 0, count);
62 sources = b;
63 }
64 sources[count++] = p;
65 }
66 } else {
67 count = sources.length;
68 }
69
70 if (count == 0) {
71 EmptySubscription.complete(s);
72 return;
73 }
74
75 ZipCoordinator<T, R> coordinator = new ZipCoordinator<>(s, zipper, count, bufferSize, delayError);
76
77 s.onSubscribe(coordinator);
78
79 coordinator.subscribe(sources, count);
80 }
81
82 static final class ZipCoordinator<T, R>
83 extends AtomicInteger
84 implements Subscription {
85
86 private static final long serialVersionUID = -2434867452883857743L;
87
88 final Subscriber<? super R> downstream;
89
90 final ZipSubscriber<T, R>[] subscribers;
91
92 final Function<? super Object[], ? extends R> zipper;
93
94 final AtomicLong requested;
95
96 final AtomicThrowable errors;
97
98 final boolean delayErrors;
99
100 volatile boolean cancelled;
101
102 final Object[] current;
103
104 ZipCoordinator(Subscriber<? super R> actual,
105 Function<? super Object[], ? extends R> zipper, int n, int prefetch, boolean delayErrors) {
106 this.downstream = actual;
107 this.zipper = zipper;
108 this.delayErrors = delayErrors;
109 @SuppressWarnings("unchecked")
110 ZipSubscriber<T, R>[] a = new ZipSubscriber[n];
111 for (int i = 0; i < n; i++) {
112 a[i] = new ZipSubscriber<>(this, prefetch);
113 }
114 this.current = new Object[n];
115 this.subscribers = a;
116 this.requested = new AtomicLong();
117 this.errors = new AtomicThrowable();
118 }
119
120 void subscribe(Publisher<? extends T>[] sources, int n) {
121 ZipSubscriber<T, R>[] a = subscribers;
122 for (int i = 0; i < n; i++) {
123 if (cancelled || (!delayErrors && errors.get() != null)) {
124 return;
125 }
126 sources[i].subscribe(a[i]);
127 }
128 }
129
130 @Override
131 public void request(long n) {
132 if (SubscriptionHelper.validate(n)) {
133 BackpressureHelper.add(requested, n);
134 drain();
135 }
136 }
137
138 @Override
139 public void cancel() {
140 if (!cancelled) {
141 cancelled = true;
142
143 cancelAll();
144 }
145 }
146
147 void error(ZipSubscriber<T, R> inner, Throwable e) {
148 if (errors.tryAddThrowableOrReport(e)) {
149 inner.done = true;
150 drain();
151 }
152 }
153
154 void cancelAll() {
155 for (ZipSubscriber<T, R> s : subscribers) {
156 s.cancel();
157 }
158 }
159
160 void drain() {
161
162 if (getAndIncrement() != 0) {
163 return;
164 }
165
166 final Subscriber<? super R> a = downstream;
167 final ZipSubscriber<T, R>[] qs = subscribers;
168 final int n = qs.length;
169 Object[] values = current;
170
171 int missed = 1;
172
173 for (;;) {
174
175 long r = requested.get();
176 long e = 0L;
177
178 while (r != e) {
179
180 if (cancelled) {
181 return;
182 }
183
184 if (!delayErrors && errors.get() != null) {
185 cancelAll();
186 errors.tryTerminateConsumer(a);
187 return;
188 }
189
190 boolean empty = false;
191
192 for (int j = 0; j < n; j++) {
193 ZipSubscriber<T, R> inner = qs[j];
194 if (values[j] == null) {
195 boolean d = inner.done;
196 SimpleQueue<T> q = inner.queue;
197 T v = null;
198 try {
199
200 v = q != null ? q.poll() : null;
201 } catch (Throwable ex) {
202 Exceptions.throwIfFatal(ex);
203
204 errors.tryAddThrowableOrReport(ex);
205 if (!delayErrors) {
206 cancelAll();
207 errors.tryTerminateConsumer(a);
208 return;
209 }
210 d = true;
211 }
212
213 boolean sourceEmpty = v == null;
214 if (d && sourceEmpty) {
215 cancelAll();
216 errors.tryTerminateConsumer(a);
217 return;
218 }
219 if (!sourceEmpty) {
220 values[j] = v;
221 } else {
222 empty = true;
223 }
224 }
225 }
226
227 if (empty) {
228 break;
229 }
230
231 R v;
232
233 try {
234 v = Objects.requireNonNull(zipper.apply(values.clone()), "The zipper returned a null value");
235 } catch (Throwable ex) {
236 Exceptions.throwIfFatal(ex);
237 cancelAll();
238 errors.tryAddThrowableOrReport(ex);
239 errors.tryTerminateConsumer(a);
240 return;
241 }
242
243 a.onNext(v);
244
245 e++;
246
247 Arrays.fill(values, null);
248 }
249
250 if (r == e) {
251 if (cancelled) {
252 return;
253 }
254
255 if (!delayErrors && errors.get() != null) {
256 cancelAll();
257 errors.tryTerminateConsumer(a);
258 return;
259 }
260
261 for (int j = 0; j < n; j++) {
262 ZipSubscriber<T, R> inner = qs[j];
263 if (values[j] == null) {
264 boolean d = inner.done;
265 SimpleQueue<T> q = inner.queue;
266 T v = null;
267 try {
268 v = q != null ? q.poll() : null;
269 } catch (Throwable ex) {
270 Exceptions.throwIfFatal(ex);
271 errors.tryAddThrowableOrReport(ex);
272 if (!delayErrors) {
273 cancelAll();
274 errors.tryTerminateConsumer(a);
275 return;
276 }
277 d = true;
278 }
279 boolean empty = v == null;
280 if (d && empty) {
281 cancelAll();
282 errors.tryTerminateConsumer(a);
283 return;
284 }
285 if (!empty) {
286 values[j] = v;
287 }
288 }
289 }
290
291 }
292
293 if (e != 0L) {
294
295 for (ZipSubscriber<T, R> inner : qs) {
296 inner.request(e);
297 }
298
299 if (r != Long.MAX_VALUE) {
300 requested.addAndGet(-e);
301 }
302 }
303
304 missed = addAndGet(-missed);
305 if (missed == 0) {
306 break;
307 }
308 }
309 }
310 }
311
312 static final class ZipSubscriber<T, R> extends AtomicReference<Subscription> implements FlowableSubscriber<T>, Subscription {
313
314 private static final long serialVersionUID = -4627193790118206028L;
315
316 final ZipCoordinator<T, R> parent;
317
318 final int prefetch;
319
320 final int limit;
321
322 SimpleQueue<T> queue;
323
324 long produced;
325
326 volatile boolean done;
327
328 int sourceMode;
329
330 ZipSubscriber(ZipCoordinator<T, R> parent, int prefetch) {
331 this.parent = parent;
332 this.prefetch = prefetch;
333 this.limit = prefetch - (prefetch >> 2);
334 }
335
336 @SuppressWarnings("unchecked")
337 @Override
338 public void onSubscribe(Subscription s) {
339 if (SubscriptionHelper.setOnce(this, s)) {
340 if (s instanceof QueueSubscription) {
341 QueueSubscription<T> f = (QueueSubscription<T>) s;
342
343 int m = f.requestFusion(QueueSubscription.ANY | QueueSubscription.BOUNDARY);
344
345 if (m == QueueSubscription.SYNC) {
346 sourceMode = m;
347 queue = f;
348 done = true;
349 parent.drain();
350 return;
351 }
352 if (m == QueueSubscription.ASYNC) {
353 sourceMode = m;
354 queue = f;
355 s.request(prefetch);
356 return;
357 }
358 }
359
360 queue = new SpscArrayQueue<>(prefetch);
361
362 s.request(prefetch);
363 }
364 }
365
366 @Override
367 public void onNext(T t) {
368 if (sourceMode != QueueSubscription.ASYNC) {
369 queue.offer(t);
370 }
371 parent.drain();
372 }
373
374 @Override
375 public void onError(Throwable t) {
376 parent.error(this, t);
377 }
378
379 @Override
380 public void onComplete() {
381 done = true;
382 parent.drain();
383 }
384
385 @Override
386 public void cancel() {
387 SubscriptionHelper.cancel(this);
388 }
389
390 @Override
391 public void request(long n) {
392 if (sourceMode != QueueSubscription.SYNC) {
393 long p = produced + n;
394 if (p >= limit) {
395 produced = 0L;
396 get().request(p);
397 } else {
398 produced = p;
399 }
400 }
401 }
402 }
403 }