Interface Mapper<T>

Type Parameters:
T - Type of objects the mapper handles.
All Known Subinterfaces:
OneColumnMapper<ObjectT>, PojoMapper<T>

public interface Mapper<T>
Mapper interface defines marshaller methods for mapping class field names to table columns.

Note: Only natively supported types, top-level POJOs, and static nested classes are supported. Anonymous, local, inner classes, interfaces, annotation, etc., cause an exception.

See Also:
ApiNote:
Implementation shouldn't use this interface directly; use PojoMapper or OneColumnMapper instead.
  • Method Summary

    Modifier and Type
    Method
    Description
    static <O> MapperBuilder<O>
    builder(Class<O> type)
    Creates a mapper builder for objects of a given class.
    static <O> Class<O>
    Ensures a class is of natively supported kind and can be used in one-column mapping.
    static boolean
    Checks if a class is of natively supported type.
    static <O> Mapper<O>
    of(Class<O> type)
    Creates a mapper for the specified class.
    static <O> Mapper<O>
    of(Class<O> type, String columnName)
    Creates a mapper for a case where an object represents a single column.
    static <O> Mapper<O>
    of(Class<O> type, String fieldName, String columnName, String... fieldColumnPairs)
    Creates a mapper for a case where object's individual fields map to columns by name.
    static <ObjectT, ColumnT>
    Mapper<ObjectT>
    of(Class<ObjectT> type, String columnName, TypeConverter<ObjectT,ColumnT> converter)
    Creates a mapper for a case where an object represents a single column and an additional transformation is required.
    Returns a type of the object the mapper handles.
  • Method Details

    • of

      static <O> Mapper<O> of(Class<O> type)
      Creates a mapper for the specified class. Natively supported types are mapped to a single schema column, otherwise individual object fields are mapped to columns with the same name.

      Note: Natively supported types can be mapped only to a single key/value column. If a table may have more than one column, the table operation fails with an exception. Use of(Class, String) to map to a specific column name.

      Parameters:
      type - Target type.
      Returns:
      Mapper for key objects that represent a single key column.
      Throws:
      IllegalArgumentException - If type is not supported.
    • of

      static <O> Mapper<O> of(Class<O> type, String columnName)
      Creates a mapper for a case where an object represents a single column.

      The mapper can be used as a key, value, or record mapper. However, a single-column record looks like a degraded case.

      Parameters:
      type - Parametrized object type the mapper handles.
      columnName - Column name to map an object to.
      Returns:
      Mapper for objects that represent a single column.
      Throws:
      IllegalArgumentException - If type is not supported.
    • of

      static <ObjectT, ColumnT> Mapper<ObjectT> of(Class<ObjectT> type, String columnName, TypeConverter<ObjectT,ColumnT> converter)
      Creates a mapper for a case where an object represents a single column and an additional transformation is required.

      The mapper can be used as a key, value, or record mapper. However, a single column record looks as degraded case.

      Type Parameters:
      ObjectT - Mapper target type.
      ColumnT - MUST be a type compatible with the column type.
      Parameters:
      type - Parametrized object type the mapper handles.
      columnName - Column name to map an object to.
      Returns:
      Mapper for objects that represent a single column.
      Throws:
      IllegalArgumentException - If type is not supported.
    • of

      static <O> Mapper<O> of(Class<O> type, String fieldName, String columnName, String... fieldColumnPairs)
      Creates a mapper for a case where object's individual fields map to columns by name.

      The mapper can be used as a key, value, or record mapper.

      Parameters:
      type - Parametrized object type the mapper handles.
      fieldName - Object field name.
      columnName - Column name.
      fieldColumnPairs - Vararg that accepts (fieldName, columnName) pairs.
      Returns:
      Mapper .
      Throws:
      IllegalArgumentException - If a field name has no matching column name in fieldColumnPairs, or if type is not unsupported.
    • builder

      static <O> MapperBuilder<O> builder(Class<O> type)
      Creates a mapper builder for objects of a given class.

      Note: The builder cannot be reused.

      Parameters:
      type - Parametrized object type the mapper handles. Class MUST have a default constructor.
      Returns:
      Mapper builder.
      Throws:
      IllegalArgumentException - If type is not supported.
    • ensureNativelySupported

      static <O> Class<O> ensureNativelySupported(Class<O> type)
      Ensures a class is of natively supported kind and can be used in one-column mapping.
      Parameters:
      type - Class to validate.
      Returns:
      type if it is of a natively supported kind.
      Throws:
      IllegalArgumentException - If type is invalid and cannot be used in one-column mapping.
    • nativelySupported

      static boolean nativelySupported(Class<?> type)
      Checks if a class is of natively supported type.
      Parameters:
      type - Class to check.
      Returns:
      True if the given type is supported natively and can be mapped to a single column.
    • targetType

      Class<T> targetType()
      Returns a type of the object the mapper handles.
      Returns:
      Mapper target type.