Class Try<T>
java.lang.Object
com.github.darksoulq.abyssallib.common.util.Try<T>
- Type Parameters:
T- The type of the successful value.
A monadic container type that represents a computation that may either result in a
successful value or a failure containing an exception.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceA functional interface for a function that maps an input to an output and may throw an exception.static interfaceA functional interface for a runnable task that may throw a checked or unchecked exception.static interfaceA functional interface for a supplier that may throw a checked or unchecked exception. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionabstract <U> Try<U> flatMap(Try.ThrowingFunction<? super T, Try<U>> mapper) Transforms the successful value into another Try using a throwing mapper function.abstract Tget()Retrieves the successful value or throws a RuntimeException wrapping the failure.abstract ThrowableRetrieves the exception that caused the failure.abstract booleanDetermines if the computation completed successfully.abstract <U> Try<U> map(Try.ThrowingFunction<? super T, ? extends U> mapper) Transforms the successful value using the provided throwing mapper function.static <T> Try<T> of(Try.ThrowingSupplier<T> supplier) Executes a supplier that may throw an exception and wraps the result in a Try.Executes the provided consumer action if the computation resulted in a failure.Executes the provided consumer action if the computation was successful.abstract TReturns the successful value if present, otherwise returns the specified default.abstract TReturns the successful value if present, otherwise invokes the supplier for a fallback.orElseThrow(Function<Throwable, X> exceptionProvider) Returns the successful value or throws a transformed exception.run(Try.ThrowingRunnable runnable) Executes a runnable that may throw an exception and wraps the result in a Try.Converts this Try instance into a standard Java Optional.
-
Constructor Details
-
Try
public Try()
-
-
Method Details
-
of
Executes a supplier that may throw an exception and wraps the result in a Try.- Type Parameters:
T- The result type of the supplier.- Parameters:
supplier- The computation to perform which may throw a Throwable.- Returns:
- A Success containing the value if successful, or a Failure containing the caught Throwable.
-
run
Executes a runnable that may throw an exception and wraps the result in a Try.- Parameters:
runnable- The action to perform which may throw a Throwable.- Returns:
- A Success containing null if successful, or a Failure containing the caught Throwable.
-
isSuccess
public abstract boolean isSuccess()Determines if the computation completed successfully.- Returns:
- True if the instance is a Success, false if it is a Failure.
-
get
Retrieves the successful value or throws a RuntimeException wrapping the failure.- Returns:
- The successful value of type T.
-
getException
Retrieves the exception that caused the failure.- Returns:
- The Throwable caught during the computation.
- Throws:
NoSuchElementException- If the Try is a Success and contains no exception.
-
map
Transforms the successful value using the provided throwing mapper function.- Type Parameters:
U- The new result type after mapping.- Parameters:
mapper- The function to apply to the successful value.- Returns:
- A new Success instance if mapping succeeds, or a Failure if an exception is caught.
-
flatMap
Transforms the successful value into another Try using a throwing mapper function.- Type Parameters:
U- The result type of the new Try.- Parameters:
mapper- The function to apply which returns a Try.- Returns:
- The result of the mapper, or a Failure if an exception occurs during execution.
-
onFailure
-
onSuccess
-
orElse
-
orElseGet
-
orElseThrow
public abstract <X extends Throwable> T orElseThrow(Function<Throwable, X> exceptionProvider) throws X Returns the successful value or throws a transformed exception.- Type Parameters:
X- The specific type of Throwable to be thrown.- Parameters:
exceptionProvider- Function to convert the original Throwable into type X.- Returns:
- The successful value.
- Throws:
X- If the computation failed.
-
toOptional
-