Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/observable/ObservableRetryPredicate.java

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