Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/flowable/FlowableAmb.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.concurrent.atomic.*;
17
18 import org.reactivestreams.*;
19
20 import io.reactivex.rxjava3.core.*;
21 import io.reactivex.rxjava3.exceptions.Exceptions;
22 import io.reactivex.rxjava3.internal.subscriptions.*;
23 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
24
25 public final class FlowableAmb<T> extends Flowable<T> {
26 final Publisher<? extends T>[] sources;
27 final Iterable<? extends Publisher<? extends T>> sourcesIterable;
28
29 public FlowableAmb(Publisher<? extends T>[] sources, Iterable<? extends Publisher<? extends T>> sourcesIterable) {
30 this.sources = sources;
31 this.sourcesIterable = sourcesIterable;
32 }
33
34 @Override
35 @SuppressWarnings("unchecked")
36 public void subscribeActual(Subscriber<? super T> s) {
37 Publisher<? extends T>[] sources = this.sources;
38 int count = 0;
39 if (sources == null) {
40 sources = new Publisher[8];
41 try {
42 for (Publisher<? extends T> p : sourcesIterable) {
43 if (p == null) {
44 EmptySubscription.error(new NullPointerException("One of the sources is null"), s);
45 return;
46 }
47 if (count == sources.length) {
48 Publisher<? extends T>[] b = new Publisher[count + (count >> 2)];
49 System.arraycopy(sources, 0, b, 0, count);
50 sources = b;
51 }
52 sources[count++] = p;
53 }
54 } catch (Throwable e) {
55 Exceptions.throwIfFatal(e);
56 EmptySubscription.error(e, s);
57 return;
58 }
59 } else {
60 count = sources.length;
61 }
62
63 if (count == 0) {
64 EmptySubscription.complete(s);
65 return;
66 } else
67 if (count == 1) {
68 sources[0].subscribe(s);
69 return;
70 }
71
72 AmbCoordinator<T> ac = new AmbCoordinator<>(s, count);
73 ac.subscribe(sources);
74 }
75
76 static final class AmbCoordinator<T> implements Subscription {
77 final Subscriber<? super T> downstream;
78 final AmbInnerSubscriber<T>[] subscribers;
79
80 final AtomicInteger winner = new AtomicInteger();
81
82 @SuppressWarnings("unchecked")
83 AmbCoordinator(Subscriber<? super T> actual, int count) {
84 this.downstream = actual;
85 this.subscribers = new AmbInnerSubscriber[count];
86 }
87
88 public void subscribe(Publisher<? extends T>[] sources) {
89 AmbInnerSubscriber<T>[] as = subscribers;
90 int len = as.length;
91 for (int i = 0; i < len; i++) {
92 as[i] = new AmbInnerSubscriber<>(this, i + 1, downstream);
93 }
94 winner.lazySet(0); // release the contents of 'as'
95 downstream.onSubscribe(this);
96
97 for (int i = 0; i < len; i++) {
98 if (winner.get() != 0) {
99 return;
100 }
101
102 sources[i].subscribe(as[i]);
103 }
104 }
105
106 @Override
107 public void request(long n) {
108 if (SubscriptionHelper.validate(n)) {
109 int w = winner.get();
110 if (w > 0) {
111 subscribers[w - 1].request(n);
112 } else
113 if (w == 0) {
114 for (AmbInnerSubscriber<T> a : subscribers) {
115 a.request(n);
116 }
117 }
118 }
119 }
120
121 public boolean win(int index) {
122 int w = winner.get();
123 if (w == 0) {
124 if (winner.compareAndSet(0, index)) {
125 AmbInnerSubscriber<T>[] a = subscribers;
126 int n = a.length;
127 for (int i = 0; i < n; i++) {
128 if (i + 1 != index) {
129 a[i].cancel();
130 }
131 }
132 return true;
133 }
134 }
135 return false;
136 }
137
138 @Override
139 public void cancel() {
140 if (winner.get() != -1) {
141 winner.lazySet(-1);
142
143 for (AmbInnerSubscriber<T> a : subscribers) {
144 a.cancel();
145 }
146 }
147 }
148 }
149
150 static final class AmbInnerSubscriber<T> extends AtomicReference<Subscription> implements FlowableSubscriber<T>, Subscription {
151
152 private static final long serialVersionUID = -1185974347409665484L;
153 final AmbCoordinator<T> parent;
154 final int index;
155 final Subscriber<? super T> downstream;
156
157 boolean won;
158
159 final AtomicLong missedRequested = new AtomicLong();
160
161 AmbInnerSubscriber(AmbCoordinator<T> parent, int index, Subscriber<? super T> downstream) {
162 this.parent = parent;
163 this.index = index;
164 this.downstream = downstream;
165 }
166
167 @Override
168 public void onSubscribe(Subscription s) {
169 SubscriptionHelper.deferredSetOnce(this, missedRequested, s);
170 }
171
172 @Override
173 public void request(long n) {
174 SubscriptionHelper.deferredRequest(this, missedRequested, n);
175 }
176
177 @Override
178 public void onNext(T t) {
179 if (won) {
180 downstream.onNext(t);
181 } else {
182 if (parent.win(index)) {
183 won = true;
184 downstream.onNext(t);
185 } else {
186 get().cancel();
187 }
188 }
189 }
190
191 @Override
192 public void onError(Throwable t) {
193 if (won) {
194 downstream.onError(t);
195 } else {
196 if (parent.win(index)) {
197 won = true;
198 downstream.onError(t);
199 } else {
200 get().cancel();
201 RxJavaPlugins.onError(t);
202 }
203 }
204 }
205
206 @Override
207 public void onComplete() {
208 if (won) {
209 downstream.onComplete();
210 } else {
211 if (parent.win(index)) {
212 won = true;
213 downstream.onComplete();
214 } else {
215 get().cancel();
216 }
217 }
218 }
219
220 @Override
221 public void cancel() {
222 SubscriptionHelper.cancel(this);
223 }
224
225 }
226 }