Package nva.commons.core.attempt
Class Try<T>
java.lang.Object
nva.commons.core.attempt.Try<T>
- Type Parameters:
T- the contained object.
Utility class for handling checked exceptions in map functions. Check tests for examples
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <S> Try<S>A wrapper for actions that throw checked Exceptions.attempt(FunctionWithException<T, R, E> fe) A wrapper for functions that throw checked Exceptions.flatMap(FunctionWithException<T, Try<S>, E> action) forEach(ConsumerWithException<T, E> consumer) abstract Tget()abstract Exceptionfinal booleanabstract booleanmap(FunctionWithException<T, S, E> action) static <T> Try<T>of(T input) orElse(FunctionWithException<Failure<T>, T, E> action) abstract TorElseThrow(Function<Failure<T>, E> action) stream()toOptional(ConsumerWithException<Failure<T>, E> action)
-
Field Details
-
NULL_ACTION_MESSAGE
- See Also:
-
-
Constructor Details
-
Try
public Try()
-
-
Method Details
-
of
-
attempt
A wrapper for actions that throw checked Exceptions. See: "https://www.oreilly.com/content/handling-checked-exceptions-in-java-streams" Try to perform the action. Any exception will be enclosed in a Failure. -
attempt
public static <T,R, Function<T,E extends Exception> Try<R>> attempt(FunctionWithException<T, R, E> fe) A wrapper for functions that throw checked Exceptions. See: "https://www.oreilly.com/content/handling-checked-exceptions-in-java-streams" Try to perform the action. Any exception will be enclosed in a Failure.- Type Parameters:
T- the type of the argument of the function.R- the type of the result of the functionE- the type of the thrown Exception- Parameters:
fe- aFunctionWithExceptionfunction that throws or does not throw a checked Exception- Returns:
- a new
Tryinstance
-
stream
-
isSuccess
public abstract boolean isSuccess() -
get
-
getException
-
isFailure
public final boolean isFailure() -
map
-
flatMap
-
forEach
-
orElseThrow
- Throws:
E extends Exception
-
orElseThrow
-
orElseThrow
-
orElse
public abstract <E extends Exception> T orElse(FunctionWithException<Failure<T>, T, throws EE> action) - Throws:
E extends Exception
-
toOptional
public abstract <E extends Exception> Optional<T> toOptional(ConsumerWithException<Failure<T>, E> action) throws E- Throws:
E extends Exception
-
toOptional
-
or
-