Debian Codegraph @

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