Package org.apache.empire.commons
Class ValueUtils
java.lang.Object
org.apache.empire.commons.ValueUtils
This class allows to customize value type conversion as well as other value related functions.
The functions and methods are called indirectly via the corresponding static functions in the ObjectUtils class. *
You may create your own derived class and overwrite the methods.
In order to activate your ValueUtils implementation use the static function
OjectUtils.setValueUtils(myValueUtils)
- Author:
- doebele
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected static final Stringprotected static final ThreadLocal<SimpleDateFormat>protected static final Stringprotected static final ThreadLocal<SimpleDateFormat>static final Doublestatic final Integerstatic final Long -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionintCompares two objects for equalitybooleancompareEqual(Object o1, Object o2) Compares two objects for equality<T> TconvertColumnValue(ColumnExpr column, Object value, Class<T> vt) Converts a column value to a Java type Allows custom conversions and handling of special cases.convertToData(DataType dataType, Object value) Converts a value to a specific DataType The returned value is used for generating SQL statements<T> TconvertToJava(Class<T> c, Object v) Generic conversion function that will convert a object to another value type.enumToString(Enum<?> enumValue) Converts an Enum to a StringenumToValue(Enum<?> enumValue, boolean isNumeric) Convert Enum to ObjectformatColumnValue(ColumnExpr column, Object value, Locale locale) formats a column value into a stringformatDate(Date date, boolean withTime) Formats a given date object to a standard ISO date string.booleanChecks whether an object has no value.booleanChecks whether a number is null or zerointreturns the string length of an objectConverts an object value to a boolean.Converts an object value to a Date.converts an object to a decimal.converts an object to a double.<T extends Enum<?>>
TConverts an object to an enum of the given type<T extends Enum<?>>
TtoEnumByName(Class<T> enumType, String name) find by nameconverts an object to an integer.Converts an object value to a Date.Converts an object value to a Date.converts an object to a long.Converts an Object to a StringConverts an object value to a Timestamp.
-
Field Details
-
DATE_FORMAT
- See Also:
-
DATETIME_FORMAT
- See Also:
-
dateOnlyFormatter
-
dateTimeFormatter
-
INTEGER_ZERO
-
LONG_ZERO
-
DOUBLE_ZERO
-
-
Constructor Details
-
ValueUtils
protected ValueUtils()
-
-
Method Details
-
isEmpty
Checks whether an object has no value. A Object is empty if and only if the Object is null or if its an empty string.- Parameters:
o- the object to check- Returns:
- true if the Object is null or if its an empty string.
-
isZero
Checks whether a number is null or zero- Parameters:
value- the number to check- Returns:
- true if the value is null or zero
-
compareEqual
Compares two objects for equality- Parameters:
o1- the first objecto2- the second object- Returns:
- true if both objects are equal or false otherwise
-
compare
Compares two objects for equality- Parameters:
o1- the first objecto2- the second object- Returns:
- true if both objects are equal or false otherwise
-
toInteger
converts an object to an integer. If conversion is not possible, an error is thrown- Parameters:
v- the value to convert- Returns:
- the integer value
-
toLong
converts an object to a long. If conversion is not possible, an error is thrown- Parameters:
v- the value to convert- Returns:
- the long value
-
toDouble
converts an object to a double. If conversion is not possible, an error is thrown- Parameters:
v- the value to convert- Returns:
- the double value
-
toDecimal
converts an object to a decimal. If conversion is not possible, an error is thrown- Parameters:
v- the value to convert- Returns:
- the decimal value
-
toBoolean
Converts an object value to a boolean.If the object value supplied is empty then the defValue is returned Numbers are considered true if they are not equal to zero String are considered true only if the string is "Y" or "true"
- Parameters:
v- the object to convertdefValue- the default value- Returns:
- the boolean value or defValue if v is null or empty
-
toEnum
Converts an object to an enum of the given type- Type Parameters:
T- the type of the enum- Parameters:
enumType- the enum typevalue- the value to convert- Returns:
- the enum
-
toEnumByName
find by name- Type Parameters:
T- the type of the enum- Parameters:
enumType- the enum typename- the enum name- Returns:
- the enum
-
enumToValue
Convert Enum to Object- Parameters:
enumValue- the enumisNumeric- flag if number or string is required- Returns:
- the number or string representing this enum
-
enumToString
Converts an Enum to a String- Parameters:
enumValue- the enum- Returns:
- the corresponding string value
-
toString
Converts an Object to a String- Parameters:
value- the value to convert- Returns:
- the corresponding string value
-
lengthOf
returns the string length of an object- Parameters:
o- the object to check- Returns:
- the string length of the object
-
toDate
Converts an object value to a Date.- Parameters:
v- the object to convert- Returns:
- the Date value of v or null
- Throws:
ParseException- exception if date cannot be parsed from string
-
toLocalDate
Converts an object value to a Date.- Parameters:
v- the object to convert- Returns:
- the LocalDate value of v or null
-
toLocalDateTime
Converts an object value to a Date.- Parameters:
v- the object to convert- Returns:
- the LocalDateTime value of v or null
-
toTimestamp
Converts an object value to a Timestamp.- Parameters:
v- the object to convert- Returns:
- the Timestamp or null
-
formatDate
Formats a given date object to a standard ISO date string. The format is "yyyy-MM-dd hh:mm:ss"- Parameters:
date- the date to be formatedwithTime- indicates whether the date string should include the time or not- Returns:
- the date string
-
formatColumnValue
formats a column value into a string- Parameters:
column- the columnvalue- the value to convertlocale- the locale (optional)- Returns:
- the corresponding string value
-
convertToJava
Generic conversion function that will convert a object to another value type. This function is intended to be used for converting values coming from the database to be used by the program- Type Parameters:
T- the java type- Parameters:
c- the class type to convert tov- the value to convert- Returns:
- the converted value
- Throws:
ClassCastException- if the object is not null and is not assignable to the type T.
-
convertColumnValue
public <T> T convertColumnValue(ColumnExpr column, Object value, Class<T> vt) throws ClassCastException Converts a column value to a Java type Allows custom conversions and handling of special cases. For compatibility reasons we must use the old converter functions such as toString(), toInteger(), toDecimal(), toDate(), etc.- Type Parameters:
T- the type to convert to- Parameters:
column- the column expression for metadata accessvalue- the value to convertvt- the desired value type- Returns:
- the converted value
- Throws:
ClassCastException- if the object is not null and is not assignable to the type T.
-
convertToData
Converts a value to a specific DataType The returned value is used for generating SQL statements- Parameters:
dataType- the target data typevalue- the value to convert- Returns:
- the value to be used in SQL statements
-