Class ELSupport


  • public class ELSupport
    extends java.lang.Object
    A helper class that implements the EL Specification.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected static boolean COERCE_TO_ZERO  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.Boolean coerceToBoolean​(ELContext ctx, java.lang.Object obj, boolean primitive)
      Convert an object to Boolean.
      static java.lang.Enum<?> coerceToEnum​(ELContext ctx, java.lang.Object obj, java.lang.Class type)
      Coerces an object to an Enum value of the specified type.
      static java.lang.Number coerceToNumber​(ELContext ctx, java.lang.Object obj, java.lang.Class<?> type)
      Coerces an object to a Number of the specified type.
      protected static java.lang.Number coerceToNumber​(java.lang.Number number, java.lang.Class<?> type)
      Coerces a Number to the specified numeric type.
      protected static java.lang.Number coerceToNumber​(java.lang.String val, java.lang.Class<?> type)
      Coerces a String to a Number of the specified type.
      static java.lang.String coerceToString​(ELContext ctx, java.lang.Object obj)
      Coerce an object to a string.
      static <T> T coerceToType​(ELContext ctx, java.lang.Object obj, java.lang.Class<T> type)
      Coerces an object to the specified target type.
      static int compare​(ELContext ctx, java.lang.Object obj0, java.lang.Object obj1)
      Compare two objects, after coercing to the same type if appropriate.
      static boolean equals​(ELContext ctx, java.lang.Object obj0, java.lang.Object obj1)
      Compare two objects for equality, after coercing to the same type if appropriate.
      static boolean isBigDecimalOp​(java.lang.Object obj0, java.lang.Object obj1)
      Checks if either operand is a BigDecimal.
      static boolean isBigIntegerOp​(java.lang.Object obj0, java.lang.Object obj1)
      Checks if either operand is a BigInteger.
      static boolean isDoubleOp​(java.lang.Object obj0, java.lang.Object obj1)
      Checks if either operand is a Double or Float.
      static boolean isLongOp​(java.lang.Object obj0, java.lang.Object obj1)
      Checks if either operand is a Long, Integer, Character, Short, or Byte.
      static boolean isStringFloat​(java.lang.String str)
      Checks if a string represents a floating-point number by looking for 'E', 'e', or '.' characters.
      • Methods inherited from class java.lang.Object

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

      • COERCE_TO_ZERO

        protected static final boolean COERCE_TO_ZERO
    • Method Detail

      • compare

        public static int compare​(ELContext ctx,
                                  java.lang.Object obj0,
                                  java.lang.Object obj1)
                           throws ELException
        Compare two objects, after coercing to the same type if appropriate.

        If the objects are identical, or they are equal according to equals(ELContext, Object, Object) then return 0.

        If either object is a BigDecimal, then coerce both to BigDecimal first. Similarly for Double(Float), BigInteger, and Long(Integer, Char, Short, Byte).

        Otherwise, check that the first object is an instance of Comparable, and compare against the second object. If that is null, return 1, otherwise return the result of comparing against the second object.

        Similarly, if the second object is Comparable, if the first is null, return -1, else return the result of comparing against the first object.

        A null object is considered as:

        • ZERO when compared with Numbers
        • the empty string for String compares
        • Otherwise null is considered to be lower than anything else.
        Parameters:
        ctx - the context in which this comparison is taking place
        obj0 - first object
        obj1 - second object
        Returns:
        -1, 0, or 1 if this object is less than, equal to, or greater than val.
        Throws:
        ELException - if neither object is Comparable
        java.lang.ClassCastException - if the objects are not mutually comparable
      • equals

        public static boolean equals​(ELContext ctx,
                                     java.lang.Object obj0,
                                     java.lang.Object obj1)
                              throws ELException
        Compare two objects for equality, after coercing to the same type if appropriate.

        If the objects are identical (including both null) return true.

        If either object is null, return false.

        If either object is Boolean, coerce both to Boolean and check equality.

        Similarly for Enum, String, BigDecimal, Double(Float), Long(Integer, Short, Byte, Character)

        Otherwise default to using Object.equals().

        Parameters:
        ctx - the context in which this equality test is taking place
        obj0 - the first object
        obj1 - the second object
        Returns:
        true if the objects are equal
        Throws:
        ELException - if one of the coercion fails
      • coerceToEnum

        public static java.lang.Enum<?> coerceToEnum​(ELContext ctx,
                                                     java.lang.Object obj,
                                                     java.lang.Class type)
        Coerces an object to an Enum value of the specified type.

        If the object is null or an empty string, null is returned. If the object is already an instance of the target enum type, it is returned directly. Otherwise, the object must be a String matching one of the enum constant names.

        Parameters:
        ctx - the EL context
        obj - the object to coerce
        type - the target enum type
        Returns:
        the coerced enum value, or null if the object is null or empty
        Throws:
        ELException - if the object cannot be coerced to the target enum type
      • coerceToBoolean

        public static java.lang.Boolean coerceToBoolean​(ELContext ctx,
                                                        java.lang.Object obj,
                                                        boolean primitive)
                                                 throws ELException
        Convert an object to Boolean. Null and empty string are false.
        Parameters:
        ctx - the context in which this conversion is taking place
        obj - the object to convert
        primitive - is the target a primitive in which case coercion to null is not permitted
        Returns:
        the Boolean value of the object
        Throws:
        ELException - if object is not Boolean or String
      • coerceToNumber

        protected static java.lang.Number coerceToNumber​(java.lang.Number number,
                                                         java.lang.Class<?> type)
                                                  throws ELException
        Coerces a Number to the specified numeric type.
        Parameters:
        number - the number to coerce
        type - the target numeric type
        Returns:
        the coerced number
        Throws:
        ELException - if the number cannot be coerced to the target type
      • coerceToNumber

        public static java.lang.Number coerceToNumber​(ELContext ctx,
                                                      java.lang.Object obj,
                                                      java.lang.Class<?> type)
                                               throws ELException
        Coerces an object to a Number of the specified type.

        Supports coercion from String, Number, and Character types. If the object is null and the target type is not primitive, null is returned (unless COERCE_TO_ZERO is set).

        Parameters:
        ctx - the EL context
        obj - the object to coerce
        type - the target numeric type
        Returns:
        the coerced number, or null if the object is null and the target is not primitive
        Throws:
        ELException - if the object cannot be coerced to the target type
      • coerceToNumber

        protected static java.lang.Number coerceToNumber​(java.lang.String val,
                                                         java.lang.Class<?> type)
                                                  throws ELException
        Coerces a String to a Number of the specified type.
        Parameters:
        val - the string value to parse
        type - the target numeric type
        Returns:
        the coerced number
        Throws:
        ELException - if the string cannot be parsed as the target type
      • coerceToString

        public static java.lang.String coerceToString​(ELContext ctx,
                                                      java.lang.Object obj)
        Coerce an object to a string.
        Parameters:
        ctx - the context in which this conversion is taking place
        obj - the object to convert
        Returns:
        the String value of the object
      • coerceToType

        public static <T> T coerceToType​(ELContext ctx,
                                         java.lang.Object obj,
                                         java.lang.Class<T> type)
                                  throws ELException
        Coerces an object to the specified target type.

        Supports coercion to String, Number, Character, Boolean, Enum, Instant, Date, arrays, and functional interfaces. Uses the ELResolver's convertToType method first if a context is provided.

        Type Parameters:
        T - the target type
        Parameters:
        ctx - the EL context
        obj - the object to coerce
        type - the target type
        Returns:
        the coerced object, or null if the object is null and the target is not primitive
        Throws:
        ELException - if the object cannot be coerced to the target type
      • isBigDecimalOp

        public static boolean isBigDecimalOp​(java.lang.Object obj0,
                                             java.lang.Object obj1)
        Checks if either operand is a BigDecimal.
        Parameters:
        obj0 - the first operand
        obj1 - the second operand
        Returns:
        true if either operand is a BigDecimal
      • isBigIntegerOp

        public static boolean isBigIntegerOp​(java.lang.Object obj0,
                                             java.lang.Object obj1)
        Checks if either operand is a BigInteger.
        Parameters:
        obj0 - the first operand
        obj1 - the second operand
        Returns:
        true if either operand is a BigInteger
      • isDoubleOp

        public static boolean isDoubleOp​(java.lang.Object obj0,
                                         java.lang.Object obj1)
        Checks if either operand is a Double or Float.
        Parameters:
        obj0 - the first operand
        obj1 - the second operand
        Returns:
        true if either operand is a Double or Float
      • isLongOp

        public static boolean isLongOp​(java.lang.Object obj0,
                                       java.lang.Object obj1)
        Checks if either operand is a Long, Integer, Character, Short, or Byte.
        Parameters:
        obj0 - the first operand
        obj1 - the second operand
        Returns:
        true if either operand is a Long-compatible type
      • isStringFloat

        public static boolean isStringFloat​(java.lang.String str)
        Checks if a string represents a floating-point number by looking for 'E', 'e', or '.' characters.
        Parameters:
        str - the string to check
        Returns:
        true if the string contains floating-point notation