Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/flowable/FlowableTakeUntil.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.*;
17
18 import org.reactivestreams.*;
19
20 import io.reactivex.rxjava3.core.*;
21 import io.reactivex.rxjava3.internal.subscriptions.SubscriptionHelper;
22 import io.reactivex.rxjava3.internal.util.*;
23
24 public final class FlowableTakeUntil<T, U> extends AbstractFlowableWithUpstream<T, T> {
25 final Publisher<? extends U> other;
26 public FlowableTakeUntil(Flowable<T> source, Publisher<? extends U> other) {
27 super(source);
28 this.other = other;
29 }
30
31 @Override
32 protected void subscribeActual(Subscriber<? super T> child) {
33 TakeUntilMainSubscriber<T> parent = new TakeUntilMainSubscriber<>(child);
34 child.onSubscribe(parent);
35
36 other.subscribe(parent.other);
37
38 source.subscribe(parent);
39 }
40
41 static final class TakeUntilMainSubscriber<T> extends AtomicInteger implements FlowableSubscriber<T>, Subscription {
42
43 private static final long serialVersionUID = -4945480365982832967L;
44
45 final Subscriber<? super T> downstream;
46
47 final AtomicLong requested;
48
49 final AtomicReference<Subscription> upstream;
50
51 final AtomicThrowable error;
52
53 final OtherSubscriber other;
54
55 TakeUntilMainSubscriber(Subscriber<? super T> downstream) {
56 this.downstream = downstream;
57 this.requested = new AtomicLong();
58 this.upstream = new AtomicReference<>();
59 this.other = new OtherSubscriber();
60 this.error = new AtomicThrowable();
61 }
62
63 @Override
64 public void onSubscribe(Subscription s) {
65 SubscriptionHelper.deferredSetOnce(this.upstream, requested, s);
66 }
67
68 @Override
69 public void onNext(T t) {
70 HalfSerializer.onNext(downstream, t, this, error);
71 }
72
73 @Override
74 public void onError(Throwable t) {
75 SubscriptionHelper.cancel(other);
76 HalfSerializer.onError(downstream, t, this, error);
77 }
78
79 @Override
80 public void onComplete() {
81 SubscriptionHelper.cancel(other);
82 HalfSerializer.onComplete(downstream, this, error);
83 }
84
85 @Override
86 public void request(long n) {
87 SubscriptionHelper.deferredRequest(upstream, requested, n);
88 }
89
90 @Override
91 public void cancel() {
92 SubscriptionHelper.cancel(upstream);
93 SubscriptionHelper.cancel(other);
94 }
95
96 final class OtherSubscriber extends AtomicReference<Subscription> implements FlowableSubscriber<Object> {
97
98 private static final long serialVersionUID = -3592821756711087922L;
99
100 @Override
101 public void onSubscribe(Subscription s) {
102 SubscriptionHelper.setOnce(this, s, Long.MAX_VALUE);
103 }
104
105 @Override
106 public void onNext(Object t) {
107 SubscriptionHelper.cancel(this);
108 onComplete();
109 }
110
111 @Override
112 public void onError(Throwable t) {
113 SubscriptionHelper.cancel(upstream);
114 HalfSerializer.onError(downstream, t, TakeUntilMainSubscriber.this, error);
115 }
116
117 @Override
118 public void onComplete() {
119 SubscriptionHelper.cancel(upstream);
120 HalfSerializer.onComplete(downstream, TakeUntilMainSubscriber.this, error);
121 }
122
123 }
124 }
125 }