Class ObjectUtils

java.lang.Object
org.apache.empire.commons.ObjectUtils

public final class ObjectUtils extends Object
This class contains common functions for comparing and converting values from and to the database As well as other useful Array and List related functions. Value and value related conversion functions my be overridden by setting a customized version of the ValueUtils object via setValueUtils(ValueUtils valueUtils)
  • Field Details

    • NO_VALUE

      public static final org.apache.empire.commons.ObjectUtils.NoValue NO_VALUE
      Constant 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

      public static ValueUtils getValueUtils()
    • setValueUtils

      public static void setValueUtils(ValueUtils valueUtils)
    • isEmpty

      public static boolean isEmpty(Object o)
      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

      public static boolean isNotEmpty(Object o)
      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

      public static boolean isZero(Number value)
      Checks whether a number is null or zero
      Parameters:
      value - the number to check
      Returns:
      true if the value is null or zero
    • isNonZero

      public static boolean isNonZero(Number value)
      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

      public static int lengthOf(Object o)
      returns the string length of an object
      Parameters:
      o - the object to check
      Returns:
      the string length of the object
    • compareEqual

      public static boolean compareEqual(Object o1, Object o2)
      Compares two objects for equality
      Parameters:
      o1 - the first object
      o2 - the second object
      Returns:
      true if both objects are equal or false otherwise
    • compare

      public static int compare(Object o1, Object o2)
      Compares two objects for equality
      Parameters:
      o1 - the first object
      o2 - 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 value
      alternative - 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 public static int toInteger(Object v)
      Deprecated.
      Deprecated. Use getValueUtils().toInteger() instead
      Parameters:
      v - the value to convert
      Returns:
      the integer value
    • getInteger

      public static int getInteger(Object v, int defValue)
      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 convert
      defValue - the default value if o is null or conversion is not possible
      Returns:
      the Integer value of v or a default value
    • getInteger

      public static int getInteger(Object v)
      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 public static long toLong(Object v)
      Deprecated.
      Deprecated. Use getValueUtils().toLong() instead
      Parameters:
      v - the value to convert
      Returns:
      the long value
    • getLong

      public static long getLong(Object v, long defValue)
      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 convert
      defValue - the default value if o is null or conversion is not possible
      Returns:
      the Integer value of v or a default value
    • getLong

      public static long getLong(Object v)
      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 public static double toDouble(Object v)
      Deprecated.
      Deprecated. Use getValueUtils().toDouble() instead
      Parameters:
      v - the value to convert
      Returns:
      the double value
    • getDouble

      public static double getDouble(Object v, double defValue)
      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 convert
      defValue - the default value
      Returns:
      the Long value of v or defValue
    • getDouble

      public static double getDouble(Object v)
      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 public static BigDecimal toDecimal(Object v)
      Deprecated.
      Deprecated. Use getValueUtils().toDecimal() instead
      Parameters:
      v - the value to convert
      Returns:
      the decimal value
    • getDecimal

      public static BigDecimal getDecimal(Object v, BigDecimal defValue)
      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 convert
      defValue - the default value
      Returns:
      the BigDecimal value of v or defValue
    • getDecimal

      public static BigDecimal getDecimal(Object v)
      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

      public static boolean getBoolean(Object v, boolean defValue)
      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 convert
      defValue - the default value
      Returns:
      the boolean value or defValue if v is null or empty
    • getBoolean

      public static boolean getBoolean(Object v)
      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

      public static <T extends Enum<?>> T getEnum(Class<T> enumType, Object value)
      Converts an object to an enum of the given type
      Type Parameters:
      T - the type of the enum
      Parameters:
      enumType - the enum type
      value - the value to convert
      Returns:
      the enum
    • getEnumByName

      public static <T extends Enum<?>> T getEnumByName(Class<T> enumType, String name)
      find by name
      Type Parameters:
      T - the type of the enum
      Parameters:
      enumType - the enum type
      name - the enum name
      Returns:
      the enum
    • getEnumValue

      public static Object getEnumValue(Enum<?> enumValue, boolean isNumeric)
      Convert Enum to Object
      Parameters:
      enumValue - the enum
      isNumeric - flag if number or string is required
      Returns:
      the number or string representing this enum
    • getString

      public static String getString(Enum<?> enumValue)
      Converts an Enum to a String
      Parameters:
      enumValue - the enum
      Returns:
      the corresponding string value
    • getString

      public static String getString(Object value)
      Converts an Object to a String
      Parameters:
      value - the value to convert
      Returns:
      the corresponding string value
    • formatColumnValue

      public static String formatColumnValue(ColumnExpr column, Object value, Locale locale)
      Returns a formatted column value
      Parameters:
      column - the column
      value - the value to convert
      locale - the locale (optional)
      Returns:
      the corresponding string value
    • getDate

      public static Date getDate(Object v)
      Converts an object value to a Date.

      Parameters:
      v - the object to convert
      Returns:
      the Date value of v or null
    • getLocalDate

      public static LocalDate getLocalDate(Object v)
      Converts an object value to a LocalDate.

      Parameters:
      v - the object to convert
      Returns:
      the LocalDate value of v or null
    • getLocalDateTime

      public static LocalDateTime getLocalDateTime(Object v)
      Converts an object value to a LocalDateTime.

      Parameters:
      v - the object to convert
      Returns:
      the LocalDateTime value of v or null
    • getTimestamp

      public static Timestamp getTimestamp(Object v)
      Converts an object value to a LocalDateTime.

      Parameters:
      v - the object to convert
      Returns:
      the LocalDateTime value of v or null
    • formatDate

      public static String formatDate(Date date, boolean withTime)
      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 formated
      withTime - indicates whether the date string should include the time or not
      Returns:
      the date string
    • convert

      public static <T> T convert(Class<T> c, Object v) throws ClassCastException
      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 to
      v - 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 access
      value - the value to convert
      valueType - 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

      public static Object convertValue(DataType type, Object value)
      Converts a value to a specific DataType The returned value is used for generating SQL statements
      Parameters:
      type - the target data type
      value - the value to convert
      Returns:
      the value to be used in SQL statements
    • isAssignmentCompatible

      public static boolean isAssignmentCompatible(Class<?> target, Class<?> source)
      Checks if a class is assignment compatible with another class
      Parameters:
      target - the target class
      source - the source class
      Returns:
      true if assignment compatible or false otherwise
    • compareEqual

      public static boolean compareEqual(Object[] array1, Object[] array2)
      Compares two arrays for equality
      Parameters:
      array1 - the first array
      array2 - the second array
      Returns:
      true if both arrays are equal or false otherwise
    • compareEqual

      public static boolean compareEqual(ColumnExpr expr, ColumnExpr other)
      Compares two ColumnExpr for equality
      Parameters:
      expr - a column expression
      other - a column expression
      Returns:
      true if both expressions are equal or false otherwise
    • convert

      public static <T> List<T> convert(Class<T> t, Collection<? extends T> source)
      Generic conversion function that will convert a list to another list type.
      Type Parameters:
      T - the type of elements
      Parameters:
      t - the type class
      source - the source collection
      Returns:
      the new list type
    • toArray

      @SafeVarargs public static <T> T[] toArray(Class<T> t, T... values)
      Converts varArgs to an array
      Type Parameters:
      T - the type of elements
      Parameters:
      t - the type of the array
      values - the array values
      Returns:
      the array
    • toStringArray

      public static String[] toStringArray(Object[] objArray, String defValue)
      Converts an Object array to a String array.
      Parameters:
      objArray - the object array to convert
      defValue - default value which will be set for all null objects
      Returns:
      the String array
    • arrayToList

      public static <T> List<T> arrayToList(Class<T> t, T[] array)
      Converts an array to a list
      Type Parameters:
      T - the type of elements
      Parameters:
      t - the type of the list items
      array - the array to be converted
      Returns:
      the list
    • listToArray

      public static <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)
      Type Parameters:
      T - the type of the items in the resulting array
      Parameters:
      type - the array type
      list - the item list
      Returns:
      the array
    • isWrapper

      public static boolean isWrapper(Object object)
      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 search
      item - 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 search
      item - 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 array
      right - 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 array
      element - the new element
      Returns:
      the combined array