io.vlingo.common.CompletesOutcomeT Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of vlingo-common Show documentation
Show all versions of vlingo-common Show documentation
These are just a few common tools shared across various vlingo projects.
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;
}
}