Class ObjectUtils
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final org.apache.empire.commons.ObjectUtils.NoValueConstant that defines a object of type NoValue. -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> T[]append(T[] array, T element) appends an element to an arraystatic <T> List<T>arrayToList(Class<T> t, T[] array) Converts an array to a liststatic <T> Tcoalesce(T preferred, T alternative) Checks whether a preferred value is valid and returns an alternative value if not.static <T> T[]combine(T[] left, T[] right) combines two arraysstatic intCompares two objects for equalitystatic booleancompareEqual(Object[] array1, Object[] array2) Compares two arrays for equalitystatic booleancompareEqual(Object o1, Object o2) Compares two objects for equalitystatic booleancompareEqual(ColumnExpr expr, ColumnExpr other) Compares two ColumnExpr for equalitystatic <T> booleancontains(T[] array, T item) returns whether or not a array contains a certain item performs a simple (==) comparison (fast)static <T> TGeneric conversion function that will convert a object to another value type.static <T> List<T>convert(Class<T> t, Collection<? extends T> source) Generic conversion function that will convert a list to another list type.static <T> TconvertColumnValue(ColumnExpr column, Object value, Class<T> valueType) Converts a column value to a Java typestatic ObjectconvertValue(DataType type, Object value) Converts a value to a specific DataType The returned value is used for generating SQL statementsstatic StringformatColumnValue(ColumnExpr column, Object value, Locale locale) Returns a formatted column valuestatic StringformatDate(Date date, boolean withTime) Formats a given date object to a standard ISO date string.static booleangetBoolean(Object v) Converts an object value to a boolean.static booleangetBoolean(Object v, boolean defValue) Converts an object value to a boolean.static DateConverts an object value to a Date.static BigDecimalgetDecimal(Object v) Converts an object value to a BigDecimal.static BigDecimalgetDecimal(Object v, BigDecimal defValue) Converts an object value to a BigDecimal.static doubleConverts an object value to a double.static doubleConverts an object value to a double.static <T extends Enum<?>>
TConverts an object to an enum of the given typestatic <T extends Enum<?>>
TgetEnumByName(Class<T> enumType, String name) find by namestatic ObjectgetEnumValue(Enum<?> enumValue, boolean isNumeric) Convert Enum to Objectstatic intgetInteger(Object v) Converts an object value to an integer.static intgetInteger(Object v, int defValue) Converts an object value to an integer.static LocalDateConverts an object value to a LocalDate.static LocalDateTimeConverts an object value to a LocalDateTime.static longConverts an object value to a long.static longConverts an object value to a long.static StringConverts an Enum to a Stringstatic StringConverts an Object to a Stringstatic TimestampConverts an object value to a LocalDateTime.static ValueUtilsstatic <T> intindexOf(T[] array, T item) returns whether or not a array contains a certain item performs a quick (==) comparison first if not found a second check is made using equals and unwrapping of itemsstatic booleanisAssignmentCompatible(Class<?> target, Class<?> source) Checks if a class is assignment compatible with another classstatic booleanChecks whether an object has no value.static booleanChecks whether a number is NOT null or zerostatic booleanisNotEmpty(Object o) Checks whether an object has a value.static booleanChecks whether a object implements the Unwrappable interface and is also a wrapper If the object does not Implement the Interface or is not a wrapper then false is returnedstatic booleanChecks whether a number is null or zerostatic intreturns the string length of an objectstatic <T> T[]listToArray(Class<? extends T[]> type, List<? extends T> list) Converts a list to an array e.g.: MyItem[] array = ObjectUtils.listToArray(MyItem[].class, myList)static voidsetValueUtils(ValueUtils valueUtils) static <T> T[]Converts varArgs to an arraystatic BigDecimalDeprecated.static doubleDeprecated.static intDeprecated.static longDeprecated.static String[]toStringArray(Object[] objArray, String defValue) Converts an Object array to a String array.static <T> Tunwrap(T object) Unwraps an object implementing the Unwrappable interface If the object does not Implement the Interface or is not a wrapper then the object itself is returned
-
Field Details
-
NO_VALUE
public static final org.apache.empire.commons.ObjectUtils.NoValue NO_VALUEConstant that defines a object of type NoValue. This may be used in cases where the value of null is a valid value.
-
-
Method Details
-
getValueUtils
-
setValueUtils
-
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.
-
isNotEmpty
Checks whether an object has a value. A Object is considered to have a value if it is not null and not an empty string- Parameters:
o- the object to check- Returns:
- true if the Object is neither null nor 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
-
isNonZero
Checks whether a number is NOT null or zero- Parameters:
value- the number to check- Returns:
- true if the value is NOT null or zero
-
lengthOf
returns the string length of an object- Parameters:
o- the object to check- Returns:
- the string length of the object
-
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
-
coalesce
public static <T> T coalesce(T preferred, T alternative) Checks whether a preferred value is valid and returns an alternative value if not.- Type Parameters:
T- the type of the values- Parameters:
preferred- the preferred return valuealternative- the alternative return value used if the preferred value is null- Returns:
- the preferred value if it is not null or the alternative value otherwise
-
toInteger
Deprecated.Deprecated. Use getValueUtils().toInteger() instead- Parameters:
v- the value to convert- Returns:
- the integer value
-
getInteger
Converts an object value to an integer.If the object value supplied is null or if conversion is not possible then the default value is returned.
- Parameters:
v- the obect to convertdefValue- the default value if o is null or conversion is not possible- Returns:
- the Integer value of v or a default value
-
getInteger
Converts an object value to an integer.If the object value supplied is null or if conversion is not possible then 0 is returned.
- Parameters:
v- the object value to convert- Returns:
- the Integer value of v or 0
-
toLong
Deprecated.Deprecated. Use getValueUtils().toLong() instead- Parameters:
v- the value to convert- Returns:
- the long value
-
getLong
Converts an object value to a long.If the object value supplied is null or if conversion is not possible then the default value is returned.
- Parameters:
v- the obect to convertdefValue- the default value if o is null or conversion is not possible- Returns:
- the Integer value of v or a default value
-
getLong
Converts an object value to a long.If the object value supplied is null or if conversion is not possible then 0 is returned.
- Parameters:
v- the object value to convert- Returns:
- the Long value of v or 0
-
toDouble
Deprecated.Deprecated. Use getValueUtils().toDouble() instead- Parameters:
v- the value to convert- Returns:
- the double value
-
getDouble
Converts an object value to a double.If the object value supplied is null or if conversion is not possible then defValue is returned.
- Parameters:
v- the object value to convertdefValue- the default value- Returns:
- the Long value of v or defValue
-
getDouble
Converts an object value to a double.If the object value supplied is null or if conversion is not possible then 0.0 is returned.
- Parameters:
v- the object value to convert- Returns:
- the Long value of v or 0
-
toDecimal
Deprecated.Deprecated. Use getValueUtils().toDecimal() instead- Parameters:
v- the value to convert- Returns:
- the decimal value
-
getDecimal
Converts an object value to a BigDecimal.If the object value supplied is null or if conversion is not possible then defValue is returned.
- Parameters:
v- the object value to convertdefValue- the default value- Returns:
- the BigDecimal value of v or defValue
-
getDecimal
Converts an object value to a BigDecimal.If the object value supplied is null or if conversion is not possible then 0.0 is returned.
- Parameters:
v- the object value to convert- Returns:
- the Long value of v or 0
-
getBoolean
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
-
getBoolean
Converts an object value to a boolean. see getBoolean(Object v, boolean defValue) for details.- Parameters:
v- the object to convert- Returns:
- the boolean value or false if v is null or empty
-
getEnum
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
-
getEnumByName
find by name- Type Parameters:
T- the type of the enum- Parameters:
enumType- the enum typename- the enum name- Returns:
- the enum
-
getEnumValue
Convert Enum to Object- Parameters:
enumValue- the enumisNumeric- flag if number or string is required- Returns:
- the number or string representing this enum
-
getString
Converts an Enum to a String- Parameters:
enumValue- the enum- Returns:
- the corresponding string value
-
getString
Converts an Object to a String- Parameters:
value- the value to convert- Returns:
- the corresponding string value
-
formatColumnValue
Returns a formatted column value- Parameters:
column- the columnvalue- the value to convertlocale- the locale (optional)- Returns:
- the corresponding string value
-
getDate
Converts an object value to a Date.- Parameters:
v- the object to convert- Returns:
- the Date value of v or null
-
getLocalDate
Converts an object value to a LocalDate.- Parameters:
v- the object to convert- Returns:
- the LocalDate value of v or null
-
getLocalDateTime
Converts an object value to a LocalDateTime.- Parameters:
v- the object to convert- Returns:
- the LocalDateTime value of v or null
-
getTimestamp
Converts an object value to a LocalDateTime.- Parameters:
v- the object to convert- Returns:
- the LocalDateTime value of v 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
-
convert
Generic conversion function that will convert a object to another value type.- Type Parameters:
T- the type to convert to- Parameters:
c- the class type to convert tov- the object to convert- Returns:
- the converted value of v or null
- Throws:
ClassCastException- if the object is not null and is not assignable to the type T.
-
convertColumnValue
public static <T> T convertColumnValue(ColumnExpr column, Object value, Class<T> valueType) throws ClassCastException Converts a column value to a Java type- Type Parameters:
T- the type to convert to- Parameters:
column- the column expression for metadata accessvalue- the value to convertvalueType- the desired value type- Returns:
- the converted value of v or null
- Throws:
ClassCastException- if the object is not null and is not assignable to the type T.
-
convertValue
Converts a value to a specific DataType The returned value is used for generating SQL statements- Parameters:
type- the target data typevalue- the value to convert- Returns:
- the value to be used in SQL statements
-
isAssignmentCompatible
Checks if a class is assignment compatible with another class- Parameters:
target- the target classsource- the source class- Returns:
- true if assignment compatible or false otherwise
-
compareEqual
Compares two arrays for equality- Parameters:
array1- the first arrayarray2- the second array- Returns:
- true if both arrays are equal or false otherwise
-
compareEqual
Compares two ColumnExpr for equality- Parameters:
expr- a column expressionother- a column expression- Returns:
- true if both expressions are equal or false otherwise
-
convert
Generic conversion function that will convert a list to another list type.- Type Parameters:
T- the type of elements- Parameters:
t- the type classsource- the source collection- Returns:
- the new list type
-
toArray
Converts varArgs to an array- Type Parameters:
T- the type of elements- Parameters:
t- the type of the arrayvalues- the array values- Returns:
- the array
-
toStringArray
Converts an Object array to a String array.- Parameters:
objArray- the object array to convertdefValue- default value which will be set for all null objects- Returns:
- the String array
-
arrayToList
Converts an array to a list- Type Parameters:
T- the type of elements- Parameters:
t- the type of the list itemsarray- the array to be converted- Returns:
- the list
-
listToArray
Converts a list to an array e.g.: MyItem[] array = ObjectUtils.listToArray(MyItem[].class, myList)- Type Parameters:
T- the type of the items in the resulting array- Parameters:
type- the array typelist- the item list- Returns:
- the array
-
isWrapper
Checks whether a object implements the Unwrappable interface and is also a wrapper If the object does not Implement the Interface or is not a wrapper then false is returned- Parameters:
object- the object to check- Returns:
- true if the object is a wrapper or false otherwise
-
unwrap
public static <T> T unwrap(T object) Unwraps an object implementing the Unwrappable interface If the object does not Implement the Interface or is not a wrapper then the object itself is returned- Type Parameters:
T- the type of the object- Parameters:
object- the object to unwrap- Returns:
- the unwrapped object or the object itself
-
indexOf
public static <T> int indexOf(T[] array, T item) returns whether or not a array contains a certain item performs a quick (==) comparison first if not found a second check is made using equals and unwrapping of items- Type Parameters:
T- the type of the object- Parameters:
array- the array to searchitem- the item to search for- Returns:
- true if the array contains the item or false otherwise
-
contains
public static <T> boolean contains(T[] array, T item) returns whether or not a array contains a certain item performs a simple (==) comparison (fast)- Type Parameters:
T- the type of elements- Parameters:
array- the array to searchitem- the item to search for- Returns:
- true if the array contains the item or false otherwise
-
combine
public static <T> T[] combine(T[] left, T[] right) combines two arrays- Type Parameters:
T- the type of the array items- Parameters:
left- the left arrayright- the right array- Returns:
- the combined array
-
append
public static <T> T[] append(T[] array, T element) appends an element to an array- Type Parameters:
T- the type of the array items- Parameters:
array- the arrayelement- the new element- Returns:
- the combined array
-