Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/observers/FutureMultiObserver.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.observers;
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.annotations.NonNull;
22 import io.reactivex.rxjava3.core.*;
23 import io.reactivex.rxjava3.disposables.Disposable;
24 import io.reactivex.rxjava3.internal.disposables.DisposableHelper;
25 import io.reactivex.rxjava3.internal.util.BlockingHelper;
26 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
27
28 /**
29 * An Observer + Future that expects exactly one upstream value and provides it
30 * via the (blocking) Future API.
31 *
32 * @param <T> the value type
33 */
34 public final class FutureMultiObserver<T> extends CountDownLatch
35 implements MaybeObserver<T>, SingleObserver<T>, CompletableObserver, Future<T>, Disposable {
36
37 T value;
38 Throwable error;
39
40 final AtomicReference<Disposable> upstream;
41
42 public FutureMultiObserver() {
43 super(1);
44 this.upstream = new AtomicReference<>();
45 }
46
47 @Override
48 public boolean cancel(boolean mayInterruptIfRunning) {
49 for (;;) {
50 Disposable a = upstream.get();
51 if (a == this || a == DisposableHelper.DISPOSED) {
52 return false;
53 }
54
55 if (upstream.compareAndSet(a, DisposableHelper.DISPOSED)) {
56 if (a != null) {
57 a.dispose();
58 }
59 countDown();
60 return true;
61 }
62 }
63 }
64
65 @Override
66 public boolean isCancelled() {
67 return DisposableHelper.isDisposed(upstream.get());
68 }
69
70 @Override
71 public boolean isDone() {
72 return getCount() == 0;
73 }
74
75 @Override
76 public T get() throws InterruptedException, ExecutionException {
77 if (getCount() != 0) {
78 BlockingHelper.verifyNonBlocking();
79 await();
80 }
81
82 if (isCancelled()) {
83 throw new CancellationException();
84 }
85 Throwable ex = error;
86 if (ex != null) {
87 throw new ExecutionException(ex);
88 }
89 return value;
90 }
91
92 @Override
93 public T get(long timeout, @NonNull TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
94 if (getCount() != 0) {
95 BlockingHelper.verifyNonBlocking();
96 if (!await(timeout, unit)) {
97 throw new TimeoutException(timeoutMessage(timeout, unit));
98 }
99 }
100
101 if (isCancelled()) {
102 throw new CancellationException();
103 }
104
105 Throwable ex = error;
106 if (ex != null) {
107 throw new ExecutionException(ex);
108 }
109 return value;
110 }
111
112 @Override
113 public void onSubscribe(Disposable d) {
114 DisposableHelper.setOnce(this.upstream, d);
115 }
116
117 @Override
118 public void onSuccess(T t) {
119 Disposable a = upstream.get();
120 if (a == DisposableHelper.DISPOSED) {
121 return;
122 }
123 value = t;
124 upstream.compareAndSet(a, this);
125 countDown();
126 }
127
128 @Override
129 public void onError(Throwable t) {
130 for (;;) {
131 Disposable a = upstream.get();
132 if (a == DisposableHelper.DISPOSED) {
133 RxJavaPlugins.onError(t);
134 return;
135 }
136 error = t;
137 if (upstream.compareAndSet(a, this)) {
138 countDown();
139 return;
140 }
141 }
142 }
143
144 @Override
145 public void onComplete() {
146 Disposable a = upstream.get();
147 if (a == DisposableHelper.DISPOSED) {
148 return;
149 }
150 upstream.compareAndSet(a, this);
151 countDown();
152 }
153
154 @Override
155 public void dispose() {
156 // ignoring as `this` means a finished Disposable only
157 }
158
159 @Override
160 public boolean isDisposed() {
161 return isDone();
162 }
163 }