| 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.AtomicInteger; |
| 17 |
|
| 18 |
import io.reactivex.rxjava3.core.*; |
| 19 |
import io.reactivex.rxjava3.disposables.Disposable; |
| 20 |
import io.reactivex.rxjava3.exceptions.*; |
| 21 |
import io.reactivex.rxjava3.functions.BiPredicate; |
| 22 |
import io.reactivex.rxjava3.internal.disposables.SequentialDisposable; |
| 23 |
|
| 24 |
public final class ObservableRetryBiPredicate<T> extends AbstractObservableWithUpstream<T, T> { |
| 25 |
final BiPredicate<? super Integer, ? super Throwable> predicate; |
| 26 |
public ObservableRetryBiPredicate( |
| 27 |
Observable<T> source, |
| 28 |
BiPredicate<? super Integer, ? super Throwable> predicate) { |
| 29 |
super(source); |
| 30 |
this.predicate = predicate; |
| 31 |
} |
| 32 |
|
| 33 |
@Override |
| 34 |
public void subscribeActual(Observer<? super T> observer) { |
| 35 |
SequentialDisposable sa = new SequentialDisposable(); |
| 36 |
observer.onSubscribe(sa); |
| 37 |
|
| 38 |
RetryBiObserver<T> rs = new RetryBiObserver<>(observer, predicate, sa, source); |
| 39 |
rs.subscribeNext(); |
| 40 |
} |
| 41 |
|
| 42 |
static final class RetryBiObserver<T> extends AtomicInteger implements Observer<T> { |
| 43 |
|
| 44 |
private static final long serialVersionUID = -7098360935104053232L; |
| 45 |
|
| 46 |
final Observer<? super T> downstream; |
| 47 |
final SequentialDisposable upstream; |
| 48 |
final ObservableSource<? extends T> source; |
| 49 |
final BiPredicate<? super Integer, ? super Throwable> predicate; |
| 50 |
int retries; |
| 51 |
RetryBiObserver(Observer<? super T> actual, |
| 52 |
BiPredicate<? super Integer, ? super Throwable> predicate, SequentialDisposable sa, ObservableSource<? extends T> source) { |
| 53 |
this.downstream = actual; |
| 54 |
this.upstream = sa; |
| 55 |
this.source = source; |
| 56 |
this.predicate = predicate; |
| 57 |
} |
| 58 |
|
| 59 |
@Override |
| 60 |
public void onSubscribe(Disposable d) { |
| 61 |
upstream.replace(d); |
| 62 |
} |
| 63 |
|
| 64 |
@Override |
| 65 |
public void onNext(T t) { |
| 66 |
downstream.onNext(t); |
| 67 |
} |
| 68 |
|
| 69 |
@Override |
| 70 |
public void onError(Throwable t) { |
| 71 |
boolean b; |
| 72 |
try { |
| 73 |
b = predicate.test(++retries, t); |
| 74 |
} catch (Throwable e) { |
| 75 |
Exceptions.throwIfFatal(e); |
| 76 |
downstream.onError(new CompositeException(t, e)); |
| 77 |
return; |
| 78 |
} |
| 79 |
if (!b) { |
| 80 |
downstream.onError(t); |
| 81 |
return; |
| 82 |
} |
| 83 |
subscribeNext(); |
| 84 |
} |
| 85 |
|
| 86 |
@Override |
| 87 |
public void onComplete() { |
| 88 |
downstream.onComplete(); |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Subscribes to the source again via trampolining. |
| 93 |
*/ |
| 94 |
void subscribeNext() { |
| 95 |
if (getAndIncrement() == 0) { |
| 96 |
int missed = 1; |
| 97 |
for (;;) { |
| 98 |
if (upstream.isDisposed()) { |
| 99 |
return; |
| 100 |
} |
| 101 |
source.subscribe(this); |
| 102 |
|
| 103 |
missed = addAndGet(-missed); |
| 104 |
if (missed == 0) { |
| 105 |
break; |
| 106 |
} |
| 107 |
} |
| 108 |
} |
| 109 |
} |
| 110 |
} |
| 111 |
} |