Class Response<T>
java.lang.Object
org.brapi.schematools.core.response.Response<T>
- Type Parameters:
T- The type of result
A generic Response class for functional programming.
A successful Response is one that has no errors, whereas a failed Response has errors
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classWrapper around an error in the response.static interfaceFunctional Interface to support error logging.static enumType of error -
Method Summary
Modifier and TypeMethodDescriptionaddError(Response.ErrorType type, String code, String message) Adds an error to the response.addError(Response.ErrorType type, Response.Error error) Adds an error to the response.conditionalMapResult(boolean condition, UnaryOperator<T> function) If the condition istrueand this response has no errors creates a new Response with the result from the function, otherwise a new Response with the errors from this resource.conditionalMapResultToResponse(boolean condition, Function<T, Response<T>> function) If the condition istrueand this response has no errors use the function to get the new responseconditionalMapResultToResponse(boolean condition, Supplier<Response<T>> supplier) If the condition istrueand this response has no errors use the supplier to get the new responseIf the predicate returnstrueand this response has no errors creates a new Response with the result from the function, otherwise a new Response with the errors from this resource.If the predicate returnstrueand this response has no errors creates a new Response with the result from the function, otherwise a new Response with the errors from this resource.static <T> Response<T> empty()Creates an empty responsestatic <T> Response<T> fail(Response.ErrorType type, String message) Creates a failed responsestatic <T> Response<T> fail(Response.ErrorType type, String code, String message) Creates a failed responsestatic <T> Response<T> fail(Response.ErrorType type, Path path, String message) Creates a failed response for the validation of a file pathstatic <T> Response<T> failWithLogging(Response.ErrorType type, String message, Response.ErrorLogging errorLogging) Creates a failed response, and executes a logging call backGet the all errors in this response, regardless of typeGets a list of all the error message for this responsegetMessagesCombined(String delimiter) Gets a combined error message for all the error messages for this response, separated with the provided delimiterGet the other errors in this responseGet the permission errors in this responseGet the result of this response if there are no errors.getResultIfPresentOrElseResult(Supplier<T> supplier) If response has no errors and the result is not null return the result, otherwise return the result from the suppliergetResultIfPresentOrElseResult(T result) If response has no errors and the result is not null return the result, otherwise return the provider result from the supplierGet the result of this response if there are no errors in this response, or throws anResponseFailedExceptionGet the result of this response if there are no errors in this response, otherwise throws the provided exceptiongetResultOrThrow(Function<Response<?>, ? extends RuntimeException> function) Get the result of this response if there are no errors in this response, otherwise obtains an exception from the function to be thrown.getResultOrThrow(Supplier<RuntimeException> supplier) Get the result of this response if there are no errors in this response, otherwise obtains an exception from the supplier to be thrown.Get the validation errors in this responsebooleanDetermines if this response has any errors.booleanDetermines if this response has no errors.<U> Response<U> If response has no errors and the result is not null apply the function to the result, otherwise use the to get the new resourcebooleanisEmpty()Determines if response has no errors and the result is nullbooleanDetermines if response has no errors and the result is not nullCall the supplier and return thisjustDoOnCondition(boolean condition, Supplier<Void> supplier) If the condition istruecall the supplier, otherwise don't call the supplier<U> Response<U> Merges the response from the function if this response has no errors, otherwise create a new response and merges in the errors from this response.<U> Response<U> Merges the response from the supplier if this response has no errors, otherwise create a new response and merges in the errors from this response.Map the errors response of the provided function executed on this response, removing any existing errors on this response<U> Response<U> mapOnCondition(boolean condition, Function<Response<T>, Response<U>> function) If the condition istrueand there are no errors from this response merge the response obtained from the provided function, otherwise create a new response and merge in the errors from this response.<U> Response<U> mapOnCondition(boolean condition, Supplier<Response<U>> supplier) If the condition istrueand there are no errors from this response merge any errors from this response into response obtained from the provided supplier, otherwise create a new response and merge in the errors from this response.<U> Response<U> mapResponseOnCondition(boolean condition, Function<T, Response<U>> function) If the condition istruemerges any errors from this response into response obtained from the provided function, otherwise create a new response and merges in the errors from this response.<U> Response<U> If this response has no errors returns a new response that takes the result of provided function that takes the result of this resource as an input, otherwise create a new response and merges in the errors from this response.mapResultErrors(Function<T, Response<C>> function) If this response has no errors it maps result of the provided function executed on the result on this response<U> Response<U> mapResultOnCondition(boolean condition, Function<T, U> function) If condition istrueand this response has no errors returns a new response that takes the result of provided function that takes the result of this resource as an input, otherwise create a new response and merges in the errors from this response.<U> Response<U> mapResultToResponse(Function<T, Response<U>> function) If this response has no errors use the function to get the new response<U> Response<U> Merges any errors from this response into response obtained from the provider supplier, the result from this response is lost.<U> Response<U> Merges any errors from this response into provided response, the result from this response is lost.<U> Response<U> merge(Validation validation) Merges any errors from the validation,mergeErrors(Supplier<Response<C>> supplier) Merge the errors from the provided response supplier into this responsemergeErrors(Response<?> response) Merge the errors from the provided response into this response<U> Response<U> mergeOnCondition(boolean condition, Supplier<Response<U>> supplier) If the condition istruemerges any errors from this response into response obtained from the provided supplier, the result from this response is lost.If this response has errors run the action and return thisonFailDoWithResponse(Consumer<Response<T>> consumer) If this response has no errors pass this response to the provider consumeronSuccessDo(Runnable action) If this response has no errors run the action and return thisonSuccessDo(Consumer<Response<T>> consumer) If this response has no errors pass this response to the provider consumeronSuccessDo(Supplier<Void> supplier) If this response has no errors call the supplier and return thisonSuccessDoOnCondition(boolean condition, Runnable action) If the condition istrueand this response has no errors call run the action and return thisonSuccessDoOnCondition(Predicate<T> predicate, Runnable action) If the predicate returnstrueand this response has no errors call run the action and return thisonSuccessDoOnCondition(Predicate<T> predicate, Supplier<Void> supplier) If the predicate returnstrueand this response has no errors call the supplier and return thisonSuccessDoWithResult(Consumer<T> consumer) If this response has no errors pass the result of this response to the provider consumeronSuccessDoWithResultOnCondition(boolean condition, Consumer<T> consumer) If the condition istrueand this response has no errors pass the result of this response to the provider consumerReturns this response if this response has no errors, otherwise create a new response and merges in the errors from this response.Returns this response if it has no errors, otherwise returns the provided response from the supplierReturns this response if it has no errors, otherwise returns the provided responseorElseGetResult(Supplier<T> supplier) Get the result of this response if there are no errors in this response, otherwise returns supplier is used to provide resultorElseResult(T result) Get the result of this response if there are no errors in this response, otherwise returns the provided resultstatic <T> Response<T> success(T result) Creates a successful response for the provided resulttoList()toSet()<U> Response<U> withResult(Supplier<U> supplier) If this response has no errors creates a new Response with the result from the supplier, otherwise a new Response with the errors from this resource.<RESULT> Response<RESULT> withResult(RESULT result) If this response has no errors creates a new Response with the provided result, otherwise a new Response with the errors from this resource.
-
Method Details
-
success
Creates a successful response for the provided result- Type Parameters:
T- The type of the result- Parameters:
result- the result- Returns:
- a successful response for the provided result
-
empty
Creates an empty response- Type Parameters:
T- The type of the result- Returns:
- a empty response
-
fail
Creates a failed response- Type Parameters:
T- The type of the result- Parameters:
type- The type of errorcode- The error codemessage- The error message- Returns:
- an empty response with the added error
-
fail
Creates a failed response- Type Parameters:
T- The type of the result- Parameters:
type- The type of errormessage- The error message- Returns:
- an empty response with the added error
-
fail
Creates a failed response for the validation of a file path- Type Parameters:
T- The type of the result- Parameters:
type- The type of errorpath- the path of the file being validatedmessage- The error message- Returns:
- an empty response with the added error
-
failWithLogging
public static <T> Response<T> failWithLogging(Response.ErrorType type, String message, Response.ErrorLogging errorLogging) Creates a failed response, and executes a logging call back- Type Parameters:
T- The type of the result- Parameters:
type- The type of errormessage- The error messageerrorLogging- the error logging functional interface- Returns:
- an empty response with the added error
-
mergeErrors
Merge the errors from the provided response into this response- Parameters:
response- the provided response which is the source of the errors- Returns:
- this response
-
mergeErrors
Merge the errors from the provided response supplier into this response- Type Parameters:
C- the result type of the output response for the provided supplier- Parameters:
supplier- the supplier which providers the response that is the source of the errors- Returns:
- this response
-
mapErrors
Map the errors response of the provided function executed on this response, removing any existing errors on this response- Type Parameters:
C- the result type of the output response for the provided function- Parameters:
function- the function which providers the response that is the source of the errors- Returns:
- this response
-
mapResultErrors
If this response has no errors it maps result of the provided function executed on the result on this response- Type Parameters:
C- the result type of the output response for the provided function- Parameters:
function- the function which takes the result of this and returns a new response- Returns:
- this response
-
getResult
Get the result of this response if there are no errors. If there are errors aRuntimeExceptionis thrown- Returns:
- the result of this response if there are no errors
-
orElseResult
Get the result of this response if there are no errors in this response, otherwise returns the provided result- Parameters:
result- the result that is return if there are errors in this response- Returns:
- the result of this response if there are no errors in this response or the provided result
-
orElseGetResult
Get the result of this response if there are no errors in this response, otherwise returns supplier is used to provide result- Parameters:
supplier- the supplier that provides the result if there are errors in this response- Returns:
- the result of this response if there are no errors in this response or the result from the supplier
-
getResultOrThrow
Get the result of this response if there are no errors in this response, otherwise throws the provided exception- Parameters:
ex- the exception to be thrown if there are errors in this response- Returns:
- the result of this response if there are no errors in this response
- Throws:
Exception- the provided exception if there are errors
-
getResultOrThrow
Get the result of this response if there are no errors in this response, otherwise obtains an exception from the supplier to be thrown.- Parameters:
supplier- the supplier exception to be used if there are errors in this response- Returns:
- the result of this response if there are no errors in this response
-
getResultOrThrow
Get the result of this response if there are no errors in this response, otherwise obtains an exception from the function to be thrown.- Parameters:
function- the function use to create the exception to be thrown if there are errors in this response- Returns:
- the result of this response if there are no errors in this response
-
getResultOrThrow
Get the result of this response if there are no errors in this response, or throws anResponseFailedException- Returns:
- the result of this response if there are no errors in this response
-
hasErrors
public boolean hasErrors()Determines if this response has any errors.- Returns:
trueif this response has any errors,falseotherwise
-
hasNoErrors
public boolean hasNoErrors()Determines if this response has no errors.- Returns:
trueif this response has no errors,falseotherwise
-
merge
Merges any errors from this response into provided response, the result from this response is lost.- Type Parameters:
U- the result type of the provided response- Parameters:
response- the response used as a source of errors.- Returns:
- the provided response
-
merge
Merges any errors from this response into response obtained from the provider supplier, the result from this response is lost.- Type Parameters:
U- the result type of the provided response- Parameters:
responseSupplier- the supplier used to provide the response- Returns:
- the provided response from the supplier
-
merge
Merges any errors from the validation,- Type Parameters:
U- the result type of the provided validation- Parameters:
validation- the validation used as a source of errors.- Returns:
- the provided response
-
mergeOnCondition
If the condition istruemerges any errors from this response into response obtained from the provided supplier, the result from this response is lost.- Type Parameters:
U- the result type of the provided response- Parameters:
condition- set totrueto perform the merge,falsenot to perform the mergesupplier- a supplier of the provided response- Returns:
- the provided response from the supplier
-
mapOnCondition
public <U> Response<U> mapOnCondition(boolean condition, Function<Response<T>, Response<U>> function) If the condition istrueand there are no errors from this response merge the response obtained from the provided function, otherwise create a new response and merge in the errors from this response. In either case the result from this response is lost.- Type Parameters:
U- the result type of the provided response- Parameters:
condition- set totrueto perform the mapping,falsenot to perform the mappingfunction- a function that takes this response and provides a new response- Returns:
- the provided response from the function if the condition is
trueand there are no errors from this response or a new response
-
mapOnCondition
If the condition istrueand there are no errors from this response merge any errors from this response into response obtained from the provided supplier, otherwise create a new response and merge in the errors from this response. In either case the result from this response is lost.- Type Parameters:
U- the result type of the provided response- Parameters:
condition- set totrueto perform the merge,falsenot to perform the mergesupplier- a supplier that provides a new response- Returns:
- the provided response from the supplier
-
mapResponseOnCondition
If the condition istruemerges any errors from this response into response obtained from the provided function, otherwise create a new response and merges in the errors from this response. In either case the result from this response is lost.- Type Parameters:
U- the result type of the provided response- Parameters:
condition- set totrueto perform the merge,falsenot to perform the mergefunction- a function that takes this response and provides a new response- Returns:
- the provided response from the function
-
map
Merges the response from the supplier if this response has no errors, otherwise create a new response and merges in the errors from this response. In either case the result from this response is lost.- Type Parameters:
U- the result type of the provided response- Parameters:
supplier- a supplier that provides a new response- Returns:
- the provided response from the supplier
-
map
Merges the response from the function if this response has no errors, otherwise create a new response and merges in the errors from this response. In either case the result from this response is lost.- Type Parameters:
U- the result type of the provided response- Parameters:
function- a function that takes this response and provides a new response- Returns:
- the provided response from the function
-
or
Returns this response if it has no errors, otherwise returns the provided response from the supplier- Parameters:
supplier- a supplier that provides a new response- Returns:
- this response if it has no errors, otherwise returns the provided response from the supplier
-
or
Returns this response if this response has no errors, otherwise create a new response and merges in the errors from this response. In either case the result from this response is lost.- Parameters:
function- a function that takes this response and provides a new response- Returns:
- the provided response from the function
-
orElse
Returns this response if it has no errors, otherwise returns the provided response- Parameters:
response- a new response- Returns:
- this response if it has no errors, otherwise returns the provided response
-
mapResult
If this response has no errors returns a new response that takes the result of provided function that takes the result of this resource as an input, otherwise create a new response and merges in the errors from this response.- Type Parameters:
U- the result type of the new response- Parameters:
function- a function that takes the result of this response as an input- Returns:
- a new response with result of the function, or with any merged errors
-
mapResultOnCondition
If condition istrueand this response has no errors returns a new response that takes the result of provided function that takes the result of this resource as an input, otherwise create a new response and merges in the errors from this response.- Type Parameters:
U- the result type of the new response- Parameters:
condition- set totrueto perform the merge,falsenot to perform the mergefunction- a function that takes the result of this response as an input- Returns:
- a new response with result of the function, or with any merged errors
-
withResult
If this response has no errors creates a new Response with the provided result, otherwise a new Response with the errors from this resource.- Type Parameters:
RESULT- the result type of the new response- Parameters:
result- the new result- Returns:
- the new Response
-
withResult
If this response has no errors creates a new Response with the result from the supplier, otherwise a new Response with the errors from this resource.- Type Parameters:
U- the result type of the new response- Parameters:
supplier- a supplier for this new result- Returns:
- the new Response
-
conditionalMapResult
If the condition istrueand this response has no errors creates a new Response with the result from the function, otherwise a new Response with the errors from this resource.- Parameters:
condition-truemap the result,falsedo not map the resultfunction- that takes the result from this response and returns a new result of the same type- Returns:
- a new Response or returns this
-
conditionalMapResultToResponse
public Response<T> conditionalMapResultToResponse(Predicate<T> predicate, Supplier<Response<T>> supplier) If the predicate returnstrueand this response has no errors creates a new Response with the result from the function, otherwise a new Response with the errors from this resource.- Parameters:
predicate- if the predicate returnstruemap the result,falsedo not map the resultsupplier- that provides a new result of the same type- Returns:
- a new Response or returns this
-
conditionalMapResultToResponse
public Response<T> conditionalMapResultToResponse(Predicate<T> predicate, Function<T, Response<T>> function) If the predicate returnstrueand this response has no errors creates a new Response with the result from the function, otherwise a new Response with the errors from this resource.- Parameters:
predicate- if the predicate returnstruemap the result,falsedo not map the resultfunction- that takes the result from this response and returns a new result of the same type- Returns:
- a new Response or returns this
-
conditionalMapResultToResponse
public Response<T> conditionalMapResultToResponse(boolean condition, Supplier<Response<T>> supplier) If the condition istrueand this response has no errors use the supplier to get the new response- Parameters:
condition-truemap the result,falsedo not map the resultsupplier- that providers a new Response- Returns:
- a new Response or returns this
-
conditionalMapResultToResponse
public Response<T> conditionalMapResultToResponse(boolean condition, Function<T, Response<T>> function) If the condition istrueand this response has no errors use the function to get the new response- Parameters:
condition-truemap the result,falsedo not map the resultfunction- a function that providers a new Response taking the result of this response as input- Returns:
- a new Response or returns this
-
mapResultToResponse
If this response has no errors use the function to get the new response- Type Parameters:
U- the result type of the new response- Parameters:
function- a function that providers a new Response taking the result of this response as input- Returns:
- a new Response or returns this
-
justDo
Call the supplier and return this- Parameters:
supplier- the supplier to be called.- Returns:
- this response
-
justDoOnCondition
If the condition istruecall the supplier, otherwise don't call the supplier- Parameters:
condition-truecall the supplier,falsedon't call the suppliersupplier- the supplier to be called if the condition istrue- Returns:
- this response
-
onSuccessDo
If this response has no errors pass this response to the provider consumer- Parameters:
consumer- a consumer for this response- Returns:
- this response
-
onSuccessDo
If this response has no errors call the supplier and return this- Parameters:
supplier- the supplier to be called if there are no errors- Returns:
- this response
-
onSuccessDo
If this response has no errors run the action and return this- Parameters:
action- the action to be run if there are no errors- Returns:
- this response
-
onSuccessDoOnCondition
If the predicate returnstrueand this response has no errors call the supplier and return this- Parameters:
predicate- if the predicate returnstruecall the supplier,falsedo not call the suppliersupplier- the supplier to be called if the predicate returnstrueand there are no errors- Returns:
- this response
-
onSuccessDoOnCondition
If the predicate returnstrueand this response has no errors call run the action and return this- Parameters:
predicate- if the predicate returnstruerun the action,falsedo not run the actionaction- the action to be run if the predicate returnstrueand there are no errors- Returns:
- this response
-
onSuccessDoOnCondition
If the condition istrueand this response has no errors call run the action and return this- Parameters:
condition- if istruerun the action,falsedo not run the actionaction- the action to be run if the predicate returnstrueand there are no errors- Returns:
- this response
-
onSuccessDoWithResult
If this response has no errors pass the result of this response to the provider consumer- Parameters:
consumer- a consumer for the result of this response- Returns:
- this response
-
onSuccessDoWithResultOnCondition
If the condition istrueand this response has no errors pass the result of this response to the provider consumer- Parameters:
condition- if istruerun the action,falsedo not run the actionconsumer- a consumer for the result of this response- Returns:
- this response
-
onFailDo
If this response has errors run the action and return this- Parameters:
action- the action to be run if there are errors- Returns:
- this response
-
onFailDoWithResponse
If this response has no errors pass this response to the provider consumer- Parameters:
consumer- a consumer for this response- Returns:
- this response
-
isPresent
public boolean isPresent()Determines if response has no errors and the result is not null- Returns:
- If response has no errors and the result is not null
-
isEmpty
public boolean isEmpty()Determines if response has no errors and the result is null- Returns:
- If response has no errors and the result is null
-
getResultIfPresentOrElseResult
If response has no errors and the result is not null return the result, otherwise return the result from the supplier- Parameters:
supplier- the supplier to be used if response has no errors and the result is not null- Returns:
- the result from this response or from the supplier
-
getResultIfPresentOrElseResult
If response has no errors and the result is not null return the result, otherwise return the provider result from the supplier- Parameters:
result- the result to returned if response has no errors and the result for this response is not null- Returns:
- the result from this response or from the supplier
-
ifPresentMapResultToResponseOr
public <U> Response<U> ifPresentMapResultToResponseOr(Function<T, Response<U>> function, Supplier<Response<U>> supplier) If response has no errors and the result is not null apply the function to the result, otherwise use the to get the new resource- Type Parameters:
U- the result type of the new response- Parameters:
function- a function that takes the result of this response to create a new responsesupplier- a supplier that provides a new response- Returns:
- a new response from the function or supplier
-
addError
Adds an error to the response.- Parameters:
type- The type of errorerror- The error- Returns:
- this response
-
addError
Adds an error to the response.- Parameters:
type- The type of errorcode- The error codemessage- The error message- Returns:
- this response
-
getValidationErrors
Get the validation errors in this response- Returns:
- the validation errors in this response
-
getPermissionErrors
Get the permission errors in this response- Returns:
- the permission errors in this response
-
getOtherErrors
Get the other errors in this response- Returns:
- the other errors in this response
-
getAllErrors
Get the all errors in this response, regardless of type- Returns:
- the all errors in this response, regardless of type
-
getMessages
Gets a list of all the error message for this response- Returns:
- a list of all the error message for this response
-
getMessagesCombined
Gets a combined error message for all the error messages for this response, separated with the provided delimiter- Parameters:
delimiter- the delimiter to separate the individual error messages in the combined error message- Returns:
- a combined error message for all the error messages for this response, separated with the provided delimiter
-
toList
Combines all the results from a stream of Responses of typeResponseinto a single Response that has a result of typeList<T>, merging any errors from the individual responses into the returned response.- Type Parameters:
T- the type of the result- Returns:
- a single Response that has a result of type
List<T>
-
toSet
Combines all the results a stream of Responses of typeResponseinto a single Response that has a result of typeSet<T>, merging any errors from the individual responses into the returned response.- Type Parameters:
T- the type of the result- Returns:
- a single Response that has a result of type
Set<T>
-
mergeLists
Combines all the results a stream of Responses of typeList<T>into a single Response that has a result of typeList<T>, merging any errors from the individual responses into the returned response.- Type Parameters:
T- the type of the result- Returns:
- a single Response that has a result of
List<T>
-