Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/observable/ObservableZip.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.Arrays;
17 import java.util.Objects;
18 import java.util.concurrent.atomic.*;
19
20 import io.reactivex.rxjava3.core.*;
21 import io.reactivex.rxjava3.disposables.Disposable;
22 import io.reactivex.rxjava3.exceptions.Exceptions;
23 import io.reactivex.rxjava3.functions.Function;
24 import io.reactivex.rxjava3.internal.disposables.*;
25 import io.reactivex.rxjava3.operators.SpscLinkedArrayQueue;
26
27 public final class ObservableZip<T, R> extends Observable<R> {
28
29 final ObservableSource<? extends T>[] sources;
30 final Iterable<? extends ObservableSource<? extends T>> sourcesIterable;
31 final Function<? super Object[], ? extends R> zipper;
32 final int bufferSize;
33 final boolean delayError;
34
35 public ObservableZip(ObservableSource<? extends T>[] sources,
36 Iterable<? extends ObservableSource<? extends T>> sourcesIterable,
37 Function<? super Object[], ? extends R> zipper,
38 int bufferSize,
39 boolean delayError) {
40 this.sources = sources;
41 this.sourcesIterable = sourcesIterable;
42 this.zipper = zipper;
43 this.bufferSize = bufferSize;
44 this.delayError = delayError;
45 }
46
47 @Override
48 @SuppressWarnings("unchecked")
49 public void subscribeActual(Observer<? super R> observer) {
50 ObservableSource<? extends T>[] sources = this.sources;
51 int count = 0;
52 if (sources == null) {
53 sources = new ObservableSource[8];
54 for (ObservableSource<? extends T> p : sourcesIterable) {
55 if (count == sources.length) {
56 ObservableSource<? extends T>[] b = new ObservableSource[count + (count >> 2)];
57 System.arraycopy(sources, 0, b, 0, count);
58 sources = b;
59 }
60 sources[count++] = p;
61 }
62 } else {
63 count = sources.length;
64 }
65
66 if (count == 0) {
67 EmptyDisposable.complete(observer);
68 return;
69 }
70
71 ZipCoordinator<T, R> zc = new ZipCoordinator<>(observer, zipper, count, delayError);
72 zc.subscribe(sources, bufferSize);
73 }
74
75 static final class ZipCoordinator<T, R> extends AtomicInteger implements Disposable {
76
77 private static final long serialVersionUID = 2983708048395377667L;
78 final Observer<? super R> downstream;
79 final Function<? super Object[], ? extends R> zipper;
80 final ZipObserver<T, R>[] observers;
81 final T[] row;
82 final boolean delayError;
83
84 volatile boolean cancelled;
85
86 @SuppressWarnings("unchecked")
87 ZipCoordinator(Observer<? super R> actual,
88 Function<? super Object[], ? extends R> zipper,
89 int count, boolean delayError) {
90 this.downstream = actual;
91 this.zipper = zipper;
92 this.observers = new ZipObserver[count];
93 this.row = (T[])new Object[count];
94 this.delayError = delayError;
95 }
96
97 public void subscribe(ObservableSource<? extends T>[] sources, int bufferSize) {
98 ZipObserver<T, R>[] s = observers;
99 int len = s.length;
100 for (int i = 0; i < len; i++) {
101 s[i] = new ZipObserver<>(this, bufferSize);
102 }
103 // this makes sure the contents of the observers array is visible
104 this.lazySet(0);
105 downstream.onSubscribe(this);
106 for (int i = 0; i < len; i++) {
107 if (cancelled) {
108 return;
109 }
110 sources[i].subscribe(s[i]);
111 }
112 }
113
114 @Override
115 public void dispose() {
116 if (!cancelled) {
117 cancelled = true;
118 cancelSources();
119 if (getAndIncrement() == 0) {
120 clear();
121 }
122 }
123 }
124
125 @Override
126 public boolean isDisposed() {
127 return cancelled;
128 }
129
130 void cancel() {
131 clear();
132 cancelSources();
133 }
134
135 void cancelSources() {
136 for (ZipObserver<?, ?> zs : observers) {
137 zs.dispose();
138 }
139 }
140
141 void clear() {
142 for (ZipObserver<?, ?> zs : observers) {
143 zs.queue.clear();
144 }
145 }
146
147 public void drain() {
148 if (getAndIncrement() != 0) {
149 return;
150 }
151
152 int missing = 1;
153
154 final ZipObserver<T, R>[] zs = observers;
155 final Observer<? super R> a = downstream;
156 final T[] os = row;
157 final boolean delayError = this.delayError;
158
159 for (;;) {
160
161 for (;;) {
162 int i = 0;
163 int emptyCount = 0;
164 for (ZipObserver<T, R> z : zs) {
165 if (os[i] == null) {
166 boolean d = z.done;
167 T v = z.queue.poll();
168 boolean empty = v == null;
169
170 if (checkTerminated(d, empty, a, delayError, z)) {
171 return;
172 }
173 if (!empty) {
174 os[i] = v;
175 } else {
176 emptyCount++;
177 }
178 } else {
179 if (z.done && !delayError) {
180 Throwable ex = z.error;
181 if (ex != null) {
182 cancelled = true;
183 cancel();
184 a.onError(ex);
185 return;
186 }
187 }
188 }
189 i++;
190 }
191
192 if (emptyCount != 0) {
193 break;
194 }
195
196 R v;
197 try {
198 v = Objects.requireNonNull(zipper.apply(os.clone()), "The zipper returned a null value");
199 } catch (Throwable ex) {
200 Exceptions.throwIfFatal(ex);
201 cancel();
202 a.onError(ex);
203 return;
204 }
205
206 a.onNext(v);
207
208 Arrays.fill(os, null);
209 }
210
211 missing = addAndGet(-missing);
212 if (missing == 0) {
213 return;
214 }
215 }
216 }
217
218 boolean checkTerminated(boolean d, boolean empty, Observer<? super R> a, boolean delayError, ZipObserver<?, ?> source) {
219 if (cancelled) {
220 cancel();
221 return true;
222 }
223
224 if (d) {
225 if (delayError) {
226 if (empty) {
227 Throwable e = source.error;
228 cancelled = true;
229 cancel();
230 if (e != null) {
231 a.onError(e);
232 } else {
233 a.onComplete();
234 }
235 return true;
236 }
237 } else {
238 Throwable e = source.error;
239 if (e != null) {
240 cancelled = true;
241 cancel();
242 a.onError(e);
243 return true;
244 } else
245 if (empty) {
246 cancelled = true;
247 cancel();
248 a.onComplete();
249 return true;
250 }
251 }
252 }
253
254 return false;
255 }
256 }
257
258 static final class ZipObserver<T, R> implements Observer<T> {
259
260 final ZipCoordinator<T, R> parent;
261 final SpscLinkedArrayQueue<T> queue;
262
263 volatile boolean done;
264 Throwable error;
265
266 final AtomicReference<Disposable> upstream = new AtomicReference<>();
267
268 ZipObserver(ZipCoordinator<T, R> parent, int bufferSize) {
269 this.parent = parent;
270 this.queue = new SpscLinkedArrayQueue<>(bufferSize);
271 }
272
273 @Override
274 public void onSubscribe(Disposable d) {
275 DisposableHelper.setOnce(this.upstream, d);
276 }
277
278 @Override
279 public void onNext(T t) {
280 queue.offer(t);
281 parent.drain();
282 }
283
284 @Override
285 public void onError(Throwable t) {
286 error = t;
287 done = true;
288 parent.drain();
289 }
290
291 @Override
292 public void onComplete() {
293 done = true;
294 parent.drain();
295 }
296
297 public void dispose() {
298 DisposableHelper.dispose(upstream);
299 }
300 }
301 }