Package nva.commons.core.attempt
Class Try<T>
- java.lang.Object
-
- nva.commons.core.attempt.Try<T>
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.StringNULL_ACTION_MESSAGE
-
Constructor Summary
Constructors Constructor Description Try()
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description static <S> Try<S>attempt(java.util.concurrent.Callable<S> action)A wrapper for actions that throw checked Exceptions.static <T,R,E extends java.lang.Exception>
java.util.function.Function<T,Try<R>>attempt(FunctionWithException<T,R,E> fe)A wrapper for functions that throw checked Exceptions.abstract <S,E extends java.lang.Exception>
Try<S>flatMap(FunctionWithException<T,Try<S>,E> action)abstract <E extends java.lang.Exception>
Try<java.lang.Void>forEach(ConsumerWithException<T,E> consumer)abstract Tget()abstract java.lang.ExceptiongetException()booleanisFailure()abstract booleanisSuccess()abstract <S,E extends java.lang.Exception>
Try<S>map(FunctionWithException<T,S,E> action)static <T> Try<T>of(T input)abstract <E extends java.lang.Exception>
TorElse(FunctionWithException<Failure<T>,T,E> action)abstract TorElseThrow()abstract <E extends java.lang.Exception>
TorElseThrow(java.util.function.Function<Failure<T>,E> action)abstract java.util.stream.Stream<T>stream()abstract java.util.Optional<T>toOptional()abstract <E extends java.lang.Exception>
java.util.Optional<T>toOptional(ConsumerWithException<Failure<T>,E> action)
-
-
-
Field Detail
-
NULL_ACTION_MESSAGE
public static final java.lang.String NULL_ACTION_MESSAGE
- See Also:
- Constant Field Values
-
-
Method Detail
-
of
public static <T> Try<T> of(T input)
-
attempt
public static <S> Try<S> attempt(java.util.concurrent.Callable<S> action)
A wrapper for actions that throw checked Exceptions. See {@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:
S- the resulting object- Parameters:
action- aCallableaction that throws or does not throw a checked Exception- Returns:
- a new
Tryinstance
-
attempt
public static <T,R,E extends java.lang.Exception> java.util.function.Function<T,Try<R>> attempt(FunctionWithException<T,R,E> fe)
A wrapper for functions that throw checked Exceptions. See {@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
public abstract java.util.stream.Stream<T> stream()
-
isSuccess
public abstract boolean isSuccess()
-
get
public abstract T get()
-
getException
public abstract java.lang.Exception getException()
-
isFailure
public final boolean isFailure()
-
map
public abstract <S,E extends java.lang.Exception> Try<S> map(FunctionWithException<T,S,E> action)
-
flatMap
public abstract <S,E extends java.lang.Exception> Try<S> flatMap(FunctionWithException<T,Try<S>,E> action)
-
forEach
public abstract <E extends java.lang.Exception> Try<java.lang.Void> forEach(ConsumerWithException<T,E> consumer)
-
orElseThrow
public abstract <E extends java.lang.Exception> T orElseThrow(java.util.function.Function<Failure<T>,E> action) throws E extends java.lang.Exception
- Throws:
E extends java.lang.Exception
-
orElseThrow
public abstract T orElseThrow()
-
orElse
public abstract <E extends java.lang.Exception> T orElse(FunctionWithException<Failure<T>,T,E> action) throws E extends java.lang.Exception
- Throws:
E extends java.lang.Exception
-
toOptional
public abstract <E extends java.lang.Exception> java.util.Optional<T> toOptional(ConsumerWithException<Failure<T>,E> action) throws E extends java.lang.Exception
- Throws:
E extends java.lang.Exception
-
toOptional
public abstract java.util.Optional<T> toOptional()
-
-