Package eu.woolplatform.utils.validation
Class TypeConversion
- java.lang.Object
-
- eu.woolplatform.utils.validation.TypeConversion
-
public class TypeConversion extends Object
This class can perform various type conversions.
-
-
Constructor Summary
Constructors Constructor Description TypeConversion()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static BooleangetBoolean(Object obj)Converts the specified object to a boolean.static DoublegetDouble(Object obj)Converts the specified object to a double.static <T extends Enum<?>>
TgetEnum(Object obj, Class<T> enumClass)Converts the specified object to an enum.static IntegergetInt(Object obj)Converts the specified object to an integer.static <T> TgetJson(Object obj, com.fasterxml.jackson.core.type.TypeReference<T> typeRef)Converts an object to the specified type using the Jackson ObjectMapper.static <T> TgetJson(Object obj, Class<T> clazz)Converts an object to the specified type using the Jackson ObjectMapper.static LonggetLong(Object obj)Converts the specified object to a long.static StringgetString(Object obj)Converts the specified object to a string.
-
-
-
Method Detail
-
getString
public static String getString(Object obj)
Converts the specified object to a string. If the object is null, this method returns null. Otherwise it will call toString().- Parameters:
obj- the object or null- Returns:
- the string or null
-
getInt
public static Integer getInt(Object obj) throws ParseException
Converts the specified object to an integer. If the object is null, this method returns null. If it's a Number, it will return the integer value. Otherwise it will call toString() and try to parse the string value.- Parameters:
obj- the object or null- Returns:
- the integer or null
- Throws:
ParseException- if a string value can't be parsed as an integer
-
getLong
public static Long getLong(Object obj) throws ParseException
Converts the specified object to a long. If the object is null, this method returns null. If it's a Number, it will return the long value. Otherwise it will call toString() and try to parse the string value.- Parameters:
obj- the object or null- Returns:
- the long or null
- Throws:
ParseException- if a string value can't be parsed as a long
-
getDouble
public static Double getDouble(Object obj) throws ParseException
Converts the specified object to a double. If the object is null, this method returns null. If it's a Number, it will return the double value. Otherwise it will call toString() and try to parse the string value.- Parameters:
obj- the object or null- Returns:
- the double or null
- Throws:
ParseException- if a string value can't be parsed as a double
-
getBoolean
public static Boolean getBoolean(Object obj) throws ParseException
Converts the specified object to a boolean. If the object is null, this method returns null. If it's a Boolean, it will return the boolean value. Otherwise it will call toString() and try to parse the string value.- Parameters:
obj- the object or null- Returns:
- the boolean or null
- Throws:
ParseException- if a string value can't be parsed as a boolean
-
getEnum
public static <T extends Enum<?>> T getEnum(Object obj, Class<T> enumClass) throws ParseException
Converts the specified object to an enum. If the object is null, this method returns null. If it's an instance of the specified enum class, it just returns the object. Otherwise it gets the string value and does a case-insensitive comparison with the possible enum values.- Type Parameters:
T- the type of enum to return- Parameters:
obj- the object or nullenumClass- the enum class- Returns:
- the enum or null
- Throws:
ParseException- if the enum value is invalid
-
getJson
public static <T> T getJson(Object obj, Class<T> clazz) throws ParseException
Converts an object to the specified type using the Jackson ObjectMapper. For example a Map could be converted to an object. This method does not parse JSON strings. If the object is null, this method returns null.- Type Parameters:
T- the return type- Parameters:
obj- the object or nullclazz- the return type- Returns:
- the converted object or null
- Throws:
ParseException- if the object can't be converted to the specified type
-
getJson
public static <T> T getJson(Object obj, com.fasterxml.jackson.core.type.TypeReference<T> typeRef) throws ParseException
Converts an object to the specified type using the Jackson ObjectMapper. For example a Map could be converted to an object. This method does not parse JSON strings. If the object is null, this method returns null.If you want to convert to MyObject, you can specify:
new TypeReference<MyObject>() {}- Type Parameters:
T- the return type- Parameters:
obj- the object or nulltypeRef- the return type- Returns:
- the converted object or null
- Throws:
ParseException- if the object can't be converted to the specified type
-
-