Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/maybe/MaybeTakeUntilPublisher.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.maybe;
15
16 import java.util.concurrent.atomic.AtomicReference;
17
18 import org.reactivestreams.*;
19
20 import io.reactivex.rxjava3.core.*;
21 import io.reactivex.rxjava3.disposables.Disposable;
22 import io.reactivex.rxjava3.internal.disposables.DisposableHelper;
23 import io.reactivex.rxjava3.internal.subscriptions.SubscriptionHelper;
24 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
25
26 /**
27 * Relays the main source's event unless the other Publisher signals an item first or just completes
28 * at which point the resulting Maybe is completed.
29 *
30 * @param <T> the value type
31 * @param <U> the other's value type
32 */
33 public final class MaybeTakeUntilPublisher<T, U> extends AbstractMaybeWithUpstream<T, T> {
34
35 final Publisher<U> other;
36
37 public MaybeTakeUntilPublisher(MaybeSource<T> source, Publisher<U> other) {
38 super(source);
39 this.other = other;
40 }
41
42 @Override
43 protected void subscribeActual(MaybeObserver<? super T> observer) {
44 TakeUntilMainMaybeObserver<T, U> parent = new TakeUntilMainMaybeObserver<>(observer);
45 observer.onSubscribe(parent);
46
47 other.subscribe(parent.other);
48
49 source.subscribe(parent);
50 }
51
52 static final class TakeUntilMainMaybeObserver<T, U>
53 extends AtomicReference<Disposable> implements MaybeObserver<T>, Disposable {
54
55 private static final long serialVersionUID = -2187421758664251153L;
56
57 final MaybeObserver<? super T> downstream;
58
59 final TakeUntilOtherMaybeObserver<U> other;
60
61 TakeUntilMainMaybeObserver(MaybeObserver<? super T> downstream) {
62 this.downstream = downstream;
63 this.other = new TakeUntilOtherMaybeObserver<>(this);
64 }
65
66 @Override
67 public void dispose() {
68 DisposableHelper.dispose(this);
69 SubscriptionHelper.cancel(other);
70 }
71
72 @Override
73 public boolean isDisposed() {
74 return DisposableHelper.isDisposed(get());
75 }
76
77 @Override
78 public void onSubscribe(Disposable d) {
79 DisposableHelper.setOnce(this, d);
80 }
81
82 @Override
83 public void onSuccess(T value) {
84 SubscriptionHelper.cancel(other);
85 if (getAndSet(DisposableHelper.DISPOSED) != DisposableHelper.DISPOSED) {
86 downstream.onSuccess(value);
87 }
88 }
89
90 @Override
91 public void onError(Throwable e) {
92 SubscriptionHelper.cancel(other);
93 if (getAndSet(DisposableHelper.DISPOSED) != DisposableHelper.DISPOSED) {
94 downstream.onError(e);
95 } else {
96 RxJavaPlugins.onError(e);
97 }
98 }
99
100 @Override
101 public void onComplete() {
102 SubscriptionHelper.cancel(other);
103 if (getAndSet(DisposableHelper.DISPOSED) != DisposableHelper.DISPOSED) {
104 downstream.onComplete();
105 }
106 }
107
108 void otherError(Throwable e) {
109 if (DisposableHelper.dispose(this)) {
110 downstream.onError(e);
111 } else {
112 RxJavaPlugins.onError(e);
113 }
114 }
115
116 void otherComplete() {
117 if (DisposableHelper.dispose(this)) {
118 downstream.onComplete();
119 }
120 }
121
122 static final class TakeUntilOtherMaybeObserver<U>
123 extends AtomicReference<Subscription> implements FlowableSubscriber<U> {
124
125 private static final long serialVersionUID = -1266041316834525931L;
126
127 final TakeUntilMainMaybeObserver<?, U> parent;
128
129 TakeUntilOtherMaybeObserver(TakeUntilMainMaybeObserver<?, U> parent) {
130 this.parent = parent;
131 }
132
133 @Override
134 public void onSubscribe(Subscription s) {
135 SubscriptionHelper.setOnce(this, s, Long.MAX_VALUE);
136 }
137
138 @Override
139 public void onNext(Object value) {
140 SubscriptionHelper.cancel(this);
141 parent.otherComplete();
142 }
143
144 @Override
145 public void onError(Throwable e) {
146 parent.otherError(e);
147 }
148
149 @Override
150 public void onComplete() {
151 parent.otherComplete();
152 }
153 }
154 }
155
156 }