- java.lang.Object
-
- org.jooq.tools.Convert
-
public final class Convert extends Object
Utility methods for type conversionsThis class provides less type-safety than the general jOOQ API methods. For instance, it accepts arbitrary
Converterobjects in methods likeconvert(Collection, Converter)andconvert(Object, Class), trying to retrofit theObjectargument to the type provided inConverter.fromType()before performing the actual conversion.- Author:
- Lukas Eder
-
-
Field Summary
Fields Modifier and Type Field Description static Set<String>FALSE_VALUESAll string values that can be transformed into a booleanfalsevalue.static Set<String>TRUE_VALUESAll string values that can be transformed into a booleantruevalue.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static Object[]convert(Object[] values, Class<?>[] types)Convert an array of values to a matching data typestatic Object[]convert(Object[] values, Field<?>[] fields)Convert an array of values to a matching data typestatic <T> Tconvert(Object from, Class<? extends T> toClass)Convert an object to a type.static <U> Uconvert(Object from, Converter<?,? extends U> converter)Convert an object to a type.static <T> List<T>convert(Collection<?> collection, Class<? extends T> type)Convert a collection of objects to a list ofT, usingconvert(Object, Class)static <U> List<U>convert(Collection<?> collection, Converter<?,? extends U> converter)Convert a collection of objects to a list ofT, usingconvert(Object, Converter)static Object[]convertArray(Object[] from, Class<?> toClass)Convert an array into another one by these rulesstatic <U> U[]convertArray(Object[] from, Converter<?,? extends U> converter)Convert an array into another one using a converterstatic <U> U[]convertCollection(Collection from, Class<? extends U[]> to)
-
-
-
Method Detail
-
convert
public static final Object[] convert(Object[] values, Field<?>[] fields)
Convert an array of values to a matching data typeThis converts
values[i]tofields[i].getType()
-
convert
public static final Object[] convert(Object[] values, Class<?>[] types)
Convert an array of values to a matching data typeThis converts
values[i]totypes[i]
-
convertArray
public static final <U> U[] convertArray(Object[] from, Converter<?,? extends U> converter) throws DataTypeException
Convert an array into another one using a converterThis uses
convertArray(Object[], Class)to convert the array to an array ofConverter.fromType()first, before converting that array again toConverter.toType()- Parameters:
from- The array to convertconverter- The data type converter- Returns:
- A converted array
- Throws:
DataTypeException- - When the conversion is not possible
-
convertArray
public static final Object[] convertArray(Object[] from, Class<?> toClass) throws DataTypeException
Convert an array into another one by these rules- If
toClassis not an array class, then make it an array class first - If
toClassis an array class, then create an instance from it, and convert all elements in thefromarray one by one, usingconvert(Object, Class)
- Parameters:
from- The array to converttoClass- The target array type- Returns:
- A converted array
- Throws:
DataTypeException- - When the conversion is not possible
- If
-
convertCollection
public static final <U> U[] convertCollection(Collection from, Class<? extends U[]> to)
-
convert
public static final <U> U convert(Object from, Converter<?,? extends U> converter) throws DataTypeException
Convert an object to a type.- Parameters:
from- The source objectconverter- The data type converter- Returns:
- The target type object
- Throws:
DataTypeException- - When the conversion is not possible
-
convert
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:nullis always converted tonull, or the primitive default value, orOptional.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
Numbertypes can be converted to otherNumbertypes - All
NumberorStringtypes can be converted toBoolean. Possible (case-insensitive) values fortrue:11.0yyestrueonenabled
Possible (case-insensitive) values for
false:00.0nnofalseoffdisabled
All other values evaluate to
null - All
Datesubtypes (Date,Time,Timestamp), as well as mostTemporalsubtypes (LocalDate,LocalTime,LocalDateTime,OffsetTime,OffsetDateTime, as well asInstant) can be converted into each other. - All
Stringtypes can be converted intoURI,URLandFile byte[]can be converted intoString, using the platform's default charsetObject[]can be converted into any other array type, if array elements can be converted, too- All other combinations that are not listed above will result in a
DataTypeException
- Parameters:
from- The object to converttoClass- The target type- Returns:
- The converted object
- Throws:
DataTypeException- - When the conversion is not possible
-
convert
public static final <T> List<T> convert(Collection<?> collection, Class<? extends T> type) throws DataTypeException
Convert a collection of objects to a list ofT, usingconvert(Object, Class)- Parameters:
collection- The list of objectstype- The target type- Returns:
- The list of converted objects
- Throws:
DataTypeException- - When the conversion is not possible- See Also:
convert(Object, Class)
-
convert
public static final <U> List<U> convert(Collection<?> collection, Converter<?,? extends U> converter) throws DataTypeException
Convert a collection of objects to a list ofT, usingconvert(Object, Converter)- Parameters:
collection- The collection of objectsconverter- The data type converter- Returns:
- The list of converted objects
- Throws:
DataTypeException- - When the conversion is not possible- See Also:
convert(Object, Converter)
-
-