Module org.jooq
Package org.jooq

Interface ContextConverter<T,U>

All Superinterfaces:
Converter<T,U>, Serializable
All Known Implementing Classes:
AbstractContextConverter, AutoConverter, Converters, DateToLocalDateConverter, DelegatingConverter, IdentityConverter, TimestampToLocalDateTimeConverter, TimeToLocalTimeConverter

public interface ContextConverter<T,U> extends Converter<T,U>
A special type of Converter with alternative from(Object, ConverterContext) and to(Object, ConverterContext) methods.

This special converter type can be used wherever an ordinary Converter is used. jOOQ internal call sites will call the alternative from(Object, ConverterContext) and to(Object, ConverterContext) methods, instead of from(Object) and to(Object), allowing for accessing global Configuration.data() content.

  • Method Details

    • from

      U from(T databaseObject, ConverterContext ctx)
      Read and convert a database object to a user object.

      Implementations that don't support this conversion are expected to override Converter.fromSupported() to indicate lack of support.

      Parameters:
      databaseObject - The database object.
      ctx - The context of this conversion.
      Returns:
      The user object.
    • to

      T to(U userObject, ConverterContext ctx)
      Convert and write a user object to a database object.

      Implementations that don't support this conversion are expected to override Converter.toSupported() to indicate lack of support.

      Parameters:
      userObject - The user object.
      ctx - The context of this conversion.
      Returns:
      The database object.
    • to

      default T to(U userObject)
      Description copied from interface: Converter
      Convert and write a user object to a database object.

      Implementations that don't support this conversion are expected to override Converter.toSupported() to indicate lack of support.

      Specified by:
      to in interface Converter<T,U>
      Parameters:
      userObject - The user object.
      Returns:
      The database object.
    • from

      default U from(T databaseObject)
      Description copied from interface: Converter
      Read and convert a database object to a user object.

      Implementations that don't support this conversion are expected to override Converter.fromSupported() to indicate lack of support.

      Specified by:
      from in interface Converter<T,U>
      Parameters:
      databaseObject - The database object.
      Returns:
      The user object.
    • inverse

      @NotNull default @NotNull ContextConverter<U,T> inverse()
      Inverse this converter.
      Specified by:
      inverse in interface Converter<T,U>
    • andThen

      @NotNull default <X> @NotNull ContextConverter<T,X> andThen(Converter<? super U,X> converter)
      Chain a converter to this converter.
      Specified by:
      andThen in interface Converter<T,U>
    • forArrays

      @NotNull default @NotNull ContextConverter<T[],U[]> forArrays()
      Turn this converter into a converter for arrays.
      Specified by:
      forArrays in interface Converter<T,U>
    • scoped

      @NotNull static <T, U> @NotNull ContextConverter<T,U> scoped(Converter<T,U> converter)
    • from

      @NotNull static <T, U> @NotNull ContextConverter<T,U> from(Class<T> fromType, Class<U> toType, BiFunction<? super T,? super ConverterContext,? extends U> from)
      Construct a new read-only converter from a function.

      The resulting Converter returns false on Converter.toSupported().

      Type Parameters:
      T - the database type
      U - the user type
      Parameters:
      fromType - The database type
      toType - The user type
      from - A function converting from T to U when reading from the database.
      to - A function converting from U to T when writing to the database.
      Returns:
      The converter.
      See Also:
    • to

      @NotNull static <T, U> @NotNull ContextConverter<T,U> to(Class<T> fromType, Class<U> toType, BiFunction<? super U,? super ConverterContext,? extends T> to)
      Construct a new write-only converter from a function.

      The resulting Converter returns false on Converter.fromSupported().

      Type Parameters:
      T - the database type
      U - the user type
      Parameters:
      fromType - The database type
      toType - The user type
      to - A function converting from U to T when writing to the database.
      from - A function converting from T to U when reading from the database.
      Returns:
      The converter.
      See Also:
    • of

      @NotNull static <T, U> @NotNull ContextConverter<T,U> of(Class<T> fromType, Class<U> toType, BiFunction<? super T,? super ConverterContext,? extends U> from, BiFunction<? super U,? super ConverterContext,? extends T> to)
      Construct a new converter from functions.

      The resulting Converter is expected to return true on both Converter.fromSupported() and Converter.toSupported().

      Type Parameters:
      T - the database type.
      U - the user type.
      Parameters:
      fromType - The database type.
      toType - The user type.
      from - A function converting from T to U when reading from the database.
      to - A function converting from U to T when writing to the database.
      Returns:
      The converter.
      See Also:
    • of

      @NotNull static <T, U> @NotNull ContextConverter<T,U> of(Class<T> fromType, Class<U> toType, BiFunction<? super T,? super ConverterContext,? extends U> from, BiFunction<? super U,? super ConverterContext,? extends T> to, boolean fromSupported, boolean toSupported)
      Construct a new converter from functions.
      Type Parameters:
      T - the database type.
      U - the user type.
      Parameters:
      fromType - The database type.
      toType - The user type.
      from - A function converting from T to U when reading from the database.
      to - A function converting from U to T when writing to the database.
      fromSupported - Whether the from function is supported.
      toSupported - Whether the to function is supported.
      Returns:
      The converter.
      See Also:
    • ofNullable

      @NotNull static <T, U> @NotNull ContextConverter<T,U> ofNullable(Class<T> fromType, Class<U> toType, BiFunction<? super T,? super ConverterContext,? extends U> from, BiFunction<? super U,? super ConverterContext,? extends T> to)
      Construct a new converter from functions.

      This works like Converter.of(Class, Class, Function, Function), except that both conversion Functions are decorated with a function that always returns null for null inputs.

      Example:

      
       Converter<String, Integer> converter =
         Converter.ofNullable(String.class, Integer.class, Integer::parseInt, Object::toString);
      
       // No exceptions thrown
       assertNull(converter.from(null));
       assertNull(converter.to(null));
       

      The resulting Converter is expected to return true on both Converter.fromSupported() and Converter.toSupported().

      Type Parameters:
      T - the database type
      U - the user type
      Parameters:
      fromType - The database type
      toType - The user type
      from - A function converting from T to U when reading from the database.
      to - A function converting from U to T when writing to the database.
      Returns:
      The converter.
      See Also:
    • ofNullable

      @NotNull static <T, U> @NotNull ContextConverter<T,U> ofNullable(Class<T> fromType, Class<U> toType, BiFunction<? super T,? super ConverterContext,? extends U> from, BiFunction<? super U,? super ConverterContext,? extends T> to, boolean fromSupported, boolean toSupported)
      Construct a new converter from functions.

      This works like Converter.of(Class, Class, Function, Function), except that both conversion Functions are decorated with a function that always returns null for null inputs.

      Example:

      
       Converter<String, Integer> converter =
         Converter.ofNullable(String.class, Integer.class, Integer::parseInt, Object::toString);
      
       // No exceptions thrown
       assertNull(converter.from(null));
       assertNull(converter.to(null));
       
      Type Parameters:
      T - the database type
      U - the user type
      Parameters:
      fromType - The database type
      toType - The user type
      from - A function converting from T to U when reading from the database.
      to - A function converting from U to T when writing to the database.
      fromSupported - Whether the from function is supported.
      toSupported - Whether the to function is supported.
      Returns:
      The converter.
      See Also:
    • fromNullable

      @NotNull static <T, U> @NotNull Converter<T,U> fromNullable(Class<T> fromType, Class<U> toType, BiFunction<? super T,? super ConverterContext,? extends U> from)
      Construct a new read-only converter from a function.

      This works like Converter.from(Class, Class, Function), except that the conversion Function is decorated with a function that always returns null for null inputs.

      Example:

      
       Converter<String, Integer> converter =
         Converter.fromNullable(String.class, Integer.class, Integer::parseInt);
      
       // No exceptions thrown
       assertNull(converter.from(null));
       

      The resulting Converter returns false on Converter.toSupported().

      Type Parameters:
      T - the database type.
      U - the user type.
      Parameters:
      fromType - The database type.
      toType - The user type.
      from - A function converting from T to U when reading from the database.
      Returns:
      The converter.
      See Also:
    • toNullable

      @NotNull static <T, U> @NotNull Converter<T,U> toNullable(Class<T> fromType, Class<U> toType, BiFunction<? super U,? super ConverterContext,? extends T> to)
      Construct a new write-only converter from a function.

      This works like Converter.to(Class, Class, Function), except that the conversion Function is decorated with a function that always returns null for null inputs.

      Example:

      
       Converter<String, Integer> converter =
         Converter.toNullable(String.class, Integer.class, Object::toString);
      
       // No exceptions thrown
       assertNull(converter.to(null));
       

      The resulting Converter returns false on Converter.fromSupported().

      Type Parameters:
      T - the database type
      U - the user type
      Parameters:
      fromType - The database type
      toType - The user type
      to - A function converting from U to T when writing to the database.
      Returns:
      The converter.
      See Also: