| 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.subjects; |
| 15 |
|
| 16 |
import java.util.concurrent.atomic.*; |
| 17 |
|
| 18 |
import io.reactivex.rxjava3.annotations.*; |
| 19 |
import io.reactivex.rxjava3.core.*; |
| 20 |
import io.reactivex.rxjava3.disposables.Disposable; |
| 21 |
import io.reactivex.rxjava3.internal.util.ExceptionHelper; |
| 22 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 23 |
|
| 24 |
/** |
| 25 |
* Represents a hot Maybe-like source and consumer of events similar to Subjects. |
| 26 |
* <p> |
| 27 |
* <img width="640" height="164" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/MaybeSubject.png" alt=""> |
| 28 |
* <p> |
| 29 |
* This subject does not have a public constructor by design; a new non-terminated instance of this |
| 30 |
* {@code MaybeSubject} can be created via the {@link #create()} method. |
| 31 |
* <p> |
| 32 |
* Since the {@code MaybeSubject} is conceptionally derived from the {@code Processor} type in the Reactive Streams specification, |
| 33 |
* {@code null}s are not allowed (<a href="https://github.com/reactive-streams/reactive-streams-jvm#2.13">Rule 2.13</a>) |
| 34 |
* as parameters to {@link #onSuccess(Object)} and {@link #onError(Throwable)}. Such calls will result in a |
| 35 |
* {@link NullPointerException} being thrown and the subject's state is not changed. |
| 36 |
* <p> |
| 37 |
* Since a {@code MaybeSubject} is a {@link io.reactivex.rxjava3.core.Maybe}, calling {@code onSuccess}, {@code onError} |
| 38 |
* or {@code onComplete} will move this {@code MaybeSubject} into its terminal state atomically. |
| 39 |
* <p> |
| 40 |
* All methods are thread safe. Calling {@link #onSuccess(Object)} or {@link #onComplete()} multiple |
| 41 |
* times has no effect. Calling {@link #onError(Throwable)} multiple times relays the {@code Throwable} to |
| 42 |
* the {@link io.reactivex.rxjava3.plugins.RxJavaPlugins#onError(Throwable)} global error handler. |
| 43 |
* <p> |
| 44 |
* Even though {@code MaybeSubject} implements the {@code MaybeObserver} interface, calling |
| 45 |
* {@code onSubscribe} is not required (<a href="https://github.com/reactive-streams/reactive-streams-jvm#2.12">Rule 2.12</a>) |
| 46 |
* if the subject is used as a standalone source. However, calling {@code onSubscribe} |
| 47 |
* after the {@code MaybeSubject} reached its terminal state will result in the |
| 48 |
* given {@code Disposable} being disposed immediately. |
| 49 |
* <p> |
| 50 |
* This {@code MaybeSubject} supports the standard state-peeking methods {@link #hasComplete()}, {@link #hasThrowable()}, |
| 51 |
* {@link #getThrowable()} and {@link #hasObservers()} as well as means to read any success item in a non-blocking |
| 52 |
* and thread-safe manner via {@link #hasValue()} and {@link #getValue()}. |
| 53 |
* <p> |
| 54 |
* The {@code MaybeSubject} does not support clearing its cached {@code onSuccess} value. |
| 55 |
* <dl> |
| 56 |
* <dt><b>Scheduler:</b></dt> |
| 57 |
* <dd>{@code MaybeSubject} does not operate by default on a particular {@link io.reactivex.rxjava3.core.Scheduler} and |
| 58 |
* the {@code MaybeObserver}s get notified on the thread where the terminating {@code onSuccess}, {@code onError} or {@code onComplete} |
| 59 |
* methods were invoked.</dd> |
| 60 |
* <dt><b>Error handling:</b></dt> |
| 61 |
* <dd>When the {@link #onError(Throwable)} is called, the {@code MaybeSubject} enters into a terminal state |
| 62 |
* and emits the same {@code Throwable} instance to the last set of {@code MaybeObserver}s. During this emission, |
| 63 |
* if one or more {@code MaybeObserver}s dispose their respective {@code Disposable}s, the |
| 64 |
* {@code Throwable} is delivered to the global error handler via |
| 65 |
* {@link io.reactivex.rxjava3.plugins.RxJavaPlugins#onError(Throwable)} (multiple times if multiple {@code MaybeObserver}s |
| 66 |
* cancel at once). |
| 67 |
* If there were no {@code MaybeObserver}s subscribed to this {@code MaybeSubject} when the {@code onError()} |
| 68 |
* was called, the global error handler is not invoked. |
| 69 |
* </dd> |
| 70 |
* </dl> |
| 71 |
* <p> |
| 72 |
* Example usage: |
| 73 |
* <pre><code> |
| 74 |
* MaybeSubject<Integer> subject1 = MaybeSubject.create(); |
| 75 |
* |
| 76 |
* TestObserver<Integer> to1 = subject1.test(); |
| 77 |
* |
| 78 |
* // MaybeSubjects are empty by default |
| 79 |
* to1.assertEmpty(); |
| 80 |
* |
| 81 |
* subject1.onSuccess(1); |
| 82 |
* |
| 83 |
* // onSuccess is a terminal event with MaybeSubjects |
| 84 |
* // TestObserver converts onSuccess into onNext + onComplete |
| 85 |
* to1.assertResult(1); |
| 86 |
* |
| 87 |
* TestObserver<Integer> to2 = subject1.test(); |
| 88 |
* |
| 89 |
* // late Observers receive the terminal signal (onSuccess) too |
| 90 |
* to2.assertResult(1); |
| 91 |
* |
| 92 |
* // ----------------------------------------------------- |
| 93 |
* |
| 94 |
* MaybeSubject<Integer> subject2 = MaybeSubject.create(); |
| 95 |
* |
| 96 |
* TestObserver<Integer> to3 = subject2.test(); |
| 97 |
* |
| 98 |
* subject2.onComplete(); |
| 99 |
* |
| 100 |
* // a completed MaybeSubject completes its MaybeObservers |
| 101 |
* to3.assertResult(); |
| 102 |
* |
| 103 |
* TestObserver<Integer> to4 = subject1.test(); |
| 104 |
* |
| 105 |
* // late Observers receive the terminal signal (onComplete) too |
| 106 |
* to4.assertResult(); |
| 107 |
* </code></pre> |
| 108 |
* <p>History: 2.0.5 - experimental |
| 109 |
* @param <T> the value type received and emitted |
| 110 |
* @since 2.1 |
| 111 |
*/ |
| 112 |
public final class MaybeSubject<T> extends Maybe<T> implements MaybeObserver<T> { |
| 113 |
|
| 114 |
final AtomicReference<MaybeDisposable<T>[]> observers; |
| 115 |
|
| 116 |
@SuppressWarnings("rawtypes") |
| 117 |
static final MaybeDisposable[] EMPTY = new MaybeDisposable[0]; |
| 118 |
|
| 119 |
@SuppressWarnings("rawtypes") |
| 120 |
static final MaybeDisposable[] TERMINATED = new MaybeDisposable[0]; |
| 121 |
|
| 122 |
final AtomicBoolean once; |
| 123 |
T value; |
| 124 |
Throwable error; |
| 125 |
|
| 126 |
/** |
| 127 |
* Creates a fresh MaybeSubject. |
| 128 |
* @param <T> the value type received and emitted |
| 129 |
* @return the new MaybeSubject instance |
| 130 |
*/ |
| 131 |
@CheckReturnValue |
| 132 |
@NonNull |
| 133 |
public static <T> MaybeSubject<T> create() { |
| 134 |
return new MaybeSubject<>(); |
| 135 |
} |
| 136 |
|
| 137 |
@SuppressWarnings("unchecked") |
| 138 |
MaybeSubject() { |
| 139 |
once = new AtomicBoolean(); |
| 140 |
observers = new AtomicReference<>(EMPTY); |
| 141 |
} |
| 142 |
|
| 143 |
@Override |
| 144 |
public void onSubscribe(Disposable d) { |
| 145 |
if (observers.get() == TERMINATED) { |
| 146 |
d.dispose(); |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
@SuppressWarnings("unchecked") |
| 151 |
@Override |
| 152 |
public void onSuccess(T value) { |
| 153 |
ExceptionHelper.nullCheck(value, "onSuccess called with a null value."); |
| 154 |
if (once.compareAndSet(false, true)) { |
| 155 |
this.value = value; |
| 156 |
for (MaybeDisposable<T> md : observers.getAndSet(TERMINATED)) { |
| 157 |
md.downstream.onSuccess(value); |
| 158 |
} |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
@SuppressWarnings("unchecked") |
| 163 |
@Override |
| 164 |
public void onError(Throwable e) { |
| 165 |
ExceptionHelper.nullCheck(e, "onError called with a null Throwable."); |
| 166 |
if (once.compareAndSet(false, true)) { |
| 167 |
this.error = e; |
| 168 |
for (MaybeDisposable<T> md : observers.getAndSet(TERMINATED)) { |
| 169 |
md.downstream.onError(e); |
| 170 |
} |
| 171 |
} else { |
| 172 |
RxJavaPlugins.onError(e); |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
@SuppressWarnings("unchecked") |
| 177 |
@Override |
| 178 |
public void onComplete() { |
| 179 |
if (once.compareAndSet(false, true)) { |
| 180 |
for (MaybeDisposable<T> md : observers.getAndSet(TERMINATED)) { |
| 181 |
md.downstream.onComplete(); |
| 182 |
} |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
@Override |
| 187 |
protected void subscribeActual(MaybeObserver<? super T> observer) { |
| 188 |
MaybeDisposable<T> md = new MaybeDisposable<>(observer, this); |
| 189 |
observer.onSubscribe(md); |
| 190 |
if (add(md)) { |
| 191 |
if (md.isDisposed()) { |
| 192 |
remove(md); |
| 193 |
} |
| 194 |
} else { |
| 195 |
Throwable ex = error; |
| 196 |
if (ex != null) { |
| 197 |
observer.onError(ex); |
| 198 |
} else { |
| 199 |
T v = value; |
| 200 |
if (v == null) { |
| 201 |
observer.onComplete(); |
| 202 |
} else { |
| 203 |
observer.onSuccess(v); |
| 204 |
} |
| 205 |
} |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
boolean add(MaybeDisposable<T> inner) { |
| 210 |
for (;;) { |
| 211 |
MaybeDisposable<T>[] a = observers.get(); |
| 212 |
if (a == TERMINATED) { |
| 213 |
return false; |
| 214 |
} |
| 215 |
|
| 216 |
int n = a.length; |
| 217 |
@SuppressWarnings("unchecked") |
| 218 |
MaybeDisposable<T>[] b = new MaybeDisposable[n + 1]; |
| 219 |
System.arraycopy(a, 0, b, 0, n); |
| 220 |
b[n] = inner; |
| 221 |
if (observers.compareAndSet(a, b)) { |
| 222 |
return true; |
| 223 |
} |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
@SuppressWarnings("unchecked") |
| 228 |
void remove(MaybeDisposable<T> inner) { |
| 229 |
for (;;) { |
| 230 |
MaybeDisposable<T>[] a = observers.get(); |
| 231 |
int n = a.length; |
| 232 |
if (n == 0) { |
| 233 |
return; |
| 234 |
} |
| 235 |
|
| 236 |
int j = -1; |
| 237 |
|
| 238 |
for (int i = 0; i < n; i++) { |
| 239 |
if (a[i] == inner) { |
| 240 |
j = i; |
| 241 |
break; |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
if (j < 0) { |
| 246 |
return; |
| 247 |
} |
| 248 |
MaybeDisposable<T>[] b; |
| 249 |
if (n == 1) { |
| 250 |
b = EMPTY; |
| 251 |
} else { |
| 252 |
b = new MaybeDisposable[n - 1]; |
| 253 |
System.arraycopy(a, 0, b, 0, j); |
| 254 |
System.arraycopy(a, j + 1, b, j, n - j - 1); |
| 255 |
} |
| 256 |
|
| 257 |
if (observers.compareAndSet(a, b)) { |
| 258 |
return; |
| 259 |
} |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
/** |
| 264 |
* Returns the success value if this MaybeSubject was terminated with a success value. |
| 265 |
* @return the success value or null |
| 266 |
*/ |
| 267 |
@Nullable |
| 268 |
public T getValue() { |
| 269 |
if (observers.get() == TERMINATED) { |
| 270 |
return value; |
| 271 |
} |
| 272 |
return null; |
| 273 |
} |
| 274 |
|
| 275 |
/** |
| 276 |
* Returns true if this MaybeSubject was terminated with a success value. |
| 277 |
* @return true if this MaybeSubject was terminated with a success value |
| 278 |
*/ |
| 279 |
public boolean hasValue() { |
| 280 |
return observers.get() == TERMINATED && value != null; |
| 281 |
} |
| 282 |
|
| 283 |
/** |
| 284 |
* Returns the terminal error if this MaybeSubject has been terminated with an error, null otherwise. |
| 285 |
* @return the terminal error or null if not terminated or not with an error |
| 286 |
*/ |
| 287 |
@Nullable |
| 288 |
public Throwable getThrowable() { |
| 289 |
if (observers.get() == TERMINATED) { |
| 290 |
return error; |
| 291 |
} |
| 292 |
return null; |
| 293 |
} |
| 294 |
|
| 295 |
/** |
| 296 |
* Returns true if this MaybeSubject has been terminated with an error. |
| 297 |
* @return true if this MaybeSubject has been terminated with an error |
| 298 |
*/ |
| 299 |
public boolean hasThrowable() { |
| 300 |
return observers.get() == TERMINATED && error != null; |
| 301 |
} |
| 302 |
|
| 303 |
/** |
| 304 |
* Returns true if this MaybeSubject has been completed. |
| 305 |
* @return true if this MaybeSubject has been completed |
| 306 |
*/ |
| 307 |
public boolean hasComplete() { |
| 308 |
return observers.get() == TERMINATED && value == null && error == null; |
| 309 |
} |
| 310 |
|
| 311 |
/** |
| 312 |
* Returns true if this MaybeSubject has observers. |
| 313 |
* @return true if this MaybeSubject has observers |
| 314 |
*/ |
| 315 |
public boolean hasObservers() { |
| 316 |
return observers.get().length != 0; |
| 317 |
} |
| 318 |
|
| 319 |
/** |
| 320 |
* Returns the number of current observers. |
| 321 |
* @return the number of current observers |
| 322 |
*/ |
| 323 |
/* test */ int observerCount() { |
| 324 |
return observers.get().length; |
| 325 |
} |
| 326 |
|
| 327 |
static final class MaybeDisposable<T> |
| 328 |
extends AtomicReference<MaybeSubject<T>> implements Disposable { |
| 329 |
private static final long serialVersionUID = -7650903191002190468L; |
| 330 |
|
| 331 |
final MaybeObserver<? super T> downstream; |
| 332 |
|
| 333 |
MaybeDisposable(MaybeObserver<? super T> actual, MaybeSubject<T> parent) { |
| 334 |
this.downstream = actual; |
| 335 |
lazySet(parent); |
| 336 |
} |
| 337 |
|
| 338 |
@Override |
| 339 |
public void dispose() { |
| 340 |
MaybeSubject<T> parent = getAndSet(null); |
| 341 |
if (parent != null) { |
| 342 |
parent.remove(this); |
| 343 |
} |
| 344 |
} |
| 345 |
|
| 346 |
@Override |
| 347 |
public boolean isDisposed() { |
| 348 |
return get() == null; |
| 349 |
} |
| 350 |
} |
| 351 |
} |