RxJava - MayBe Observable

Maybe observable is similar to Single but this time, The Observable can emit either a single value or nothing

Maybe  Structure

public interface MaybeObserver {

    void onSubscribe(@NonNull Disposable d);

    void onSuccess(@NonNull T t);

    void onError(@NonNull Throwable e);

    void onComplete();
}

 

This will have 4 functions.

Example

Maybe.empty().subscribe(new MaybeObserver<Object>() {

            @Override
            public void onSubscribe(Disposable dspsbl) {
              System.out.println("Disposable "+dspsbl);    
            }

            @Override
            public void onSuccess(Object t) {
                System.out.println("onSuccess "+t);
            }

            @Override
            public void onError(Throwable thrwbl) {
                  System.out.println("onError "+thrwbl);        }

            @Override
            public void onComplete() {
                  System.out.println("onComplete ");
            }
        });

Here it will not emit any thing, so onComplete will be execute.

Output

Disposable INSTANCE
onComplete 

 

Example 2:

Maybe.just("RXMaybe Abservable")
            .subscribe(new MaybeObserver<Object>() {

            @Override
            public void onSubscribe(Disposable dspsbl) {
              System.out.println("Disposable "+dspsbl);    
            }

            @Override
            public void onSuccess(Object t) {
                System.out.println("onSuccess "+t);
            }

            @Override
            public void onError(Throwable thrwbl) {
                  System.out.println("onError "+thrwbl);        }

            @Override
            public void onComplete() {
                  System.out.println("onComplete ");
            }
        });

 

Here it will emit data and will execute onSuccess

Output

Disposable INSTANCE
onSuccess RXMaybe Abservable