public abstract class Result<T>
extends java.lang.Object
Codec
Modifier and Type | Method and Description |
---|---|
static <T> Result<T> |
fail(java.lang.String error)
Creates failure result
|
static <T> Result<T> |
fail(java.lang.String error,
java.lang.Throwable cause)
Creates failure result with an exception
|
abstract <U> Result<U> |
flatMap(java.util.function.Function<T,Result<U>> fn)
Apply the function passed on the result value.
|
abstract T |
get()
Extracts the resulting value or throw an exception if the operation has failed.
|
abstract java.util.Optional<T> |
getOptional()
Gets an
Optional type containing the result value if the operation was successful. |
abstract T |
getOrElse(T defaultValue)
Gets the result value or return the a default value if the operation has failed
|
abstract boolean |
isFailure() |
abstract boolean |
isSuccess() |
abstract <U> Result<U> |
map(java.util.function.Function<T,U> fn)
Apply the function passed on the result value.
|
T |
orNull()
Extracts the resulting value or returns null if the operation has failed.
|
static <T> Result<T> |
success(T value)
Creates a successful result
|
public static <T> Result<T> success(T value)
T
- the type of the resultvalue
- result's valuepublic static <T> Result<T> fail(java.lang.String error)
T
- the type of the resulterror
- the error messagepublic static <T> Result<T> fail(java.lang.String error, java.lang.Throwable cause)
T
- the type of the resulterror
- the error messagecause
- the exception that caused this failurepublic abstract boolean isSuccess()
public abstract boolean isFailure()
public abstract T get()
java.lang.IllegalStateException
- if the operation has failedpublic abstract java.util.Optional<T> getOptional()
Optional
type containing the result value if the operation was successful.Optional
with the result value, if successpublic abstract T getOrElse(T defaultValue)
defaultValue
- default value to return case this represents a failurepublic abstract <U> Result<U> map(java.util.function.Function<T,U> fn)
U
- the type of the resultfn
- the map function to be appliedpublic abstract <U> Result<U> flatMap(java.util.function.Function<T,Result<U>> fn)
U
- the type of the resultfn
- the map function to be appliedpublic T orNull()