| 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.*; |
| 17 |
|
| 18 |
import io.reactivex.rxjava3.core.*; |
| 19 |
import io.reactivex.rxjava3.disposables.*; |
| 20 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 21 |
|
| 22 |
public final class CompletableMergeArray extends Completable { |
| 23 |
final CompletableSource[] sources; |
| 24 |
|
| 25 |
public CompletableMergeArray(CompletableSource[] sources) { |
| 26 |
this.sources = sources; |
| 27 |
} |
| 28 |
|
| 29 |
@Override |
| 30 |
public void subscribeActual(final CompletableObserver observer) { |
| 31 |
final CompositeDisposable set = new CompositeDisposable(); |
| 32 |
final AtomicBoolean once = new AtomicBoolean(); |
| 33 |
|
| 34 |
InnerCompletableObserver shared = new InnerCompletableObserver(observer, once, set, sources.length + 1); |
| 35 |
observer.onSubscribe(shared); |
| 36 |
|
| 37 |
for (CompletableSource c : sources) { |
| 38 |
if (set.isDisposed()) { |
| 39 |
return; |
| 40 |
} |
| 41 |
|
| 42 |
if (c == null) { |
| 43 |
set.dispose(); |
| 44 |
NullPointerException npe = new NullPointerException("A completable source is null"); |
| 45 |
shared.onError(npe); |
| 46 |
return; |
| 47 |
} |
| 48 |
|
| 49 |
c.subscribe(shared); |
| 50 |
} |
| 51 |
|
| 52 |
shared.onComplete(); |
| 53 |
} |
| 54 |
|
| 55 |
static final class InnerCompletableObserver extends AtomicInteger implements CompletableObserver, Disposable { |
| 56 |
private static final long serialVersionUID = -8360547806504310570L; |
| 57 |
|
| 58 |
final CompletableObserver downstream; |
| 59 |
|
| 60 |
final AtomicBoolean once; |
| 61 |
|
| 62 |
final CompositeDisposable set; |
| 63 |
|
| 64 |
InnerCompletableObserver(CompletableObserver actual, AtomicBoolean once, CompositeDisposable set, int n) { |
| 65 |
this.downstream = actual; |
| 66 |
this.once = once; |
| 67 |
this.set = set; |
| 68 |
this.lazySet(n); |
| 69 |
} |
| 70 |
|
| 71 |
@Override |
| 72 |
public void onSubscribe(Disposable d) { |
| 73 |
set.add(d); |
| 74 |
} |
| 75 |
|
| 76 |
@Override |
| 77 |
public void onError(Throwable e) { |
| 78 |
set.dispose(); |
| 79 |
if (once.compareAndSet(false, true)) { |
| 80 |
downstream.onError(e); |
| 81 |
} else { |
| 82 |
RxJavaPlugins.onError(e); |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
@Override |
| 87 |
public void onComplete() { |
| 88 |
if (decrementAndGet() == 0) { |
| 89 |
// errors don't decrement this |
| 90 |
downstream.onComplete(); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
@Override |
| 95 |
public void dispose() { |
| 96 |
set.dispose(); |
| 97 |
once.set(true); |
| 98 |
} |
| 99 |
|
| 100 |
@Override |
| 101 |
public boolean isDisposed() { |
| 102 |
return set.isDisposed(); |
| 103 |
} |
| 104 |
} |
| 105 |
} |