Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/single/SingleTimeout.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.single;
15
16 import static io.reactivex.rxjava3.internal.util.ExceptionHelper.timeoutMessage;
17
18 import java.util.concurrent.*;
19 import java.util.concurrent.atomic.AtomicReference;
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.plugins.RxJavaPlugins;
25
26 public final class SingleTimeout<T> extends Single<T> {
27
28 final SingleSource<T> source;
29
30 final long timeout;
31
32 final TimeUnit unit;
33
34 final Scheduler scheduler;
35
36 final SingleSource<? extends T> other;
37
38 public SingleTimeout(SingleSource<T> source, long timeout, TimeUnit unit, Scheduler scheduler,
39 SingleSource<? extends T> other) {
40 this.source = source;
41 this.timeout = timeout;
42 this.unit = unit;
43 this.scheduler = scheduler;
44 this.other = other;
45 }
46
47 @Override
48 protected void subscribeActual(final SingleObserver<? super T> observer) {
49
50 TimeoutMainObserver<T> parent = new TimeoutMainObserver<>(observer, other, timeout, unit);
51 observer.onSubscribe(parent);
52
53 DisposableHelper.replace(parent.task, scheduler.scheduleDirect(parent, timeout, unit));
54
55 source.subscribe(parent);
56 }
57
58 static final class TimeoutMainObserver<T> extends AtomicReference<Disposable>
59 implements SingleObserver<T>, Runnable, Disposable {
60
61 private static final long serialVersionUID = 37497744973048446L;
62
63 final SingleObserver<? super T> downstream;
64
65 final AtomicReference<Disposable> task;
66
67 final TimeoutFallbackObserver<T> fallback;
68
69 SingleSource<? extends T> other;
70
71 final long timeout;
72
73 final TimeUnit unit;
74
75 static final class TimeoutFallbackObserver<T> extends AtomicReference<Disposable>
76 implements SingleObserver<T> {
77
78 private static final long serialVersionUID = 2071387740092105509L;
79 final SingleObserver<? super T> downstream;
80
81 TimeoutFallbackObserver(SingleObserver<? super T> downstream) {
82 this.downstream = downstream;
83 }
84
85 @Override
86 public void onSubscribe(Disposable d) {
87 DisposableHelper.setOnce(this, d);
88 }
89
90 @Override
91 public void onSuccess(T t) {
92 downstream.onSuccess(t);
93 }
94
95 @Override
96 public void onError(Throwable e) {
97 downstream.onError(e);
98 }
99 }
100
101 TimeoutMainObserver(SingleObserver<? super T> actual, SingleSource<? extends T> other, long timeout, TimeUnit unit) {
102 this.downstream = actual;
103 this.other = other;
104 this.timeout = timeout;
105 this.unit = unit;
106 this.task = new AtomicReference<>();
107 if (other != null) {
108 this.fallback = new TimeoutFallbackObserver<>(actual);
109 } else {
110 this.fallback = null;
111 }
112 }
113
114 @Override
115 public void run() {
116 Disposable d = get();
117 if (d != DisposableHelper.DISPOSED && compareAndSet(d, DisposableHelper.DISPOSED)) {
118 if (d != null) {
119 d.dispose();
120 }
121 SingleSource<? extends T> other = this.other;
122 if (other == null) {
123 downstream.onError(new TimeoutException(timeoutMessage(timeout, unit)));
124 } else {
125 this.other = null;
126 other.subscribe(fallback);
127 }
128 }
129 }
130
131 @Override
132 public void onSubscribe(Disposable d) {
133 DisposableHelper.setOnce(this, d);
134 }
135
136 @Override
137 public void onSuccess(T t) {
138 Disposable d = get();
139 if (d != DisposableHelper.DISPOSED && compareAndSet(d, DisposableHelper.DISPOSED)) {
140 DisposableHelper.dispose(task);
141 downstream.onSuccess(t);
142 }
143 }
144
145 @Override
146 public void onError(Throwable e) {
147 Disposable d = get();
148 if (d != DisposableHelper.DISPOSED && compareAndSet(d, DisposableHelper.DISPOSED)) {
149 DisposableHelper.dispose(task);
150 downstream.onError(e);
151 } else {
152 RxJavaPlugins.onError(e);
153 }
154 }
155
156 @Override
157 public void dispose() {
158 DisposableHelper.dispose(this);
159 DisposableHelper.dispose(task);
160 if (fallback != null) {
161 DisposableHelper.dispose(fallback);
162 }
163 }
164
165 @Override
166 public boolean isDisposed() {
167 return DisposableHelper.isDisposed(get());
168 }
169 }
170 }