Class Coercions


  • public class Coercions
    extends java.lang.Object

    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 Float, Double, or String containing ".", "e", or "E"
           coerce both A and B to Double
           apply operator
         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
         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 Float, Double, or String containing ".", "e" or "E"
           coerce both to Double
           apply operator
         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 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
       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 {<,>,<=,>=,lt,gt,lte,gte} B
         if A==B
           if operator is >= or <=
             return true
           otherwise
             return false
         if A or B is null
           return false
         if A or B is Float or Double
           coerce both A and B to Double
           apply operator
         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 Float or Double
           coerce both A and B to Double
           apply operator
         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 primitive 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 with less precision than N
           coerce quietly
         A is Number with greater precision than N
           coerce quietly
         A is String
           new N.valueOf(A) throws exception
             error
           return N.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
     
    • Constructor Summary

      Constructors 
      Constructor Description
      Coercions()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.Object applyArithmeticOperator​(java.lang.Object pLeft, java.lang.Object pRight, ArithmeticOperator pOperator, Logger pLogger)
      Performs all of the necessary type conversions, then calls on the appropriate operator.
      static java.lang.Object applyEqualityOperator​(java.lang.Object pLeft, java.lang.Object pRight, EqualityOperator pOperator, Logger pLogger)
      Performs all of the necessary type conversions, then calls on the appropriate operator.
      static java.lang.Object applyRelationalOperator​(java.lang.Object pLeft, java.lang.Object pRight, RelationalOperator pOperator, Logger pLogger)
      Performs all of the necessary type conversions, then calls on the appropriate operator.
      static java.lang.Object coerce​(java.lang.Object pValue, java.lang.Class pClass, Logger pLogger)
      Coerces the given value to the specified class.
      static java.lang.Boolean coerceToBoolean​(java.lang.Object pValue, Logger pLogger)
      Coerces a value to a Boolean
      static java.lang.Character coerceToCharacter​(java.lang.Object pValue, Logger pLogger)
      Coerces a value to a Character
      static java.lang.Integer coerceToInteger​(java.lang.Object pValue, Logger pLogger)
      Coerces a value to an Integer, returning null if the coercion isn't possible.
      static java.lang.Object coerceToObject​(java.lang.Object pValue, java.lang.Class pClass, Logger pLogger)
      Coerces a value to the specified Class that is not covered by any of the above cases
      (package private) static java.lang.Number coerceToPrimitiveNumber​(double pValue, java.lang.Class pClass)
      Coerces a double to the given primitive number class
      (package private) static java.lang.Number coerceToPrimitiveNumber​(long pValue, java.lang.Class pClass)
      Coerces a long to the given primitive number class
      (package private) static java.lang.Number coerceToPrimitiveNumber​(java.lang.Number pValue, java.lang.Class pClass)
      Coerces a Number to the given primitive number class
      static java.lang.Number coerceToPrimitiveNumber​(java.lang.Object pValue, java.lang.Class pClass, Logger pLogger)
      Coerces a value to the given primitive number class
      (package private) static java.lang.Number coerceToPrimitiveNumber​(java.lang.String pValue, java.lang.Class pClass)
      Coerces a String to the given primitive number class
      static java.lang.String coerceToString​(java.lang.Object pValue, Logger pLogger)
      Coerces the specified value to a String
      static boolean isFloatingPointString​(java.lang.Object pObject)
      Returns true if the given string might contain a floating point number - i.e., it contains ".", "e", or "E"
      static boolean isFloatingPointType​(java.lang.Class pClass)
      Returns true if the given class is of a floating point type
      static boolean isFloatingPointType​(java.lang.Object pObject)
      Returns true if the given Object is of a floating point type
      static boolean isIntegerType​(java.lang.Class pClass)
      Returns true if the given class is of an integer type
      static boolean isIntegerType​(java.lang.Object pObject)
      Returns true if the given Object is of an integer type
      (package private) static boolean isPrimitiveNumberClass​(java.lang.Class pClass)
      Returns true if the given class is Byte, Short, Integer, Long, Float, Double
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • Coercions

        public Coercions()
    • Method Detail

      • coerce

        public static java.lang.Object coerce​(java.lang.Object pValue,
                                              java.lang.Class pClass,
                                              Logger pLogger)
                                       throws ELException
        Coerces the given value to the specified class.
        Throws:
        ELException
      • isPrimitiveNumberClass

        static boolean isPrimitiveNumberClass​(java.lang.Class pClass)
        Returns true if the given class is Byte, Short, Integer, Long, Float, Double
      • coerceToString

        public static java.lang.String coerceToString​(java.lang.Object pValue,
                                                      Logger pLogger)
                                               throws ELException
        Coerces the specified value to a String
        Throws:
        ELException
      • coerceToPrimitiveNumber

        public static java.lang.Number coerceToPrimitiveNumber​(java.lang.Object pValue,
                                                               java.lang.Class pClass,
                                                               Logger pLogger)
                                                        throws ELException
        Coerces a value to the given primitive number class
        Throws:
        ELException
      • coerceToInteger

        public static java.lang.Integer coerceToInteger​(java.lang.Object pValue,
                                                        Logger pLogger)
                                                 throws ELException
        Coerces a value to an Integer, returning null if the coercion isn't possible.
        Throws:
        ELException
      • coerceToPrimitiveNumber

        static java.lang.Number coerceToPrimitiveNumber​(long pValue,
                                                        java.lang.Class pClass)
                                                 throws ELException
        Coerces a long to the given primitive number class
        Throws:
        ELException
      • coerceToPrimitiveNumber

        static java.lang.Number coerceToPrimitiveNumber​(double pValue,
                                                        java.lang.Class pClass)
                                                 throws ELException
        Coerces a double to the given primitive number class
        Throws:
        ELException
      • coerceToPrimitiveNumber

        static java.lang.Number coerceToPrimitiveNumber​(java.lang.Number pValue,
                                                        java.lang.Class pClass)
                                                 throws ELException
        Coerces a Number to the given primitive number class
        Throws:
        ELException
      • coerceToPrimitiveNumber

        static java.lang.Number coerceToPrimitiveNumber​(java.lang.String pValue,
                                                        java.lang.Class pClass)
                                                 throws ELException
        Coerces a String to the given primitive number class
        Throws:
        ELException
      • coerceToCharacter

        public static java.lang.Character coerceToCharacter​(java.lang.Object pValue,
                                                            Logger pLogger)
                                                     throws ELException
        Coerces a value to a Character
        Throws:
        ELException
      • coerceToBoolean

        public static java.lang.Boolean coerceToBoolean​(java.lang.Object pValue,
                                                        Logger pLogger)
                                                 throws ELException
        Coerces a value to a Boolean
        Throws:
        ELException
      • coerceToObject

        public static java.lang.Object coerceToObject​(java.lang.Object pValue,
                                                      java.lang.Class pClass,
                                                      Logger pLogger)
                                               throws ELException
        Coerces a value to the specified Class that is not covered by any of the above cases
        Throws:
        ELException
      • applyArithmeticOperator

        public static java.lang.Object applyArithmeticOperator​(java.lang.Object pLeft,
                                                               java.lang.Object pRight,
                                                               ArithmeticOperator pOperator,
                                                               Logger pLogger)
                                                        throws ELException
        Performs all of the necessary type conversions, then calls on the appropriate operator.
        Throws:
        ELException
      • applyRelationalOperator

        public static java.lang.Object applyRelationalOperator​(java.lang.Object pLeft,
                                                               java.lang.Object pRight,
                                                               RelationalOperator pOperator,
                                                               Logger pLogger)
                                                        throws ELException
        Performs all of the necessary type conversions, then calls on the appropriate operator.
        Throws:
        ELException
      • applyEqualityOperator

        public static java.lang.Object applyEqualityOperator​(java.lang.Object pLeft,
                                                             java.lang.Object pRight,
                                                             EqualityOperator pOperator,
                                                             Logger pLogger)
                                                      throws ELException
        Performs all of the necessary type conversions, then calls on the appropriate operator.
        Throws:
        ELException
      • isFloatingPointType

        public static boolean isFloatingPointType​(java.lang.Object pObject)
        Returns true if the given Object is of a floating point type
      • isFloatingPointType

        public static boolean isFloatingPointType​(java.lang.Class pClass)
        Returns true if the given class is of a floating point type
      • isFloatingPointString

        public static boolean isFloatingPointString​(java.lang.Object pObject)
        Returns true if the given string might contain a floating point number - i.e., it contains ".", "e", or "E"
      • isIntegerType

        public static boolean isIntegerType​(java.lang.Object pObject)
        Returns true if the given Object is of an integer type
      • isIntegerType

        public static boolean isIntegerType​(java.lang.Class pClass)
        Returns true if the given class is of an integer type