| 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 io.reactivex.rxjava3.annotations.*; |
| 17 |
import io.reactivex.rxjava3.core.*; |
| 18 |
|
| 19 |
/** |
| 20 |
* Represents an {@link Observer} and an {@link Observable} at the same time, allowing |
| 21 |
* multicasting events from a single source to multiple child {@code Observer}s. |
| 22 |
* <p> |
| 23 |
* All methods except the {@link #onSubscribe(io.reactivex.rxjava3.disposables.Disposable)}, {@link #onNext(Object)}, |
| 24 |
* {@link #onError(Throwable)} and {@link #onComplete()} are thread-safe. |
| 25 |
* Use {@link #toSerialized()} to make these methods thread-safe as well. |
| 26 |
* |
| 27 |
* @param <T> the item value type |
| 28 |
*/ |
| 29 |
public abstract class Subject<T> extends Observable<T> implements Observer<T> { |
| 30 |
/** |
| 31 |
* Returns true if the subject has any Observers. |
| 32 |
* <p>The method is thread-safe. |
| 33 |
* @return true if the subject has any Observers |
| 34 |
*/ |
| 35 |
@CheckReturnValue |
| 36 |
public abstract boolean hasObservers(); |
| 37 |
|
| 38 |
/** |
| 39 |
* Returns true if the subject has reached a terminal state through an error event. |
| 40 |
* <p>The method is thread-safe. |
| 41 |
* @return true if the subject has reached a terminal state through an error event |
| 42 |
* @see #getThrowable() |
| 43 |
* @see #hasComplete() |
| 44 |
*/ |
| 45 |
@CheckReturnValue |
| 46 |
public abstract boolean hasThrowable(); |
| 47 |
|
| 48 |
/** |
| 49 |
* Returns true if the subject has reached a terminal state through a complete event. |
| 50 |
* <p>The method is thread-safe. |
| 51 |
* @return true if the subject has reached a terminal state through a complete event |
| 52 |
* @see #hasThrowable() |
| 53 |
*/ |
| 54 |
@CheckReturnValue |
| 55 |
public abstract boolean hasComplete(); |
| 56 |
|
| 57 |
/** |
| 58 |
* Returns the error that caused the Subject to terminate or null if the Subject |
| 59 |
* hasn't terminated yet. |
| 60 |
* <p>The method is thread-safe. |
| 61 |
* @return the error that caused the Subject to terminate or null if the Subject |
| 62 |
* hasn't terminated yet |
| 63 |
*/ |
| 64 |
@Nullable |
| 65 |
@CheckReturnValue |
| 66 |
public abstract Throwable getThrowable(); |
| 67 |
|
| 68 |
/** |
| 69 |
* Wraps this Subject and serializes the calls to the onSubscribe, onNext, onError and |
| 70 |
* onComplete methods, making them thread-safe. |
| 71 |
* <p>The method is thread-safe. |
| 72 |
* @return the wrapped and serialized subject |
| 73 |
*/ |
| 74 |
@NonNull |
| 75 |
@CheckReturnValue |
| 76 |
public final Subject<T> toSerialized() { |
| 77 |
if (this instanceof SerializedSubject) { |
| 78 |
return this; |
| 79 |
} |
| 80 |
return new SerializedSubject<>(this); |
| 81 |
} |
| 82 |
} |