Class ConfigurableTypeConverter

java.lang.Object
org.litebridge.convert.ConfigurableTypeConverter
All Implemented Interfaces:
TypeConverter
Direct Known Subclasses:
DefaultTypeConverter

public class ConfigurableTypeConverter extends Object implements TypeConverter
A concrete implementation of TypeConverter that allows manual registration and unregistration of converters.

This class provides a flexible way to manage Converter instances for both Java types and SQL types.

  • Constructor Details

    • ConfigurableTypeConverter

      public ConfigurableTypeConverter()
  • Method Details

    • convert

      public @Nullable Object convert(@Nullable Object value, int dbDataType)
      Converts a value to a database-specific representation (or vice-versa) based on the Types code.
      Specified by:
      convert in interface TypeConverter
      Parameters:
      value - the value to convert, may be null
      dbDataType - the Types code for the database data type
      Returns:
      the converted value, or null if the input was null
      Throws:
      IllegalArgumentException - if no converter is found for the specified SQL type
    • convert

      public <T> @Nullable T convert(@Nullable Object value, Class<T> type)
      Converts a value to a specific Java type.
      Specified by:
      convert in interface TypeConverter
      Type Parameters:
      T - the target Java type
      Parameters:
      value - the value to convert, may be null
      type - the target Java type
      Returns:
      the converted value, or null if the input was null
      Throws:
      IllegalArgumentException - if no converter is found for the specified Java type
    • getSqlDataType

      public int getSqlDataType(Class<?> fieldType) throws IllegalArgumentException
      Description copied from interface: TypeConverter
      Determines the SQL data type corresponding to the provided Java class.
      Specified by:
      getSqlDataType in interface TypeConverter
      Parameters:
      fieldType - the Java Class object representing the field type to be mapped to a SQL data type
      Returns:
      an integer representing the SQL data type, typically a constant defined in Types
      Throws:
      IllegalArgumentException - if no SQL data type mapping is found for the given Java class
    • getClassForSqlType

      public Class<?> getClassForSqlType(int sqlDataType) throws IllegalArgumentException
      Description copied from interface: TypeConverter
      Retrieves the Java Class corresponding to the specified SQL data type.
      Specified by:
      getClassForSqlType in interface TypeConverter
      Parameters:
      sqlDataType - the database-specific SQL data type as an integer code, typically a constant defined in Types
      Returns:
      the Java Class that represents the SQL data type, or null if no mapping is found
      Throws:
      IllegalArgumentException - if no class mapping is found for the given SQL data type
    • register

      public void register(Converter<?> converter)
      Registers a new converter.
      Parameters:
      converter - the converter to register
    • register

      public <T> void register(Class<T> type, ConverterFunction<T> converterFunction)
      Registers a converter for a specific Java type using a functional interface.
      Type Parameters:
      T - the target Java type
      Parameters:
      type - the target Java type
      converterFunction - the conversion logic
    • register

      public <T> void register(Class<T> type, int[] sqlTypes, ConverterFunction<T> converterFunction)
      Registers a converter for a specific Java type and its associated SQL types using a functional interface.
      Type Parameters:
      T - the target Java type
      Parameters:
      type - the target Java type
      sqlTypes - an array of Types codes associated with this converter
      converterFunction - the conversion logic
    • unregister

      public void unregister(Class<?> type)
      Removes a converter for a specific Java type.
      Parameters:
      type - the Java type to unregister
    • unregister

      public void unregister(int sqlType)
      Removes a converter for a specific SQL type.
      Parameters:
      sqlType - the Types code to unregister