Class ValueUtils

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

public class ValueUtils extends Object
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 Details

  • Constructor Details

    • ValueUtils

      protected ValueUtils()
  • Method Details

    • isEmpty

      public 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.
    • isZero

      public 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
    • compareEqual

      public 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 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
    • toInteger

      public Integer toInteger(Object v)
      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

      public Long toLong(Object v)
      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

      public Double toDouble(Object v)
      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

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

      public Boolean toBoolean(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
    • toEnum

      public <T extends Enum<?>> T toEnum(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
    • toEnumByName

      public <T extends Enum<?>> T toEnumByName(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
    • enumToValue

      public Object enumToValue(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
    • enumToString

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

      public String toString(Object value)
      Converts an Object to a String
      Parameters:
      value - the value to convert
      Returns:
      the corresponding string value
    • lengthOf

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

      public Date toDate(Object v) throws ParseException
      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

      public LocalDate toLocalDate(Object v)
      Converts an object value to a Date.

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

      public LocalDateTime toLocalDateTime(Object v)
      Converts an object value to a Date.

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

      public Timestamp toTimestamp(Object v)
      Converts an object value to a Timestamp.

      Parameters:
      v - the object to convert
      Returns:
      the Timestamp or null
    • formatDate

      public 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
    • formatColumnValue

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

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

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