Debian Codegraph @

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