Debian Codegraph @

src/main/java/io/reactivex/rxjava3/core/MaybeEmitter.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.*;
17 import io.reactivex.rxjava3.disposables.Disposable;
18 import io.reactivex.rxjava3.functions.*;
19
20 /**
21 * Abstraction over an RxJava {@link MaybeObserver} that allows associating
22 * a resource with it.
23 * <p>
24 * All methods are safe to call from multiple threads, but note that there is no guarantee
25 * whose terminal event will win and get delivered to the downstream.
26 * <p>
27 * Calling {@link #onSuccess(Object)} or {@link #onComplete()} multiple times has no effect.
28 * Calling {@link #onError(Throwable)} multiple times or after the other two will route the
29 * exception into the global error handler via {@link io.reactivex.rxjava3.plugins.RxJavaPlugins#onError(Throwable)}.
30 * <p>
31 * The emitter allows the registration of a single resource, in the form of a {@link Disposable}
32 * or {@link Cancellable} via {@link #setDisposable(Disposable)} or {@link #setCancellable(Cancellable)}
33 * respectively. The emitter implementations will dispose/cancel this instance when the
34 * downstream cancels the flow or after the event generator logic calls {@link #onSuccess(Object)},
35 * {@link #onError(Throwable)}, {@link #onComplete()} or when {@link #tryOnError(Throwable)} succeeds.
36 * <p>
37 * Only one {@code Disposable} or {@code Cancellable} object can be associated with the emitter at
38 * a time. Calling either {@code set} method will dispose/cancel any previous object. If there
39 * is a need for handling multiple resources, one can create a {@link io.reactivex.rxjava3.disposables.CompositeDisposable}
40 * and associate that with the emitter instead.
41 * <p>
42 * The {@link Cancellable} is logically equivalent to {@code Disposable} but allows using cleanup logic that can
43 * throw a checked exception (such as many {@code close()} methods on Java IO components). Since
44 * the release of resources happens after the terminal events have been delivered or the sequence gets
45 * cancelled, exceptions throw within {@code Cancellable} are routed to the global error handler via
46 * {@link io.reactivex.rxjava3.plugins.RxJavaPlugins#onError(Throwable)}.
47 *
48 * @param <T> the value type to emit
49 */
50 public interface MaybeEmitter<@NonNull T> {
51
52 /**
53 * Signal a success value.
54 * @param t the value, not null
55 */
56 void onSuccess(@NonNull T t);
57
58 /**
59 * Signal an exception.
60 * @param t the exception, not {@code null}
61 */
62 void onError(@NonNull Throwable t);
63
64 /**
65 * Signal the completion.
66 */
67 void onComplete();
68
69 /**
70 * Sets a {@link Disposable} on this emitter; any previous {@code Disposable}
71 * or {@link Cancellable} will be disposed/cancelled.
72 * <p>This method is thread-safe.
73 * @param d the disposable, {@code null} is allowed
74 */
75 void setDisposable(@Nullable Disposable d);
76
77 /**
78 * Sets a {@link Cancellable} on this emitter; any previous {@link Disposable}
79 * or {@code Cancellable} will be disposed/cancelled.
80 * <p>This method is thread-safe.
81 * @param c the {@code Cancellable} resource, {@code null} is allowed
82 */
83 void setCancellable(@Nullable Cancellable c);
84
85 /**
86 * Returns true if the downstream disposed the sequence or the
87 * emitter was terminated via {@link #onSuccess(Object)}, {@link #onError(Throwable)},
88 * {@link #onComplete} or a
89 * successful {@link #tryOnError(Throwable)}.
90 * <p>This method is thread-safe.
91 * @return true if the downstream disposed the sequence or the emitter was terminated
92 */
93 boolean isDisposed();
94
95 /**
96 * Attempts to emit the specified {@link Throwable} error if the downstream
97 * hasn't cancelled the sequence or is otherwise terminated, returning false
98 * if the emission is not allowed to happen due to lifecycle restrictions.
99 * <p>
100 * Unlike {@link #onError(Throwable)}, the {@link io.reactivex.rxjava3.plugins.RxJavaPlugins#onError(Throwable) RxjavaPlugins.onError}
101 * is not called if the error could not be delivered.
102 * <p>History: 2.1.1 - experimental
103 * @param t the {@code Throwable} error to signal if possible
104 * @return true if successful, false if the downstream is not able to accept further
105 * events
106 * @since 2.2
107 */
108 boolean tryOnError(@NonNull Throwable t);
109 }