Class Try<T>
java.lang.Object
com.dudko.tools.safejavastreams.core.Try<T>
- Direct Known Subclasses:
Try.Failure,Try.Success
Try is a container for a computation that may either result in a value (Success) or an exception (Failure).
Similar to
Either<Throwable, T>.-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classRepresents a failed computation.static final classRepresents a successful computation. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> Try<T>Creates a Failure instance.Applies a predicate to value if Success.abstract <U> Try<U>Applies a function that returns Try if Success, otherwise propagates the Failure.abstract <U> Ufold(Function<? super Throwable, ? extends U> failureMapper, Function<? super T, ? extends U> successMapper) Applies one of two functions depending on whether it's a Success or Failure.abstract Tget()Returns the value if this is a Success, otherwise throws the exception.abstract ThrowablegetError()Returns value or default if Failure.getOrElseGet(Function<Throwable, ? extends T> supplier) Returns value or result of supplier if Failure.Returns value or throws RuntimeException if Failure.getOrThrow(Function<Throwable, ? extends RuntimeException> mapper) Returns value or throws custom RuntimeException from mapper.booleanReturns true if this is a Failure.booleanisFailureOf(Class<? extends Throwable> type) Checks if this Try is a Failure of specific exception type.abstract booleanReturns true if this is a Success.abstract <U> Try<U>Applies a function to the value if Success, otherwise propagates the Failure.static <T> Try<T>of(ThrowingSupplier<T> supplier) Executes given code block and wraps result in Try.Executes an action if Failure.Executes an action if Success.Performs side-effecting operation on Success.peekFailure(Consumer<? super Throwable> action) Performs side-effecting operation on Failure.Applies a recovery function if this is a Failure.recoverWith(Function<? super Throwable, Try<T>> recovery) Applies a recovery function that returns Try if this is a Failure.static <T> Try<T>success(T value) Creates a Success instance.Returns the value as Optional if Success, otherwise empty.toStream()Converts to Java Stream with 0 or 1 element.
-
Constructor Details
-
Try
public Try()
-
-
Method Details
-
isSuccess
public abstract boolean isSuccess()Returns true if this is a Success. -
isFailure
public boolean isFailure()Returns true if this is a Failure. -
get
Returns the value if this is a Success, otherwise throws the exception.- Throws:
Throwable
-
map
Applies a function to the value if Success, otherwise propagates the Failure.- Type Parameters:
U- mapped value type- Parameters:
mapper- function to apply- Returns:
- new Try with mapped value or the original Failure
-
flatMap
Applies a function that returns Try if Success, otherwise propagates the Failure.- Type Parameters:
U- mapped value type- Parameters:
mapper- function to apply- Returns:
- result of function or the original Failure
-
recover
Applies a recovery function if this is a Failure.- Parameters:
recovery- recovery function- Returns:
- Success with recovered value or original Success
-
recoverWith
Applies a recovery function that returns Try if this is a Failure.- Parameters:
recovery- recovery function- Returns:
- Try from recovery or original Success
-
fold
public abstract <U> U fold(Function<? super Throwable, ? extends U> failureMapper, Function<? super T, ? extends U> successMapper) Applies one of two functions depending on whether it's a Success or Failure.- Type Parameters:
U- return type- Parameters:
failureMapper- mapper for FailuresuccessMapper- mapper for Success- Returns:
- result of applying the corresponding function
-
peek
Performs side-effecting operation on Success.- Parameters:
action- consumer to run if Success- Returns:
- the same Try instance
-
peekFailure
Performs side-effecting operation on Failure.- Parameters:
action- consumer to run if Failure- Returns:
- the same Try instance
-
toOptional
Returns the value as Optional if Success, otherwise empty. -
getError
-
toEither
-
isFailureOf
Checks if this Try is a Failure of specific exception type.- Parameters:
type- exception type to check- Returns:
- true if this is Failure with given type
-
onSuccess
Executes an action if Success. -
onFailure
Executes an action if Failure. -
getOrElse
Returns value or default if Failure. -
getOrElseGet
Returns value or result of supplier if Failure. -
getOrThrow
Returns value or throws RuntimeException if Failure. -
getOrThrow
Returns value or throws custom RuntimeException from mapper. -
filter
Applies a predicate to value if Success. Returns Failure if predicate fails. -
toStream
Converts to Java Stream with 0 or 1 element. -
of
Executes given code block and wraps result in Try. -
success
Creates a Success instance.- Type Parameters:
T- type of the value- Parameters:
value- the value- Returns:
- Success with the value
-
failure
Creates a Failure instance.- Type Parameters:
T- type of the value- Parameters:
exception- the exception- Returns:
- Failure with the exception
-