SuccessType
- the result type for a successful operation. Not used by the Failure
type but required by the ServiceResult
public 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, wait
ifSuccessful
isSuccessful
public 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)
Result
SuccessType
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 typeT
public SuccessType getMessage()
Result
SuccessType
getMessage
in interface Result<SuccessType,String>
getMessage
in interface ServiceResult<SuccessType>
public String getErrorMessage()
Result
FailureType
getErrorMessage
in interface Result<SuccessType,String>
getErrorMessage
in interface ServiceResult<SuccessType>
public ServiceResult<SuccessType> ifFailure(Consumer<? super String> consumer)
ServiceResult
ifFailure
in interface ServiceResult<SuccessType>
consumer
- block to be executed if a value is presentServiceResult
public boolean isFailure()
Result
isFailure
in interface Result<SuccessType,String>