Package com.goodcode.online.result
Class Result<Success,Failure>
java.lang.Object
com.goodcode.online.result.Result<Success,Failure>
A monad that represents the result of an operation, which can either be a success or a failure.
-
Method Summary
Modifier and TypeMethodDescriptionstatic <Success,Failure>
Result<Success, Failure> failure(Failure failure) Creates a new failure result.<NewSuccess,NewFailure>
Result<NewSuccess, NewFailure> flat(Function<Success, Result<NewSuccess, NewFailure>> successMapper, Function<Failure, Result<NewSuccess, NewFailure>> failureMapper) Flat maps both the success and failure values to new values using the given mapper functions, depending on whether this is a success or failure result.flatFailure(Function<Failure, Result<Success, NewFailure>> mapper) Flat maps the failure value to a new failure<NewFailure>using the given mapper function, if this is a failure result.flatSuccess(Function<Success, Result<NewSuccess, Failure>> mapper) Flat maps the success value to a new Success<NewSuccess>the given mapper function, if this is a success result.<Value> ValueApplies the appropriate function to the success or failure value and returns the result.Gets the failure result, or null if this is a success.Gets the success result, or null if this is a failure.If this is a failure result, applies the given consumer to the failure value.If this is a success result, applies the given consumer to the success value.booleanChecks if this is a failure result.booleanChecks if this is a success result.<NewSuccess,NewFailure>
Result<NewSuccess, NewFailure> Maps both the success and failure values to new values using the given mapper functions, depending on whether this is a success or failure result.mapFailure(Function<Failure, NewFailure> mapper) Maps the failure value to a new failure value of type<NewFailure>using the given mapper function, if this is a failure result.mapSuccess(Function<Success, NewSuccess> mapper) Maps the success value to a new success value of type<NewSuccess>using the given mapper function, if this is a success result.static <Success,Failure>
Result<Success, Failure> success(Success success) Creates a new success result.
-
Method Details
-
success
Creates a new success result.- Type Parameters:
Success- the type of the success resultFailure- the type of the failure result- Parameters:
success- the success result value- Returns:
- a new Result instance representing success
-
failure
Creates a new failure result.- Type Parameters:
Success- the type of the success resultFailure- the type of the failure result- Parameters:
failure- the failure result value- Returns:
- a new Result instance representing failure
-
getSuccess
Gets the success result, or null if this is a failure.- Returns:
- the success result, or null if this is a failure
-
getFailure
Gets the failure result, or null if this is a success.- Returns:
- the failure result, or null if this is a success
-
ifSuccess
If this is a success result, applies the given consumer to the success value.- Parameters:
consumer- the consumer to apply to the success value- Returns:
- this Result instance
-
isFailure
public boolean isFailure()Checks if this is a failure result.- Returns:
- true if this is a failure result, false if it is a success
-
isSuccess
public boolean isSuccess()Checks if this is a success result.- Returns:
- true if this is a success result, false if it is a failure
-
ifFailure
If this is a failure result, applies the given consumer to the failure value.- Parameters:
consumer- the consumer to apply to the failure value- Returns:
- this Result instance
-
get
public <Value> Value get(Function<Success, Value> successMapper, Function<Failure, Value> failureMapper) Applies the appropriate function to the success or failure value and returns the result.- Type Parameters:
Value- the type of the return value- Parameters:
successMapper- the function to apply to the success value if this is a successfailureMapper- the function to apply to the failure value if this is a failure- Returns:
- the result of applying the appropriate function
-
mapSuccess
Maps the success value to a new success value of type<NewSuccess>using the given mapper function, if this is a success result. If it is a failure result, the original failure is retained.- Type Parameters:
NewSuccess- the type of the new success value- Parameters:
mapper- the function to map the success value- Returns:
- a new Result instance with the mapped success value, or the original failure
-
mapFailure
Maps the failure value to a new failure value of type<NewFailure>using the given mapper function, if this is a failure result. If it is a success result, the original success is retained.- Type Parameters:
NewFailure- the type of the new failure value- Parameters:
mapper- the function to map the failure value- Returns:
- a new Result instance with the original success or the mapped failure value
-
map
public <NewSuccess,NewFailure> Result<NewSuccess,NewFailure> map(Function<Success, NewSuccess> successMapper, Function<Failure, NewFailure> failureMapper) Maps both the success and failure values to new values using the given mapper functions, depending on whether this is a success or failure result.- Type Parameters:
NewSuccess- the type of the new success valueNewFailure- the type of the new failure value- Parameters:
successMapper- the function to map the success valuefailureMapper- the function to map the failure value- Returns:
- a new Result instance with the mapped success or failure values
-
flatSuccess
public <NewSuccess> Result<NewSuccess,Failure> flatSuccess(Function<Success, Result<NewSuccess, Failure>> mapper) Flat maps the success value to a new Success<NewSuccess>the given mapper function, if this is a success result. If it is a failure result, the original failure is retained.- Type Parameters:
NewSuccess- the type of the new success value- Parameters:
mapper- the function to flat map the success value- Returns:
- the result of applying the mapper function if this is a success, or the original failure
-
flatFailure
public <NewFailure> Result<Success,NewFailure> flatFailure(Function<Failure, Result<Success, NewFailure>> mapper) Flat maps the failure value to a new failure<NewFailure>using the given mapper function, if this is a failure result. If it is a success result, the original success is retained.- Type Parameters:
NewFailure- the type of the new failure value- Parameters:
mapper- the function to flat map the failure value- Returns:
- the result of applying the mapper function if this is a failure, or the original success
-
flat
public <NewSuccess,NewFailure> Result<NewSuccess,NewFailure> flat(Function<Success, Result<NewSuccess, NewFailure>> successMapper, Function<Failure, Result<NewSuccess, NewFailure>> failureMapper) Flat maps both the success and failure values to new values using the given mapper functions, depending on whether this is a success or failure result.- Type Parameters:
NewSuccess- the type of the new success valueNewFailure- the type of the new failure value- Parameters:
successMapper- the function to flat map the success value if this is a successfailureMapper- the function to flat map the failure value if this is a failure- Returns:
- the result of applying the appropriate mapper function
-