Class JexlArithmetic


  • public class JexlArithmetic
    extends java.lang.Object
    Perform arithmetic, implements JexlOperator methods.

    This is the class to derive to implement new operator behaviors.

    The 5 base arithmetic operators (+, - , *, /, %) follow the same evaluation rules regarding their arguments.

    1. If both are null, result is 0 if arithmetic (or operator) is non-strict, ArithmeticException is thrown otherwise
    2. If both arguments are numberable - any kind of integer including boolean -, coerce both to Long and coerce result to the most precise argument class (boolean < byte < short < int < long); if long operation would cause overflow, return a BigInteger
    3. If either argument is a BigDecimal, coerce both to BigDecimal, operator returns BigDecimal
    4. If either argument is a floating point number, coerce both to Double, operator returns Double
    5. Else treat as BigInteger, perform operation and narrow result to the most precise argument class
    Note that the only exception thrown by JexlArithmetic is and must be ArithmeticException.
    Since:
    2.0
    See Also:
    JexlOperator
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected static java.math.BigDecimal BIGD_DOUBLE_MAX_VALUE
      Double.MAX_VALUE as BigDecimal.
      protected static java.math.BigDecimal BIGD_DOUBLE_MIN_VALUE
      -Double.MAX_VALUE as BigDecimal.
      protected static int BIGD_SCALE
      Default BigDecimal scale.
      protected static java.math.BigInteger BIGI_LONG_MAX_VALUE
      Long.MAX_VALUE as BigInteger.
      protected static java.math.BigInteger BIGI_LONG_MIN_VALUE
      Long.MIN_VALUE as BigInteger.
      private boolean compare321
      Determines if the compare method(Object, Object, String) is overriden in this class or one of its superclasses.
      private java.lang.reflect.Constructor<? extends JexlArithmetic> ctor
      The dynamic constructor.
      static java.util.regex.Pattern FLOAT_PATTERN
      The float regular expression pattern.
      private java.math.MathContext mathContext
      The big decimal math context.
      private int mathScale
      The big decimal scale.
      private boolean strict
      Whether this JexlArithmetic instance behaves in strict or lenient mode.
    • Constructor Summary

      Constructors 
      Constructor Description
      JexlArithmetic​(boolean astrict)
      Creates a JexlArithmetic.
      JexlArithmetic​(boolean astrict, java.math.MathContext bigdContext, int bigdScale)
      Creates a JexlArithmetic.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      java.lang.Object add​(java.lang.Object left, java.lang.Object right)
      Add two values together.
      java.lang.Object and​(java.lang.Object left, java.lang.Object right)
      Performs a bitwise and.
      JexlArithmetic.ArrayBuilder arrayBuilder​(int size)
      Deprecated.
      since 3.3.1
      JexlArithmetic.ArrayBuilder arrayBuilder​(int size, boolean extended)
      Called by the interpreter when evaluating a literal array.
      private static java.lang.Object arrayWrap​(java.lang.Object container)
      Attempts transformation of potential array in an abstract list or leave as is.
      protected java.lang.Number asLongNumber​(boolean strict, java.lang.Object value)
      Checks if value class is a number that can be represented exactly in a long.
      protected java.lang.Number asLongNumber​(java.lang.Object value)
      Checks if value class is a number that can be represented exactly in a long.
      java.lang.Object bitwiseAnd​(java.lang.Object lhs, java.lang.Object rhs)
      Deprecated.
      3.0
      java.lang.Object bitwiseOr​(java.lang.Object lhs, java.lang.Object rhs)
      Deprecated.
      3.0
      java.lang.Object bitwiseXor​(java.lang.Object lhs, java.lang.Object rhs)
      Deprecated.
      3.0
      protected java.lang.Boolean collectionContains​(java.lang.Object collection, java.lang.Object value)
      Checks whether a potential collection contains another.
      protected int compare​(java.lang.Object left, java.lang.Object right, java.lang.String symbol)
      Deprecated.
      3.3
      protected int compare​(java.lang.Object left, java.lang.Object right, JexlOperator operator)
      Performs a comparison.
      java.lang.Object complement​(java.lang.Object val)
      Performs a bitwise complement.
      private static boolean computeCompare321​(JexlArithmetic arithmetic)  
      java.lang.Boolean contains​(java.lang.Object container, java.lang.Object value)
      Test if left contains right (right matches/in left).
      protected java.lang.Object controlNullNullOperands()
      Deprecated.
      3.3
      protected java.lang.Object controlNullNullOperands​(JexlOperator operator)
      The result of +,/,-,*,% when both operands are null.
      protected void controlNullOperand()
      Deprecated.
      3.3
      protected <T> T controlNullOperand​(boolean strictCast, T defaultValue)
      Throws an NullOperand exception if arithmetic is strict-cast.
      java.lang.Object controlReturn​(java.lang.Object returned)
      The last method called before returning a result from a script execution.
      java.lang.Iterable<?> createRange​(java.lang.Object from, java.lang.Object to)
      Creates a literal range.
      protected JexlArithmetic createWithOptions​(boolean astrict, java.math.MathContext bigdContext, int bigdScale)
      Creates a JexlArithmetic instance.
      java.lang.Object decrement​(java.lang.Object val)
      Decrements argument by 1.
      java.lang.Object divide​(java.lang.Object left, java.lang.Object right)
      Divide the left value by the right.
      private int doCompare​(java.lang.Object left, java.lang.Object right, JexlOperator operator)  
      java.lang.Boolean empty​(java.lang.Object object)
      Check for emptiness of various types: Number, Collection, Array, Map, String.
      java.lang.Boolean endsWith​(java.lang.Object left, java.lang.Object right)
      Test if left ends with right.
      boolean equals​(java.lang.Object left, java.lang.Object right)
      Test if left and right are equal.
      java.math.MathContext getMathContext()
      The MathContext instance used for +,-,/,*,% operations on big decimals.
      int getMathScale()
      The BigDecimal scale used for comparison and coercion operations.
      boolean greaterThan​(java.lang.Object left, java.lang.Object right)
      Test if left > right.
      boolean greaterThanOrEqual​(java.lang.Object left, java.lang.Object right)
      Test if left >= right.
      java.lang.Object increment​(java.lang.Object val)
      Increments argument by 1.
      protected java.lang.Object increment​(java.lang.Object val, int incr)
      Add value to number argument.
      java.lang.Boolean isEmpty​(java.lang.Object object)
      Check for emptiness of various types: Number, Collection, Array, Map, String.
      java.lang.Boolean isEmpty​(java.lang.Object object, java.lang.Boolean def)
      Check for emptiness of various types: Number, Collection, Array, Map, String.
      protected boolean isFloatingPoint​(java.lang.Object o)
      Is Object a floating point number.
      protected boolean isFloatingPointNumber​(java.lang.Object val)
      Test if the passed value is a floating point number, i.e.
      protected static boolean isMultiplyExact​(long x, long y, long r)
      Checks if the product of the arguments overflows a long.
      boolean isNegateStable()
      Whether negate called with a given argument will always return the same result.
      protected boolean isNullOperand​(java.lang.Object value)
      Checks if an operand is considered null.
      protected boolean isNumberable​(java.lang.Object o)
      Is Object a whole number.
      boolean isPositivizeStable()
      Whether positivize called with a given argument will always return the same result.
      boolean isStrict()
      Checks whether this JexlArithmetic instance strictly considers null as an error when used as operand unexpectedly.
      boolean isStrict​(JexlOperator operator)
      Checks whether this arithmetic considers a given operator as strict or null-safe.
      boolean lessThan​(java.lang.Object left, java.lang.Object right)
      Test if left < right.
      boolean lessThanOrEqual​(java.lang.Object left, java.lang.Object right)
      Test if left <= right.
      java.lang.Object logicalNot​(java.lang.Object arg)
      Deprecated.
      3.0
      JexlArithmetic.MapBuilder mapBuilder​(int size)
      Deprecated.
      JexlArithmetic.MapBuilder mapBuilder​(int size, boolean extended)
      Called by the interpreter when evaluating a literal map.
      java.lang.Object matches​(java.lang.Object lhs, java.lang.Object rhs)
      Deprecated.
      3.0
      java.lang.Object mod​(java.lang.Object left, java.lang.Object right)
      left value modulo right.
      java.lang.Object multiply​(java.lang.Object left, java.lang.Object right)
      Multiply the left value by the right.
      private java.lang.Number narrow​(java.lang.Class<?> narrow, double value)
      Narrows a double to a float if there is no information loss.
      java.lang.Number narrow​(java.lang.Number original)
      Given a Number, return the value using the smallest type the result will fit into.
      protected boolean narrowAccept​(java.lang.Class<?> narrow, java.lang.Class<?> source)
      Whether we consider the narrow class as a potential candidate for narrowing the source.
      boolean narrowArguments​(java.lang.Object[] args)
      Replace all numbers in an arguments array with the smallest type that will fit.
      protected java.lang.Number narrowBigDecimal​(java.lang.Object lhs, java.lang.Object rhs, java.math.BigDecimal big)
      Given a BigDecimal, attempt to narrow it to an Integer or Long if it fits and one of the arguments is numberable.
      protected java.lang.Number narrowBigInteger​(java.lang.Object lhs, java.lang.Object rhs, java.math.BigInteger big)
      Given a BigInteger, narrow it to an Integer or Long if it fits and the arguments class allow it.
      protected java.lang.Number narrowLong​(java.lang.Object lhs, java.lang.Object rhs, long r)
      Given a long, attempt to narrow it to an int.
      java.lang.Number narrowNumber​(java.lang.Number original, java.lang.Class<?> narrow)
      Given a Number, return the value attempting to narrow it to a target class.
      java.lang.Object negate​(java.lang.Object val)
      Negates a value (unary minus for numbers).
      java.lang.Object not​(java.lang.Object val)
      Performs a logical not.
      JexlArithmetic options​(JexlContext context)
      Apply options to this arithmetic which eventually may create another instance.
      JexlArithmetic options​(JexlEngine.Options options)
      Deprecated.
      3.2
      JexlArithmetic options​(JexlOptions options)
      Apply options to this arithmetic which eventually may create another instance.
      java.lang.Object or​(java.lang.Object left, java.lang.Object right)
      Performs a bitwise or.
      private java.math.BigDecimal parseBigDecimal​(java.lang.String arg)
      Convert a string to a BigDecimal.
      private java.math.BigInteger parseBigInteger​(java.lang.String arg)
      Converts a string to a big integer.
      private double parseDouble​(java.lang.String arg)
      Convert a string to a double.
      private int parseInteger​(java.lang.String arg)
      Converts a string to an int.
      private long parseLong​(java.lang.String arg)
      Converts a string to a long.
      java.lang.Object positivize​(java.lang.Object val)
      Positivize value (unary plus for numbers).
      protected java.math.BigDecimal roundBigDecimal​(java.math.BigDecimal number)
      Ensure a big decimal is rounded by this arithmetic scale and rounding mode.
      JexlArithmetic.SetBuilder setBuilder​(int size)
      Deprecated.
      since 3.3.1
      JexlArithmetic.SetBuilder setBuilder​(int size, boolean extended)
      Called by the interpreter when evaluating a literal set.
      java.lang.Object shiftLeft​(java.lang.Object left, java.lang.Object right)
      Shifts a bit pattern to the right.
      java.lang.Object shiftRight​(java.lang.Object left, java.lang.Object right)
      Shifts a bit pattern to the right.
      java.lang.Object shiftRightUnsigned​(java.lang.Object left, java.lang.Object right)
      Shifts a bit pattern to the right unsigned.
      java.lang.Integer size​(java.lang.Object object)
      Calculate the size of various types: Collection, Array, Map, String.
      java.lang.Integer size​(java.lang.Object object, java.lang.Integer def)
      Calculate the size of various types: Collection, Array, Map, String.
      java.lang.Boolean startsWith​(java.lang.Object left, java.lang.Object right)
      Test if left starts with right.
      boolean strictEquals​(java.lang.Object left, java.lang.Object right)
      Test if left and right are strictly equal.
      java.lang.Object subtract​(java.lang.Object left, java.lang.Object right)
      Subtract the right value from the left.
      boolean testPredicate​(java.lang.Object object)
      Test if a condition is true or false.
      protected java.math.BigDecimal toBigDecimal​(boolean strict, java.lang.Object val)
      Coerce to a BigDecimal.
      java.math.BigDecimal toBigDecimal​(java.lang.Object val)
      Coerce to a BigDecimal.
      protected java.math.BigInteger toBigInteger​(boolean strict, java.lang.Object val)
      Coerce to a BigInteger.
      java.math.BigInteger toBigInteger​(java.lang.Object val)
      Coerce to a BigInteger.
      protected boolean toBoolean​(boolean strict, java.lang.Object val)
      Coerce to a primitive boolean.
      boolean toBoolean​(java.lang.Object val)
      Coerce to a primitive boolean.
      protected double toDouble​(boolean strict, java.lang.Object val)
      Coerce to a primitive double.
      double toDouble​(java.lang.Object val)
      Coerce to a primitive double.
      protected int toInteger​(boolean strict, java.lang.Object val)
      Coerce to a primitive int.
      int toInteger​(java.lang.Object val)
      Coerce to a primitive int.
      protected long toLong​(boolean strict, java.lang.Object val)
      Coerce to a primitive long.
      long toLong​(java.lang.Object val)
      Coerce to a primitive long.
      protected java.lang.String toString​(boolean strict, java.lang.Object val)
      Coerce to a string.
      java.lang.String toString​(java.lang.Object val)
      Coerce to a string.
      java.lang.Object xor​(java.lang.Object left, java.lang.Object right)
      Performs a bitwise xor.
      • Methods inherited from class java.lang.Object

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

      • BIGD_DOUBLE_MAX_VALUE

        protected static final java.math.BigDecimal BIGD_DOUBLE_MAX_VALUE
        Double.MAX_VALUE as BigDecimal.
      • BIGD_DOUBLE_MIN_VALUE

        protected static final java.math.BigDecimal BIGD_DOUBLE_MIN_VALUE
        -Double.MAX_VALUE as BigDecimal.
      • BIGI_LONG_MAX_VALUE

        protected static final java.math.BigInteger BIGI_LONG_MAX_VALUE
        Long.MAX_VALUE as BigInteger.
      • BIGI_LONG_MIN_VALUE

        protected static final java.math.BigInteger BIGI_LONG_MIN_VALUE
        Long.MIN_VALUE as BigInteger.
      • BIGD_SCALE

        protected static final int BIGD_SCALE
        Default BigDecimal scale.
        See Also:
        Constant Field Values
      • FLOAT_PATTERN

        public static final java.util.regex.Pattern FLOAT_PATTERN
        The float regular expression pattern.

        The decimal and exponent parts are optional and captured allowing to determine if the number is a real by checking whether one of these 2 capturing groups is not empty.

      • strict

        private final boolean strict
        Whether this JexlArithmetic instance behaves in strict or lenient mode.
      • mathContext

        private final java.math.MathContext mathContext
        The big decimal math context.
      • mathScale

        private final int mathScale
        The big decimal scale.
      • ctor

        private final java.lang.reflect.Constructor<? extends JexlArithmetic> ctor
        The dynamic constructor.
      • compare321

        private final boolean compare321
        Determines if the compare method(Object, Object, String) is overriden in this class or one of its superclasses.
    • Constructor Detail

      • JexlArithmetic

        public JexlArithmetic​(boolean astrict)
        Creates a JexlArithmetic.

        If you derive your own arithmetic, implement the other constructor that may be needed when dealing with options.

        Parameters:
        astrict - whether this arithmetic is strict or lenient
      • JexlArithmetic

        public JexlArithmetic​(boolean astrict,
                              java.math.MathContext bigdContext,
                              int bigdScale)
        Creates a JexlArithmetic.

        The constructor to define in derived classes.

        Parameters:
        astrict - whether this arithmetic is lenient or strict
        bigdContext - the math context instance to use for +,-,/,*,% operations on big decimals.
        bigdScale - the scale used for big decimals.
    • Method Detail

      • arrayWrap

        private static java.lang.Object arrayWrap​(java.lang.Object container)
        Attempts transformation of potential array in an abstract list or leave as is.

        An array (as in int[]) is not convenient to call methods so when encountered we turn them into lists

        Parameters:
        container - an array or on object
        Returns:
        an abstract list wrapping the array instance or the initial argument
        See Also:
        ArrayListWrapper
      • computeCompare321

        private static boolean computeCompare321​(JexlArithmetic arithmetic)
      • isMultiplyExact

        protected static boolean isMultiplyExact​(long x,
                                                 long y,
                                                 long r)
        Checks if the product of the arguments overflows a long.

        see java8 Math.multiplyExact

        Parameters:
        x - the first value
        y - the second value
        r - the product
        Returns:
        true if product fits a long, false if it overflows
      • add

        public java.lang.Object add​(java.lang.Object left,
                                    java.lang.Object right)
        Add two values together.

        If any numeric add fails on coercion to the appropriate type, treat as Strings and do concatenation.

        Parameters:
        left - left argument
        right - right argument
        Returns:
        left + right.
      • and

        public java.lang.Object and​(java.lang.Object left,
                                    java.lang.Object right)
        Performs a bitwise and.
        Parameters:
        left - the left operand
        right - the right operator
        Returns:
        left & right
      • arrayBuilder

        @Deprecated
        public JexlArithmetic.ArrayBuilder arrayBuilder​(int size)
        Deprecated.
        since 3.3.1
        Creates an array builder.
        Parameters:
        size - the number of elements in the array
        Returns:
        an array builder instance
      • arrayBuilder

        public JexlArithmetic.ArrayBuilder arrayBuilder​(int size,
                                                        boolean extended)
        Called by the interpreter when evaluating a literal array.
        Parameters:
        size - the number of elements in the array
        extended - whether the map is extended or not
        Returns:
        the array builder
      • asLongNumber

        protected java.lang.Number asLongNumber​(boolean strict,
                                                java.lang.Object value)
        Checks if value class is a number that can be represented exactly in a long.

        For convenience, booleans are converted as 1/0 (true/false).

        Parameters:
        strict - whether null argument is converted as 0 or remains null
        value - argument
        Returns:
        a non-null value if argument can be represented by a long
      • asLongNumber

        protected java.lang.Number asLongNumber​(java.lang.Object value)
        Checks if value class is a number that can be represented exactly in a long.

        For convenience, booleans are converted as 1/0 (true/false).

        Parameters:
        value - argument
        Returns:
        a non-null value if argument can be represented by a long
      • bitwiseAnd

        @Deprecated
        public final java.lang.Object bitwiseAnd​(java.lang.Object lhs,
                                                 java.lang.Object rhs)
        Deprecated.
        3.0
        Use or overload and() instead.
        Parameters:
        lhs - left hand side
        rhs - right hand side
        Returns:
        lhs & rhs
        See Also:
        and(java.lang.Object, java.lang.Object)
      • bitwiseOr

        @Deprecated
        public final java.lang.Object bitwiseOr​(java.lang.Object lhs,
                                                java.lang.Object rhs)
        Deprecated.
        3.0
        Use or overload or() instead.
        Parameters:
        lhs - left hand side
        rhs - right hand side
        Returns:
        lhs | rhs
        See Also:
        or(java.lang.Object, java.lang.Object)
      • bitwiseXor

        @Deprecated
        public final java.lang.Object bitwiseXor​(java.lang.Object lhs,
                                                 java.lang.Object rhs)
        Deprecated.
        3.0
        Use or overload xor() instead.
        Parameters:
        lhs - left hand side
        rhs - right hand side
        Returns:
        lhs ^ rhs
        See Also:
        xor(java.lang.Object, java.lang.Object)
      • collectionContains

        protected java.lang.Boolean collectionContains​(java.lang.Object collection,
                                                       java.lang.Object value)
        Checks whether a potential collection contains another.

        Made protected to make it easier to override if needed.

        Parameters:
        collection - the container which can be a collection or an array (even of primitive)
        value - the value which can be a collection or an array (even of primitive) or a singleton
        Returns:
        test result or null if there is no arithmetic solution
      • compare

        protected int compare​(java.lang.Object left,
                              java.lang.Object right,
                              JexlOperator operator)
        Performs a comparison.
        Parameters:
        left - the left operand
        right - the right operator
        operator - the operator
        Returns:
        -1 if left < right; +1 if left > right; 0 if left == right
        Throws:
        java.lang.ArithmeticException - if either left or right is null
      • compare

        @Deprecated
        protected int compare​(java.lang.Object left,
                              java.lang.Object right,
                              java.lang.String symbol)
        Deprecated.
        3.3
        Any override of this method (pre 3.3) should be modified to match the new signature.
        Parameters:
        left - left operand
        right - right operand
        symbol - the operator symbol
        Returns:
        -1 if left < right; +1 if left > right; 0 if left == right compare(Object, Object, JexlOperator)
      • complement

        public java.lang.Object complement​(java.lang.Object val)
        Performs a bitwise complement.
        Parameters:
        val - the operand
        Returns:
        ~val
      • contains

        public java.lang.Boolean contains​(java.lang.Object container,
                                          java.lang.Object value)
        Test if left contains right (right matches/in left).

        Beware that this "contains " method arguments order is the opposite of the "in/matches" operator arguments. x =~ y means y contains x thus contains(x, y).

        When this method returns null during evaluation, the operator code continues trying to find one through the uberspect.

        Parameters:
        container - the container
        value - the value
        Returns:
        test result or null if there is no arithmetic solution
      • controlNullNullOperands

        @Deprecated
        protected java.lang.Object controlNullNullOperands()
        Deprecated.
        3.3
        The result of +,/,-,*,% when both operands are null.
        Returns:
        Integer(0) if lenient
        Throws:
        JexlArithmetic.NullOperand - if strict
      • controlNullNullOperands

        protected java.lang.Object controlNullNullOperands​(JexlOperator operator)
        The result of +,/,-,*,% when both operands are null.
        Parameters:
        operator - the actual operator
        Returns:
        Integer(0) if lenient
        Throws:
        JexlArithmetic.NullOperand - if strict-cast
        Since:
        3.3
      • controlNullOperand

        @Deprecated
        protected void controlNullOperand()
        Deprecated.
        3.3
        Throws an NullOperand exception if arithmetic is strict-cast.
        Throws:
        JexlArithmetic.NullOperand - if strict
      • controlReturn

        public java.lang.Object controlReturn​(java.lang.Object returned)
        The last method called before returning a result from a script execution.
        Parameters:
        returned - the returned value
        Returns:
        the controlled returned value
      • createRange

        public java.lang.Iterable<?> createRange​(java.lang.Object from,
                                                 java.lang.Object to)
                                          throws java.lang.ArithmeticException
        Creates a literal range.

        The default implementation only accepts integers and longs.

        Parameters:
        from - the included lower bound value (null if none)
        to - the included upper bound value (null if none)
        Returns:
        the range as an iterable
        Throws:
        java.lang.ArithmeticException - as an option if creation fails
      • createWithOptions

        protected JexlArithmetic createWithOptions​(boolean astrict,
                                                   java.math.MathContext bigdContext,
                                                   int bigdScale)
        Creates a JexlArithmetic instance. Called by options(...) method when another instance of the same class of arithmetic is required.
        Parameters:
        astrict - whether this arithmetic is lenient or strict
        bigdContext - the math context instance to use for +,-,/,*,% operations on big decimals.
        bigdScale - the scale used for big decimals.
        Returns:
        default is a new JexlArithmetic instance
        Since:
        3.1
        See Also:
        options(org.apache.commons.jexl3.JexlEngine.Options)
      • decrement

        public java.lang.Object decrement​(java.lang.Object val)
        Decrements argument by 1.
        Parameters:
        val - the argument
        Returns:
        val - 1
      • divide

        public java.lang.Object divide​(java.lang.Object left,
                                       java.lang.Object right)
        Divide the left value by the right.
        Parameters:
        left - left argument
        right - right argument
        Returns:
        left / right
        Throws:
        java.lang.ArithmeticException - if right == 0
      • doCompare

        private int doCompare​(java.lang.Object left,
                              java.lang.Object right,
                              JexlOperator operator)
      • empty

        public java.lang.Boolean empty​(java.lang.Object object)
        Check for emptiness of various types: Number, Collection, Array, Map, String.

        Override or overload this method to add new signatures to the size operators.

        Parameters:
        object - the object to check the emptiness of
        Returns:
        the boolean or false if object is not null
        Since:
        3.2
      • endsWith

        public java.lang.Boolean endsWith​(java.lang.Object left,
                                          java.lang.Object right)
        Test if left ends with right.
        Parameters:
        left - left argument
        right - right argument
        Returns:
        left $= right if there is no arithmetic solution
      • equals

        public boolean equals​(java.lang.Object left,
                              java.lang.Object right)
        Test if left and right are equal.
        Parameters:
        left - left argument
        right - right argument
        Returns:
        the test result
      • getMathContext

        public java.math.MathContext getMathContext()
        The MathContext instance used for +,-,/,*,% operations on big decimals.
        Returns:
        the math context
      • getMathScale

        public int getMathScale()
        The BigDecimal scale used for comparison and coercion operations.
        Returns:
        the scale
      • greaterThan

        public boolean greaterThan​(java.lang.Object left,
                                   java.lang.Object right)
        Test if left > right.
        Parameters:
        left - left argument
        right - right argument
        Returns:
        the test result
      • greaterThanOrEqual

        public boolean greaterThanOrEqual​(java.lang.Object left,
                                          java.lang.Object right)
        Test if left >= right.
        Parameters:
        left - left argument
        right - right argument
        Returns:
        the test result
      • increment

        public java.lang.Object increment​(java.lang.Object val)
        Increments argument by 1.
        Parameters:
        val - the argument
        Returns:
        val + 1
      • increment

        protected java.lang.Object increment​(java.lang.Object val,
                                             int incr)
        Add value to number argument.
        Parameters:
        val - the number
        incr - the value to add
        Returns:
        val + incr
      • isEmpty

        public java.lang.Boolean isEmpty​(java.lang.Object object)
        Check for emptiness of various types: Number, Collection, Array, Map, String.
        Parameters:
        object - the object to check the emptiness of
        Returns:
        the boolean or null if there is no arithmetic solution
      • isEmpty

        public java.lang.Boolean isEmpty​(java.lang.Object object,
                                         java.lang.Boolean def)
        Check for emptiness of various types: Number, Collection, Array, Map, String.
        Parameters:
        object - the object to check the emptiness of
        def - the default value if object emptyness can not be determined
        Returns:
        the boolean or null if there is no arithmetic solution
      • isFloatingPoint

        protected boolean isFloatingPoint​(java.lang.Object o)
        Is Object a floating point number.
        Parameters:
        o - Object to be analyzed.
        Returns:
        true if it is a Float or a Double.
      • isFloatingPointNumber

        protected boolean isFloatingPointNumber​(java.lang.Object val)
        Test if the passed value is a floating point number, i.e. a float, double or string with ( "." | "E" | "e").
        Parameters:
        val - the object to be tested
        Returns:
        true if it is, false otherwise.
      • isNegateStable

        public boolean isNegateStable()
        Whether negate called with a given argument will always return the same result.

        This is used to determine whether negate results on number literals can be cached. If the result on calling negate with the same constant argument may change between calls, which means the function is not deterministic, this method must return false.

        Returns:
        true if negate is idempotent, false otherwise
      • isNullOperand

        protected boolean isNullOperand​(java.lang.Object value)
        Checks if an operand is considered null.
        Parameters:
        value - the operand
        Returns:
        true if operand is considered null
      • isNumberable

        protected boolean isNumberable​(java.lang.Object o)
        Is Object a whole number.
        Parameters:
        o - Object to be analyzed.
        Returns:
        true if Integer, Long, Byte, Short or Character.
      • isPositivizeStable

        public boolean isPositivizeStable()
        Whether positivize called with a given argument will always return the same result.

        This is used to determine whether positivize results on number literals can be cached. If the result on calling positivize with the same constant argument may change between calls, which means the function is not deterministic, this method must return false.

        Returns:
        true if positivize is idempotent, false otherwise
      • isStrict

        public boolean isStrict()
        Checks whether this JexlArithmetic instance strictly considers null as an error when used as operand unexpectedly.
        Returns:
        true if strict, false if lenient
      • isStrict

        public boolean isStrict​(JexlOperator operator)
        Checks whether this arithmetic considers a given operator as strict or null-safe.

        When an operator is strict, it does not accept null arguments when the arithmetic is strict. If null-safe (ie not-strict), the operator does accept null arguments even if the arithmetic itself is strict.

        The default implementation considers equal/not-equal operators as null-safe so one can check for null as in if (myvar == null) {...}. Note that this operator is used for equal and not-equal syntax. The complete list of operators that are not strict are (==, [], []=, ., .=, empty, size, contains).

        An arithmetic refining its strict behavior handling for more operators must declare which by overriding this method.

        Parameters:
        operator - the operator to check for null-argument(s) handling
        Returns:
        true if operator considers null arguments as errors, false if operator has appropriate semantics for null argument(s)
      • lessThan

        public boolean lessThan​(java.lang.Object left,
                                java.lang.Object right)
        Test if left < right.
        Parameters:
        left - left argument
        right - right argument
        Returns:
        the test result
      • lessThanOrEqual

        public boolean lessThanOrEqual​(java.lang.Object left,
                                       java.lang.Object right)
        Test if left <= right.
        Parameters:
        left - left argument
        right - right argument
        Returns:
        the test result
      • logicalNot

        @Deprecated
        public final java.lang.Object logicalNot​(java.lang.Object arg)
        Deprecated.
        3.0
        Use or overload not() instead.
        Parameters:
        arg - argument
        Returns:
        !arg
        See Also:
        not(java.lang.Object)
      • mapBuilder

        @Deprecated
        public JexlArithmetic.MapBuilder mapBuilder​(int size)
        Deprecated.
        Creates a map-builder.
        Parameters:
        size - the number of elements in the map
        Returns:
        a map-builder instance
      • mapBuilder

        public JexlArithmetic.MapBuilder mapBuilder​(int size,
                                                    boolean extended)
        Called by the interpreter when evaluating a literal map.
        Parameters:
        size - the number of elements in the map
        extended - whether the map is extended or not
        Returns:
        the map builder
      • matches

        @Deprecated
        public final java.lang.Object matches​(java.lang.Object lhs,
                                              java.lang.Object rhs)
        Deprecated.
        3.0
        Use or overload contains() instead.
        Parameters:
        lhs - left hand side
        rhs - right hand side
        Returns:
        contains(rhs, lhs)
        See Also:
        contains(java.lang.Object, java.lang.Object)
      • mod

        public java.lang.Object mod​(java.lang.Object left,
                                    java.lang.Object right)
        left value modulo right.
        Parameters:
        left - left argument
        right - right argument
        Returns:
        left % right
        Throws:
        java.lang.ArithmeticException - if right == 0.0
      • multiply

        public java.lang.Object multiply​(java.lang.Object left,
                                         java.lang.Object right)
        Multiply the left value by the right.
        Parameters:
        left - left argument
        right - right argument
        Returns:
        left * right.
      • narrow

        private java.lang.Number narrow​(java.lang.Class<?> narrow,
                                        double value)
        Narrows a double to a float if there is no information loss.
        Parameters:
        value - the double value
        narrow - the target narrow class
        Returns:
        the narrowed or initial number
      • narrow

        public java.lang.Number narrow​(java.lang.Number original)
        Given a Number, return the value using the smallest type the result will fit into.

        This works hand in hand with parameter 'widening' in Java method calls, e.g. a call to substring(int,int) with an int and a long will fail, but a call to substring(int,int) with an int and a short will succeed.

        Parameters:
        original - the original number.
        Returns:
        a value of the smallest type the original number will fit into.
      • narrowAccept

        protected boolean narrowAccept​(java.lang.Class<?> narrow,
                                       java.lang.Class<?> source)
        Whether we consider the narrow class as a potential candidate for narrowing the source.
        Parameters:
        narrow - the target narrow class
        source - the original source class
        Returns:
        true if attempt to narrow source to target is accepted
      • narrowArguments

        public boolean narrowArguments​(java.lang.Object[] args)
        Replace all numbers in an arguments array with the smallest type that will fit.
        Parameters:
        args - the argument array
        Returns:
        true if some arguments were narrowed and args array is modified, false if no narrowing occurred and args array has not been modified
      • narrowBigDecimal

        protected java.lang.Number narrowBigDecimal​(java.lang.Object lhs,
                                                    java.lang.Object rhs,
                                                    java.math.BigDecimal big)
        Given a BigDecimal, attempt to narrow it to an Integer or Long if it fits and one of the arguments is numberable.
        Parameters:
        lhs - the left-hand side operand that lead to the bigd result
        rhs - the right-hand side operand that lead to the bigd result
        big - the BigDecimal to narrow
        Returns:
        an Integer or Long if narrowing is possible, the original BigDecimal otherwise
      • narrowBigInteger

        protected java.lang.Number narrowBigInteger​(java.lang.Object lhs,
                                                    java.lang.Object rhs,
                                                    java.math.BigInteger big)
        Given a BigInteger, narrow it to an Integer or Long if it fits and the arguments class allow it.

        The rules are: if either arguments is a BigInteger, no narrowing will occur if either arguments is a Long, no narrowing to Integer will occur

        Parameters:
        lhs - the left-hand side operand that lead to the bigi result
        rhs - the right-hand side operand that lead to the bigi result
        big - the BigInteger to narrow
        Returns:
        an Integer or Long if narrowing is possible, the original BigInteger otherwise
      • narrowLong

        protected java.lang.Number narrowLong​(java.lang.Object lhs,
                                              java.lang.Object rhs,
                                              long r)
        Given a long, attempt to narrow it to an int.

        Narrowing will only occur if no operand is a Long.

        Parameters:
        lhs - the left hand side operand that lead to the long result
        rhs - the right hand side operand that lead to the long result
        r - the long to narrow
        Returns:
        an Integer if narrowing is possible, the original Long otherwise
      • narrowNumber

        public java.lang.Number narrowNumber​(java.lang.Number original,
                                             java.lang.Class<?> narrow)
        Given a Number, return the value attempting to narrow it to a target class.
        Parameters:
        original - the original number
        narrow - the attempted target class
        Returns:
        the narrowed number or the source if no narrowing was possible
      • negate

        public java.lang.Object negate​(java.lang.Object val)
        Negates a value (unary minus for numbers).
        Parameters:
        val - the value to negate
        Returns:
        the negated value
        See Also:
        isNegateStable()
      • not

        public java.lang.Object not​(java.lang.Object val)
        Performs a logical not.
        Parameters:
        val - the operand
        Returns:
        !val
      • or

        public java.lang.Object or​(java.lang.Object left,
                                   java.lang.Object right)
        Performs a bitwise or.
        Parameters:
        left - the left operand
        right - the right operator
        Returns:
        left | right
      • parseBigDecimal

        private java.math.BigDecimal parseBigDecimal​(java.lang.String arg)
                                              throws java.lang.ArithmeticException
        Convert a string to a BigDecimal. Empty string is considered as 0.
        Parameters:
        arg - the arg
        Returns:
        a BigDecimal
        Throws:
        JexlArithmetic.CoercionException - if the string can not be coerced into a BigDecimal
        java.lang.ArithmeticException
      • parseBigInteger

        private java.math.BigInteger parseBigInteger​(java.lang.String arg)
                                              throws java.lang.ArithmeticException
        Converts a string to a big integer. Empty string is considered as 0.
        Parameters:
        arg - the arg
        Returns:
        a big integer
        Throws:
        java.lang.ArithmeticException - if the string can not be coerced into a big integer
      • parseDouble

        private double parseDouble​(java.lang.String arg)
                            throws java.lang.ArithmeticException
        Convert a string to a double. Empty string is considered as NaN.
        Parameters:
        arg - the arg
        Returns:
        a double
        Throws:
        java.lang.ArithmeticException - if the string can not be coerced into a double
      • parseInteger

        private int parseInteger​(java.lang.String arg)
                          throws java.lang.ArithmeticException
        Converts a string to an int.

        This ensure the represented number is a natural (not a real).

        Parameters:
        arg - the arg
        Returns:
        an int
        Throws:
        java.lang.ArithmeticException - if the string can not be coerced into a long
      • parseLong

        private long parseLong​(java.lang.String arg)
                        throws java.lang.ArithmeticException
        Converts a string to a long.

        This ensure the represented number is a natural (not a real).

        Parameters:
        arg - the arg
        Returns:
        a long
        Throws:
        java.lang.ArithmeticException - if the string can not be coerced into a long
      • positivize

        public java.lang.Object positivize​(java.lang.Object val)
        Positivize value (unary plus for numbers).

        C/C++/C#/Java perform integral promotion of the operand, ie cast to int if type can be represented as int without loss of precision.

        Parameters:
        val - the value to positivize
        Returns:
        the positive value
        See Also:
        isPositivizeStable()
      • roundBigDecimal

        protected java.math.BigDecimal roundBigDecimal​(java.math.BigDecimal number)
        Ensure a big decimal is rounded by this arithmetic scale and rounding mode.
        Parameters:
        number - the big decimal to round
        Returns:
        the rounded big decimal
      • setBuilder

        @Deprecated
        public JexlArithmetic.SetBuilder setBuilder​(int size)
        Deprecated.
        since 3.3.1
        Creates a set-builder.
        Parameters:
        size - the number of elements in the set
        Returns:
        a set-builder instance
      • setBuilder

        public JexlArithmetic.SetBuilder setBuilder​(int size,
                                                    boolean extended)
        Called by the interpreter when evaluating a literal set.
        Parameters:
        size - the number of elements in the set
        extended - whether the set is extended or not
        Returns:
        the array builder
      • shiftLeft

        public java.lang.Object shiftLeft​(java.lang.Object left,
                                          java.lang.Object right)
        Shifts a bit pattern to the right.
        Parameters:
        left - left argument
        right - right argument
        Returns:
        left << right.
      • shiftRight

        public java.lang.Object shiftRight​(java.lang.Object left,
                                           java.lang.Object right)
        Shifts a bit pattern to the right.
        Parameters:
        left - left argument
        right - right argument
        Returns:
        left >> right.
      • shiftRightUnsigned

        public java.lang.Object shiftRightUnsigned​(java.lang.Object left,
                                                   java.lang.Object right)
        Shifts a bit pattern to the right unsigned.
        Parameters:
        left - left argument
        right - right argument
        Returns:
        left >>> right.
      • size

        public java.lang.Integer size​(java.lang.Object object)
        Calculate the size of various types: Collection, Array, Map, String.
        Parameters:
        object - the object to get the size of
        Returns:
        the size of object, 0 if null, 1 if there is no better solution
      • size

        public java.lang.Integer size​(java.lang.Object object,
                                      java.lang.Integer def)
        Calculate the size of various types: Collection, Array, Map, String.
        Parameters:
        object - the object to get the size of
        def - the default value if object size can not be determined
        Returns:
        the size of object or null if there is no arithmetic solution
      • startsWith

        public java.lang.Boolean startsWith​(java.lang.Object left,
                                            java.lang.Object right)
        Test if left starts with right.
        Parameters:
        left - left argument
        right - right argument
        Returns:
        left ^= right or null if there is no arithmetic solution
      • strictEquals

        public boolean strictEquals​(java.lang.Object left,
                                    java.lang.Object right)
        Test if left and right are strictly equal.

        They must have the same class, comparable and the comparison returns 0.

        Parameters:
        left - left argument
        right - right argument
        Returns:
        the test result
      • subtract

        public java.lang.Object subtract​(java.lang.Object left,
                                         java.lang.Object right)
        Subtract the right value from the left.
        Parameters:
        left - left argument
        right - right argument
        Returns:
        left - right.
      • testPredicate

        public boolean testPredicate​(java.lang.Object object)
        Test if a condition is true or false.
        Parameters:
        object - the object to use as condition
        Returns:
        true or false
        Since:
        3.3
      • toBigDecimal

        protected java.math.BigDecimal toBigDecimal​(boolean strict,
                                                    java.lang.Object val)
        Coerce to a BigDecimal.

        Double.NaN, null and empty string coerce to zero.

        Boolean false is 0, true is 1.

        Parameters:
        strict - true if the calling operator or casting is strict, false otherwise
        val - the object to be coerced.
        Returns:
        a BigDecimal.
        Throws:
        java.lang.ArithmeticException - if val is null and mode is strict or if coercion is not possible
        Since:
        3.3
      • toBigDecimal

        public java.math.BigDecimal toBigDecimal​(java.lang.Object val)
        Coerce to a BigDecimal.

        Double.NaN, null and empty string coerce to zero.

        Boolean false is 0, true is 1.

        Parameters:
        val - the object to be coerced.
        Returns:
        a BigDecimal.
        Throws:
        java.lang.ArithmeticException - if val is null and mode is strict or if coercion is not possible
      • toBigInteger

        protected java.math.BigInteger toBigInteger​(boolean strict,
                                                    java.lang.Object val)
        Coerce to a BigInteger.

        Double.NaN, null and empty string coerce to zero.

        Boolean false is 0, true is 1.

        Parameters:
        strict - true if the calling operator or casting is strict, false otherwise
        val - the object to be coerced.
        Returns:
        a BigDecimal
        Throws:
        java.lang.ArithmeticException - if val is null and mode is strict or if coercion is not possible
        Since:
        3.3
      • toBigInteger

        public java.math.BigInteger toBigInteger​(java.lang.Object val)
        Coerce to a BigInteger.

        Double.NaN, null and empty string coerce to zero.

        Boolean false is 0, true is 1.

        Parameters:
        val - the object to be coerced.
        Returns:
        a BigDecimal
        Throws:
        java.lang.ArithmeticException - if val is null and mode is strict or if coercion is not possible
      • toBoolean

        protected boolean toBoolean​(boolean strict,
                                    java.lang.Object val)
        Coerce to a primitive boolean.

        Double.NaN, null, "false" and empty string coerce to false.

        Parameters:
        val - value to coerce
        strict - true if the calling operator or casting is strict, false otherwise
        Returns:
        the boolean value if coercion is possible, true if value was not null.
      • toBoolean

        public boolean toBoolean​(java.lang.Object val)
        Coerce to a primitive boolean.

        Double.NaN, null, "false" and empty string coerce to false.

        Parameters:
        val - value to coerce
        Returns:
        the boolean value if coercion is possible, true if value was not null.
      • toDouble

        protected double toDouble​(boolean strict,
                                  java.lang.Object val)
        Coerce to a primitive double.

        Double.NaN, null and empty string coerce to zero.

        Boolean false is 0, true is 1.

        Parameters:
        strict - true if the calling operator or casting is strict, false otherwise
        val - value to coerce.
        Returns:
        The double coerced value.
        Throws:
        java.lang.ArithmeticException - if val is null and mode is strict or if coercion is not possible
        Since:
        3.3
      • toDouble

        public double toDouble​(java.lang.Object val)
        Coerce to a primitive double.

        Double.NaN, null and empty string coerce to zero.

        Boolean false is 0, true is 1.

        Parameters:
        val - value to coerce.
        Returns:
        The double coerced value.
        Throws:
        java.lang.ArithmeticException - if val is null and mode is strict or if coercion is not possible
      • toInteger

        protected int toInteger​(boolean strict,
                                java.lang.Object val)
        Coerce to a primitive int.

        Double.NaN, null and empty string coerce to zero.

        Boolean false is 0, true is 1.

        Parameters:
        strict - true if the calling operator or casting is strict, false otherwise
        val - value to coerce
        Returns:
        the value coerced to int
        Throws:
        java.lang.ArithmeticException - if val is null and mode is strict or if coercion is not possible
        Since:
        3.3
      • toInteger

        public int toInteger​(java.lang.Object val)
        Coerce to a primitive int.

        Double.NaN, null and empty string coerce to zero.

        Boolean false is 0, true is 1.

        Parameters:
        val - value to coerce
        Returns:
        the value coerced to int
        Throws:
        java.lang.ArithmeticException - if val is null and mode is strict or if coercion is not possible
      • toLong

        protected long toLong​(boolean strict,
                              java.lang.Object val)
        Coerce to a primitive long.

        Double.NaN, null and empty string coerce to zero.

        Boolean false is 0, true is 1.

        Parameters:
        strict - true if the calling operator or casting is strict, false otherwise
        val - value to coerce
        Returns:
        the value coerced to long
        Throws:
        java.lang.ArithmeticException - if value is null and mode is strict or if coercion is not possible
        Since:
        3.3
      • toLong

        public long toLong​(java.lang.Object val)
        Coerce to a primitive long.

        Double.NaN, null and empty string coerce to zero.

        Boolean false is 0, true is 1.

        Parameters:
        val - value to coerce
        Returns:
        the value coerced to long
        Throws:
        java.lang.ArithmeticException - if value is null and mode is strict or if coercion is not possible
      • toString

        protected java.lang.String toString​(boolean strict,
                                            java.lang.Object val)
        Coerce to a string.

        Double.NaN coerce to the empty string.

        Parameters:
        strict - true if the calling operator or casting is strict, false otherwise
        val - value to coerce.
        Returns:
        The String coerced value.
        Throws:
        java.lang.ArithmeticException - if val is null and mode is strict or if coercion is not possible
        Since:
        3.3
      • toString

        public java.lang.String toString​(java.lang.Object val)
        Coerce to a string.

        Double.NaN coerce to the empty string.

        Parameters:
        val - value to coerce.
        Returns:
        The String coerced value.
        Throws:
        java.lang.ArithmeticException - if val is null and mode is strict or if coercion is not possible
      • xor

        public java.lang.Object xor​(java.lang.Object left,
                                    java.lang.Object right)
        Performs a bitwise xor.
        Parameters:
        left - the left operand
        right - the right operator
        Returns:
        left ^ right