Package org.apache.commons.el
Class Coercions
java.lang.Object
org.apache.commons.el.Coercions
This class contains the logic for coercing data types before operators are applied to them.
The following is the list of rules applied for various type conversions.
Applying arithmetic operator Binary operator - A {+,-,*} B if A and B are null return 0 if A or B is BigDecimal, coerce both to BigDecimal and then: if operator is +, returnA.add(B)
if operator is -, returnA.subtract(B)
if operator is *, returnA.multiply(B)
if A or B is Float, Double, or String containing ".", "e", or "E" if A or B is BigInteger, coerce both A and B to BigDecimal and apply operator coerce both A and B to Double and apply operator if A or B is BigInteger, coerce both to BigInteger and then: if operator is +, returnA.add(B)
if operator is -, returnA.subtract(B)
if operator is *, returnA.multiply(B)
otherwise coerce both A and B to Long apply operator if operator results in exception (such as divide by 0), error Binary operator - A {/,div} B if A and B are null return 0 if A or B is a BigDecimal or BigInteger, coerce both to BigDecimal and returnA.divide(B, BigDecimal.ROUND_HALF_UP)
otherwise coerce both A and B to Double apply operator if operator results in exception (such as divide by 0), error Binary operator - A {%,mod} B if A and B are null return 0 if A or B is BigDecimal, Float, Double, or String containing ".", "e" or "E" coerce both to Double apply operator if A or B is BigInteger, coerce both to BigInteger and returnA.remainder(B)
otherwise coerce both A and B to Long apply operator if operator results in exception (such as divide by 0), error Unary minus operator - -A if A is null return 0 if A is BigInteger or BigDecimal, returnA.negate()
if A is String if A contains ".", "e", or "E" coerce to Double, apply operator otherwise coerce to a Long and apply operator if A is Byte,Short,Integer,Long,Float,Double retain type, apply operator if operator results in exception, error otherwise error Applying "empty" operator - empty A if A is null return true if A is zero-length String return true if A is zero-length array return true if A is List and ((List) A).isEmpty() return true if A is Map and ((Map) A).isEmpty() return true if A is Collection an ((Collection) A).isEmpty() return true otherwise return false Applying logical operators Binary operator - A {and,or} B coerce both A and B to Boolean, apply operator NOTE - operator stops as soon as expression can be determined, i.e., A and B and C and D - if B is false, then only A and B is evaluated Unary not operator - not A coerce A to Boolean, apply operator Applying relational operator A {invalid input: '<',>,invalid input: '<'=,>=,lt,gt,lte,gte} B if A==B if operator is >= or invalid input: '<'= return true otherwise return false if A or B is null return false if A or B is BigDecimal, coerce both A and B to BigDecimal and use the return value ofA.compareTo(B)
if A or B is Float or Double coerce both A and B to Double apply operator if A or B is BigInteger, coerce both A and B to BigInteger and use the return value ofA.compareTo(B)
if A or B is Byte,Short,Character,Integer,Long coerce both A and B to Long apply operator if A or B is String coerce both A and B to String, compare lexically if A is Comparable if A.compareTo (B) throws exception error otherwise use result of A.compareTo(B) if B is Comparable if B.compareTo (A) throws exception error otherwise use result of B.compareTo(A) otherwise error Applying equality operator A {==,!=} B if A==B apply operator if A or B is null return false for ==, true for != if A or B is BigDecimal, coerce both A and B to BigDecimal and then: if operator is == or eq, returnA.equals(B)
if operator is != or ne, return!A.equals(B)
if A or B is Float or Double coerce both A and B to Double apply operator if A or B is BigInteger, coerce both A and B to BigInteger and then: if operator is == or eq, returnA.equals(B)
if operator is != or ne, return!A.equals(B)
if A or B is Byte,Short,Character,Integer,Long coerce both A and B to Long apply operator if A or B is Boolean coerce both A and B to Boolean apply operator if A or B is String coerce both A and B to String, compare lexically otherwise if an error occurs while calling A.equals(B) error apply operator to result of A.equals(B) coercions coerce A to String A is String return A A is null return "" A.toString throws exception error otherwise return A.toString coerce A to Number type N A is null or "" return 0 A is Character convert to short, apply following rules A is Boolean error A is Number type N return A A is Number, coerce quietly to type N using the following algorithm If N is BigInteger If A is BigDecimal, returnA.toBigInteger()
Otherwise, returnBigInteger.valueOf(A.longValue())
if N is BigDecimal If A is a BigInteger, returnnew BigDecimal(A)
Otherwise, returnnew BigDecimal(A.doubleValue())
If N is Byte, returnnew Byte(A.byteValue())
If N is Short, returnnew Short(A.shortValue())
If N is Integer, returnnew Integer(A.integerValue())
If N is Long, returnnew Long(A.longValue())
If N is Float, returnnew Float(A.floatValue())
If N is Double, returnnew Double(A.doubleValue())
otherwise ERROR A is String If N is BigDecimal then: Ifnew BigDecimal(A)
throws an exception then ERROR Otherwise, returnnew BigDecimal(A)
If N is BigInteger then: Ifnew BigInteger(A)
throws an exception, then ERROR Otherwise, returnnew BigInteger(A)
newN.valueOf(A)
throws exception error returnN.valueOf(A)
otherwise error coerce A to Character should be A is null or "" return (char) 0 A is Character return A A is Boolean error A is Number with less precision than short coerce quietly - return (char) A A is Number with greater precision than short coerce quietly - return (char) A A is String return A.charAt (0) otherwise error coerce A to Boolean A is null or "" return false A is Boolean return A A is String Boolean.valueOf(A) throws exception error return Boolean.valueOf(A) otherwise error coerce A to any other type T A is null return null A is assignable to T coerce quietly A is String T has no PropertyEditor if A is "", return null otherwise error T's PropertyEditor throws exception if A is "", return null otherwise error otherwise apply T's PropertyEditor otherwise error
- Version:
- $Change: 181177 $$DateTime: 2001/06/26 08:45:09 $$Author: luehe $
- Author:
- Nathan Abramson - Art Technology Group
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic Object
applyArithmeticOperator
(Object pLeft, Object pRight, ArithmeticOperator pOperator, Logger pLogger) Performs all of the necessary type conversions, then calls on the appropriate operator.static Object
applyEqualityOperator
(Object pLeft, Object pRight, EqualityOperator pOperator, Logger pLogger) Performs all of the necessary type conversions, then calls on the appropriate operator.static Object
applyRelationalOperator
(Object pLeft, Object pRight, RelationalOperator pOperator, Logger pLogger) Performs all of the necessary type conversions, then calls on the appropriate operator.static Object
Coerces the given value to the specified class.static Boolean
coerceToBoolean
(Object pValue, Logger pLogger) Coerces a value to a Booleanstatic Character
coerceToCharacter
(Object pValue, Logger pLogger) Coerces a value to a Characterstatic Integer
coerceToInteger
(Object pValue, Logger pLogger) Coerces a value to an Integer, returning null if the coercion isn't possible.static Object
coerceToObject
(Object pValue, Class pClass, Logger pLogger) Coerces a value to the specified Class that is not covered by any of the above cases(package private) static Number
coerceToPrimitiveNumber
(double pValue, Class pClass) Coerces a double to the given primitive number class(package private) static Number
coerceToPrimitiveNumber
(long pValue, Class pClass) Coerces a long to the given primitive number class(package private) static Number
coerceToPrimitiveNumber
(Number pValue, Class pClass) Coerces a Number to the given primitive number classstatic Number
coerceToPrimitiveNumber
(Object pValue, Class pClass, Logger pLogger) Coerces a value to the given primitive number class(package private) static Number
coerceToPrimitiveNumber
(String pValue, Class pClass) Coerces a String to the given primitive number classstatic String
coerceToString
(Object pValue, Logger pLogger) Coerces the specified value to a Stringstatic boolean
isBigDecimal
(Object pObject) Returns true if the given object is BigDecimal.static boolean
isBigInteger
(Object pObject) Returns true if the given object is BigInteger.static boolean
isFloatingPointString
(Object pObject) Returns true if the given string might contain a floating point number - i.e., it contains ".", "e", or "E"static boolean
isFloatingPointType
(Class pClass) Returns true if the given class is of a floating point typestatic boolean
isFloatingPointType
(Object pObject) Returns true if the given Object is of a floating point typestatic boolean
isIntegerType
(Class pClass) Returns true if the given class is of an integer typestatic boolean
isIntegerType
(Object pObject) Returns true if the given Object is of an integer type(package private) static boolean
isNumberClass
(Class pClass) Returns true if the given class is Byte, Short, Integer, Long, Float, Double, BigInteger, or BigDecimal
-
Field Details
-
ZERO
-
-
Constructor Details
-
Coercions
public Coercions()
-
-
Method Details
-
coerce
public static Object coerce(Object pValue, Class pClass, Logger pLogger) throws javax.servlet.jsp.el.ELException Coerces the given value to the specified class.- Throws:
javax.servlet.jsp.el.ELException
-
isNumberClass
Returns true if the given class is Byte, Short, Integer, Long, Float, Double, BigInteger, or BigDecimal -
coerceToString
public static String coerceToString(Object pValue, Logger pLogger) throws javax.servlet.jsp.el.ELException Coerces the specified value to a String- Throws:
javax.servlet.jsp.el.ELException
-
coerceToPrimitiveNumber
public static Number coerceToPrimitiveNumber(Object pValue, Class pClass, Logger pLogger) throws javax.servlet.jsp.el.ELException Coerces a value to the given primitive number class- Throws:
javax.servlet.jsp.el.ELException
-
coerceToInteger
public static Integer coerceToInteger(Object pValue, Logger pLogger) throws javax.servlet.jsp.el.ELException Coerces a value to an Integer, returning null if the coercion isn't possible.- Throws:
javax.servlet.jsp.el.ELException
-
coerceToPrimitiveNumber
static Number coerceToPrimitiveNumber(long pValue, Class pClass) throws javax.servlet.jsp.el.ELException Coerces a long to the given primitive number class- Throws:
javax.servlet.jsp.el.ELException
-
coerceToPrimitiveNumber
static Number coerceToPrimitiveNumber(double pValue, Class pClass) throws javax.servlet.jsp.el.ELException Coerces a double to the given primitive number class- Throws:
javax.servlet.jsp.el.ELException
-
coerceToPrimitiveNumber
static Number coerceToPrimitiveNumber(Number pValue, Class pClass) throws javax.servlet.jsp.el.ELException Coerces a Number to the given primitive number class- Throws:
javax.servlet.jsp.el.ELException
-
coerceToPrimitiveNumber
static Number coerceToPrimitiveNumber(String pValue, Class pClass) throws javax.servlet.jsp.el.ELException Coerces a String to the given primitive number class- Throws:
javax.servlet.jsp.el.ELException
-
coerceToCharacter
public static Character coerceToCharacter(Object pValue, Logger pLogger) throws javax.servlet.jsp.el.ELException Coerces a value to a Character- Throws:
javax.servlet.jsp.el.ELException
-
coerceToBoolean
public static Boolean coerceToBoolean(Object pValue, Logger pLogger) throws javax.servlet.jsp.el.ELException Coerces a value to a Boolean- Throws:
javax.servlet.jsp.el.ELException
-
coerceToObject
public static Object coerceToObject(Object pValue, Class pClass, Logger pLogger) throws javax.servlet.jsp.el.ELException Coerces a value to the specified Class that is not covered by any of the above cases- Throws:
javax.servlet.jsp.el.ELException
-
applyArithmeticOperator
public static Object applyArithmeticOperator(Object pLeft, Object pRight, ArithmeticOperator pOperator, Logger pLogger) throws javax.servlet.jsp.el.ELException Performs all of the necessary type conversions, then calls on the appropriate operator.- Throws:
javax.servlet.jsp.el.ELException
-
applyRelationalOperator
public static Object applyRelationalOperator(Object pLeft, Object pRight, RelationalOperator pOperator, Logger pLogger) throws javax.servlet.jsp.el.ELException Performs all of the necessary type conversions, then calls on the appropriate operator.- Throws:
javax.servlet.jsp.el.ELException
-
applyEqualityOperator
public static Object applyEqualityOperator(Object pLeft, Object pRight, EqualityOperator pOperator, Logger pLogger) throws javax.servlet.jsp.el.ELException Performs all of the necessary type conversions, then calls on the appropriate operator.- Throws:
javax.servlet.jsp.el.ELException
-
isFloatingPointType
Returns true if the given Object is of a floating point type -
isFloatingPointType
Returns true if the given class is of a floating point type -
isFloatingPointString
Returns true if the given string might contain a floating point number - i.e., it contains ".", "e", or "E" -
isIntegerType
Returns true if the given Object is of an integer type -
isIntegerType
Returns true if the given class is of an integer type -
isBigInteger
Returns true if the given object is BigInteger.- Parameters:
pObject
- - Object to evaluate- Returns:
- - true if the given object is BigInteger
-
isBigDecimal
Returns true if the given object is BigDecimal.- Parameters:
pObject
- - Object to evaluate- Returns:
- - true if the given object is BigDecimal
-