Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/single/SingleAmb.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.single;
15
16 import java.util.concurrent.atomic.AtomicBoolean;
17
18 import io.reactivex.rxjava3.core.*;
19 import io.reactivex.rxjava3.disposables.*;
20 import io.reactivex.rxjava3.exceptions.Exceptions;
21 import io.reactivex.rxjava3.internal.disposables.EmptyDisposable;
22 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
23
24 public final class SingleAmb<T> extends Single<T> {
25 private final SingleSource<? extends T>[] sources;
26 private final Iterable<? extends SingleSource<? extends T>> sourcesIterable;
27
28 public SingleAmb(SingleSource<? extends T>[] sources, Iterable<? extends SingleSource<? extends T>> sourcesIterable) {
29 this.sources = sources;
30 this.sourcesIterable = sourcesIterable;
31 }
32
33 @Override
34 @SuppressWarnings("unchecked")
35 protected void subscribeActual(final SingleObserver<? super T> observer) {
36 SingleSource<? extends T>[] sources = this.sources;
37 int count = 0;
38 if (sources == null) {
39 sources = new SingleSource[8];
40 try {
41 for (SingleSource<? extends T> element : sourcesIterable) {
42 if (element == null) {
43 EmptyDisposable.error(new NullPointerException("One of the sources is null"), observer);
44 return;
45 }
46 if (count == sources.length) {
47 SingleSource<? extends T>[] b = new SingleSource[count + (count >> 2)];
48 System.arraycopy(sources, 0, b, 0, count);
49 sources = b;
50 }
51 sources[count++] = element;
52 }
53 } catch (Throwable e) {
54 Exceptions.throwIfFatal(e);
55 EmptyDisposable.error(e, observer);
56 return;
57 }
58 } else {
59 count = sources.length;
60 }
61
62 final AtomicBoolean winner = new AtomicBoolean();
63 final CompositeDisposable set = new CompositeDisposable();
64
65 observer.onSubscribe(set);
66
67 for (int i = 0; i < count; i++) {
68 SingleSource<? extends T> s1 = sources[i];
69 if (set.isDisposed()) {
70 return;
71 }
72
73 if (s1 == null) {
74 set.dispose();
75 Throwable e = new NullPointerException("One of the sources is null");
76 if (winner.compareAndSet(false, true)) {
77 observer.onError(e);
78 } else {
79 RxJavaPlugins.onError(e);
80 }
81 return;
82 }
83
84 s1.subscribe(new AmbSingleObserver<T>(observer, set, winner));
85 }
86 }
87
88 static final class AmbSingleObserver<T> implements SingleObserver<T> {
89
90 final CompositeDisposable set;
91
92 final SingleObserver<? super T> downstream;
93
94 final AtomicBoolean winner;
95
96 Disposable upstream;
97
98 AmbSingleObserver(SingleObserver<? super T> observer, CompositeDisposable set, AtomicBoolean winner) {
99 this.downstream = observer;
100 this.set = set;
101 this.winner = winner;
102 }
103
104 @Override
105 public void onSubscribe(Disposable d) {
106 this.upstream = d;
107 set.add(d);
108 }
109
110 @Override
111 public void onSuccess(T value) {
112 if (winner.compareAndSet(false, true)) {
113 set.delete(upstream);
114 set.dispose();
115 downstream.onSuccess(value);
116 }
117 }
118
119 @Override
120 public void onError(Throwable e) {
121 if (winner.compareAndSet(false, true)) {
122 set.delete(upstream);
123 set.dispose();
124 downstream.onError(e);
125 } else {
126 RxJavaPlugins.onError(e);
127 }
128 }
129 }
130
131 }