|
37 | 37 | import io.reactivex.schedulers.Schedulers;
|
38 | 38 |
|
39 | 39 | /**
|
40 |
| - * The Single class implements the Reactive Pattern for a single value response. |
41 |
| - * See {@link Flowable} or {@link Observable} for the |
42 |
| - * implementation of the Reactive Pattern for a stream or vector of values. |
| 40 | + * The {@code Single} class implements the Reactive Pattern for a single value response. |
| 41 | + * <p> |
| 42 | + * {@code Single} behaves similarly to {@link Observable} except that it can only emit either a single successful |
| 43 | + * value or an error (there is no "onComplete" notification as there is for an {@link Observable}). |
| 44 | + * <p> |
| 45 | + * The {@code Single} class implements the {@link SingleSource} base interface and the default consumer |
| 46 | + * type it interacts with is the {@link SingleObserver} via the {@link #subscribe(SingleObserver)} method. |
| 47 | + * <p> |
| 48 | + * The {@code Single} operates with the following sequential protocol: |
| 49 | + * <pre> |
| 50 | + * <code>onSubscribe (onSuccess | onError)?</code> |
| 51 | + * </pre> |
| 52 | + * <p> |
| 53 | + * Note that {@code onSuccess} and {@code onError} are mutually exclusive events; unlike {@code Observable}, |
| 54 | + * {@code onSuccess} is never followed by {@code onError}. |
43 | 55 | * <p>
|
44 |
| - * {@code Single} behaves the same as {@link Observable} except that it can only emit either a single successful |
45 |
| - * value, or an error (there is no "onComplete" notification as there is for {@link Observable}) |
| 56 | + * Like {@code Observable}, a running {@code Single} can be stopped through the {@link Disposable} instance |
| 57 | + * provided to consumers through {@link SingleObserver#onSubscribe}. |
46 | 58 | * <p>
|
47 |
| - * Like an {@link Observable}, a {@code Single} is lazy, can be either "hot" or "cold", synchronous or |
48 |
| - * asynchronous. |
| 59 | + * Like an {@code Observable}, a {@code Single} is lazy, can be either "hot" or "cold", synchronous or |
| 60 | + * asynchronous. {@code Single} instances returned by the methods of this class are <em>cold</em> |
| 61 | + * and there is a standard <em>hot</em> implementation in the form of a subject: |
| 62 | + * {@link io.reactivex.subjects.SingleSubject SingleSubject}. |
49 | 63 | * <p>
|
50 | 64 | * The documentation for this class makes use of marble diagrams. The following legend explains these diagrams:
|
51 | 65 | * <p>
|
52 | 66 | * <img width="640" height="301" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/Single.legend.png" alt="">
|
53 | 67 | * <p>
|
| 68 | + * See {@link Flowable} or {@link Observable} for the |
| 69 | + * implementation of the Reactive Pattern for a stream or vector of values. |
| 70 | + * <p> |
54 | 71 | * For more information see the <a href="http://reactivex.io/documentation/single.html">ReactiveX
|
55 | 72 | * documentation</a>.
|
| 73 | + * <p> |
| 74 | + * Example: |
| 75 | + * <pre><code> |
| 76 | + * Disposable d = Single.just("Hello World") |
| 77 | + * .delay(10, TimeUnit.SECONDS, Schedulers.io()) |
| 78 | + * .subscribeWith(new DisposableSingleObserver<String>() { |
| 79 | + * @Override |
| 80 | + * public void onStart() { |
| 81 | + * System.out.println("Started"); |
| 82 | + * } |
| 83 | + * |
| 84 | + * @Override |
| 85 | + * public void onSuccess(String value) { |
| 86 | + * System.out.println("Success: " + value); |
| 87 | + * } |
56 | 88 | *
|
| 89 | + * @Override |
| 90 | + * public void onError(Throwable error) { |
| 91 | + * error.printStackTrace(); |
| 92 | + * } |
| 93 | + * }); |
| 94 | + * |
| 95 | + * Thread.sleep(5000); |
| 96 | + * |
| 97 | + * d.dispose(); |
| 98 | + * </code></pre> |
| 99 | + * <p> |
| 100 | + * Note that by design, subscriptions via {@link #subscribe(SingleObserver)} can't be cancelled/disposed |
| 101 | + * from the outside (hence the |
| 102 | + * {@code void} return of the {@link #subscribe(SingleObserver)} method) and it is the |
| 103 | + * responsibility of the implementor of the {@code SingleObserver} to allow this to happen. |
| 104 | + * RxJava supports such usage with the standard |
| 105 | + * {@link io.reactivex.observers.DisposableSingleObserver DisposableSingleObserver} instance. |
| 106 | + * For convenience, the {@link #subscribeWith(SingleObserver)} method is provided as well to |
| 107 | + * allow working with a {@code SingleObserver} (or subclass) instance to be applied with in |
| 108 | + * a fluent manner (such as in the example above). |
57 | 109 | * @param <T>
|
58 | 110 | * the type of the item emitted by the Single
|
59 | 111 | * @since 2.0
|
| 112 | + * @see io.reactivex.observers.DisposableSingleObserver |
60 | 113 | */
|
61 | 114 | public abstract class Single<T> implements SingleSource<T> {
|
62 | 115 |
|
|
0 commit comments