package com.bvn13.jmonad;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Function;
import java.util.function.Supplier;
/**
* Full implementation of Try monad
* @see Source article
*
* Created by bvn13 on 10.06.2019.
*/
public abstract class Try {
public static Try ofThrowable(Supplier f) {
Objects.requireNonNull(f);
try {
return Try.successful(f.get());
} catch (Throwable e) {
return Try.failure(e);
}
}
public static Success successful(U u) {
return new Success<>(u);
}
public static Failure failure(Throwable e) {
return new Failure<>(e);
}
public abstract T get() throws Throwable;
public abstract T orElse(T value);
public abstract Try flatMap(Function super T, ? extends U> f);
public abstract Try filter(Function super T, ? extends Boolean> f);
public abstract Try map(Function super T, ? extends T> f);
public abstract T orElseThrow(Throwable e) throws Throwable;
public abstract T orElseThrow() throws Throwable;
public abstract Optional toOptional();
public abstract boolean isSuccess();
}