public final class Converter extends Object
Constructor and Description |
---|
Converter(ConverterOptions options) |
Modifier and Type | Method and Description |
---|---|
Convert<?> |
addConversion(Class<?> source,
Class<?> target,
Convert<?> conversionFunction)
Add a new conversion.
|
Map<Class<?>,Set<Class<?>>> |
allSupportedConversions() |
<T> T |
convert(Object from,
Class<T> toType)
Turn the passed in value to the class indicated.
|
ConverterOptions |
getOptions() |
Map<String,Set<String>> |
getSupportedConversions() |
boolean |
isConversionSupportedFor(Class<?> source,
Class<?> target)
Check to see if a conversion from type to another type is supported (may use inheritance via super classes/interfaces).
|
public Converter(ConverterOptions options)
public ConverterOptions getOptions()
public <T> T convert(Object from, Class<T> toType)
Examples: Long x = convert("35", Long.class); Date d = convert("2015/01/01", Date.class) int y = convert(45.0, int.class) String date = convert(date, String.class) String date = convert(calendar, String.class) Short t = convert(true, short.class); // returns (short) 1 or (short) 0 Long date = convert(calendar, long.class); // get calendar's time into long Map containing ["_v": "75.0"] convert(map, double.class) // Converter will extract the value associated to the "_v" (or "value") key and convert it.
from
- A value used to create the targetType, even though it may
not (most likely will not) be the same data type as the targetTypetoType
- Class which indicates the targeted (final) data type.
Please note that in addition to the 8 Java primitives, the targeted class
can also be Date.class, String.class, BigInteger.class, BigDecimal.class, and
many other JDK classes, including Map. For Map, often it will seek a 'value'
field, however, for some complex objects, like UUID, it will look for specific
fields within the Map to perform the conversion.getSupportedConversions()
public boolean isConversionSupportedFor(Class<?> source, Class<?> target)
source
- Class of source type.target
- Class of target type.public Map<Class<?>,Set<Class<?>>> allSupportedConversions()
public Map<String,Set<String>> getSupportedConversions()
public Convert<?> addConversion(Class<?> source, Class<?> target, Convert<?> conversionFunction)
source
- Class to convert from.target
- Class to convert to.conversionFunction
- Convert function that converts from the source type to the destination type.Copyright © 2024. All rights reserved.