- Type Parameters:
T
- the type of the value
A sealed interface representing a computation that may either result in a value (success)
or an exception (failure).
- Author:
- Juan Antonio BreƱa Moral, ChatGPT-40
-
Nested Class Summary
Modifier and TypeInterfaceDescriptionstatic interface
A functional interface representing a supplier that may throw an exception. -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> Result
<T> Creates a failed Result with the given exception.<U> Result
<U> Applies the given function to the value if the Result is successful and returns a new Result.<U> U
Reduces the Result to a single value by applying a function to the successful value or a default value if the result is a failure.Returns the exception if present, otherwise returns an empty Optional.Returns the value if the Result is successful, otherwise returns the result of the specified Supplier.getValue()
Returns the value if present, otherwise returns an empty Optional.void
Performs the given action if the Result is a failure.void
Performs the given action if the Result is successful.boolean
Checks if the Result is a failure.boolean
Checks if the Result is successful.<U> Result
<U> Applies the given function to the value if the Result is successful and returns a new Result.Applies the given function to the exception if the Result is a failure and returns a new Result with the mapped value.recoverCatching
(Function<? super Throwable, Result<T>> mapper) Applies the given function to the exception if the Result is a failure and returns a new Result.static <T> Result
<T> runCatching
(Result.CheckedSupplier<T> supplier) Executes the given supplier and returns a Result.static <T> Result
<T> success
(T value) Creates a successful Result with the given value.
-
Method Details
-
failure
-
success
Creates a successful Result with the given value.- Type Parameters:
T
- the type of the value- Parameters:
value
- the value- Returns:
- a successful Result
-
isSuccess
boolean isSuccess()Checks if the Result is successful.- Returns:
- true if successful, false otherwise
-
isFailure
boolean isFailure()Checks if the Result is a failure.- Returns:
- true if a failure, false otherwise
-
ifSuccess
-
ifFailure
-
getValue
-
getException
-
getOrElse
Returns the value if the Result is successful, otherwise returns the result of the specified Supplier.- Parameters:
other
- a Supplier whose result is returned if the Result is a failure- Returns:
- the value if the Result is successful, otherwise the result of the Supplier
-
map
Applies the given function to the value if the Result is successful and returns a new Result.- Type Parameters:
U
- the type of the value of the new Result- Parameters:
mapper
- the function to apply to the value- Returns:
- a new Result with the mapped value if successful, otherwise the original failure
-
fold
Reduces the Result to a single value by applying a function to the successful value or a default value if the result is a failure.- Type Parameters:
U
- the type of the folded value- Parameters:
initialValue
- the initial value to use if the result is a failurefolder
- the function to apply to the successful value or the initial value- Returns:
- the folded value
-
flatMap
Applies the given function to the value if the Result is successful and returns a new Result. The function should return a new Result.- Type Parameters:
U
- the type of the value of the new Result- Parameters:
mapper
- the function to apply to the value- Returns:
- the Result returned by the mapper if successful, otherwise the original failure
-
recover
Applies the given function to the exception if the Result is a failure and returns a new Result with the mapped value.- Parameters:
mapper
- the function to apply to the exception- Returns:
- a new Result with the mapped value if failure, otherwise the original success
-
recoverCatching
Applies the given function to the exception if the Result is a failure and returns a new Result. The function should return a new Result.- Parameters:
mapper
- the function to apply to the exception- Returns:
- the Result returned by the mapper if failure, otherwise the original success
-
runCatching
Executes the given supplier and returns a Result. If the supplier throws an exception, it returns a failed Result with the thrown exception.- Type Parameters:
T
- the type of the value- Parameters:
supplier
- the supplier to execute- Returns:
- a successful Result if the supplier succeeds, otherwise a failed Result
-