Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/flowable/FlowableRepeatUntil.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.flowable;
15
16 import java.util.concurrent.atomic.AtomicInteger;
17
18 import org.reactivestreams.*;
19
20 import io.reactivex.rxjava3.core.*;
21 import io.reactivex.rxjava3.exceptions.Exceptions;
22 import io.reactivex.rxjava3.functions.BooleanSupplier;
23 import io.reactivex.rxjava3.internal.subscriptions.SubscriptionArbiter;
24
25 public final class FlowableRepeatUntil<T> extends AbstractFlowableWithUpstream<T, T> {
26 final BooleanSupplier until;
27 public FlowableRepeatUntil(Flowable<T> source, BooleanSupplier until) {
28 super(source);
29 this.until = until;
30 }
31
32 @Override
33 public void subscribeActual(Subscriber<? super T> s) {
34 SubscriptionArbiter sa = new SubscriptionArbiter(false);
35 s.onSubscribe(sa);
36
37 RepeatSubscriber<T> rs = new RepeatSubscriber<>(s, until, sa, source);
38 rs.subscribeNext();
39 }
40
41 static final class RepeatSubscriber<T> extends AtomicInteger implements FlowableSubscriber<T> {
42
43 private static final long serialVersionUID = -7098360935104053232L;
44
45 final Subscriber<? super T> downstream;
46 final SubscriptionArbiter sa;
47 final Publisher<? extends T> source;
48 final BooleanSupplier stop;
49
50 long produced;
51
52 RepeatSubscriber(Subscriber<? super T> actual, BooleanSupplier until, SubscriptionArbiter sa, Publisher<? extends T> source) {
53 this.downstream = actual;
54 this.sa = sa;
55 this.source = source;
56 this.stop = until;
57 }
58
59 @Override
60 public void onSubscribe(Subscription s) {
61 sa.setSubscription(s);
62 }
63
64 @Override
65 public void onNext(T t) {
66 produced++;
67 downstream.onNext(t);
68 }
69
70 @Override
71 public void onError(Throwable t) {
72 downstream.onError(t);
73 }
74
75 @Override
76 public void onComplete() {
77 boolean b;
78 try {
79 b = stop.getAsBoolean();
80 } catch (Throwable e) {
81 Exceptions.throwIfFatal(e);
82 downstream.onError(e);
83 return;
84 }
85 if (b) {
86 downstream.onComplete();
87 } else {
88 subscribeNext();
89 }
90 }
91
92 /**
93 * Subscribes to the source again via trampolining.
94 */
95 void subscribeNext() {
96 if (getAndIncrement() == 0) {
97 int missed = 1;
98 for (;;) {
99 if (sa.isCancelled()) {
100 return;
101 }
102
103 long p = produced;
104 if (p != 0L) {
105 produced = 0L;
106 sa.produced(p);
107 }
108
109 source.subscribe(this);
110
111 missed = addAndGet(-missed);
112 if (missed == 0) {
113 break;
114 }
115 }
116 }
117 }
118 }
119 }