Debian Codegraph @

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