Interface ConversionService<Impl extends ConversionService>

  • Type Parameters:
    Impl - The type
    All Known Implementing Classes:
    DefaultConversionService

    public interface ConversionService<Impl extends ConversionService>
    A service for allowing conversion from one type to another.
    Since:
    1.0
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static ConversionService<?> SHARED
      Deprecated.
      This will be removed in the next major version.
    • Method Summary

      All Methods Instance Methods Abstract Methods Default Methods 
      Modifier and Type Method Description
      <S,​T>
      Impl
      addConverter​(java.lang.Class<S> sourceType, java.lang.Class<T> targetType, TypeConverter<S,​T> typeConverter)
      Adds a type converter.
      <S,​T>
      Impl
      addConverter​(java.lang.Class<S> sourceType, java.lang.Class<T> targetType, java.util.function.Function<S,​T> typeConverter)
      Adds a type converter.
      <S,​T>
      boolean
      canConvert​(java.lang.Class<S> sourceType, java.lang.Class<T> targetType)
      Return whether the given source type is convertible to the given target type.
      default <T> java.util.Optional<T> convert​(java.lang.Object object, ArgumentConversionContext<T> context)
      Attempts to convert the given object to the given target type.
      default <T> java.util.Optional<T> convert​(java.lang.Object object, Argument<T> targetType)
      Attempts to convert the given object to the given target type.
      default <T> java.util.Optional<T> convert​(java.lang.Object object, java.lang.Class<T> targetType)
      Attempts to convert the given object to the given target type.
      <T> java.util.Optional<T> convert​(java.lang.Object object, java.lang.Class<T> targetType, ConversionContext context)
      Attempts to convert the given object to the given target type.
      default <T> T convertRequired​(java.lang.Object value, Argument<T> argument)
      Convert the value to the given type.
      default <T> T convertRequired​(java.lang.Object value, java.lang.Class<T> type)
      Convert the value to the given type.
    • Field Detail

      • SHARED

        @Deprecated
        static final ConversionService<?> SHARED
        Deprecated.
        This will be removed in the next major version. Use an injected ConversionService instead.
        The default shared conversion service.
    • Method Detail

      • addConverter

        <S,​T> Impl addConverter​(java.lang.Class<S> sourceType,
                                      java.lang.Class<T> targetType,
                                      java.util.function.Function<S,​T> typeConverter)
        Adds a type converter.
        Type Parameters:
        S - The source generic type
        T - The target generic type
        Parameters:
        sourceType - The source type
        targetType - The target type
        typeConverter - The type converter
        Returns:
        This conversion service
      • addConverter

        <S,​T> Impl addConverter​(java.lang.Class<S> sourceType,
                                      java.lang.Class<T> targetType,
                                      TypeConverter<S,​T> typeConverter)
        Adds a type converter.
        Type Parameters:
        S - The source generic type
        T - The target generic type
        Parameters:
        sourceType - The source type
        targetType - The target type
        typeConverter - The type converter
        Returns:
        This conversion service
      • convert

        <T> java.util.Optional<T> convert​(java.lang.Object object,
                                          java.lang.Class<T> targetType,
                                          ConversionContext context)
        Attempts to convert the given object to the given target type. If conversion fails or is not possible an empty Optional is returned.
        Type Parameters:
        T - The generic type
        Parameters:
        object - The object to convert
        targetType - The target type
        context - The conversion context
        Returns:
        The optional
      • canConvert

        <S,​T> boolean canConvert​(java.lang.Class<S> sourceType,
                                       java.lang.Class<T> targetType)
        Return whether the given source type is convertible to the given target type.
        Type Parameters:
        S - The generic source type
        T - The target source type
        Parameters:
        sourceType - The source type
        targetType - The target type
        Returns:
        True if it can be converted
      • convert

        default <T> java.util.Optional<T> convert​(java.lang.Object object,
                                                  java.lang.Class<T> targetType)
        Attempts to convert the given object to the given target type. If conversion fails or is not possible an empty Optional is returned.
        Type Parameters:
        T - The generic type
        Parameters:
        object - The object to convert
        targetType - The target type
        Returns:
        The optional
      • convert

        default <T> java.util.Optional<T> convert​(java.lang.Object object,
                                                  Argument<T> targetType)
        Attempts to convert the given object to the given target type. If conversion fails or is not possible an empty Optional is returned.
        Type Parameters:
        T - The generic type
        Parameters:
        object - The object to convert
        targetType - The target type
        Returns:
        The optional
      • convert

        default <T> java.util.Optional<T> convert​(java.lang.Object object,
                                                  ArgumentConversionContext<T> context)
        Attempts to convert the given object to the given target type. If conversion fails or is not possible an empty Optional is returned.
        Type Parameters:
        T - The generic type
        Parameters:
        object - The object to convert
        context - The ArgumentConversionContext
        Returns:
        The optional
      • convertRequired

        @Nullable
        default <T> T convertRequired​(@Nullable
                                      java.lang.Object value,
                                      java.lang.Class<T> type)
        Convert the value to the given type.
        Type Parameters:
        T - The generic type
        Parameters:
        value - The value
        type - The type
        Returns:
        The converted value
        Throws:
        ConversionErrorException - if the value cannot be converted
        Since:
        1.1.4
      • convertRequired

        @Nullable
        default <T> T convertRequired​(@Nullable
                                      java.lang.Object value,
                                      Argument<T> argument)
        Convert the value to the given type.
        Type Parameters:
        T - The generic type
        Parameters:
        value - The value
        argument - The argument
        Returns:
        The converted value
        Throws:
        ConversionErrorException - if the value cannot be converted
        Since:
        1.1.4