Class OptionalResponseType<R>

java.lang.Object
org.axonframework.messaging.responsetypes.AbstractResponseType<Optional<R>>
org.axonframework.messaging.responsetypes.OptionalResponseType<R>
Type Parameters:
R - The response type which will be matched against and converted to
All Implemented Interfaces:
Serializable, ResponseType<Optional<R>>

public class OptionalResponseType<R> extends AbstractResponseType<Optional<R>>
A ResponseType implementation that will match with query handlers which return a single instance of the expected response type, but returns that as an Optional containing the result. If matching succeeds, the ResponseType.convert(Object) function will be called, which will wrap the query handler's response into an Optional.

Note that this ResponseType will declare the same expectations on the Query Result as the ResponseType returned by InstanceResponseType. The difference is that the result provided by this ResponseType is wrapped in an Optional.

Since:
4.2
Author:
Allard Buijze
See Also:
  • Constructor Details

    • OptionalResponseType

      @ConstructorProperties("expectedResponseType") public OptionalResponseType(Class<R> expectedResponseType)
      Instantiate a OptionalResponseType with the given expectedResponseType as the type to be matched against and to which the query response should be converted to.
      Parameters:
      expectedResponseType - the response type which is expected to be matched against and returned
  • Method Details

    • matches

      public boolean matches(Type responseType)
      Match the query handler its response Type with this implementation its responseType R. Will return true if the expected type is assignable to the response type, taking generic types into account.
      Parameters:
      responseType - the response Type of the query handler which is matched against
      Returns:
      true if the response type is assignable to the expected type, taking generic types into account
    • convert

      public Optional<R> convert(Object response)
      Description copied from interface: ResponseType
      Converts the given response of type Object into the type R of this ResponseType instance. Should only be called if ResponseType.matches(Type) returns true. It is unspecified what this function does if the ResponseType.matches(Type) returned false.
      Parameters:
      response - the Object to convert into R
      Returns:
      a response of type R
    • responseMessagePayloadType

      public Class responseMessagePayloadType()
      Description copied from interface: ResponseType
      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
    • forSerialization

      public ResponseType<?> forSerialization()
      Description copied from interface: ResponseType
      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 ResponseType.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
    • toString

      public String toString()
      Overrides:
      toString in class Object