Class Converter
-
- All Implemented Interfaces:
public class ConverterConverter utilities for TestBeans
-
-
Constructor Summary
Constructors Constructor Description Converter()
-
Method Summary
Modifier and Type Method Description static Objectconvert(Object value, Class<out Object> toType)Convert the given value object to an object of the given type static CalendargetCalendar(Object date, Calendar defaultValue)Converts the given object to a calendar object. static CalendargetCalendar(Object o)Converts the given object to a calendar object. static DategetDate(Object date)Converts the given object to a Date object. static DategetDate(Object date, Date defaultValue)Converts the given object to a Date object. static floatgetFloat(Object o, float defaultValue)Convert object to float, or defaultValueif conversion failedstatic floatgetFloat(Object o)Convert object to float, or 0if conversion failedstatic doublegetDouble(Object o, double defaultValue)Convert object to double, or defaultValueif conversion failedstatic doublegetDouble(Object o)Convert object to double, or 0if conversion failedstatic booleangetBoolean(Object o)Convert object to boolean, or falseif conversion failedstatic booleangetBoolean(Object o, boolean defaultValue)Convert object to boolean, or defaultValueif conversion failedstatic intgetInt(Object o, int defaultValue)Convert object to integer, return defaultValueif object is not convertible or isnull.static chargetChar(Object o)Convert object to char, or ' ' if no conversion can be applied static chargetChar(Object o, char defaultValue)Convert object to char, or defaultValueif no conversion can be appliedstatic intgetInt(Object o)Converts object to an integer, defaults to 0if object is not convertible or isnull.static longgetLong(Object o, long defaultValue)Converts object to a long, return defaultValueif object is not convertible or isnull.static longgetLong(Object o)Converts object to a long, defaults to 0if object is not convertible or isnullstatic StringformatDate(Date date, String pattern)Format a date using a given pattern static StringformatDate(Date date, String pattern)Format a date using a given pattern static StringformatDate(String date, String pattern)Format a date using a given pattern static StringformatDate(Calendar date, String pattern)Format a date using a given pattern static StringformatCalendar(Calendar date, String pattern)Format a calendar using a given pattern static StringgetString(Object o, String defaultValue)Converts object to a String, return defaultValueif object isnull.static StringinsertLineBreaks(String v, String insertion)Replace newlines "\n" with insertionstatic StringgetString(Object o)Converts object to a String, defaults to empty string if object is null. static FilegetFile(Object o)Converts an object to a File -
-
Method Detail
-
convert
static Object convert(Object value, Class<out Object> toType)
Convert the given value object to an object of the given type
- Parameters:
value- object to converttoType- type to convert object to- Returns:
converted object or original value if no conversion could be applied
-
getCalendar
static Calendar getCalendar(Object date, Calendar defaultValue)
Converts the given object to a calendar object. Defaults to the
defaultValueif the given object can't be converted.
-
getCalendar
static Calendar getCalendar(Object o)
Converts the given object to a calendar object. Defaults to a calendar using the current time if the given object can't be converted.
- Parameters:
o- object that should be converted to a Calendar- Returns:
Calendar representing the given
oor a new GregorianCalendar using the current time if conversion failed
-
getDate
static Date getDate(Object date)
Converts the given object to a Date object. Defaults to the current time if the given object can't be converted.
-
getDate
static Date getDate(Object date, Date defaultValue)
Converts the given object to a Date object. Defaults to the
defaultValueif the given object can't be converted.
-
getFloat
static float getFloat(Object o, float defaultValue)
Convert object to float, or
defaultValueif conversion failed- Parameters:
o- object to convertdefaultValue- default value to use, when conversion failed- Returns:
converted float or
defaultValueif conversion failed
-
getFloat
static float getFloat(Object o)
Convert object to float, or
0if conversion failed- Parameters:
o- object to convert- Returns:
converted float or
0if conversion failed
-
getDouble
static double getDouble(Object o, double defaultValue)
Convert object to double, or
defaultValueif conversion failed- Parameters:
o- object to convertdefaultValue- default value to use, when conversion failed- Returns:
converted double or
defaultValueif conversion failed
-
getDouble
static double getDouble(Object o)
Convert object to double, or
0if conversion failed- Parameters:
o- object to convert- Returns:
converted double or
0if conversion failed
-
getBoolean
static boolean getBoolean(Object o)
Convert object to boolean, or
falseif conversion failed- Parameters:
o- object to convert- Returns:
converted boolean or
falseif conversion failed
-
getBoolean
static boolean getBoolean(Object o, boolean defaultValue)
Convert object to boolean, or
defaultValueif conversion failed- Parameters:
o- object to convertdefaultValue- default value to use, when conversion failed- Returns:
converted boolean or
defaultValueif conversion failed
-
getInt
static int getInt(Object o, int defaultValue)
Convert object to integer, return
defaultValueif object is not convertible or isnull.- Parameters:
o- object to convertdefaultValue- default value to be used when no conversion can be done- Returns:
converted int or default value if conversion failed
-
getChar
static char getChar(Object o)
Convert object to char, or ' ' if no conversion can be applied
- Parameters:
o- object to convert- Returns:
converted char or ' ' if conversion failed
-
getChar
static char getChar(Object o, char defaultValue)
Convert object to char, or
defaultValueif no conversion can be applied- Parameters:
o- object to convertdefaultValue- default value to use, when conversion failed- Returns:
converted char or
defaultValueif conversion failed
-
getInt
static int getInt(Object o)
Converts object to an integer, defaults to
0if object is not convertible or isnull.- Parameters:
o- object to convert- Returns:
converted int, or
0if conversion failed
-
getLong
static long getLong(Object o, long defaultValue)
Converts object to a long, return
defaultValueif object is not convertible or isnull.- Parameters:
o- object to convertdefaultValue- default value to use, when conversion failed- Returns:
converted long or
defaultValuewhen conversion failed
-
getLong
static long getLong(Object o)
Converts object to a long, defaults to
0if object is not convertible or isnull- Parameters:
o- object to convert- Returns:
converted long or
0if conversion failed
-
formatDate
static String formatDate(Date date, String pattern)
Format a date using a given pattern
- Parameters:
date- date to formatpattern- pattern to use for formatting- Returns:
formatted date, or empty string if date was
null
-
formatDate
static String formatDate(Date date, String pattern)
Format a date using a given pattern
- Parameters:
date- date to formatpattern- pattern to use for formatting- Returns:
formatted date, or empty string if date was
null
-
formatDate
static String formatDate(String date, String pattern)
Format a date using a given pattern
- Parameters:
date- date to formatpattern- pattern to use for formatting- Returns:
formatted date, or empty string if date was
null
-
formatDate
static String formatDate(Calendar date, String pattern)
Format a date using a given pattern
- Parameters:
date- date to formatpattern- pattern to use for formatting- Returns:
formatted date, or empty string if date was
null
-
formatCalendar
static String formatCalendar(Calendar date, String pattern)
Format a calendar using a given pattern
- Parameters:
date- calendar to formatpattern- pattern to use for formatting- Returns:
formatted date, or empty string if date was
null
-
getString
static String getString(Object o, String defaultValue)
Converts object to a String, return
defaultValueif object isnull.- Parameters:
o- object to convertdefaultValue- default value to use when conversion failed- Returns:
converted String or
defaultValuewhen conversion failed
-
insertLineBreaks
static String insertLineBreaks(String v, String insertion)
Replace newlines "\n" with
insertion- Parameters:
v- String in which the newlines should be replacedinsertion- new string which should be used instead of "\n"- Returns:
new string with newlines replaced by
insertion
-
getString
static String getString(Object o)
Converts object to a String, defaults to empty string if object is null.
- Parameters:
o- object to convert- Returns:
converted String or empty string when conversion failed
-
-
-
-