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

      <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 generic type
      Parameters:
      object - The object to convert
      targetType - The target type
      context - The conversion context
      Returns:
      The optional
    • 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 <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