-
- Type Parameters:
T
- The database type - i.e. any type available fromSQLDataType
U
- The user type
- All Superinterfaces:
Serializable
- All Known Implementing Classes:
AbstractConverter
,Converters
,DateToLocalDateConverter
,DelegatingConverter
,EnumConverter
,IdentityConverter
,JPAConverter
,TimestampToLocalDateTimeConverter
,TimeToLocalTimeConverter
public interface Converter<T,U> extends Serializable
AConverter
for data types.A general data type conversion interface that can be provided to jOOQ at various places in order to perform custom data type conversion. Conversion is directed, this means that the
Converter
is used- to load database types converting them to user types "FROM" the database.
Hence,
fromType()
is the type as defined in the database. - to store user types converting them to database types "TO" the database.
Hence,
toType()
is the user-defined type
Note: In order to avoid unwanted side-effects, it is highly recommended (yet not required) for
from(Object)
andto(Object)
to be reciprocal. The two methods are reciprocal, if for allX and Y
, it can be said that- if
Y.equals(converter.from(X))
, thenX.equals(converter.to(Y))
. X.equals(converter.from(converter.to(X)))
X.equals(converter.to(converter.from(X)))
Furthermore, it is recommended (yet not required) that
converter.from(null) == null
converter.to(null) == null
Creating user defined
DataType
sjOOQ provides built in data types through
SQLDataType
. Users can define their own data types programmatically by callingDataType.asConvertedDataType(Converter)
orDataType.asConvertedDataType(Binding)
, for example. Custom data types can also be defined on generated code using the<forcedType/>
configuration, see the manual for more details- Author:
- Lukas Eder
-
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description default <X> @NotNull Converter<T,X>
andThen(Converter<? super U,X> converter)
Chain a converter to this converter.default @NotNull Converter<T[],U[]>
forArrays()
Turn this converter into a converter for arrays.U
from(T databaseObject)
Convert a database object to a user object@NotNull Class<T>
fromType()
The database typedefault @NotNull Converter<U,T>
inverse()
Inverse this converter.static <T,U>
@NotNull Converter<T,U>of(Class<T> fromType, Class<U> toType, Function<? super T,? extends U> from, Function<? super U,? extends T> to)
Construct a new converter from functions.static <T,U>
@NotNull Converter<T,U>ofNullable(Class<T> fromType, Class<U> toType, Function<? super T,? extends U> from, Function<? super U,? extends T> to)
Construct a new converter from functions.T
to(U userObject)
Convert a user object to a database object@NotNull Class<U>
toType()
The user type
-
-
-
Method Detail
-
from
U from(T databaseObject)
Convert a database object to a user object- Parameters:
databaseObject
- The database object- Returns:
- The user object
-
to
T to(U userObject)
Convert a user object to a database object- Parameters:
userObject
- The user object- Returns:
- The database object
-
andThen
@NotNull default <X> @NotNull Converter<T,X> andThen(Converter<? super U,X> converter)
Chain a converter to this converter.
-
forArrays
@NotNull default @NotNull Converter<T[],U[]> forArrays()
Turn this converter into a converter for arrays.
-
of
@NotNull static <T,U> @NotNull Converter<T,U> of(Class<T> fromType, Class<U> toType, Function<? super T,? extends U> from, Function<? super U,? extends T> to)
Construct a new converter from functions.- Type Parameters:
T
- the database typeU
- the user type- Parameters:
fromType
- The database typetoType
- The user typefrom
- A function converting from T to Uto
- A function converting from U to T- Returns:
- The converter.
- See Also:
Converter
-
ofNullable
@NotNull static <T,U> @NotNull Converter<T,U> ofNullable(Class<T> fromType, Class<U> toType, Function<? super T,? extends U> from, Function<? super U,? extends T> to)
Construct a new converter from functions.This works like
of(Class, Class, Function, Function)
, except that both conversionFunction
s are decorated with a function that always returnsnull
fornull
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 typeU
- the user type- Parameters:
fromType
- The database typetoType
- The user typefrom
- A function converting from T to Uto
- A function converting from U to T- Returns:
- The converter.
- See Also:
Converter
-
-