Debian Codegraph @

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