SuccessType - the result type for a successful operation. Not used by the Failure
type but required by the ServiceResultpublic class Failure<SuccessType> extends Object implements ServiceResult<SuccessType>
ServiceResult represents a failed operation. It contains the
errorMessage for the failure.ServiceResult,
Success| Modifier and Type | Method and Description |
|---|---|
String |
getErrorMessage()
The return message of a failed operation.
|
SuccessType |
getMessage()
The return message of a successful operation.
|
ServiceResult<SuccessType> |
ifFailure(Consumer<? super String> consumer)
If the result of the operation has failed, invoke the specified consumer with the value,
otherwise do nothing.
|
boolean |
isFailure()
Returns a boolean to indicate the success or failure of the operation
|
<T> T |
map(Function<SuccessType,T> successFunction,
Function<String,T> errorFunction)
A mapping function that maps to either
SuccessType or FailureType depending on
success or
failure of the operation. |
static <T> Failure<T> |
of(String errorMessage)
Creates a
Failure object containing the errorMessage |
static <T> Failure<T> |
of(String errorMessage,
Throwable throwable)
Creates a
Failure object containing the errorMessage |
static <T> Failure<T> |
of(Throwable throwable)
Creates a
Failure object containing the throwable |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitifSuccessfulisSuccessfulpublic static <T> Failure<T> of(String errorMessage)
Failure object containing the errorMessageT - the type of the resulterrorMessage - the error message describing the reason for failure.Failure instance containing the errorMessagepublic static <T> Failure<T> of(Throwable throwable)
Failure object containing the throwableT - the type of the resultthrowable - the error message describing the reason for failure.Failure instance containing the throwablepublic static <T> Failure<T> of(String errorMessage, Throwable throwable)
Failure object containing the errorMessageT - the type of the resulterrorMessage - the error message describing the reason for failure.throwable - the throwable that caused the failureFailure instance containing the errorMessagepublic <T> T map(Function<SuccessType,T> successFunction, Function<String,T> errorFunction)
ResultSuccessType or FailureType depending on
success or
failure of the operation.map in interface Result<SuccessType,String>map in interface ServiceResult<SuccessType>T - the resultant typesuccessFunction - the mapping function to map the SuccessType to the resultant typeerrorFunction - the mapping function to map the FailureType to the resultant error typeTpublic SuccessType getMessage()
ResultSuccessTypegetMessage in interface Result<SuccessType,String>getMessage in interface ServiceResult<SuccessType>public String getErrorMessage()
ResultFailureTypegetErrorMessage in interface Result<SuccessType,String>getErrorMessage in interface ServiceResult<SuccessType>public ServiceResult<SuccessType> ifFailure(Consumer<? super String> consumer)
ServiceResultifFailure in interface ServiceResult<SuccessType>consumer - block to be executed if a value is presentServiceResultpublic boolean isFailure()
ResultisFailure in interface Result<SuccessType,String>