public final class ValueConverter extends Object
Number type (Integer, Character, Double, byte, etc.)StringBooleanCharacterValueConverter.IncompatibleTypeException| Modifier and Type | Class and Description |
|---|---|
static class |
ValueConverter.IncompatibleTypeException
This exception can be thrown in any of the conversion methods of
ValueConverter, to indicate a value could not be converted
into the target datatype. |
| Modifier and Type | Method and Description |
|---|---|
static List<Class<?>> |
collectCompatibleTypes(Class<?> c)
Determines to which types the specified value (its type) can be converted to.
|
static Object |
convert(Boolean value,
Class<?> targetType)
Attempts to convert a
Boolean to the target datatype. |
static Object |
convert(Character value,
Class<?> targetType)
Attempts to convert a
Character to the target datatype. |
static Object |
convert(Number value,
Class<?> targetType)
Attempts to convert a
Number to the target datatype. |
static Object[] |
convert(Object[] args,
Class<?>[] targetTypes,
boolean useOriginalValueWhenIncompatible)
Converts a list of values to their converted form, as indicated by the specified targetTypes.
|
static Object |
convert(Object value,
Class<?> targetType)
Converts a single value into a target output datatype.
|
static Object |
convert(String value,
Class<?> targetType)
Attempts to convert a
String to the target datatype. |
static Object |
convertEnum(String value,
Class<? extends Enum<?>> targetType)
Attempts to convert a
String to an Enum instance, by mapping to the enum's name using
Enum.valueOf(Class, String). |
static Object |
convertNumber(String value,
Class<? extends Number> numberType)
Attempts to convert a
String to the specified Number type. |
static boolean |
isCommonType(Class<?> c) |
static boolean |
isPrimitiveNumber(Class<?> targetType)
Returns whether a
Class is a primitive number. |
public static boolean isCommonType(Class<?> c)
c - The class to inspect.@Nonnull public static List<Class<?>> collectCompatibleTypes(Class<?> c)
Object.toString().c - The input type to find compatible conversion output types for@Nonnull public static Object[] convert(Object[] args, Class<?>[] targetTypes, boolean useOriginalValueWhenIncompatible) throws ValueConverter.IncompatibleTypeException
args - The list with value to convert.targetTypes - The output types the specified values should be converted into.useOriginalValueWhenIncompatible - Indicates whether an exception should be thrown for inconvertible values or that the original
value should be used instead.ValueConverter.IncompatibleTypeException - Thrown when unable to convert and not use the original value.@Nullable public static Object convert(@Nullable Object value, Class<?> targetType) throws ValueConverter.IncompatibleTypeException
collectCompatibleTypes(Class).String (value.toString())String (convert(String, Class))Number (convert(Number, Class))Boolean (convert(Boolean, Class))Character (convert(Character, Class))value - The value to convert.targetType - The target data type the value should be converted into.ValueConverter.IncompatibleTypeException - Thrown by the various convert() methods used.@Nullable public static Object convert(@Nullable Number value, Class<?> targetType) throws ValueConverter.IncompatibleTypeException
Number to the target datatype.
NOTE: precision may be lost when converting from a wide number to a narrower number (say float to integer). These
conversions are done by simply calling Number.intValue() and Number.floatValue() etc.
Conversions are as follows:
value.toString()value.intValue()value.intValue() > 0value.floatValue()value.doubleValue()value.longValue()value.byteValue()value.shortValue()Character.forDigit(value, 10) (Character.forDigit(int, int))value - The number to convert.targetType - The target datatype the number should be converted into.ValueConverter.IncompatibleTypeException - Thrown when unable to find a compatible conversion.@Nullable public static Object convert(@Nullable Boolean value, Class<?> targetType) throws ValueConverter.IncompatibleTypeException
Boolean to the target datatype.
Conversions are as follows:
valuevalue.toString()convertNumber(value ? "1" : "0") (convertNumber(String, Class))value ? '1' : '0'value - The boolean to convert.targetType - The target datatype the boolean should be converted into.ValueConverter.IncompatibleTypeException - Thrown when unable to find a compatible conversion.@Nullable public static Object convert(@Nullable Character value, Class<?> targetType) throws ValueConverter.IncompatibleTypeException
Character to the target datatype.
Conversions are as follows:
value.toString()valueconvertNumber(String.valueOf(value)) (convertNumber(String, Class))!value.equals('0')value - The character to convert.targetType - The target datatype the character should be converted into.ValueConverter.IncompatibleTypeException - Thrown when unable to find a compatible conversion.@Nullable public static Object convert(@Nullable String value, Class<?> targetType) throws ValueConverter.IncompatibleTypeException
String to the target datatype.
Conversions are as follows:
valueconvertEnum(value) (convertEnum(String, Class))value.charAt(0)convert(value.charAt(0)) (convert(Character, Class))convertNumber(value) (convertNumber(String, Class))value.equalsIgnoreCase("true") ? true : value.equalsIgnoreCase("false") ? false : nothing (IncompatibleTypeException)value - The string to convert.targetType - The target datatype the string should be converted into.ValueConverter.IncompatibleTypeException - Thrown when unable to find a compatible conversion.@Nullable public static Object convertEnum(@Nullable String value, Class<? extends Enum<?>> targetType)
String to an Enum instance, by mapping to the enum's name using
Enum.valueOf(Class, String).value - The value, which should be the name of one instance of the given enum.targetType - The enum type to which which we'll try to convert.null otherwise.@Nullable public static Object convertNumber(@Nullable String value, Class<? extends Number> numberType)
String to the specified Number type.
Conversions are as follows:
Integer.parseInt(value)Byte.parseByte(value)Short.parseShort(value)Long.parseLong(value)Float.parseFloat(value)Double.parseDouble(value)BigInteger.valueOf(Long.parseLong(value))BigDecimal.valueOf(Long.parseLong(value))value - The string value which should be a number.numberType - The Class type that should be one of Number.Number subtype value converted from value (or null if value is null).Copyright © 2018. All rights reserved.