|
| 1 | +/** |
| 2 | + * Copyright 2016 Netflix, Inc. |
| 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.internal.observers; |
| 15 | + |
| 16 | +import java.util.concurrent.*; |
| 17 | + |
| 18 | +import io.reactivex.*; |
| 19 | +import io.reactivex.disposables.Disposable; |
| 20 | +import io.reactivex.internal.util.ExceptionHelper; |
| 21 | + |
| 22 | +/** |
| 23 | + * A combined Observer that awaits the success or error signal via a CountDownLatch. |
| 24 | + * @param <T> the value type |
| 25 | + */ |
| 26 | +public final class BlockingObserver<T> |
| 27 | +extends CountDownLatch |
| 28 | +implements SingleObserver<T>, CompletableObserver, MaybeObserver<T> { |
| 29 | + |
| 30 | + T value; |
| 31 | + Throwable error; |
| 32 | + |
| 33 | + Disposable d; |
| 34 | + |
| 35 | + volatile boolean cancelled; |
| 36 | + |
| 37 | + public BlockingObserver() { |
| 38 | + super(1); |
| 39 | + } |
| 40 | + |
| 41 | + void dispose() { |
| 42 | + cancelled = true; |
| 43 | + Disposable d = this.d; |
| 44 | + if (d != null) { |
| 45 | + d.dispose(); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public void onSubscribe(Disposable d) { |
| 51 | + this.d = d; |
| 52 | + if (cancelled) { |
| 53 | + d.dispose(); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public void onSuccess(T value) { |
| 59 | + this.value = value; |
| 60 | + countDown(); |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public void onError(Throwable e) { |
| 65 | + error = e; |
| 66 | + countDown(); |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public void onComplete() { |
| 71 | + countDown(); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Block until the latch is counted down then rethrow any exception received (wrapped if checked) |
| 76 | + * or return the received value (null if none). |
| 77 | + * @return the value received or null if no value received |
| 78 | + */ |
| 79 | + public T blockingGet() { |
| 80 | + if (getCount() != 0) { |
| 81 | + try { |
| 82 | + await(); |
| 83 | + } catch (InterruptedException ex) { |
| 84 | + dispose(); |
| 85 | + throw ExceptionHelper.wrapOrThrow(ex); |
| 86 | + } |
| 87 | + } |
| 88 | + Throwable ex = error; |
| 89 | + if (ex != null) { |
| 90 | + throw ExceptionHelper.wrapOrThrow(ex); |
| 91 | + } |
| 92 | + return value; |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Block until a the latch is counted down and return the value received, otherwise |
| 97 | + * rethrow the received exception or rethrow the InterruptedException or TimeoutException |
| 98 | + * (wrapped). |
| 99 | + * @param timeout the timeout value |
| 100 | + * @param unit the time unit |
| 101 | + * @return the value received or null if no value received |
| 102 | + */ |
| 103 | + public T blockingGet(long timeout, TimeUnit unit) { |
| 104 | + if (getCount() != 0) { |
| 105 | + try { |
| 106 | + if (!await(timeout, unit)) { |
| 107 | + dispose(); |
| 108 | + throw ExceptionHelper.wrapOrThrow(new TimeoutException()); |
| 109 | + } |
| 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 | + return value; |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * Block until the latch is counted down then rethrow any exception received (wrapped if checked) |
| 124 | + * or return the received value (the defaultValue if none). |
| 125 | + * @param defaultValue the default value to return if no value was received |
| 126 | + * @return the value received or defaultValue if no value received |
| 127 | + */ |
| 128 | + public T blockingGet(T defaultValue) { |
| 129 | + if (getCount() != 0) { |
| 130 | + try { |
| 131 | + await(); |
| 132 | + } catch (InterruptedException ex) { |
| 133 | + dispose(); |
| 134 | + throw ExceptionHelper.wrapOrThrow(ex); |
| 135 | + } |
| 136 | + } |
| 137 | + Throwable ex = error; |
| 138 | + if (ex != null) { |
| 139 | + throw ExceptionHelper.wrapOrThrow(ex); |
| 140 | + } |
| 141 | + T v = value; |
| 142 | + return v != null ? v : defaultValue; |
| 143 | + } |
| 144 | + |
| 145 | + /** |
| 146 | + * Block until the latch is counted down and return the error received or null if no |
| 147 | + * error happened. |
| 148 | + * @return the error received or null |
| 149 | + */ |
| 150 | + public Throwable blockingGetError() { |
| 151 | + if (getCount() != 0) { |
| 152 | + try { |
| 153 | + await(); |
| 154 | + } catch (InterruptedException ex) { |
| 155 | + dispose(); |
| 156 | + return ex; |
| 157 | + } |
| 158 | + } |
| 159 | + return error; |
| 160 | + } |
| 161 | + |
| 162 | + /** |
| 163 | + * Block until the latch is counted down and return the error received or |
| 164 | + * when the wait is interrupted or times out, null otherwise. |
| 165 | + * @param timeout the timeout value |
| 166 | + * @param unit the time unit |
| 167 | + * @return the error received or null |
| 168 | + */ |
| 169 | + public Throwable blockingGetError(long timeout, TimeUnit unit) { |
| 170 | + if (getCount() != 0) { |
| 171 | + try { |
| 172 | + if (!await(timeout, unit)) { |
| 173 | + dispose(); |
| 174 | + throw ExceptionHelper.wrapOrThrow(new TimeoutException()); |
| 175 | + } |
| 176 | + } catch (InterruptedException ex) { |
| 177 | + dispose(); |
| 178 | + throw ExceptionHelper.wrapOrThrow(ex); |
| 179 | + } |
| 180 | + } |
| 181 | + return error; |
| 182 | + } |
| 183 | + |
| 184 | + /** |
| 185 | + * Block until the observer terminates and return true; return false if |
| 186 | + * the wait times out. |
| 187 | + * @param timeout the timeout value |
| 188 | + * @param unit the time unit |
| 189 | + * @return true if the observer terminated in time, false otherwise |
| 190 | + */ |
| 191 | + public boolean blockingAwait(long timeout, TimeUnit unit) { |
| 192 | + if (getCount() != 0) { |
| 193 | + try { |
| 194 | + if (!await(timeout, unit)) { |
| 195 | + dispose(); |
| 196 | + return false; |
| 197 | + } |
| 198 | + } catch (InterruptedException ex) { |
| 199 | + dispose(); |
| 200 | + throw ExceptionHelper.wrapOrThrow(ex); |
| 201 | + } |
| 202 | + } |
| 203 | + Throwable ex = error; |
| 204 | + if (ex != null) { |
| 205 | + throw ExceptionHelper.wrapOrThrow(ex); |
| 206 | + } |
| 207 | + return true; |
| 208 | + } |
| 209 | +} |
0 commit comments