Class ChainedConverter<S,T>

java.lang.Object
org.axonframework.conversion.ChainedConverter<S,T>
Type Parameters:
S - The source type of the converter.
T - The target type of the converter.
All Implemented Interfaces:
ContentTypeConverter<S,T>

public class ChainedConverter<S,T> extends Object implements ContentTypeConverter<S,T>
A ContentTypeConverter implementation that delegates to a chain of other ContentTypeConverters to convert from a source to a target for which there is not necessarily a single converter available.
Since:
2.0.0
Author:
Allard Buijze
  • Constructor Details

    • ChainedConverter

      public ChainedConverter(@Nonnull List<ContentTypeConverter<?,?>> delegates)
      Creates a new instance that uses the given delegates to form a chain of converters.

      Note that the delegates must for a continuous chain, meaning that each item must produce an object of a type that can be consumed by the next delegate.

      To automatically calculate a route between converters, see calculateChain(source, target, candidates)

      Parameters:
      delegates - The chain of delegates to perform the conversion.
  • Method Details

    • calculateChain

      public static <S, T> ChainedConverter<S,T> calculateChain(@Nonnull Class<S> sourceType, @Nonnull Class<T> targetType, @Nonnull Collection<ContentTypeConverter<?,?>> candidates)
      Returns a ChainedConverter that can convert an object from the given sourceType to the given targetType using a chain formed with the given candidates.

      The returned converter uses some, or all, of the given candidates as delegates.

      Type Parameters:
      S - The source type of the converter.
      T - The target type of the converter.
      Parameters:
      sourceType - The source type of the converter.
      targetType - The target type of the converter.
      candidates - The candidates to form a chain with.
      Returns:
      A converter for the given source and target types.
      Throws:
      ConversionException - if no converter can be created using given candidates.
    • canConvert

      public static <S, T> boolean canConvert(@Nonnull Class<S> sourceContentType, @Nonnull Class<T> targetContentType, @Nonnull List<ContentTypeConverter<?,?>> converters)
      Indicates whether this converter is capable of converting the given sourceContentType into targetContentType, using the given converters.

      When true, it may use any number of the given converters to form a chain.

      Type Parameters:
      S - The content type of the source object.
      T - The content type of the target object.
      Parameters:
      sourceContentType - The content type of the source object.
      targetContentType - The content type of the target object.
      converters - The converters eligible for use.
      Returns:
      true if this ChainedConverter can convert between the given sourceContentType and targetContentType, using the given converters, Otherwise false.
    • expectedSourceType

      @Nonnull public Class<S> expectedSourceType()
      Description copied from interface: ContentTypeConverter
      Returns the expected type of input data for this ContentTypeConverter to ContentTypeConverter.convert(Object).
      Specified by:
      expectedSourceType in interface ContentTypeConverter<S,T>
      Returns:
      The expected type of input data for this ContentTypeConverter to ContentTypeConverter.convert(Object).
    • targetType

      @Nonnull public Class<T> targetType()
      Description copied from interface: ContentTypeConverter
      Returns the type of output for this ContentTypeConverter to ContentTypeConverter.convert(Object) into.
      Specified by:
      targetType in interface ContentTypeConverter<S,T>
      Returns:
      The type of output for this ContentTypeConverter to ContentTypeConverter.convert(Object) into.
    • convert

      @Nullable public T convert(@Nullable S input)
      Description copied from interface: ContentTypeConverter
      Converts the given input object of generic type S into an object of generic type T.
      Specified by:
      convert in interface ContentTypeConverter<S,T>
      Parameters:
      input - The object of generic type S to convert into an object of generic type T.
      Returns:
      The converted version of the given input in type T.