Skip to content

Commit f7a2870

Browse files
authored
2.x: Improve JavaDoc of XObserver types. (#5841)
* 2.x: Improve JavaDoc of XObserver types. * Use "disposing", add missing space * Add more explanation about throwing exceptions.
1 parent 363f038 commit f7a2870

File tree

4 files changed

+138
-21
lines changed

4 files changed

+138
-21
lines changed

src/main/java/io/reactivex/CompletableObserver.java

+30-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,36 @@
1717
import io.reactivex.disposables.Disposable;
1818

1919
/**
20-
* Represents the subscription API callbacks when subscribing to a Completable instance.
20+
* Provides a mechanism for receiving push-based notification of a valueless completion or an error.
21+
* <p>
22+
* When a {@code CompletableObserver} is subscribed to a {@link CompletableSource} through the {@link CompletableSource#subscribe(CompletableObserver)} method,
23+
* the {@code CompletableSource} calls {@link #onSubscribe(Disposable)} with a {@link Disposable} that allows
24+
* disposing the sequence at any time. A well-behaved
25+
* {@code CompletableSource} will call a {@code CompletableObserver}'s {@link #onError(Throwable)}
26+
* or {@link #onComplete()} method exactly once as they are considered mutually exclusive <strong>terminal signals</strong>.
27+
* <p>
28+
* Calling the {@code CompletableObserver}'s method must happen in a serialized fashion, that is, they must not
29+
* be invoked concurrently by multiple threads in an overlapping fashion and the invocation pattern must
30+
* adhere to the following protocol:
31+
* <p>
32+
* <pre><code> onSubscribe (onError | onComplete)?</code></pre>
33+
* <p>
34+
* Subscribing a {@code CompletableObserver} to multiple {@code CompletableSource}s is not recommended. If such reuse
35+
* happens, it is the duty of the {@code CompletableObserver} implementation to be ready to receive multiple calls to
36+
* its methods and ensure proper concurrent behavior of its business logic.
37+
* <p>
38+
* Calling {@link #onSubscribe(Disposable)} or {@link #onError(Throwable)} with a
39+
* {@code null} argument is forbidden.
40+
* <p>
41+
* The implementations of the {@code onXXX} methods should avoid throwing runtime exceptions other than the following cases:
42+
* <ul>
43+
* <li>If the argument is {@code null}, the methods can throw a {@code NullPointerException}.
44+
* Note though that RxJava prevents {@code null}s to enter into the flow and thus there is generally no
45+
* need to check for nulls in flows assembled from standard sources and intermediate operators.
46+
* </li>
47+
* <li>If there is a fatal error (such as {@code VirtualMachineError}).</li>
48+
* </ul>
49+
* @since 2.0
2150
*/
2251
public interface CompletableObserver {
2352
/**

src/main/java/io/reactivex/MaybeObserver.java

+31-7
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,38 @@
1717
import io.reactivex.disposables.Disposable;
1818

1919
/**
20-
* Provides a mechanism for receiving push-based notifications.
20+
* Provides a mechanism for receiving push-based notification of a single value, an error or completion without any value.
2121
* <p>
22-
* After a MaybeObserver calls a {@link Maybe}'s {@link Maybe#subscribe subscribe} method,
23-
* first the Maybe calls {@link #onSubscribe(Disposable)} with a {@link Disposable} that allows
24-
* cancelling the sequence at any time, then the
25-
* {@code Maybe} calls only one of the MaybeObserver's {@link #onSuccess}, {@link #onError} or
26-
* {@link #onComplete} methods to provide notifications.
27-
*
22+
* When a {@code MaybeObserver} is subscribed to a {@link MaybeSource} through the {@link MaybeSource#subscribe(MaybeObserver)} method,
23+
* the {@code MaybeSource} calls {@link #onSubscribe(Disposable)} with a {@link Disposable} that allows
24+
* disposing the sequence at any time. A well-behaved
25+
* {@code MaybeSource} will call a {@code MaybeObserver}'s {@link #onSuccess(Object)}, {@link #onError(Throwable)}
26+
* or {@link #onComplete()} method exactly once as they are considered mutually exclusive <strong>terminal signals</strong>.
27+
* <p>
28+
* Calling the {@code MaybeObserver}'s method must happen in a serialized fashion, that is, they must not
29+
* be invoked concurrently by multiple threads in an overlapping fashion and the invocation pattern must
30+
* adhere to the following protocol:
31+
* <p>
32+
* <pre><code> onSubscribe (onSuccess | onError | onComplete)?</code></pre>
33+
* <p>
34+
* Note that unlike with the {@code Observable} protocol, {@link #onComplete()} is not called after the success item has been
35+
* signalled via {@link #onSuccess(Object)}.
36+
* <p>
37+
* Subscribing a {@code MaybeObserver} to multiple {@code MaybeSource}s is not recommended. If such reuse
38+
* happens, it is the duty of the {@code MaybeObserver} implementation to be ready to receive multiple calls to
39+
* its methods and ensure proper concurrent behavior of its business logic.
40+
* <p>
41+
* Calling {@link #onSubscribe(Disposable)}, {@link #onSuccess(Object)} or {@link #onError(Throwable)} with a
42+
* {@code null} argument is forbidden.
43+
* <p>
44+
* The implementations of the {@code onXXX} methods should avoid throwing runtime exceptions other than the following cases:
45+
* <ul>
46+
* <li>If the argument is {@code null}, the methods can throw a {@code NullPointerException}.
47+
* Note though that RxJava prevents {@code null}s to enter into the flow and thus there is generally no
48+
* need to check for nulls in flows assembled from standard sources and intermediate operators.
49+
* </li>
50+
* <li>If there is a fatal error (such as {@code VirtualMachineError}).</li>
51+
* </ul>
2852
* @see <a href="http://reactivex.io/documentation/observable.html">ReactiveX documentation: Observable</a>
2953
* @param <T>
3054
* the type of item the MaybeObserver expects to observe

src/main/java/io/reactivex/Observer.java

+49-6
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,57 @@
1919
/**
2020
* Provides a mechanism for receiving push-based notifications.
2121
* <p>
22-
* After an Observer calls an {@link Observable}'s {@link Observable#subscribe subscribe} method,
23-
* first the Observable calls {@link #onSubscribe(Disposable)} with a {@link Disposable} that allows
24-
* cancelling the sequence at any time, then the
25-
* {@code Observable} may call the Observer's {@link #onNext} method any number of times
22+
* When an {@code Observer} is subscribed to an {@link ObservableSource} through the {@link ObservableSource#subscribe(Observer)} method,
23+
* the {@code ObservableSource} calls {@link #onSubscribe(Disposable)} with a {@link Disposable} that allows
24+
* disposing the sequence at any time, then the
25+
* {@code ObservableSource} may call the Observer's {@link #onNext} method any number of times
2626
* to provide notifications. A well-behaved
27-
* {@code Observable} will call an Observer's {@link #onComplete} method exactly once or the Observer's
27+
* {@code ObservableSource} will call an {@code Observer}'s {@link #onComplete} method exactly once or the {@code Observer}'s
2828
* {@link #onError} method exactly once.
29-
*
29+
* <p>
30+
* Calling the {@code Observer}'s method must happen in a serialized fashion, that is, they must not
31+
* be invoked concurrently by multiple threads in an overlapping fashion and the invocation pattern must
32+
* adhere to the following protocol:
33+
* <p>
34+
* <pre><code> onSubscribe onNext* (onError | onComplete)?</code></pre>
35+
* <p>
36+
* Subscribing an {@code Observer} to multiple {@code ObservableSource}s is not recommended. If such reuse
37+
* happens, it is the duty of the {@code Observer} implementation to be ready to receive multiple calls to
38+
* its methods and ensure proper concurrent behavior of its business logic.
39+
* <p>
40+
* Calling {@link #onSubscribe(Disposable)}, {@link #onNext(Object)} or {@link #onError(Throwable)} with a
41+
* {@code null} argument is forbidden.
42+
* <p>
43+
* The implementations of the {@code onXXX} methods should avoid throwing runtime exceptions other than the following cases
44+
* (see <a href="https://github.com/reactive-streams/reactive-streams-jvm#2.13">Rule 2.13</a> of the Reactive Streams specification):
45+
* <ul>
46+
* <li>If the argument is {@code null}, the methods can throw a {@code NullPointerException}.
47+
* Note though that RxJava prevents {@code null}s to enter into the flow and thus there is generally no
48+
* need to check for nulls in flows assembled from standard sources and intermediate operators.
49+
* </li>
50+
* <li>If there is a fatal error (such as {@code VirtualMachineError}).</li>
51+
* </ul>
52+
* <p>
53+
* Violating Rule 2.13 results in undefined flow behavior. Generally, the following can happen:
54+
* <ul>
55+
* <li>An upstream operator turns it into an {@link #onError} call.</li>
56+
* <li>If the flow is synchronous, the {@link ObservableSource#subscribe(Observer)} throws instead of returning normally.</li>
57+
* <li>If the flow is asynchronous, the exception propagates up to the component ({@link Scheduler} or {@link java.util.concurrent.Executor})
58+
* providing the asynchronous boundary the code is running and either routes the exception to the global
59+
* {@link io.reactivex.plugins.RxJavaPlugins#onError(Throwable)} handler or the current thread's
60+
* {@link java.lang.Thread.UncaughtExceptionHandler#uncaughtException(Thread, Throwable)} handler.</li>
61+
* </ul>
62+
* From the {@code Observable}'s perspective, an {@code Observer} is the end consumer thus it is the {@code Observer}'s
63+
* responsibility to handle the error case and signal it "further down". This means unreliable code in the {@code onXXX}
64+
* methods should be wrapped into `try-catch`es, specifically in {@link #onError(Throwable)} or {@link #onComplete()}, and handled there
65+
* (for example, by logging it or presenting the user with an error dialog). However, if the error would be thrown from
66+
* {@link #onNext(Object)}, <a href="https://github.com/reactive-streams/reactive-streams-jvm#2.13">Rule 2.13</a> mandates
67+
* the implementation calls {@link Disposable#dispose()} and signals the exception in a way that is adequate to the target context,
68+
* for example, by calling {@link #onError(Throwable)} on the same {@code Observer} instance.
69+
* <p>
70+
* If, for some reason, the {@code Observer} won't follow Rule 2.13, the {@link Observable#safeSubscribe(Observer)} can wrap it
71+
* with the necessary safeguards and route exceptions thrown from {@code onNext} into {@code onError} and route exceptions thrown
72+
* from {@code onError} and {@code onComplete} into the global error handler via {@link io.reactivex.plugins.RxJavaPlugins#onError(Throwable)}.
3073
* @see <a href="http://reactivex.io/documentation/observable.html">ReactiveX documentation: Observable</a>
3174
* @param <T>
3275
* the type of item the Observer expects to observe

src/main/java/io/reactivex/SingleObserver.java

+28-7
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,35 @@
1717
import io.reactivex.disposables.Disposable;
1818

1919
/**
20-
* Provides a mechanism for receiving push-based notifications.
20+
* Provides a mechanism for receiving push-based notification of a single value or an error.
2121
* <p>
22-
* After a SingleObserver calls a {@link Single}'s {@link Single#subscribe subscribe} method,
23-
* first the Single calls {@link #onSubscribe(Disposable)} with a {@link Disposable} that allows
24-
* cancelling the sequence at any time, then the
25-
* {@code Single} calls only one of the SingleObserver {@link #onSuccess} and {@link #onError} methods to provide
26-
* notifications.
27-
*
22+
* When a {@code SingleObserver} is subscribed to a {@link SingleSource} through the {@link SingleSource#subscribe(SingleObserver)} method,
23+
* the {@code SingleSource} calls {@link #onSubscribe(Disposable)} with a {@link Disposable} that allows
24+
* disposing the sequence at any time. A well-behaved
25+
* {@code SingleSource} will call a {@code SingleObserver}'s {@link #onSuccess(Object)} method exactly once or the {@code SingleObserver}'s
26+
* {@link #onError} method exactly once as they are considered mutually exclusive <strong>terminal signals</strong>.
27+
* <p>
28+
* Calling the {@code SingleObserver}'s method must happen in a serialized fashion, that is, they must not
29+
* be invoked concurrently by multiple threads in an overlapping fashion and the invocation pattern must
30+
* adhere to the following protocol:
31+
* <p>
32+
* <pre><code> onSubscribe (onSuccess | onError)?</code></pre>
33+
* <p>
34+
* Subscribing a {@code SingleObserver} to multiple {@code SingleSource}s is not recommended. If such reuse
35+
* happens, it is the duty of the {@code SingleObserver} implementation to be ready to receive multiple calls to
36+
* its methods and ensure proper concurrent behavior of its business logic.
37+
* <p>
38+
* Calling {@link #onSubscribe(Disposable)}, {@link #onSuccess(Object)} or {@link #onError(Throwable)} with a
39+
* {@code null} argument is forbidden.
40+
* <p>
41+
* The implementations of the {@code onXXX} methods should avoid throwing runtime exceptions other than the following cases:
42+
* <ul>
43+
* <li>If the argument is {@code null}, the methods can throw a {@code NullPointerException}.
44+
* Note though that RxJava prevents {@code null}s to enter into the flow and thus there is generally no
45+
* need to check for nulls in flows assembled from standard sources and intermediate operators.
46+
* </li>
47+
* <li>If there is a fatal error (such as {@code VirtualMachineError}).</li>
48+
* </ul>
2849
* @see <a href="http://reactivex.io/documentation/observable.html">ReactiveX documentation: Observable</a>
2950
* @param <T>
3051
* the type of item the SingleObserver expects to observe

0 commit comments

Comments
 (0)