Interface ConversionService

All Known Subinterfaces:
Environment, MutableConversionService
All Known Implementing Classes:
DefaultEnvironment, DefaultMutableConversionService

public interface ConversionService
A service for allowing conversion from one type to another.
Since:
1.0
Author:
Graeme Rocher
  • Field Details

    • SHARED

      static final ConversionService SHARED
      The default shared conversion service.
  • Method Details

    • convert

      default <T> Optional<T> convert(Object object, 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 target type
      Parameters:
      object - The object to convert
      targetType - The target type
      context - The conversion context
      Returns:
      The optional
    • convert

      default <S, T> Optional<T> convert(S object, Class<? super S> sourceType, Class<T> targetType, ConversionContext context)
      Attempts to convert the given object to the given target type from the given source type. If conversion fails or is not possible an empty Optional is returned.
      Type Parameters:
      S - The source type
      T - The target type
      Parameters:
      object - The object to convert
      sourceType - The source type
      targetType - The target type
      context - The conversion context
      Returns:
      The optional
      Since:
      4.2.0
    • canConvert

      <S, T> boolean canConvert(Class<S> sourceType, 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> Optional<T> convert(Object object, 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> Optional<T> convert(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 <S, T> Optional<T> convert(S object, Class<? super S> sourceType, Argument<T> targetType)
      Attempts to convert the given object to the given target type from the given source type. If conversion fails or is not possible an empty Optional is returned.
      Type Parameters:
      S - The source type
      T - The target type
      Parameters:
      object - The object to convert
      sourceType - The source type
      targetType - The target type
      Returns:
      The optional
      Since:
      4.2.0
    • convert

      default <T> Optional<T> convert(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 @Nullable Object value, 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 @Nullable 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
    • convertRequired

      default <T> T convertRequired(Object value, ArgumentConversionContext<T> context)
      Convert the value to the given type.
      Type Parameters:
      T - The generic type
      Parameters:
      value - The value
      context - The conversion context
      Returns:
      The converted value
      Throws:
      ConversionErrorException - if the value cannot be converted
      Since:
      4.1.0