Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/observers/BlockingMultiObserver.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 java.util.concurrent.*;
17
18 import io.reactivex.rxjava3.core.*;
19 import io.reactivex.rxjava3.disposables.Disposable;
20 import io.reactivex.rxjava3.exceptions.Exceptions;
21 import io.reactivex.rxjava3.functions.*;
22 import io.reactivex.rxjava3.internal.util.*;
23 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
24
25 /**
26 * A combined Observer that awaits the success or error signal via a CountDownLatch.
27 * @param <T> the value type
28 */
29 public final class BlockingMultiObserver<T>
30 extends CountDownLatch
31 implements SingleObserver<T>, CompletableObserver, MaybeObserver<T> {
32
33 T value;
34 Throwable error;
35
36 Disposable upstream;
37
38 volatile boolean cancelled;
39
40 public BlockingMultiObserver() {
41 super(1);
42 }
43
44 void dispose() {
45 cancelled = true;
46 Disposable d = this.upstream;
47 if (d != null) {
48 d.dispose();
49 }
50 }
51
52 @Override
53 public void onSubscribe(Disposable d) {
54 this.upstream = d;
55 if (cancelled) {
56 d.dispose();
57 }
58 }
59
60 @Override
61 public void onSuccess(T value) {
62 this.value = value;
63 countDown();
64 }
65
66 @Override
67 public void onError(Throwable e) {
68 error = e;
69 countDown();
70 }
71
72 @Override
73 public void onComplete() {
74 countDown();
75 }
76
77 /**
78 * Block until the latch is counted down then rethrow any exception received (wrapped if checked)
79 * or return the received value (null if none).
80 * @return the value received or null if no value received
81 */
82 public T blockingGet() {
83 if (getCount() != 0) {
84 try {
85 BlockingHelper.verifyNonBlocking();
86 await();
87 } catch (InterruptedException ex) {
88 dispose();
89 throw ExceptionHelper.wrapOrThrow(ex);
90 }
91 }
92 Throwable ex = error;
93 if (ex != null) {
94 throw ExceptionHelper.wrapOrThrow(ex);
95 }
96 return value;
97 }
98
99 /**
100 * Block until the latch is counted down then rethrow any exception received (wrapped if checked)
101 * or return the received value (the defaultValue if none).
102 * @param defaultValue the default value to return if no value was received
103 * @return the value received or defaultValue if no value received
104 */
105 public T blockingGet(T defaultValue) {
106 if (getCount() != 0) {
107 try {
108 BlockingHelper.verifyNonBlocking();
109 await();
110 } catch (InterruptedException ex) {
111 dispose();
112 throw ExceptionHelper.wrapOrThrow(ex);
113 }
114 }
115 Throwable ex = error;
116 if (ex != null) {
117 throw ExceptionHelper.wrapOrThrow(ex);
118 }
119 T v = value;
120 return v != null ? v : defaultValue;
121 }
122
123 /**
124 * Block until the observer terminates and return true; return false if
125 * the wait times out.
126 * @param timeout the timeout value
127 * @param unit the time unit
128 * @return true if the observer terminated in time, false otherwise
129 */
130 public boolean blockingAwait(long timeout, TimeUnit unit) {
131 if (getCount() != 0) {
132 try {
133 BlockingHelper.verifyNonBlocking();
134 if (!await(timeout, unit)) {
135 dispose();
136 return false;
137 }
138 } catch (InterruptedException ex) {
139 dispose();
140 throw ExceptionHelper.wrapOrThrow(ex);
141 }
142 }
143 Throwable ex = error;
144 if (ex != null) {
145 throw ExceptionHelper.wrapOrThrow(ex);
146 }
147 return true;
148 }
149
150 /**
151 * Blocks until the source completes and calls the appropriate callback.
152 * @param onSuccess for a succeeding source
153 * @param onError for a failing source
154 * @param onComplete for an empty source
155 */
156 public void blockingConsume(Consumer<? super T> onSuccess, Consumer<? super Throwable> onError, Action onComplete) {
157 try {
158 if (getCount() != 0) {
159 try {
160 BlockingHelper.verifyNonBlocking();
161 await();
162 } catch (InterruptedException ex) {
163 dispose();
164 onError.accept(ex);
165 return;
166 }
167 }
168 Throwable ex = error;
169 if (ex != null) {
170 onError.accept(ex);
171 return;
172 }
173 T v = value;
174 if (v != null) {
175 onSuccess.accept(v);
176 } else {
177 onComplete.run();
178 }
179 } catch (Throwable t) {
180 Exceptions.throwIfFatal(t);
181 RxJavaPlugins.onError(t);
182 }
183 }
184 }