All Downloads are FREE. Search and download functionalities are using the official Maven repository.

se.fortnox.reactivewizard.util.rx.IfThenElse Maven / Gradle / Ivy

There is a newer version: 24.6.0
Show newest version
package se.fortnox.reactivewizard.util.rx;

import rx.Observable;

/**
 * Utility for creating simple conditional workflows using Observables.
 */
public class IfThenElse {
    private Observable ifValue;
    private Observable       thenValue;

    IfThenElse(Observable ifValue) {
        this.ifValue = ifValue;
    }

    /**
     * Executes when the boolean Observable is true.
     *
     * @param thenValue The observable to execute
     */
    public IfThenElse then(Observable thenValue) {
        this.thenValue = thenValue;
        return this;
    }

    /**
     * Thrown when the boolean Observable is false.
     *
     * @param throwable The exception to throw
     */
    public Observable elseThrow(Throwable throwable) {
        return ifValue.flatMap(exists -> {
            if (exists) {
                return thenValue;
            } else {
                return Observable.error(throwable);
            }
        });
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy