Package org.apache.jorphan.util
Class Converter
- java.lang.Object
-
- org.apache.jorphan.util.Converter
-
public class Converter extends Object
Converter utilities for TestBeans
-
-
Constructor Summary
Constructors Constructor Description Converter()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static Object
convert(Object value, Class<?> toType)
Convert the given value object to an object of the given typestatic String
formatCalendar(Calendar date, String pattern)
Format a calendar using a given patternstatic String
formatDate(String date, String pattern)
Format a date using a given patternstatic String
formatDate(Date date, String pattern)
Format a date using a given patternstatic String
formatDate(Calendar date, String pattern)
Format a date using a given patternstatic String
formatDate(Date date, String pattern)
Format a date using a given patternstatic boolean
getBoolean(Object o)
Convert object to boolean, orfalse
if conversion failedstatic boolean
getBoolean(Object o, boolean defaultValue)
Convert object to boolean, ordefaultValue
if conversion failedstatic Calendar
getCalendar(Object o)
Converts the given object to a calendar object.static Calendar
getCalendar(Object date, Calendar defaultValue)
Converts the given object to a calendar object.static char
getChar(Object o)
Convert object to char, or ' ' if no conversion can be appliedstatic char
getChar(Object o, char defaultValue)
Convert object to char, ordefaultValue
if no conversion can be appliedstatic Date
getDate(Object date)
Converts the given object to aDate
object.static Date
getDate(Object date, Date defaultValue)
Converts the given object to aDate
object.static double
getDouble(Object o)
Convert object to double, or0
if conversion failedstatic double
getDouble(Object o, double defaultValue)
Convert object to double, ordefaultValue
if conversion failedstatic File
getFile(Object o)
Converts an object to aFile
static float
getFloat(Object o)
Convert object to float, or0
if conversion failedstatic float
getFloat(Object o, float defaultValue)
Convert object to float, ordefaultValue
if conversion failedstatic int
getInt(Object o)
Converts object to an integer, defaults to0
if object is not convertible or isnull
.static int
getInt(Object o, int defaultValue)
Convert object to integer, returndefaultValue
if object is not convertible or isnull
.static long
getLong(Object o)
Converts object to a long, defaults to0
if object is not convertible or isnull
static long
getLong(Object o, long defaultValue)
Converts object to a long, returndefaultValue
if object is not convertible or isnull
.static String
getString(Object o)
Converts object to a String, defaults to empty string if object is null.static String
getString(Object o, String defaultValue)
Converts object to a String, returndefaultValue
if object isnull
.static String
insertLineBreaks(String v, String insertion)
Replace newlines "\n" withinsertion
-
-
-
Method Detail
-
convert
public static Object convert(Object value, Class<?> 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
public static Calendar getCalendar(Object date, Calendar defaultValue)
Converts the given object to a calendar object. Defaults to thedefaultValue
if the given object can't be converted.
-
getCalendar
public 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 aCalendar
- Returns:
Calendar
representing the giveno
or a newGregorianCalendar
using the current time if conversion failed
-
getDate
public static Date getDate(Object date)
Converts the given object to aDate
object. Defaults to the current time if the given object can't be converted.
-
getDate
public static Date getDate(Object date, Date defaultValue)
Converts the given object to aDate
object. Defaults to thedefaultValue
if the given object can't be converted.
-
getFloat
public static float getFloat(Object o, float defaultValue)
Convert object to float, ordefaultValue
if conversion failed- Parameters:
o
- object to convertdefaultValue
- default value to use, when conversion failed- Returns:
- converted float or
defaultValue
if conversion failed
-
getFloat
public static float getFloat(Object o)
Convert object to float, or0
if conversion failed- Parameters:
o
- object to convert- Returns:
- converted float or
0
if conversion failed
-
getDouble
public static double getDouble(Object o, double defaultValue)
Convert object to double, ordefaultValue
if conversion failed- Parameters:
o
- object to convertdefaultValue
- default value to use, when conversion failed- Returns:
- converted double or
defaultValue
if conversion failed
-
getDouble
public static double getDouble(Object o)
Convert object to double, or0
if conversion failed- Parameters:
o
- object to convert- Returns:
- converted double or
0
if conversion failed
-
getBoolean
public static boolean getBoolean(Object o)
Convert object to boolean, orfalse
if conversion failed- Parameters:
o
- object to convert- Returns:
- converted boolean or
false
if conversion failed
-
getBoolean
public static boolean getBoolean(Object o, boolean defaultValue)
Convert object to boolean, ordefaultValue
if conversion failed- Parameters:
o
- object to convertdefaultValue
- default value to use, when conversion failed- Returns:
- converted boolean or
defaultValue
if conversion failed
-
getInt
public static int getInt(Object o, int defaultValue)
Convert object to integer, returndefaultValue
if 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
public 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
public static char getChar(Object o, char defaultValue)
Convert object to char, ordefaultValue
if no conversion can be applied- Parameters:
o
- object to convertdefaultValue
- default value to use, when conversion failed- Returns:
- converted char or
defaultValue
if conversion failed
-
getInt
public static int getInt(Object o)
Converts object to an integer, defaults to0
if object is not convertible or isnull
.- Parameters:
o
- object to convert- Returns:
- converted int, or
0
if conversion failed
-
getLong
public static long getLong(Object o, long defaultValue)
Converts object to a long, returndefaultValue
if object is not convertible or isnull
.- Parameters:
o
- object to convertdefaultValue
- default value to use, when conversion failed- Returns:
- converted long or
defaultValue
when conversion failed
-
getLong
public static long getLong(Object o)
Converts object to a long, defaults to0
if object is not convertible or isnull
- Parameters:
o
- object to convert- Returns:
- converted long or
0
if conversion failed
-
formatDate
public 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
- Throws:
IllegalArgumentException
- whenpattern
is invalid
-
formatDate
public 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
- Throws:
IllegalArgumentException
- whenpattern
is invalid
-
formatDate
public 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
- Throws:
IllegalArgumentException
- whenpattern
is invalid
-
formatDate
public 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
- Throws:
IllegalArgumentException
- whenpattern
is invalid
-
formatCalendar
public 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
- Throws:
IllegalArgumentException
- whenpattern
is invalid
-
getString
public static String getString(Object o, String defaultValue)
Converts object to a String, returndefaultValue
if object isnull
.- Parameters:
o
- object to convertdefaultValue
- default value to use when conversion failed- Returns:
- converted String or
defaultValue
when conversion failed
-
insertLineBreaks
public static String insertLineBreaks(String v, String insertion)
Replace newlines "\n" withinsertion
- 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
public 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
-
-