Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/maybe/MaybeTimeoutPublisher.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.TimeoutException;
17 import java.util.concurrent.atomic.AtomicReference;
18
19 import org.reactivestreams.*;
20
21 import io.reactivex.rxjava3.core.*;
22 import io.reactivex.rxjava3.disposables.Disposable;
23 import io.reactivex.rxjava3.internal.disposables.DisposableHelper;
24 import io.reactivex.rxjava3.internal.subscriptions.SubscriptionHelper;
25 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
26
27 /**
28 * Switches to the fallback Maybe if the other Publisher signals a success or completes, or
29 * signals TimeoutException if fallback is null.
30 *
31 * @param <T> the main value type
32 * @param <U> the other value type
33 */
34 public final class MaybeTimeoutPublisher<T, U> extends AbstractMaybeWithUpstream<T, T> {
35
36 final Publisher<U> other;
37
38 final MaybeSource<? extends T> fallback;
39
40 public MaybeTimeoutPublisher(MaybeSource<T> source, Publisher<U> other, MaybeSource<? extends T> fallback) {
41 super(source);
42 this.other = other;
43 this.fallback = fallback;
44 }
45
46 @Override
47 protected void subscribeActual(MaybeObserver<? super T> observer) {
48 TimeoutMainMaybeObserver<T, U> parent = new TimeoutMainMaybeObserver<>(observer, fallback);
49 observer.onSubscribe(parent);
50
51 other.subscribe(parent.other);
52
53 source.subscribe(parent);
54 }
55
56 static final class TimeoutMainMaybeObserver<T, U>
57 extends AtomicReference<Disposable>
58 implements MaybeObserver<T>, Disposable {
59
60 private static final long serialVersionUID = -5955289211445418871L;
61
62 final MaybeObserver<? super T> downstream;
63
64 final TimeoutOtherMaybeObserver<T, U> other;
65
66 final MaybeSource<? extends T> fallback;
67
68 final TimeoutFallbackMaybeObserver<T> otherObserver;
69
70 TimeoutMainMaybeObserver(MaybeObserver<? super T> actual, MaybeSource<? extends T> fallback) {
71 this.downstream = actual;
72 this.other = new TimeoutOtherMaybeObserver<>(this);
73 this.fallback = fallback;
74 this.otherObserver = fallback != null ? new TimeoutFallbackMaybeObserver<>(actual) : null;
75 }
76
77 @Override
78 public void dispose() {
79 DisposableHelper.dispose(this);
80 SubscriptionHelper.cancel(other);
81 TimeoutFallbackMaybeObserver<T> oo = otherObserver;
82 if (oo != null) {
83 DisposableHelper.dispose(oo);
84 }
85 }
86
87 @Override
88 public boolean isDisposed() {
89 return DisposableHelper.isDisposed(get());
90 }
91
92 @Override
93 public void onSubscribe(Disposable d) {
94 DisposableHelper.setOnce(this, d);
95 }
96
97 @Override
98 public void onSuccess(T value) {
99 SubscriptionHelper.cancel(other);
100 if (getAndSet(DisposableHelper.DISPOSED) != DisposableHelper.DISPOSED) {
101 downstream.onSuccess(value);
102 }
103 }
104
105 @Override
106 public void onError(Throwable e) {
107 SubscriptionHelper.cancel(other);
108 if (getAndSet(DisposableHelper.DISPOSED) != DisposableHelper.DISPOSED) {
109 downstream.onError(e);
110 } else {
111 RxJavaPlugins.onError(e);
112 }
113 }
114
115 @Override
116 public void onComplete() {
117 SubscriptionHelper.cancel(other);
118 if (getAndSet(DisposableHelper.DISPOSED) != DisposableHelper.DISPOSED) {
119 downstream.onComplete();
120 }
121 }
122
123 public void otherError(Throwable e) {
124 if (DisposableHelper.dispose(this)) {
125 downstream.onError(e);
126 } else {
127 RxJavaPlugins.onError(e);
128 }
129 }
130
131 public void otherComplete() {
132 if (DisposableHelper.dispose(this)) {
133 if (fallback == null) {
134 downstream.onError(new TimeoutException());
135 } else {
136 fallback.subscribe(otherObserver);
137 }
138 }
139 }
140 }
141
142 static final class TimeoutOtherMaybeObserver<T, U>
143 extends AtomicReference<Subscription>
144 implements FlowableSubscriber<Object> {
145
146 private static final long serialVersionUID = 8663801314800248617L;
147
148 final TimeoutMainMaybeObserver<T, U> parent;
149
150 TimeoutOtherMaybeObserver(TimeoutMainMaybeObserver<T, U> parent) {
151 this.parent = parent;
152 }
153
154 @Override
155 public void onSubscribe(Subscription s) {
156 SubscriptionHelper.setOnce(this, s, Long.MAX_VALUE);
157 }
158
159 @Override
160 public void onNext(Object value) {
161 get().cancel();
162 parent.otherComplete();
163 }
164
165 @Override
166 public void onError(Throwable e) {
167 parent.otherError(e);
168 }
169
170 @Override
171 public void onComplete() {
172 parent.otherComplete();
173 }
174 }
175
176 static final class TimeoutFallbackMaybeObserver<T>
177 extends AtomicReference<Disposable>
178 implements MaybeObserver<T> {
179
180 private static final long serialVersionUID = 8663801314800248617L;
181
182 final MaybeObserver<? super T> downstream;
183
184 TimeoutFallbackMaybeObserver(MaybeObserver<? super T> downstream) {
185 this.downstream = downstream;
186 }
187
188 @Override
189 public void onSubscribe(Disposable d) {
190 DisposableHelper.setOnce(this, d);
191 }
192
193 @Override
194 public void onSuccess(T value) {
195 downstream.onSuccess(value);
196 }
197
198 @Override
199 public void onError(Throwable e) {
200 downstream.onError(e);
201 }
202
203 @Override
204 public void onComplete() {
205 downstream.onComplete();
206 }
207 }
208 }