Class Maybe<T>

java.lang.Object
io.reactivex.rxjava3.core.Maybe<T>
Type Parameters:
T - the value type
All Implemented Interfaces:
MaybeSource<T>
Direct Known Subclasses:
AbstractMaybeWithUpstream, CompletableOnErrorReturn, FlowableElementAtMaybe, FlowableElementAtMaybePublisher, FlowableLastMaybe, FlowableReduceMaybe, FlowableSingleMaybe, MaybeAmb, MaybeCache, MaybeCreate, MaybeDefer, MaybeDelayWithCompletable, MaybeDoOnTerminate, MaybeEmpty, MaybeError, MaybeErrorCallable, MaybeFilterSingle, MaybeFlatMapSingle, MaybeFromAction, MaybeFromCallable, MaybeFromCompletable, MaybeFromCompletionStage, MaybeFromFuture, MaybeFromRunnable, MaybeFromSingle, MaybeFromSupplier, MaybeJust, MaybeMapOptional, MaybeNever, MaybeSubject, MaybeTimeInterval, MaybeTimer, MaybeUsing, MaybeZipArray, MaybeZipIterable, ObservableElementAtMaybe, ObservableLastMaybe, ObservableReduceMaybe, ObservableSingleMaybe, SingleDematerialize, SingleFlatMapMaybe, SingleMapOptional, SingleOnErrorComplete

public abstract class Maybe<@NonNull T> extends Object implements MaybeSource<T>
The Maybe class represents a deferred computation and emission of a single value, no value at all or an exception.

The Maybe class implements the MaybeSource base interface and the default consumer type it interacts with is the MaybeObserver via the subscribe(MaybeObserver) method.

The Maybe operates with the following sequential protocol:


     onSubscribe (onSuccess | onError | onComplete)?
 

Note that onSuccess, onError and onComplete are mutually exclusive events; unlike Observable, onSuccess is never followed by onError or onComplete.

Like Observable, a running Maybe can be stopped through the Disposable instance provided to consumers through MaybeObserver.onSubscribe(io.reactivex.rxjava3.disposables.Disposable).

Like an Observable, a Maybe is lazy, can be either "hot" or "cold", synchronous or asynchronous. Maybe instances returned by the methods of this class are cold and there is a standard hot implementation in the form of a subject: MaybeSubject.

The documentation for this class makes use of marble diagrams. The following legend explains these diagrams:

See Flowable or Observable for the implementation of the Reactive Pattern for a stream or vector of values.

Example:


 Disposable d = Maybe.just("Hello World")
    .delay(10, TimeUnit.SECONDS, Schedulers.io())
    .subscribeWith(new DisposableMaybeObserver<String>() {
        @Override
        public void onStart() {
            System.out.println("Started");
        }

        @Override
        public void onSuccess(String value) {
            System.out.println("Success: " + value);
        }

        @Override
        public void onError(Throwable error) {
            error.printStackTrace();
        }

        @Override
        public void onComplete() {
            System.out.println("Done!");
        }
    });
 
 Thread.sleep(5000);
 
 d.dispose();
 

Note that by design, subscriptions via subscribe(MaybeObserver) can't be disposed from the outside (hence the void return of the subscribe(MaybeObserver) method) and it is the responsibility of the implementor of the MaybeObserver to allow this to happen. RxJava supports such usage with the standard DisposableMaybeObserver instance. For convenience, the subscribeWith(MaybeObserver) method is provided as well to allow working with a MaybeObserver (or subclass) instance to be applied with in a fluent manner (such as in the example above).

Since:
2.0
See Also: