Class Result<T,E>
java.lang.Object
com.dudko.tools.safejavastreams.core.Result<T,E>
- Type Parameters:
T
- the type of the success valueE
- the type of the error value
- Direct Known Subclasses:
Result.Failure
,Result.Success
Result is a container for a computation that may either result in a value (Success) or an error (Failure).
Similar to
Either<E, T>
.-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final class
Failure branch implementation.static final class
Success branch implementation. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <T,
E> Result<T, E> failure
(E error) Creates a failure result.Applies predicate to the value, turning success into failure if predicate fails.Applies the function to the success value, returning another Result.abstract <U> U
Applies one of two functions depending on the result type.abstract T
get()
Returns the success value or throws if failure.abstract E
getError()
Returns the error value or throws if success.Returns the success value or a default if failure.getOrElseGet
(Function<? super E, ? extends T> supplier) Returns the success value or result of a supplier if failure.boolean
Returns true if this is a failure result.abstract boolean
Returns true if this is a success result.Applies the function to the success value.Maps the error value if this is a failure.Executes action if failure.Executes action if success.orElseThrow
(Function<? super E, ? extends RuntimeException> exceptionMapper) Returns the success value or throws mapped exception if failure.Executes action regardless of result type.Recovers from failure with a fallback value.Recovers from failure with another Result.static <T,
E> Result<T, E> success
(T value) Creates a success result.toEither()
Converts this Result to an Either.Returns success value as Optional or empty if failure.
-
Constructor Details
-
Result
public Result()
-
-
Method Details
-
isSuccess
public abstract boolean isSuccess()Returns true if this is a success result. -
isFailure
public boolean isFailure()Returns true if this is a failure result. -
get
Returns the success value or throws if failure. -
getError
Returns the error value or throws if success. -
getOrElse
Returns the success value or a default if failure. -
getOrElseGet
Returns the success value or result of a supplier if failure. -
orElseThrow
Returns the success value or throws mapped exception if failure. -
toOptional
Returns success value as Optional or empty if failure. -
toEither
Converts this Result to an Either. -
map
Applies the function to the success value. -
flatMap
Applies the function to the success value, returning another Result. -
fold
public abstract <U> U fold(Function<? super E, ? extends U> failureMapper, Function<? super T, ? extends U> successMapper) Applies one of two functions depending on the result type. -
onSuccess
Executes action if success. -
onFailure
Executes action if failure. -
peek
Executes action regardless of result type. -
filter
public Result<T,E> filter(Predicate<? super T> predicate, Function<? super T, ? extends E> onFailure) Applies predicate to the value, turning success into failure if predicate fails. -
mapError
Maps the error value if this is a failure. -
recover
Recovers from failure with a fallback value. -
recoverWith
Recovers from failure with another Result. -
success
Creates a success result. -
failure
Creates a failure result.
-