Class Response<T>

java.lang.Object
org.brapi.schematools.core.response.Response<T>
Type Parameters:
T - The type of result

public class Response<T> extends Object
A generic Response class for functional programming. A successful Response is one that has no errors, whereas a failed Response has errors
  • Method Details

    • success

      public static <T> Response<T> success(T result)
      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

      public static <T> Response<T> empty()
      Creates an empty response
      Type Parameters:
      T - The type of the result
      Returns:
      a empty response
    • fail

      public static <T> Response<T> fail(Response.ErrorType type, String code, String message)
      Creates a failed response
      Type Parameters:
      T - The type of the result
      Parameters:
      type - The type of error
      code - The error code
      message - The error message
      Returns:
      an empty response with the added error
    • fail

      public static <T> Response<T> fail(Response.ErrorType type, String message)
      Creates a failed response
      Type Parameters:
      T - The type of the result
      Parameters:
      type - The type of error
      message - The error message
      Returns:
      an empty response with the added error
    • fail

      public static <T> Response<T> fail(Response.ErrorType type, Path path, String message)
      Creates a failed response for the validation of a file path
      Type Parameters:
      T - The type of the result
      Parameters:
      type - The type of error
      path - the path of the file being validated
      message - 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 error
      message - The error message
      errorLogging - the error logging functional interface
      Returns:
      an empty response with the added error
    • mergeErrors

      public Response<T> mergeErrors(Response<?> response)
      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

      public <C> Response<T> mergeErrors(Supplier<Response<C>> supplier)
      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

      public <C> Response<T> mapErrors(Function<Response<T>,Response<C>> function)
      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

      public <C> Response<T> 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
      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

      public T getResult()
      Get the result of this response if there are no errors. If there are errors a RuntimeException is thrown
      Returns:
      the result of this response if there are no errors
    • orElseResult

      public T orElseResult(T result)
      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

      public T orElseGetResult(Supplier<T> supplier)
      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

      public T getResultOrThrow(Exception ex) throws Exception
      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

      public T 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.
      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

      public T getResultOrThrow(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.
      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

      public T getResultOrThrow()
      Get the result of this response if there are no errors in this response, or throws an ResponseFailedException
      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:
      true if this response has any errors, false otherwise
    • hasNoErrors

      public boolean hasNoErrors()
      Determines if this response has no errors.
      Returns:
      true if this response has no errors, false otherwise
    • merge

      public <U> Response<U> merge(Response<U> response)
      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

      public <U> Response<U> merge(Supplier<Response<U>> responseSupplier)
      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

      public <U> Response<U> merge(Validation validation)
      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

      public <U> Response<U> mergeOnCondition(boolean condition, Supplier<Response<U>> supplier)
      If the condition is true merges 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 to true to perform the merge, false not to perform the merge
      supplier - 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 is true and 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 to true to perform the mapping, false not to perform the mapping
      function - a function that takes this response and provides a new response
      Returns:
      the provided response from the function if the condition is true and there are no errors from this response or a new response
    • mapOnCondition

      public <U> Response<U> mapOnCondition(boolean condition, Supplier<Response<U>> supplier)
      If the condition is true and 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 to true to perform the merge, false not to perform the merge
      supplier - a supplier that provides a new response
      Returns:
      the provided response from the supplier
    • mapResponseOnCondition

      public <U> Response<U> mapResponseOnCondition(boolean condition, Function<T,Response<U>> function)
      If the condition is true merges 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 to true to perform the merge, false not to perform the merge
      function - a function that takes this response and provides a new response
      Returns:
      the provided response from the function
    • map

      public <U> Response<U> map(Supplier<Response<U>> supplier)
      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

      public <U> Response<U> map(Function<Response<T>,Response<U>> function)
      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

      public Response<T> or(Supplier<Response<T>> supplier)
      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

      public Response<T> or(Function<Response<T>,Response<T>> function)
      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

      public Response<T> orElse(Response<T> response)
      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

      public <U> Response<U> mapResult(Function<T,U> function)
      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

      public <U> Response<U> mapResultOnCondition(boolean condition, Function<T,U> function)
      If condition is true and 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 to true to perform the merge, false not to perform the merge
      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
    • withResult

      public <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.
      Type Parameters:
      RESULT - the result type of the new response
      Parameters:
      result - the new result
      Returns:
      the new Response
    • withResult

      public <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.
      Type Parameters:
      U - the result type of the new response
      Parameters:
      supplier - a supplier for this new result
      Returns:
      the new Response
    • conditionalMapResult

      public Response<T> conditionalMapResult(boolean condition, UnaryOperator<T> function)
      If the condition is true and 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 - true map the result, false do not map the result
      function - 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 returns true and 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 returns true map the result, false do not map the result
      supplier - 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 returns true and 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 returns true map the result, false do not map the result
      function - 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 is true and this response has no errors use the supplier to get the new response
      Parameters:
      condition - true map the result, false do not map the result
      supplier - 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 is true and this response has no errors use the function to get the new response
      Parameters:
      condition - true map the result, false do not map the result
      function - a function that providers a new Response taking the result of this response as input
      Returns:
      a new Response or returns this
    • mapResultToResponse

      public <U> Response<U> mapResultToResponse(Function<T,Response<U>> function)
      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

      public Response<T> justDo(Supplier<Void> supplier)
      Call the supplier and return this
      Parameters:
      supplier - the supplier to be called.
      Returns:
      this response
    • justDoOnCondition

      public Response<T> justDoOnCondition(boolean condition, Supplier<Void> supplier)
      If the condition is true call the supplier, otherwise don't call the supplier
      Parameters:
      condition - true call the supplier, false don't call the supplier
      supplier - the supplier to be called if the condition is true
      Returns:
      this response
    • onSuccessDo

      public Response<T> onSuccessDo(Consumer<Response<T>> consumer)
      If this response has no errors pass this response to the provider consumer
      Parameters:
      consumer - a consumer for this response
      Returns:
      this response
    • onSuccessDo

      public Response<T> onSuccessDo(Supplier<Void> supplier)
      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

      public Response<T> onSuccessDo(Runnable action)
      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

      public Response<T> onSuccessDoOnCondition(Predicate<T> predicate, Supplier<Void> supplier)
      If the predicate returns true and this response has no errors call the supplier and return this
      Parameters:
      predicate - if the predicate returns true call the supplier, false do not call the supplier
      supplier - the supplier to be called if the predicate returns true and there are no errors
      Returns:
      this response
    • onSuccessDoOnCondition

      public Response<T> onSuccessDoOnCondition(Predicate<T> predicate, Runnable action)
      If the predicate returns true and this response has no errors call run the action and return this
      Parameters:
      predicate - if the predicate returns true run the action, false do not run the action
      action - the action to be run if the predicate returns true and there are no errors
      Returns:
      this response
    • onSuccessDoOnCondition

      public Response<T> onSuccessDoOnCondition(boolean condition, Runnable action)
      If the condition is true and this response has no errors call run the action and return this
      Parameters:
      condition - if is true run the action, false do not run the action
      action - the action to be run if the predicate returns true and there are no errors
      Returns:
      this response
    • onSuccessDoWithResult

      public Response<T> onSuccessDoWithResult(Consumer<T> consumer)
      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

      public Response<T> onSuccessDoWithResultOnCondition(boolean condition, Consumer<T> consumer)
      If the condition is true and this response has no errors pass the result of this response to the provider consumer
      Parameters:
      condition - if is true run the action, false do not run the action
      consumer - a consumer for the result of this response
      Returns:
      this response
    • onFailDo

      public Response<T> onFailDo(Runnable action)
      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

      public Response<T> onFailDoWithResponse(Consumer<Response<T>> consumer)
      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

      public T getResultIfPresentOrElseResult(Supplier<T> supplier)
      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

      public T getResultIfPresentOrElseResult(T result)
      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 response
      supplier - a supplier that provides a new response
      Returns:
      a new response from the function or supplier
    • addError

      public Response<T> addError(Response.ErrorType type, Response.Error error)
      Adds an error to the response.
      Parameters:
      type - The type of error
      error - The error
      Returns:
      this response
    • addError

      public Response<T> addError(Response.ErrorType type, String code, String message)
      Adds an error to the response.
      Parameters:
      type - The type of error
      code - The error code
      message - The error message
      Returns:
      this response
    • getValidationErrors

      public Collection<Response.Error> getValidationErrors()
      Get the validation errors in this response
      Returns:
      the validation errors in this response
    • getPermissionErrors

      public Collection<Response.Error> getPermissionErrors()
      Get the permission errors in this response
      Returns:
      the permission errors in this response
    • getOtherErrors

      public Collection<Response.Error> getOtherErrors()
      Get the other errors in this response
      Returns:
      the other errors in this response
    • getAllErrors

      public Collection<Response.Error> getAllErrors()
      Get the all errors in this response, regardless of type
      Returns:
      the all errors in this response, regardless of type
    • getMessages

      public List<String> getMessages()
      Gets a list of all the error message for this response
      Returns:
      a list of all the error message for this response
    • getMessagesCombined

      public String getMessagesCombined(String delimiter)
      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

      public static <T> Collector<Response<T>,Response<List<T>>,Response<List<T>>> toList()
      Combines all the results from a stream of Responses of type Response into a single Response that has a result of type List<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

      public static <T> Collector<Response<T>,Response<Set<T>>,Response<Set<T>>> toSet()
      Combines all the results a stream of Responses of typeResponse into a single Response that has a result of type Set<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

      public static <T> Collector<Response<List<T>>,Response<List<T>>,Response<List<T>>> mergeLists()
      Combines all the results a stream of Responses of type List<T>into a single Response that has a result of type List<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>