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

io.vlingo.common.CompletesOutcomeT Maven / Gradle / Ivy

There is a newer version: 1.7.5
Show newest version
package io.vlingo.common;

import java.util.function.Function;


/**
 * Monad transformer implementation for Outcome nested in Completes
 *
 * @param  the Outcome's error type
 * @param  the Outcome's success type
 */
public final class CompletesOutcomeT {

    public static  CompletesOutcomeT of(
        Completes> value) {

        return new CompletesOutcomeT<>(value);
    }


    private final Completes> value;


    private CompletesOutcomeT(Completes> value) {
        this.value = value;
    }


    public final  CompletesOutcomeT andThen(Function function) {
        return new CompletesOutcomeT<>(
            value.andThen(outcome ->
                outcome.andThen(function)));
    }

    public final  CompletesOutcomeT andThenTo(
        Function> function) {

        return new CompletesOutcomeT<>(value.andThenTo(outcome ->
            outcome.resolve(
                f -> Completes.withSuccess(Failure.of(f)),
                s -> {
                    try {
                        return function.apply(outcome.get()).value();
                    } catch (Throwable t) {
                        throw new RuntimeException(
                            "Unexpected exception thrown getting the value out of a successful Outcome!", t);
                    }
                })
        ));
    }

    public final Completes> value() {
        return value;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy