Class ValueUtils


  • public class ValueUtils
    extends Object
    Author:
    Ken Wenzel
    • Constructor Detail

      • ValueUtils

        protected ValueUtils()
    • Method Detail

      • getInstance

        public static ValueUtils getInstance()
      • getNumericType

        public ValueType getNumericType​(ValueType t1,
                                        ValueType t2,
                                        boolean canBeNonNumeric)
        Returns the constant from the NumericTypes interface that best expresses the type of an operation, which can be either numeric or not, on the two given types.
        Parameters:
        t1 - one argument type to an operator
        t2 - the other argument type
        canBeNonNumeric - whether the operator can be interpreted as non-numeric
        Returns:
        the appropriate constant from the NumericTypes interface
      • compareWithConversion

        public int compareWithConversion​(Object v1,
                                         Object v2)
      • compareWithConversion

        public int compareWithConversion​(Object v1,
                                         Object v2,
                                         Number epsilon)
      • stringValue

        public String stringValue​(Object value,
                                  boolean trim)
        Evaluates the given object as a String and trims it if the trim flag is true.
        Parameters:
        value - an object to interpret as a String
        trim - if true this returned string value is trimmed of whitespace using String.trim().
        Returns:
        the String value implied by the given object as returned by the toString() method, or "null" if the object is null.
      • longValue

        public long longValue​(Object value)
                       throws NumberFormatException
        Evaluates the given object as a long integer.
        Parameters:
        value - an object to interpret as a long integer
        Returns:
        the long integer value implied by the given object
        Throws:
        NumberFormatException - if the given object can't be understood as a long integer
      • doubleValue

        public double doubleValue​(Object value)
                           throws NumberFormatException
        Evaluates the given object as a double-precision floating-point number.
        Parameters:
        value - an object to interpret as a double
        Returns:
        the double value implied by the given object
        Throws:
        NumberFormatException - if the given object can't be understood as a double
      • bigDecValue

        public BigDecimal bigDecValue​(Object value)
                               throws NumberFormatException
        Evaluates the given object as a BigDecimal.
        Parameters:
        value - an object to interpret as a BigDecimal
        Returns:
        the BigDecimal value implied by the given object
        Throws:
        NumberFormatException - if the given object can't be understood as a BigDecimal
      • bigIntValue

        public BigInteger bigIntValue​(Object value)
                               throws NumberFormatException
        Evaluates the given object as a BigInteger.
        Parameters:
        value - an object to interpret as a BigInteger
        Returns:
        the BigInteger value implied by the given object
        Throws:
        NumberFormatException - if the given object can't be understood as a BigInteger
      • add

        public Object add​(Object v1,
                          Object v2)
        Adds the two objects given. For numeric values this will add them numerically; for one of the arguments being non-numeric this will catenate them as Strings.
        Parameters:
        v1 - First addend
        v2 - Second addend
        Returns:
        v1 + v2 or concatenated String
      • subtract

        public Object subtract​(Object v1,
                               Object v2)
        Subtracts v2 from v1.
        Parameters:
        v1 - Value to be subtracted from.
        v2 - Value to be subtracted from v1.
        Returns:
        v1 - v2
      • negate

        public Object negate​(Object v1)
        Negates v1.
        Parameters:
        v1 - Value to be negated.
        Returns:
        -v1
      • multiply

        public Object multiply​(Object v1,
                               Object v2)
        Multiplies v1 times v2.
        Parameters:
        v1 - First multiplicand
        v2 - Second multiplicand
        Returns:
        v1 * v2
      • divide

        public Object divide​(Object v1,
                             Object v2)
        Divides the first argument by the second
        Parameters:
        v1 - Dividend
        v2 - Divisor
        Returns:
        Value of v1 / v2.
      • remainder

        public Object remainder​(Object v1,
                                Object v2)
        Returns the number of the first argument, v1, divided by the second number's remainder (modulus).
        Parameters:
        v1 - Dividend
        v2 - Divisor
        Returns:
        Remainder of dividing v1 / v2.
      • createInteger

        public Number createInteger​(ValueType type,
                                    long value)
        Returns a new Number object of an appropriate type to hold the given integer value. The type of the returned object is consistent with the given type argument, which is a constant from the NumericTypes interface.
        Parameters:
        type - the nominal numeric type of the result
        value - the integer value to convert to a Number object
        Returns:
        a Number object with the given value, of type implied by the type argument
      • createReal

        public Number createReal​(ValueType type,
                                 double value)
        Returns a new Number object of an appropriate type to hold the given real value. The type of the returned object is always either Float or Double, and is only Float if the given type tag (a constant from the NumericTypes interface) is FLOAT.
        Parameters:
        type - the nominal numeric type of the result
        value - the real value to convert to a Number object
        Returns:
        a Number object with the given value, of type implied by the type argument
      • convertValue

        public Object convertValue​(Class<?> toType,
                                   Object value,
                                   Object defaultValue)
        Returns the value converted numerically to the given class type.
        Parameters:
        toType - class type to be converted to
        value - an object to be converted to the given type
        defaultValue - value returned in the event that no conversion is possible to the given type
        Returns:
        converted value of the type given, or defaultValue if the value cannot be converted to the given type.