Package net.enilink.commons.util
Class ValueUtils
- java.lang.Object
-
- net.enilink.commons.util.ValueUtils
-
public class ValueUtils extends Object
- Author:
- Ken Wenzel
-
-
Constructor Summary
Constructors Modifier Constructor Description protectedValueUtils()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Objectadd(Object v1, Object v2)Adds the two objects given.BigDecimalbigDecValue(Object value)Evaluates the given object as a BigDecimal.BigIntegerbigIntValue(Object value)Evaluates the given object as a BigInteger.BooleanbooleanValue(Object value)intcompareWithConversion(Object v1, Object v2)intcompareWithConversion(Object v1, Object v2, Number epsilon)ObjectconvertValue(Class<?> toType, Object value, Object defaultValue)Returns the value converted numerically to the given class type.NumbercreateInteger(ValueType type, long value)Returns a new Number object of an appropriate type to hold the given integer value.NumbercreateReal(ValueType type, double value)Returns a new Number object of an appropriate type to hold the given real value.Objectdivide(Object v1, Object v2)Divides the first argument by the seconddoubledoubleValue(Object value)Evaluates the given object as a double-precision floating-point number.static ValueUtilsgetInstance()ValueTypegetNumericType(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.ValueTypegetType(Object value)longlongValue(Object value)Evaluates the given object as a long integer.Objectmultiply(Object v1, Object v2)Multiplies v1 times v2.Objectnegate(Object v1)Negates v1.Objectremainder(Object v1, Object v2)Returns the number of the first argument, v1, divided by the second number's remainder (modulus).StringstringValue(Object value, boolean trim)Evaluates the given object as a String and trims it if the trim flag is true.Objectsubtract(Object v1, Object v2)Subtracts v2 from v1.
-
-
-
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 operatort2- the other argument typecanBeNonNumeric- whether the operator can be interpreted as non-numeric- Returns:
- the appropriate constant from the NumericTypes interface
-
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 Stringtrim- 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 addendv2- 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 multiplicandv2- Second multiplicand- Returns:
- v1 * v2
-
divide
public Object divide(Object v1, Object v2)
Divides the first argument by the second- Parameters:
v1- Dividendv2- 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- Dividendv2- 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 resultvalue- 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 resultvalue- 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 tovalue- an object to be converted to the given typedefaultValue- 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.
-
-