Debian Codegraph @

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

raw
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.core;
15
16 import io.reactivex.rxjava3.annotations.NonNull;
17 import io.reactivex.rxjava3.disposables.Disposable;
18
19 /**
20 * Provides a mechanism for receiving push-based notification of a single value, an error or completion without any value.
21 * <p>
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 * <pre><code> onSubscribe (onSuccess | onError | onComplete)?</code></pre>
32 * <p>
33 * Note that unlike with the {@code Observable} protocol, {@link #onComplete()} is not called after the success item has been
34 * signalled via {@link #onSuccess(Object)}.
35 * <p>
36 * Subscribing a {@code MaybeObserver} to multiple {@code MaybeSource}s is not recommended. If such reuse
37 * happens, it is the duty of the {@code MaybeObserver} 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 #onSuccess(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 * <ul>
45 * <li>If the argument is {@code null}, the methods can throw a {@code NullPointerException}.
46 * Note though that RxJava prevents {@code null}s to enter into the flow and thus there is generally no
47 * need to check for nulls in flows assembled from standard sources and intermediate operators.
48 * </li>
49 * <li>If there is a fatal error (such as {@code VirtualMachineError}).</li>
50 * </ul>
51 * @see <a href="http://reactivex.io/documentation/observable.html">ReactiveX documentation: Observable</a>
52 * @param <T>
53 * the type of item the MaybeObserver expects to observe
54 * @since 2.0
55 */
56 public interface MaybeObserver<@NonNull T> {
57
58 /**
59 * Provides the {@link MaybeObserver} with the means of cancelling (disposing) the
60 * connection (channel) with the {@link Maybe} in both
61 * synchronous (from within {@code onSubscribe(Disposable)} itself) and asynchronous manner.
62 * @param d the {@link Disposable} instance whose {@link Disposable#dispose()} can
63 * be called anytime to cancel the connection
64 */
65 void onSubscribe(@NonNull Disposable d);
66
67 /**
68 * Notifies the {@link MaybeObserver} with one item and that the {@link Maybe} has finished sending
69 * push-based notifications.
70 * <p>
71 * The {@link Maybe} will not call this method if it calls {@link #onError}.
72 *
73 * @param t
74 * the item emitted by the {@code Maybe}
75 */
76 void onSuccess(@NonNull T t);
77
78 /**
79 * Notifies the {@link MaybeObserver} that the {@link Maybe} has experienced an error condition.
80 * <p>
81 * If the {@link Maybe} calls this method, it will not thereafter call {@link #onSuccess}.
82 *
83 * @param e
84 * the exception encountered by the {@code Maybe}
85 */
86 void onError(@NonNull Throwable e);
87
88 /**
89 * Called once the deferred computation completes normally.
90 */
91 void onComplete();
92 }