Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/observable/ObservableWithLatestFromMany.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.annotations.*;
21 import io.reactivex.rxjava3.core.*;
22 import io.reactivex.rxjava3.disposables.Disposable;
23 import io.reactivex.rxjava3.exceptions.Exceptions;
24 import io.reactivex.rxjava3.functions.Function;
25 import io.reactivex.rxjava3.internal.disposables.*;
26 import io.reactivex.rxjava3.internal.util.*;
27 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
28
29 /**
30 * Combines a main sequence of values with the latest from multiple other sequences via
31 * a selector function.
32 *
33 * @param <T> the main sequence's type
34 * @param <R> the output type
35 */
36 public final class ObservableWithLatestFromMany<T, R> extends AbstractObservableWithUpstream<T, R> {
37
38 @Nullable
39 final ObservableSource<?>[] otherArray;
40
41 @Nullable
42 final Iterable<? extends ObservableSource<?>> otherIterable;
43
44 @NonNull
45 final Function<? super Object[], R> combiner;
46
47 public ObservableWithLatestFromMany(@NonNull ObservableSource<T> source, @NonNull ObservableSource<?>[] otherArray, @NonNull Function<? super Object[], R> combiner) {
48 super(source);
49 this.otherArray = otherArray;
50 this.otherIterable = null;
51 this.combiner = combiner;
52 }
53
54 public ObservableWithLatestFromMany(@NonNull ObservableSource<T> source, @NonNull Iterable<? extends ObservableSource<?>> otherIterable, @NonNull Function<? super Object[], R> combiner) {
55 super(source);
56 this.otherArray = null;
57 this.otherIterable = otherIterable;
58 this.combiner = combiner;
59 }
60
61 @Override
62 protected void subscribeActual(Observer<? super R> observer) {
63 ObservableSource<?>[] others = otherArray;
64 int n = 0;
65 if (others == null) {
66 others = new ObservableSource[8];
67
68 try {
69 for (ObservableSource<?> p : otherIterable) {
70 if (n == others.length) {
71 others = Arrays.copyOf(others, n + (n >> 1));
72 }
73 others[n++] = p;
74 }
75 } catch (Throwable ex) {
76 Exceptions.throwIfFatal(ex);
77 EmptyDisposable.error(ex, observer);
78 return;
79 }
80
81 } else {
82 n = others.length;
83 }
84
85 if (n == 0) {
86 new ObservableMap<>(source, new SingletonArrayFunc()).subscribeActual(observer);
87 return;
88 }
89
90 WithLatestFromObserver<T, R> parent = new WithLatestFromObserver<>(observer, combiner, n);
91 observer.onSubscribe(parent);
92 parent.subscribe(others, n);
93
94 source.subscribe(parent);
95 }
96
97 static final class WithLatestFromObserver<T, R>
98 extends AtomicInteger
99 implements Observer<T>, Disposable {
100
101 private static final long serialVersionUID = 1577321883966341961L;
102
103 final Observer<? super R> downstream;
104
105 final Function<? super Object[], R> combiner;
106
107 final WithLatestInnerObserver[] observers;
108
109 final AtomicReferenceArray<Object> values;
110
111 final AtomicReference<Disposable> upstream;
112
113 final AtomicThrowable error;
114
115 volatile boolean done;
116
117 WithLatestFromObserver(Observer<? super R> actual, Function<? super Object[], R> combiner, int n) {
118 this.downstream = actual;
119 this.combiner = combiner;
120 WithLatestInnerObserver[] s = new WithLatestInnerObserver[n];
121 for (int i = 0; i < n; i++) {
122 s[i] = new WithLatestInnerObserver(this, i);
123 }
124 this.observers = s;
125 this.values = new AtomicReferenceArray<>(n);
126 this.upstream = new AtomicReference<>();
127 this.error = new AtomicThrowable();
128 }
129
130 void subscribe(ObservableSource<?>[] others, int n) {
131 WithLatestInnerObserver[] observers = this.observers;
132 AtomicReference<Disposable> upstream = this.upstream;
133 for (int i = 0; i < n; i++) {
134 if (DisposableHelper.isDisposed(upstream.get()) || done) {
135 return;
136 }
137 others[i].subscribe(observers[i]);
138 }
139 }
140
141 @Override
142 public void onSubscribe(Disposable d) {
143 DisposableHelper.setOnce(this.upstream, d);
144 }
145
146 @Override
147 public void onNext(T t) {
148 if (done) {
149 return;
150 }
151 AtomicReferenceArray<Object> ara = values;
152 int n = ara.length();
153 Object[] objects = new Object[n + 1];
154 objects[0] = t;
155
156 for (int i = 0; i < n; i++) {
157 Object o = ara.get(i);
158 if (o == null) {
159 // no latest, skip this value
160 return;
161 }
162 objects[i + 1] = o;
163 }
164
165 R v;
166
167 try {
168 v = Objects.requireNonNull(combiner.apply(objects), "combiner returned a null value");
169 } catch (Throwable ex) {
170 Exceptions.throwIfFatal(ex);
171 dispose();
172 onError(ex);
173 return;
174 }
175
176 HalfSerializer.onNext(downstream, v, this, error);
177 }
178
179 @Override
180 public void onError(Throwable t) {
181 if (done) {
182 RxJavaPlugins.onError(t);
183 return;
184 }
185 done = true;
186 cancelAllBut(-1);
187 HalfSerializer.onError(downstream, t, this, error);
188 }
189
190 @Override
191 public void onComplete() {
192 if (!done) {
193 done = true;
194 cancelAllBut(-1);
195 HalfSerializer.onComplete(downstream, this, error);
196 }
197 }
198
199 @Override
200 public boolean isDisposed() {
201 return DisposableHelper.isDisposed(upstream.get());
202 }
203
204 @Override
205 public void dispose() {
206 DisposableHelper.dispose(upstream);
207 for (WithLatestInnerObserver observer : observers) {
208 observer.dispose();
209 }
210 }
211
212 void innerNext(int index, Object o) {
213 values.set(index, o);
214 }
215
216 void innerError(int index, Throwable t) {
217 done = true;
218 DisposableHelper.dispose(upstream);
219 cancelAllBut(index);
220 HalfSerializer.onError(downstream, t, this, error);
221 }
222
223 void innerComplete(int index, boolean nonEmpty) {
224 if (!nonEmpty) {
225 done = true;
226 cancelAllBut(index);
227 HalfSerializer.onComplete(downstream, this, error);
228 }
229 }
230
231 void cancelAllBut(int index) {
232 WithLatestInnerObserver[] observers = this.observers;
233 for (int i = 0; i < observers.length; i++) {
234 if (i != index) {
235 observers[i].dispose();
236 }
237 }
238 }
239 }
240
241 static final class WithLatestInnerObserver
242 extends AtomicReference<Disposable>
243 implements Observer<Object> {
244
245 private static final long serialVersionUID = 3256684027868224024L;
246
247 final WithLatestFromObserver<?, ?> parent;
248
249 final int index;
250
251 boolean hasValue;
252
253 WithLatestInnerObserver(WithLatestFromObserver<?, ?> parent, int index) {
254 this.parent = parent;
255 this.index = index;
256 }
257
258 @Override
259 public void onSubscribe(Disposable d) {
260 DisposableHelper.setOnce(this, d);
261 }
262
263 @Override
264 public void onNext(Object t) {
265 if (!hasValue) {
266 hasValue = true;
267 }
268 parent.innerNext(index, t);
269 }
270
271 @Override
272 public void onError(Throwable t) {
273 parent.innerError(index, t);
274 }
275
276 @Override
277 public void onComplete() {
278 parent.innerComplete(index, hasValue);
279 }
280
281 public void dispose() {
282 DisposableHelper.dispose(this);
283 }
284 }
285
286 final class SingletonArrayFunc implements Function<T, R> {
287 @Override
288 public R apply(T t) throws Throwable {
289 return Objects.requireNonNull(combiner.apply(new Object[] { t }), "The combiner returned a null value");
290 }
291 }
292 }