Interface Converter<S,T>

Type Parameters:
S - The source/input type.
T - The target/result type.
All Superinterfaces:
Function<S,T>
All Known Implementing Classes:
AbstractConverter, AbstractNoArgsConverter, AbstractNumberToString, AbstractSequenceConverter, AbstractSequenceToString, AbstractStringToNumber, AbstractStringToSequence, AbstractStringToTemporal, AbstractTemporalToString, AndThenConverter, ArrayToString, BigDecimalToString, BigIntegerToString, BooleanArrayToString, BooleanListToString, BooleanToString, ByteArrayToString, ByteListToString, ByteToString, CharToString, ClassToString, ConverterAdapter, DateToString, DoubleArrayToString, DoubleListToString, DoubleToString, EnumToString, FileToString, FloatArrayToString, FloatListToString, FloatToString, Identity, IntegerArrayToString, IntegerListToString, IntegerToString, ListToString, LocalDateTimeToString, LocalDateToString, LocaleToString, LocalTimeToString, LongArrayToString, LongListToString, LongToString, ObjectToString, OrElseConverter, RawRefConverter, ShortArrayToString, ShortListToString, ShortToString, StringFiller, StringRandomizer, StringToArray, StringToBigDecimal, StringToBigInteger, StringToBoolean, StringToBooleanArray, StringToBooleanList, StringToByte, StringToByteArray, StringToByteList, StringToChar, StringToClass, StringToConverter, StringToDate, StringToDouble, StringToDoubleArray, StringToDoubleList, StringToEnum, StringToFile, StringToFloat, StringToFloatArray, StringToFloatList, StringToInteger, StringToIntegerArray, StringToIntegerList, StringToList, StringToLocalDate, StringToLocalDateTime, StringToLocale, StringToLocalTime, StringToLong, StringToLongArray, StringToLongList, StringToShort, StringToShortArray, StringToShortList, ToLowerCase, ToUpperCase

public interface Converter<S,T> extends Function<S,T>
Base interface of typed converters.

A Converter is a Function with additional properties:

  • The class of source/input type.
  • The class of target/result type.
  • Some configuration parameters.
