| 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.completable; |
| 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 CompletableAmb extends Completable { |
| 25 |
private final CompletableSource[] sources; |
| 26 |
private final Iterable<? extends CompletableSource> sourcesIterable; |
| 27 |
|
| 28 |
public CompletableAmb(CompletableSource[] sources, Iterable<? extends CompletableSource> sourcesIterable) { |
| 29 |
this.sources = sources; |
| 30 |
this.sourcesIterable = sourcesIterable; |
| 31 |
} |
| 32 |
|
| 33 |
@Override |
| 34 |
public void subscribeActual(final CompletableObserver observer) { |
| 35 |
CompletableSource[] sources = this.sources; |
| 36 |
int count = 0; |
| 37 |
if (sources == null) { |
| 38 |
sources = new CompletableSource[8]; |
| 39 |
try { |
| 40 |
for (CompletableSource element : sourcesIterable) { |
| 41 |
if (element == null) { |
| 42 |
EmptyDisposable.error(new NullPointerException("One of the sources is null"), observer); |
| 43 |
return; |
| 44 |
} |
| 45 |
if (count == sources.length) { |
| 46 |
CompletableSource[] b = new CompletableSource[count + (count >> 2)]; |
| 47 |
System.arraycopy(sources, 0, b, 0, count); |
| 48 |
sources = b; |
| 49 |
} |
| 50 |
sources[count++] = element; |
| 51 |
} |
| 52 |
} catch (Throwable e) { |
| 53 |
Exceptions.throwIfFatal(e); |
| 54 |
EmptyDisposable.error(e, observer); |
| 55 |
return; |
| 56 |
} |
| 57 |
} else { |
| 58 |
count = sources.length; |
| 59 |
} |
| 60 |
|
| 61 |
final CompositeDisposable set = new CompositeDisposable(); |
| 62 |
observer.onSubscribe(set); |
| 63 |
|
| 64 |
final AtomicBoolean once = new AtomicBoolean(); |
| 65 |
|
| 66 |
for (int i = 0; i < count; i++) { |
| 67 |
CompletableSource c = sources[i]; |
| 68 |
if (set.isDisposed()) { |
| 69 |
return; |
| 70 |
} |
| 71 |
if (c == null) { |
| 72 |
NullPointerException npe = new NullPointerException("One of the sources is null"); |
| 73 |
if (once.compareAndSet(false, true)) { |
| 74 |
set.dispose(); |
| 75 |
observer.onError(npe); |
| 76 |
} else { |
| 77 |
RxJavaPlugins.onError(npe); |
| 78 |
} |
| 79 |
return; |
| 80 |
} |
| 81 |
|
| 82 |
// no need to have separate subscribers because inner is stateless |
| 83 |
c.subscribe(new Amb(once, set, observer)); |
| 84 |
} |
| 85 |
|
| 86 |
if (count == 0) { |
| 87 |
observer.onComplete(); |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
static final class Amb implements CompletableObserver { |
| 92 |
|
| 93 |
final AtomicBoolean once; |
| 94 |
|
| 95 |
final CompositeDisposable set; |
| 96 |
|
| 97 |
final CompletableObserver downstream; |
| 98 |
|
| 99 |
Disposable upstream; |
| 100 |
|
| 101 |
Amb(AtomicBoolean once, CompositeDisposable set, CompletableObserver observer) { |
| 102 |
this.once = once; |
| 103 |
this.set = set; |
| 104 |
this.downstream = observer; |
| 105 |
} |
| 106 |
|
| 107 |
@Override |
| 108 |
public void onComplete() { |
| 109 |
if (once.compareAndSet(false, true)) { |
| 110 |
set.delete(upstream); |
| 111 |
set.dispose(); |
| 112 |
downstream.onComplete(); |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
@Override |
| 117 |
public void onError(Throwable e) { |
| 118 |
if (once.compareAndSet(false, true)) { |
| 119 |
set.delete(upstream); |
| 120 |
set.dispose(); |
| 121 |
downstream.onError(e); |
| 122 |
} else { |
| 123 |
RxJavaPlugins.onError(e); |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
@Override |
| 128 |
public void onSubscribe(Disposable d) { |
| 129 |
upstream = d; |
| 130 |
set.add(d); |
| 131 |
} |
| 132 |
} |
| 133 |
} |