Interface ResponseType<R>

Type Parameters:
R - the generic type of this ResponseType to be matched and converted.
All Superinterfaces:
Serializable
All Known Implementing Classes:
AbstractResponseType, InstanceResponseType, MultipleInstancesResponseType, OptionalResponseType, PublisherResponseType

public interface ResponseType<R> extends Serializable
Specifies the expected response type required when performing a query through the QueryBus/QueryGateway. By wrapping the response type as a generic R, we can easily service the expected response as a single instance, a list, a page etc., based on the selected implementation even while the query handler return type might be slightly different.

It is in charge of matching the response type of a query handler with the given generic R. If this match returns true, it signals the found query handler can handle the intended query. As a follow up, the response retrieved from a query handler should move through the convert(Object) function to guarantee the right response type is returned.
Since:
3.2
Author:
Steven van Beelen
See Also:
  • Field Details

    • NO_MATCH

      static final int NO_MATCH
      Indicates that the response type does not match with a given Type.
      See Also:
    • MATCH

      static final int MATCH
      Indicates that the response matches with the Type while returning a single result.
      See Also:
  • Method Details

    • matches

      boolean matches(Type responseType)
      Match the query handler its response Type with the ResponseType implementation its expected response type R. Will return true if a response can be converted based on the given responseType and false if it cannot.
      Parameters:
      responseType - the response Type of the query handler which is matched against
      Returns:
      true if a response can be converted based on the given responseType and false if it cannot
    • matchRank

      default Integer matchRank(Type responseType)
      Defines the match and its priority. The greater the value above 0, the better the match. This is particularly useful for MultipleInstancesResponseTypes when there are matches on a both multiple and single instance types. Lists should be given priority for handling.

      Parameters:
      responseType - the response Type of the query handler which is matched against
      Returns:
      NO_MATCH if there is no match, greater than 0 if there is a match. Highest match should win when choosing a query handler.
    • convert

      default R convert(Object response)
      Converts the given response of type Object into the type R of this ResponseType instance. Should only be called if matches(Type) returns true. It is unspecified what this function does if the matches(Type) returned false.
      Parameters:
      response - the Object to convert into R
      Returns:
      a response of type R
    • convertExceptional

      default Optional<R> convertExceptional(Throwable e)
      Converts the given Throwable into the type R of this ResponseType instance. Used when an error is represented as the message payload. The Optional is not empty when an error is represented as the message payload.
      Returns:
      a Optional response of type R
    • responseMessagePayloadType

      Class<R> responseMessagePayloadType()
      Returns a Class representing the type of the payload to be contained in the response message.
      Returns:
      a Class representing the type of the payload to be contained in the response message
    • getExpectedResponseType

      Class<?> getExpectedResponseType()
      Gets actual response type or generic placeholder.
      Returns:
      actual response type or generic placeholder
    • forSerialization

      default ResponseType<?> forSerialization()
      Returns the ResponseType instance that should be used when serializing responses. This method has a default implementation that returns this. Implementations that describe a Response Type that is not suited for serialization, should return an alternative that is suitable, and ensure the convert(Object) is capable of converting that type of response to the request type in this instance.
      Returns:
      a ResponseType instance describing a type suitable for serialization