| 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.Iterator; |
| 17 |
import java.util.Objects; |
| 18 |
import java.util.concurrent.atomic.AtomicInteger; |
| 19 |
|
| 20 |
import io.reactivex.rxjava3.core.*; |
| 21 |
import io.reactivex.rxjava3.disposables.Disposable; |
| 22 |
import io.reactivex.rxjava3.exceptions.Exceptions; |
| 23 |
import io.reactivex.rxjava3.internal.disposables.*; |
| 24 |
|
| 25 |
public final class CompletableConcatIterable extends Completable { |
| 26 |
final Iterable<? extends CompletableSource> sources; |
| 27 |
|
| 28 |
public CompletableConcatIterable(Iterable<? extends CompletableSource> sources) { |
| 29 |
this.sources = sources; |
| 30 |
} |
| 31 |
|
| 32 |
@Override |
| 33 |
public void subscribeActual(CompletableObserver observer) { |
| 34 |
|
| 35 |
Iterator<? extends CompletableSource> it; |
| 36 |
|
| 37 |
try { |
| 38 |
it = Objects.requireNonNull(sources.iterator(), "The iterator returned is null"); |
| 39 |
} catch (Throwable e) { |
| 40 |
Exceptions.throwIfFatal(e); |
| 41 |
EmptyDisposable.error(e, observer); |
| 42 |
return; |
| 43 |
} |
| 44 |
|
| 45 |
ConcatInnerObserver inner = new ConcatInnerObserver(observer, it); |
| 46 |
observer.onSubscribe(inner.sd); |
| 47 |
inner.next(); |
| 48 |
} |
| 49 |
|
| 50 |
static final class ConcatInnerObserver extends AtomicInteger implements CompletableObserver { |
| 51 |
|
| 52 |
private static final long serialVersionUID = -7965400327305809232L; |
| 53 |
|
| 54 |
final CompletableObserver downstream; |
| 55 |
final Iterator<? extends CompletableSource> sources; |
| 56 |
|
| 57 |
final SequentialDisposable sd; |
| 58 |
|
| 59 |
ConcatInnerObserver(CompletableObserver actual, Iterator<? extends CompletableSource> sources) { |
| 60 |
this.downstream = actual; |
| 61 |
this.sources = sources; |
| 62 |
this.sd = new SequentialDisposable(); |
| 63 |
} |
| 64 |
|
| 65 |
@Override |
| 66 |
public void onSubscribe(Disposable d) { |
| 67 |
sd.replace(d); |
| 68 |
} |
| 69 |
|
| 70 |
@Override |
| 71 |
public void onError(Throwable e) { |
| 72 |
downstream.onError(e); |
| 73 |
} |
| 74 |
|
| 75 |
@Override |
| 76 |
public void onComplete() { |
| 77 |
next(); |
| 78 |
} |
| 79 |
|
| 80 |
void next() { |
| 81 |
if (sd.isDisposed()) { |
| 82 |
return; |
| 83 |
} |
| 84 |
|
| 85 |
if (getAndIncrement() != 0) { |
| 86 |
return; |
| 87 |
} |
| 88 |
|
| 89 |
Iterator<? extends CompletableSource> a = sources; |
| 90 |
do { |
| 91 |
if (sd.isDisposed()) { |
| 92 |
return; |
| 93 |
} |
| 94 |
|
| 95 |
boolean b; |
| 96 |
try { |
| 97 |
b = a.hasNext(); |
| 98 |
} catch (Throwable ex) { |
| 99 |
Exceptions.throwIfFatal(ex); |
| 100 |
downstream.onError(ex); |
| 101 |
return; |
| 102 |
} |
| 103 |
|
| 104 |
if (!b) { |
| 105 |
downstream.onComplete(); |
| 106 |
return; |
| 107 |
} |
| 108 |
|
| 109 |
CompletableSource c; |
| 110 |
|
| 111 |
try { |
| 112 |
c = Objects.requireNonNull(a.next(), "The CompletableSource returned is null"); |
| 113 |
} catch (Throwable ex) { |
| 114 |
Exceptions.throwIfFatal(ex); |
| 115 |
downstream.onError(ex); |
| 116 |
return; |
| 117 |
} |
| 118 |
|
| 119 |
c.subscribe(this); |
| 120 |
} while (decrementAndGet() != 0); |
| 121 |
} |
| 122 |
} |
| 123 |
} |