Top Description Inners Fields Constructors Methods
org.jooq.tools

public final Class Convert

extends Object
Class Inheritance
Annotations
@Deprecated
forRemoval:true
since:3.15
Static Imports
java.time.temporal.ChronoField.INSTANT_SECONDS, .ChronoField.MILLI_OF_DAY, .ChronoField.MILLI_OF_SECOND, org.jooq.ContextConverter.scoped, org.jooq.impl.Internal.arrayType, .Internal.converterContext, org.jooq.tools.reflect.Reflect.accessible, .Reflect.wrapper, org.jooq.types.Unsigned.ubyte, .Unsigned.uint, .Unsigned.ulong, .Unsigned.ushort

Deprecated

for removal since 3.15.

- 3.15.0 - [#11898] This class will be removed in the future. Do not reuse. Data type conversion happens using internal Converter implementations, or ConverterProvider provided ones, or implementations attached to fields via generated code, or via Field#convert(Converter), or DataType#asConvertedDataType(Converter).

Utility methods for type conversions

This class provides less type-safety than the general jOOQ API methods. For instance, it accepts arbitrary Converter objects in methods like convert(Collection, Converter) and convert(Object, Class), trying to retrofit the Object argument to the type provided in Converter#fromType() before performing the actual conversion.

Author
Lukas Eder

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
Convert.ConvertAll<U>

The converter to convert them all.

Field Summary

Modifier and TypeField and Description
public static final Set<String>
FALSE_VALUES

All string values that can be transformed into a boolean false value.

private static final boolean
JAXB_AVAILABLE

Whether a JAXB implementation is available.

private static final Object
JSON_MAPPER

The Jackson ObjectMapper or Gson instance, if available.

private static final Method
JSON_READ_METHOD

The Jackson ObjectMapper::readValue or Gson::fromJson method, if available.

private static final Method
JSON_WRITE_METHOD

The Jackson ObjectMapper::writeValueToString or Gson::toJson method, if available.

private static final JooqLogger
public static final Set<String>
TRUE_VALUES

All string values that can be transformed into a boolean true value.

private static final Pattern
UUID_PATTERN

A UUID pattern for UUIDs with or without hyphens

Constructor Summary

AccessConstructor and Description
private
Convert()

No instances

Method Summary

Modifier and TypeMethod and Description
public static final Object[]
convert(Object[] values, Field<?>[] fields)

Deprecated as a consequence of Field being deprecated.
Convert an array of values to a matching data type

This converts values[i] to fields[i].getType()

public static final Object[]
convert(Object[] values, Class<?>[] types)

Convert an array of values to a matching data type

This converts values[i] to types[i]

public static final <U> U

Returns:

The target type object
convert
(Object
The source object
from
,
Converter<?, ? extends U>
The data type converter
converter
)

Convert an object to a type.

public static final <T> T

Returns:

The converted object
convert
(Object
The object to convert
from
,
Class<? extends T>
The target type
toClass
)

Convert an object to a type.

public static final <T> List<T>

Returns:

The list of converted objects
convert
(Collection<?>
The list of objects
collection
,
Class<? extends T>
The target type
type
)

Convert a collection of objects to a list of T, using convert(Object, Class)

public static final <U> List<U>

Returns:

The list of converted objects
convert
(Collection<?>
The collection of objects
collection
,
Converter<?, ? extends U>
The data type converter
converter
)

Convert a collection of objects to a list of T, using convert(Object, Converter)

private static final <T, U> U
convert0(Object from, Converter<T, ? extends U> converter)

Conversion type-safety

private static final <T, U> List<U>
convert0(Collection<?> collection, Converter<T, ? extends U> converter)

Type safe conversion

public static final <U> U[]

Returns:

A converted array
convertArray
(Object[]
The array to convert
from
,
Converter<?, ? extends U>
The data type converter
converter
)

Convert an array into another one using a converter

This uses convertArray(Object[], Class) to convert the array to an array of Converter#fromType() first, before converting that array again to Converter#toType()

public static final Object[]

Returns:

A converted array
convertArray
(Object[]
The array to convert
from
,
Class<?>
The target array type
toClass
)

Convert an array into another one by these rules

  • If toClass is not an array class, then make it an array class first
  • If toClass is an array class, then create an instance from it, and convert all elements in the from array one by one, using convert(Object, Class)
public static final <U> U[]
convertCollection(Collection<E> from, Class<? extends U[]> to)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

FALSE_VALUESback to summary
public static final Set<String> FALSE_VALUES

All string values that can be transformed into a boolean false value.

JAXB_AVAILABLEback to summary
private static final boolean JAXB_AVAILABLE

Whether a JAXB implementation is available.

JSON_MAPPERback to summary
private static final Object JSON_MAPPER

The Jackson ObjectMapper or Gson instance, if available.

JSON_READ_METHODback to summary
private static final Method JSON_READ_METHOD

The Jackson ObjectMapper::readValue or Gson::fromJson method, if available.

JSON_WRITE_METHODback to summary
private static final Method JSON_WRITE_METHOD

The Jackson ObjectMapper::writeValueToString or Gson::toJson method, if available.

logback to summary
private static final JooqLogger log
TRUE_VALUESback to summary
public static final Set<String> TRUE_VALUES

All string values that can be transformed into a boolean true value.

UUID_PATTERNback to summary
private static final Pattern UUID_PATTERN

A UUID pattern for UUIDs with or without hyphens

Constructor Detail

Convertback to summary
private Convert()

No instances

Method Detail

convertback to summary
public static final Object[] convert(Object[] values, Field<?>[] fields)

Deprecated

as a consequence of Field being deprecated. See corresponding docs for further information.

Convert an array of values to a matching data type

This converts values[i] to fields[i].getType()

convertback to summary
public static final Object[] convert(Object[] values, Class<?>[] types)

Convert an array of values to a matching data type

This converts values[i] to types[i]

convertback to summary
public static final <U> U convert(Object from, Converter<?, ? extends U> converter) throws DataTypeException

Convert an object to a type.

Parameters
from:Object

The source object

converter:Converter<?, ? extends U>

The data type converter

Returns:U

The target type object

Annotations
@SuppressWarnings:unchecked
Exceptions
DataTypeException:
- When the conversion is not possible
convertback to summary
public static final <T> T convert(Object from, Class<? extends T> toClass) throws DataTypeException

Convert an object to a type. These are the conversion rules:

  • null is always converted to null, or the primitive default value, or Optional#empty(), regardless of the target type.
  • Identity conversion (converting a value to its own type) is always possible.
  • Primitive types can be converted to their wrapper types and vice versa
  • All types can be converted to String
  • All types can be converted to Object
  • All Number types can be converted to other Number types
  • All Number or String types can be converted to Boolean. Possible (case-insensitive) values for true:
    • 1
    • 1.0
    • y
    • yes
    • true
    • on
    • enabled

    Possible (case-insensitive) values for false:

    • 0
    • 0.0
    • n
    • no
    • false
    • off
    • disabled

    All other values evaluate to null

  • All java.util.Date subtypes (Date, Time, Timestamp), as well as most Temporal subtypes ( LocalDate, LocalTime, LocalDateTime, OffsetTime, OffsetDateTime, as well as Instant) can be converted into each other.
  • All String types can be converted into URI, URL and File
  • byte[] and ByteBuffer can be converted into one another
  • byte[] can be converted into String, using the platform's default charset
  • Object[] can be converted into any other array type, if array elements can be converted, too
  • All types can be converted into types containing a single argument constructor whose argument is a type that can be converted to according to the above rules.
  • All other combinations that are not listed above will result in a DataTypeException
Parameters
from:Object

The object to convert

toClass:Class<? extends T>

The target type

Returns:T

The converted object

Annotations
@SuppressWarnings:unchecked
Exceptions
DataTypeException:
- When the conversion is not possible
convertback to summary
public static final <T> List<T> convert(Collection<?> collection, Class<? extends T> type) throws DataTypeException

Convert a collection of objects to a list of T, using convert(Object, Class)

Parameters
collection:Collection<?>

The list of objects

type:Class<? extends T>

The target type

Returns:List<T>

The list of converted objects

Exceptions
DataTypeException:
- When the conversion is not possible
See Also
convert(Object, Class)
convertback to summary
public static final <U> List<U> convert(Collection<?> collection, Converter<?, ? extends U> converter) throws DataTypeException

Convert a collection of objects to a list of T, using convert(Object, Converter)

Parameters
collection:Collection<?>

The collection of objects

converter:Converter<?, ? extends U>

The data type converter

Returns:List<U>

The list of converted objects

Exceptions
DataTypeException:
- When the conversion is not possible
See Also
convert(Object, Converter)
convert0back to summary
private static final <T, U> U convert0(Object from, Converter<T, ? extends U> converter) throws DataTypeException

Conversion type-safety

Annotations
@SuppressWarnings:unchecked
convert0back to summary
private static final <T, U> List<U> convert0(Collection<?> collection, Converter<T, ? extends U> converter) throws DataTypeException

Type safe conversion

convertArrayback to summary
public static final <U> U[] convertArray(Object[] from, Converter<?, ? extends U> converter) throws DataTypeException

Convert an array into another one using a converter

This uses convertArray(Object[], Class) to convert the array to an array of Converter#fromType() first, before converting that array again to Converter#toType()

Parameters
from:Object[]

The array to convert

converter:Converter<?, ? extends U>

The data type converter

Returns:U[]

A converted array

Annotations
@SuppressWarnings:unchecked
Exceptions
DataTypeException:
- When the conversion is not possible
convertArrayback to summary
public static final Object[] convertArray(Object[] from, Class<?> toClass) throws DataTypeException

Convert an array into another one by these rules

  • If toClass is not an array class, then make it an array class first
  • If toClass is an array class, then create an instance from it, and convert all elements in the from array one by one, using convert(Object, Class)
Parameters
from:Object[]

The array to convert

toClass:Class<?>

The target array type

Returns:Object[]

A converted array

Annotations
@SuppressWarnings:unchecked
Exceptions
DataTypeException:
- When the conversion is not possible
convertCollectionback to summary
public static final <U> U[] convertCollection(Collection<E> from, Class<? extends U[]> to)
org.jooq.tools back to summary

private final Class Convert.ConvertAll<U>

extends AbstractContextConverter<Object, U>
Class Inheritance

The converter to convert them all.

Field Summary

Modifier and TypeField and Description
pack-priv static final Pattern
private final Class<? extends U>

Constructor Summary

AccessConstructor and Description
pack-priv
ConvertAll(Class<? extends U> toClass)

Method Summary

Modifier and TypeMethod and Description
private static DataTypeException
fail(Object from, Class<?> toClass)

public U
from(Object
The database object.
from
,
ConverterContext
The context of this conversion.
scope
)

Implements org.jooq.ContextConverter.from.

Read and convert a database object to a user object.
private static final long
millis(Temporal temporal)

private static final UUID
parseUUID(String string)

Some databases do not implement the standard very well.

pack-priv static final String
private static final String
private static final String
patchIso8601Timestamp(String string, boolean t)

public Object
to(U
The user object.
to
,
ConverterContext
The context of this conversion.
scope
)

Implements org.jooq.ContextConverter.to.

Convert and write a user object to a database object.
private static <X> X
toDate(long time, Class<X> toClass)

Convert a long timestamp (millis) to any date type.

private static <X> X
toDate(long time, int nanos, Class<X> toClass)

Convert a long timestamp (millis) with nanos adjustment to any date type.

private static Timestamp
toTimestamp(long time, int nanos)

Inherited from org.jooq.impl.AbstractContextConverter:
toString

Field Detail

P_FRACTIONAL_SECONDSback to summary
pack-priv static final Pattern P_FRACTIONAL_SECONDS
toClassback to summary
private final Class<? extends U> toClass

Constructor Detail

ConvertAllback to summary
pack-priv ConvertAll(Class<? extends U> toClass)
Annotations
@SuppressWarnings:unchecked

Method Detail

failback to summary
private static DataTypeException fail(Object from, Class<?> toClass)
fromback to summary
public U from(Object from, ConverterContext scope)

Implements org.jooq.ContextConverter.from.

Doc from org.jooq.ContextConverter.from.

Read and convert a database object to a user object.

Parameters
from:Object

The database object.

scope:ConverterContext

The context of this conversion.

Returns:U

The user object.

Annotations
@SuppressWarnings:unchecked, rawtypes
@Override
millisback to summary
private static final long millis(Temporal temporal)
parseUUIDback to summary
private static final UUID parseUUID(String string)

Some databases do not implement the standard very well. Specifically, SQLDialect#SYBASE seems to omit hyphens

patchFractionalSecondsback to summary
pack-priv static final String patchFractionalSeconds(String string)
patchIso8601Timeback to summary
private static final String patchIso8601Time(String string)
patchIso8601Timestampback to summary
private static final String patchIso8601Timestamp(String string, boolean t)
toback to summary
public Object to(U to, ConverterContext scope)

Implements org.jooq.ContextConverter.to.

Doc from org.jooq.ContextConverter.to.

Convert and write a user object to a database object.

Parameters
to:U

The user object.

scope:ConverterContext

The context of this conversion.

Returns:Object

The database object.

Annotations
@Override
toDateback to summary
private static <X> X toDate(long time, Class<X> toClass)

Convert a long timestamp (millis) to any date type.

toDateback to summary
private static <X> X toDate(long time, int nanos, Class<X> toClass)

Convert a long timestamp (millis) with nanos adjustment to any date type.

Annotations
@SuppressWarnings:unchecked
toTimestampback to summary
private static Timestamp toTimestamp(long time, int nanos)