| 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.concurrent.atomic.*; |
| 17 |
|
| 18 |
import io.reactivex.rxjava3.core.*; |
| 19 |
import io.reactivex.rxjava3.disposables.Disposable; |
| 20 |
import io.reactivex.rxjava3.exceptions.Exceptions; |
| 21 |
import io.reactivex.rxjava3.internal.disposables.*; |
| 22 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 23 |
|
| 24 |
public final class ObservableAmb<T> extends Observable<T> { |
| 25 |
final ObservableSource<? extends T>[] sources; |
| 26 |
final Iterable<? extends ObservableSource<? extends T>> sourcesIterable; |
| 27 |
|
| 28 |
public ObservableAmb(ObservableSource<? extends T>[] sources, Iterable<? extends ObservableSource<? extends T>> sourcesIterable) { |
| 29 |
this.sources = sources; |
| 30 |
this.sourcesIterable = sourcesIterable; |
| 31 |
} |
| 32 |
|
| 33 |
@Override |
| 34 |
@SuppressWarnings("unchecked") |
| 35 |
public void subscribeActual(Observer<? super T> observer) { |
| 36 |
ObservableSource<? extends T>[] sources = this.sources; |
| 37 |
int count = 0; |
| 38 |
if (sources == null) { |
| 39 |
sources = new ObservableSource[8]; |
| 40 |
try { |
| 41 |
for (ObservableSource<? extends T> p : sourcesIterable) { |
| 42 |
if (p == null) { |
| 43 |
EmptyDisposable.error(new NullPointerException("One of the sources is null"), observer); |
| 44 |
return; |
| 45 |
} |
| 46 |
if (count == sources.length) { |
| 47 |
ObservableSource<? extends T>[] b = new ObservableSource[count + (count >> 2)]; |
| 48 |
System.arraycopy(sources, 0, b, 0, count); |
| 49 |
sources = b; |
| 50 |
} |
| 51 |
sources[count++] = p; |
| 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 |
if (count == 0) { |
| 63 |
EmptyDisposable.complete(observer); |
| 64 |
return; |
| 65 |
} else |
| 66 |
if (count == 1) { |
| 67 |
sources[0].subscribe(observer); |
| 68 |
return; |
| 69 |
} |
| 70 |
|
| 71 |
AmbCoordinator<T> ac = new AmbCoordinator<>(observer, count); |
| 72 |
ac.subscribe(sources); |
| 73 |
} |
| 74 |
|
| 75 |
static final class AmbCoordinator<T> implements Disposable { |
| 76 |
final Observer<? super T> downstream; |
| 77 |
final AmbInnerObserver<T>[] observers; |
| 78 |
|
| 79 |
final AtomicInteger winner = new AtomicInteger(); |
| 80 |
|
| 81 |
@SuppressWarnings("unchecked") |
| 82 |
AmbCoordinator(Observer<? super T> actual, int count) { |
| 83 |
this.downstream = actual; |
| 84 |
this.observers = new AmbInnerObserver[count]; |
| 85 |
} |
| 86 |
|
| 87 |
public void subscribe(ObservableSource<? extends T>[] sources) { |
| 88 |
AmbInnerObserver<T>[] as = observers; |
| 89 |
int len = as.length; |
| 90 |
for (int i = 0; i < len; i++) { |
| 91 |
as[i] = new AmbInnerObserver<>(this, i + 1, downstream); |
| 92 |
} |
| 93 |
winner.lazySet(0); // release the contents of 'as' |
| 94 |
downstream.onSubscribe(this); |
| 95 |
|
| 96 |
for (int i = 0; i < len; i++) { |
| 97 |
if (winner.get() != 0) { |
| 98 |
return; |
| 99 |
} |
| 100 |
|
| 101 |
sources[i].subscribe(as[i]); |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
public boolean win(int index) { |
| 106 |
int w = winner.get(); |
| 107 |
if (w == 0) { |
| 108 |
if (winner.compareAndSet(0, index)) { |
| 109 |
AmbInnerObserver<T>[] a = observers; |
| 110 |
int n = a.length; |
| 111 |
for (int i = 0; i < n; i++) { |
| 112 |
if (i + 1 != index) { |
| 113 |
a[i].dispose(); |
| 114 |
} |
| 115 |
} |
| 116 |
return true; |
| 117 |
} |
| 118 |
} |
| 119 |
return false; |
| 120 |
} |
| 121 |
|
| 122 |
@Override |
| 123 |
public void dispose() { |
| 124 |
if (winner.get() != -1) { |
| 125 |
winner.lazySet(-1); |
| 126 |
|
| 127 |
for (AmbInnerObserver<T> a : observers) { |
| 128 |
a.dispose(); |
| 129 |
} |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
@Override |
| 134 |
public boolean isDisposed() { |
| 135 |
return winner.get() == -1; |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
static final class AmbInnerObserver<T> extends AtomicReference<Disposable> implements Observer<T> { |
| 140 |
|
| 141 |
private static final long serialVersionUID = -1185974347409665484L; |
| 142 |
final AmbCoordinator<T> parent; |
| 143 |
final int index; |
| 144 |
final Observer<? super T> downstream; |
| 145 |
|
| 146 |
boolean won; |
| 147 |
|
| 148 |
AmbInnerObserver(AmbCoordinator<T> parent, int index, Observer<? super T> downstream) { |
| 149 |
this.parent = parent; |
| 150 |
this.index = index; |
| 151 |
this.downstream = downstream; |
| 152 |
} |
| 153 |
|
| 154 |
@Override |
| 155 |
public void onSubscribe(Disposable d) { |
| 156 |
DisposableHelper.setOnce(this, d); |
| 157 |
} |
| 158 |
|
| 159 |
@Override |
| 160 |
public void onNext(T t) { |
| 161 |
if (won) { |
| 162 |
downstream.onNext(t); |
| 163 |
} else { |
| 164 |
if (parent.win(index)) { |
| 165 |
won = true; |
| 166 |
downstream.onNext(t); |
| 167 |
} else { |
| 168 |
get().dispose(); |
| 169 |
} |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
@Override |
| 174 |
public void onError(Throwable t) { |
| 175 |
if (won) { |
| 176 |
downstream.onError(t); |
| 177 |
} else { |
| 178 |
if (parent.win(index)) { |
| 179 |
won = true; |
| 180 |
downstream.onError(t); |
| 181 |
} else { |
| 182 |
RxJavaPlugins.onError(t); |
| 183 |
} |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
@Override |
| 188 |
public void onComplete() { |
| 189 |
if (won) { |
| 190 |
downstream.onComplete(); |
| 191 |
} else { |
| 192 |
if (parent.win(index)) { |
| 193 |
won = true; |
| 194 |
downstream.onComplete(); |
| 195 |
} |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
public void dispose() { |
| 200 |
DisposableHelper.dispose(this); |
| 201 |
} |
| 202 |
} |
| 203 |
} |