| 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.AtomicInteger; |
| 17 |
|
| 18 |
import io.reactivex.rxjava3.core.*; |
| 19 |
import io.reactivex.rxjava3.disposables.Disposable; |
| 20 |
import io.reactivex.rxjava3.internal.disposables.SequentialDisposable; |
| 21 |
|
| 22 |
public final class CompletableConcatArray extends Completable { |
| 23 |
final CompletableSource[] sources; |
| 24 |
|
| 25 |
public CompletableConcatArray(CompletableSource[] sources) { |
| 26 |
this.sources = sources; |
| 27 |
} |
| 28 |
|
| 29 |
@Override |
| 30 |
public void subscribeActual(CompletableObserver observer) { |
| 31 |
ConcatInnerObserver inner = new ConcatInnerObserver(observer, sources); |
| 32 |
observer.onSubscribe(inner.sd); |
| 33 |
inner.next(); |
| 34 |
} |
| 35 |
|
| 36 |
static final class ConcatInnerObserver extends AtomicInteger implements CompletableObserver { |
| 37 |
|
| 38 |
private static final long serialVersionUID = -7965400327305809232L; |
| 39 |
|
| 40 |
final CompletableObserver downstream; |
| 41 |
final CompletableSource[] sources; |
| 42 |
|
| 43 |
int index; |
| 44 |
|
| 45 |
final SequentialDisposable sd; |
| 46 |
|
| 47 |
ConcatInnerObserver(CompletableObserver actual, CompletableSource[] sources) { |
| 48 |
this.downstream = actual; |
| 49 |
this.sources = sources; |
| 50 |
this.sd = new SequentialDisposable(); |
| 51 |
} |
| 52 |
|
| 53 |
@Override |
| 54 |
public void onSubscribe(Disposable d) { |
| 55 |
sd.replace(d); |
| 56 |
} |
| 57 |
|
| 58 |
@Override |
| 59 |
public void onError(Throwable e) { |
| 60 |
downstream.onError(e); |
| 61 |
} |
| 62 |
|
| 63 |
@Override |
| 64 |
public void onComplete() { |
| 65 |
next(); |
| 66 |
} |
| 67 |
|
| 68 |
void next() { |
| 69 |
if (sd.isDisposed()) { |
| 70 |
return; |
| 71 |
} |
| 72 |
|
| 73 |
if (getAndIncrement() != 0) { |
| 74 |
return; |
| 75 |
} |
| 76 |
|
| 77 |
CompletableSource[] a = sources; |
| 78 |
do { |
| 79 |
if (sd.isDisposed()) { |
| 80 |
return; |
| 81 |
} |
| 82 |
|
| 83 |
int idx = index++; |
| 84 |
if (idx == a.length) { |
| 85 |
downstream.onComplete(); |
| 86 |
return; |
| 87 |
} |
| 88 |
|
| 89 |
a[idx].subscribe(this); |
| 90 |
} while (decrementAndGet() != 0); |
| 91 |
} |
| 92 |
} |
| 93 |
} |