Author:
Damien Carbonne
  • Field Details

  • Method Details

    • getSourceClass

      Class<S> getSourceClass()
      Returns:
      The class of source objects.
    • getWrappedSourceClass

      default Class<S> getWrappedSourceClass()
      Returns:
      The wrapped type of source class.
      If source class is the class of a primitive type, returns the class of corresponding boxing type.
    • getTargetClass

      Class<T> getTargetClass()
      Returns:
      The class of target objects.
    • getWrappedTargetClass

      default Class<T> getWrappedTargetClass()
      Returns:
      The wrapped type of target class. If target class is the class of a primitive type, returns the class of corresponding boxing type.
    • getParams

      Args getParams()
      Returns:
      The parameters used to configure this converter.
    • applyRaw

      default T applyRaw(Object source)
      Applies this converter to a raw value.
      Parameters:
      source - The source value.
      Returns:
      The converted value.
      Throws:
      ClassCastException - When source is not null and can not be cast to source class.
    • isCompliantSourceClass

      default boolean isCompliantSourceClass(Class<?> cls)
      Returns true when a class is a compliant source class.

      cls must be descendant of source class.

      Parameters:
      cls - The class.
      Returns:
      true when cls is a compliant source class.
    • isCompliantTargetClass

      default boolean isCompliantTargetClass(Class<?> cls)
      Returns true when a class is a compliant target class.

      cls must be ancestor of target class.

      Parameters:
      cls - The class.
      Returns:
      true when cls is a compliant target class.
    • areCompliantSourceAndTargetClasses

      default boolean areCompliantSourceAndTargetClasses(Class<?> sourceClass, Class<?> targetClass)
      Returns true when a source and target classes are compliant.
      Parameters:
      sourceClass - The source class.
      targetClass - The target class.
      Returns:
      true when source and target classes are compliant.
    • isMatchingSourceClass

      default boolean isMatchingSourceClass(Class<?> cls)
      Returns true when a class matches source class.

      cls and source class are the same (after wrapping).

      Parameters:
      cls - The class.
      Returns:
      true when cls matches source class.
    • isMatchingTargetClass

      default boolean isMatchingTargetClass(Class<?> cls)
      Returns true when a class matches target class.

      cls and target class are the same (after wrapping).

      Parameters:
      cls - The class.
      Returns:
      true when cls matches target class.
    • areMatchingSourceAndTargetClasses

      default boolean areMatchingSourceAndTargetClasses(Class<?> sourceClass, Class<?> targetClass)
      Returns true when a source and target classes are matching.
      Parameters:
      sourceClass - The source class.
      targetClass - The target class.
      Returns:
      true when source and target classes are matching.
    • compose

      default <R> Converter<R,T> compose(Converter<R,? extends S> before)
      Returns the composition of this converter applied after another one.
      Type Parameters:
      R - The source type of the other converter.
      Parameters:
      before - The other converter, applied before this one.
      Returns:
      The composition of this converter applied after before.
    • andThen

      default <U> Converter<S,U> andThen(Converter<? super T,U> after)
      Returns the composition if this converter applied before another one.
      Type Parameters:
      U - The target type of the other converter.
      Parameters:
      after - The other converter, applied after this one.
      Returns:
      The composition of this converter applied before after.
    • orElse

      default Converter<S,T> orElse(Converter<? super S,? extends T>... others)
      Returns a converter that first tries to convert using this converter, and falls back to other ones in case of failure.

      The result of the first successful converter is used. If no converter succeeds, an exception is thrown.

      Parameters:
      others - The fallback converters, used if this one fails.
      Returns:
      A converter that first tries to convert using this converter, and falls back to others in case of failure.
    • cast

      default <R, U> Converter<R,U> cast(Class<R> sourceClass, Class<U> targetClass)
      Returns an adaptation of this converter to other source and target classes.
      Type Parameters:
      R - The result source type.
      U - The result target type.
      Parameters:
      sourceClass - The source class.
      targetClass - The target class.
      Returns:
      An adaptation of this converter to sourceClass and targetClass.
      Throws:
      IllegalArgumentException - When no adaptation is possible.
    • composeRaw

      default <R> Converter<R,T> composeRaw(Converter<R,?> before)
      Returns the composition of this converter applied after another one.
      Type Parameters:
      R - The source type of the other converter.
      Parameters:
      before - The other converter, applied before this one.
      Returns:
      The composition of this converter applied after before.
      Throws:
      IllegalArgumentException - When before is null or no adaptation of before is possible.
    • andThenRaw

      default <U> Converter<S,U> andThenRaw(Converter<?,U> after)
      Returns the composition if this converter applied before another one.
      Type Parameters:
      U - The target type of the other converter.
      Parameters:
      after - The other converter, applied after this one.
      Returns:
      The composition of this converter applied before after.
      Throws:
      IllegalArgumentException - When after is null or no adaptation of after is possible.
    • orElseRaw

      default Converter<S,T> orElseRaw(Converter<?,?>... others)
    • adapt

      default <R, U> Converter<R,U> adapt(Class<R> sourceClass, Class<U> targetClass)
      Returns an adaptation of this converter to a specific source and target classes.
      Type Parameters:
      R - The source type.
      U - The target type.
      Parameters:
      sourceClass - The source class.
      targetClass - The target class.
      Returns:
      An adaptation of this converter that matches sourceClass and targetClass.
      Throws:
      IllegalArgumentException - When no adaptation is possible.
    • adapt

      static <S, T> Converter<S,T> adapt(Class<S> sourceClass, Class<T> targetClass, Converter<?,?> delegate)
      Returns an adaptation of a converter to specific source and target classes.
      Type Parameters:
      S - The source type.
      T - The target type.
      Parameters:
      sourceClass - The source class.
      targetClass - The target class.
      delegate - The converter to adapt.
      Returns:
      An adaptation of delegate that matches sourceClass and targetClass.
      Throws:
      IllegalArgumentException - When no adaptation is possible.
    • singleton

      static <C extends Converter<S, T>, S, T> Factory<C> singleton(C instance)