Top Description Methods
org.jooq

public Interface Converter<T, U>

extends Serializable
Known Direct Subinterfaces
org.jooq.ContextConverter
Known Direct Implementers
org.jooq.impl.AbstractConverter
Type Parameters
<T>
The database type - i.e. any type available from SQLDataType
<U>
The user type
Static Imports
org.jooq.Converters.notImplemented, .Converters.nullable

A Converter 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

Note: In order to avoid unwanted side-effects, it is highly recommended (yet not required) for from(Object) and to(Object) to be reciprocal. The two methods are reciprocal, if for all X and Y, it can be said that

Furthermore, it is recommended (yet not required) that

Irrespective of the Converter's encoding of null values above, an implementation must be able to handle null values.

Creating user defined DataTypes

jOOQ provides built in data types through SQLDataType. Users can define their own data types programmatically by calling DataType#asConvertedDataType(Converter) or DataType#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

Modifier and TypeMethod and Description
public default <X> @NotNull Converter<T, X>
andThen(Converter<? super U, X> converter)

Chain a converter to this converter.

public default @NotNull Converter<T[], U[]>
forArrays()

Turn this converter into a converter for arrays.

public U

Returns:

The user object.
from
(T
The database object.
databaseObject
)

Read and convert a database object to a user object.

public static <
the database type
T
,
the user type
U
>
@NotNull Converter<T, U>

Returns:

The converter.
from
(Class<T>
The database type
fromType
,
Class<U>
The user type
toType
,
Function<? super T, ? extends U>
A function converting from T to U when reading from the database.
from
)

Construct a new read-only converter from a function.

public static <
the database type.
T
,
the user type.
U
>
@NotNull Converter<T, U>

Returns:

The converter.
fromNullable
(Class<T>
The database type.
fromType
,
Class<U>
The user type.
toType
,
Function<? super T, ? extends U>
A function converting from T to U when reading from the database.
from
)

Construct a new read-only converter from a function.

public @NotNull Class<T>
fromType()

The database type.

public default @NotNull Converter<U, T>
inverse()

Inverse this converter.

public static <
the database type.
T
,
the user type.
U
>
@NotNull Converter<T, U>

Returns:

The converter.
of
(Class<T>
The database type.
fromType
,
Class<U>
The user type.
toType
,
Function<? super T, ? extends U>
A function converting from T to U when reading from the database.
from
,
Function<? super U, ? extends T>
A function converting from U to T when writing to the database.
to
)

Construct a new converter from functions.

public static <
the database type
T
,
the user type
U
>
@NotNull Converter<T, U>

Returns:

The converter.
ofNullable
(Class<T>
The database type
fromType
,
Class<U>
The user type
toType
,
Function<? super T, ? extends U>
A function converting from T to U when reading from the database.
from
,
Function<? super U, ? extends T>
A function converting from U to T when writing to the database.
to
)

Construct a new converter from functions.

public T

Returns:

The database object.
to
(U
The user object.
userObject
)

Convert and write a user object to a database object.

public static <
the database type
T
,
the user type
U
>
@NotNull Converter<T, U>

Returns:

The converter.
to
(Class<T>
The database type
fromType
,
Class<U>
The user type
toType
,
Function<? super U, ? extends T>
A function converting from U to T when writing to the database.
to
)

Construct a new write-only converter from a function.

public static <
the database type
T
,
the user type
U
>
@NotNull Converter<T, U>

Returns:

The converter.
toNullable
(Class<T>
The database type
fromType
,
Class<U>
The user type
toType
,
Function<? super U, ? extends T>
A function converting from U to T when writing to the database.
to
)

Construct a new write-only converter from a function.

public @NotNull Class<U>
toType()

The user type.

Method Detail

andThenback to summary
public default <X> @NotNull Converter<T, X> andThen(Converter<? super U, X> converter)

Chain a converter to this converter.

Annotations
@NotNull
forArraysback to summary
public default @NotNull Converter<T[], U[]> forArrays()

Turn this converter into a converter for arrays.

Annotations
@NotNull
fromback to summary
public U from(T databaseObject)

Read and convert a database object to a user object.

Parameters
databaseObject:T

The database object.

Returns:U

The user object.

fromback to summary
public static <T, U> @NotNull Converter<T, U> from(Class<T> fromType, Class<U> toType, Function<? super T, ? extends U> from)

Construct a new read-only converter from a function.

Parameters
<T>
the database type
<U>
the user type
fromType:Class<T>

The database type

toType:Class<U>

The user type

from:Function<? super T, ? extends U>

A function converting from T to U when reading from the database.

Returns:@NotNull Converter<T, U>

The converter.

Annotations
@NotNull
See Also
Converter
fromNullableback to summary
public static <T, U> @NotNull Converter<T, U> fromNullable(Class<T> fromType, Class<U> toType, Function<? super T, ? 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));
Parameters
<T>
the database type.
<U>
the user type.
fromType:Class<T>

The database type.

toType:Class<U>

The user type.

from:Function<? super T, ? extends U>

A function converting from T to U when reading from the database.

Returns:@NotNull Converter<T, U>

The converter.

Annotations
@NotNull
See Also
Converter
fromTypeback to summary
public @NotNull Class<T> fromType()

The database type.

Annotations
@NotNull
inverseback to summary
public default @NotNull Converter<U, T> inverse()

Inverse this converter.

Annotations
@NotNull
ofback to summary
public 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.

Parameters
<T>
the database type.
<U>
the user type.
fromType:Class<T>

The database type.

toType:Class<U>

The user type.

from:Function<? super T, ? extends U>

A function converting from T to U when reading from the database.

to:Function<? super U, ? extends T>

A function converting from U to T when writing to the database.

Returns:@NotNull Converter<T, U>

The converter.

Annotations
@NotNull
See Also
Converter
ofNullableback to summary
public 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 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));
Parameters
<T>
the database type
<U>
the user type
fromType:Class<T>

The database type

toType:Class<U>

The user type

from:Function<? super T, ? extends U>

A function converting from T to U when reading from the database.

to:Function<? super U, ? extends T>

A function converting from U to T when writing to the database.

Returns:@NotNull Converter<T, U>

The converter.

Annotations
@NotNull
See Also
Converter
toback to summary
public T to(U userObject)

Convert and write a user object to a database object.

Parameters
userObject:U

The user object.

Returns:T

The database object.

toback to summary
public static <T, U> @NotNull Converter<T, U> to(Class<T> fromType, Class<U> toType, Function<? super U, ? extends T> to)

Construct a new write-only converter from a function.

Parameters
<T>
the database type
<U>
the user type
fromType:Class<T>

The database type

toType:Class<U>

The user type

to:Function<? super U, ? extends T>

A function converting from U to T when writing to the database.

Returns:@NotNull Converter<T, U>

The converter.

Annotations
@NotNull
See Also
Converter
toNullableback to summary
public static <T, U> @NotNull Converter<T, U> toNullable(Class<T> fromType, Class<U> toType, Function<? super U, ? 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));
Parameters
<T>
the database type
<U>
the user type
fromType:Class<T>

The database type

toType:Class<U>

The user type

to:Function<? super U, ? extends T>

A function converting from U to T when writing to the database.

Returns:@NotNull Converter<T, U>

The converter.

Annotations
@NotNull
See Also
Converter
toTypeback to summary
public @NotNull Class<U> toType()

The user type.

Annotations
@NotNull