Package de.scravy.bedrock
Class Try<E>
java.lang.Object
de.scravy.bedrock.Try<E>
- All Implemented Interfaces:
Iterable<E>
- Direct Known Subclasses:
Try.Failure,Try.Success
@Immutable public abstract class Try<E> extends Object implements Iterable<E>
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classTry.FailedRecoveringExceptionstatic classTry.Failure<E>static classTry.RethrownExceptionstatic classTry.Success<E> -
Constructor Summary
Constructors Constructor Description Try() -
Method Summary
Modifier and Type Method Description abstract voidconsume(ThrowingConsumer<? super Exception> ifFailure, ThrowingConsumer<E> ifSuccess)static <E> Try<E>execute(Callable<E> callable)static <E> Try<E>failure(Exception exception)abstract Try<E>fallback(E value)abstract Try<E>fallbackWith(Supplier<E> value)abstract Try<E>filter(Predicate<? super E> predicate)abstract <F> Try<F>flatMap(ThrowingFunction<? super E,Try<F>> f)<F> Try<F>flatMapOptional(ThrowingFunction<? super E,Optional<F>> function)abstract <F> Ffold(ThrowingFunction<? super Exception,F> ifFailure, ThrowingFunction<E,F> ifSuccess)static <E> Try<E>fromOptional(Optional<E> optional)abstract Eget()abstract ThrowablegetException()booleanisFailure()booleanisSuccess()static <A, R> Function<A,Try<R>>lift(Function<A,R> function)abstract <F> Try<F>map(ThrowingFunction<? super E,? extends F> f)abstract EorElse(E value)abstract EorElseDo(ThrowingConsumer<Exception> block)abstract EorElseGet(Supplier<? extends E> supplier)abstract EorElseNull()abstract EorElseThrow()abstract EorElseThrowRuntime()abstract Try<E>otherwise(Try<E> alternative)abstract <F> Try<F>recover(ThrowingFunction<Exception,F> value)abstract <F> Try<F>recoverWith(ThrowingFunction<Exception,Try<F>> value)static voidrun(ThrowingRunnable runnable)Tries to run the given ThrowingRunnable and throws a RuntimeException in case it fails.static voidrun(String message, ThrowingRunnable runnable)Tries to run the given ThrowingRunnable and throws a RuntimeException with the given message in case it fails.static <E> Try<E>success(E element)abstract Optional<E>toOptional()abstract Promise<E>toPromise()abstract <F> Try<F>transform(ThrowingFunction<Exception,F> f, ThrowingFunction<E,F> g)abstract <F> Try<F>transformWith(ThrowingFunction<Exception,Try<F>> f, ThrowingFunction<E,Try<F>> g)static voidunfailable(ThrowingRunnable runnable)Runs the given ThrowingRunnable and catches all Exceptions, reporting them to the uncaught exception handler.
-
Constructor Details
-
Try
public Try()
-
-
Method Details
-
isSuccess
public boolean isSuccess() -
isFailure
public boolean isFailure() -
map
-
flatMap
-
flatMapOptional
-
filter
-
recover
-
recoverWith
-
orElse
-
orElseDo
-
orElseGet
-
orElseNull
-
orElseThrow
- Throws:
Try.RethrownException
-
orElseThrowRuntime
-
get
-
getException
-
otherwise
-
transform
@Nonnull public abstract <F> Try<F> transform(@Nonnull ThrowingFunction<Exception,F> f, ThrowingFunction<E,F> g) -
transformWith
@Nonnull public abstract <F> Try<F> transformWith(@Nonnull ThrowingFunction<Exception,Try<F>> f, ThrowingFunction<E,Try<F>> g) -
fallback
-
fallbackWith
-
fold
public abstract <F> F fold(@Nonnull ThrowingFunction<? super Exception,F> ifFailure, @Nonnull ThrowingFunction<E,F> ifSuccess) -
consume
public abstract void consume(@Nonnull ThrowingConsumer<? super Exception> ifFailure, @Nonnull ThrowingConsumer<E> ifSuccess) -
toOptional
-
toPromise
-
fromOptional
-
success
-
failure
-
execute
-
run
Tries to run the given ThrowingRunnable and throws a RuntimeException in case it fails.If the given ThrowingRunnable throws a RuntimeException that Exception is rethrown.
- Parameters:
runnable- A runnable which might throw a checked Exception.
-
run
Tries to run the given ThrowingRunnable and throws a RuntimeException with the given message in case it fails.If the given ThrowingRunnable throws a RuntimeException it is not rethrown but wrapped in a new RuntimeException with the given message (just like a checked Exception).
- Parameters:
message- The message to create the RuntimeException with in case it fails.runnable- A runnable which might throw a checked Exception.
-
unfailable
Runs the given ThrowingRunnable and catches all Exceptions, reporting them to the uncaught exception handler.- Parameters:
runnable- A runnable which might throw a checked Exception.
-
lift
-