Class Operators


  • public class Operators
    extends java.lang.Object
    Helper class to deal with operator overloading and specifics.
    Since:
    3.0
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      protected Operators​(InterpreterBase owner)
      Constructs a new instance.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      private java.lang.Object[] arguments​(JexlOperator operator, java.lang.Object... args)
      Tidy arguments based on operator arity.
      protected boolean contains​(JexlNode node, java.lang.String op, java.lang.Object left, java.lang.Object right)
      The 'match'/'in' operator implementation.
      protected void controlNullOperands​(JexlArithmetic arithmetic, JexlOperator operator, java.lang.Object... args)
      Throw a NPE if operator is strict and one of the arguments is null.
      protected java.lang.Object empty​(JexlNode node, java.lang.Object object)
      Check for emptyness of various types: Collection, Array, Map, String, and anything that has a boolean isEmpty() method.
      protected boolean endsWith​(JexlNode node, java.lang.String operator, java.lang.Object left, java.lang.Object right)
      The 'endsWith' operator implementation.
      private boolean isArithmetic​(JexlMethod vm)
      Checks whether a method is a JexlArithmetic method.
      private static boolean isPostfix​(JexlOperator operator)
      Helper for postfix assignment operators.
      private boolean returnsBoolean​(JexlMethod vm)
      Checks whether a method returns a boolean or a Boolean.
      private boolean returnsInteger​(JexlMethod vm)
      Checks whether a method returns an int or an Integer.
      protected java.lang.Object size​(JexlNode node, java.lang.Object object)
      Calculate the size of various types: Collection, Array, Map, String, and anything that has a int size() method.
      protected boolean startsWith​(JexlNode node, java.lang.String operator, java.lang.Object left, java.lang.Object right)
      The 'startsWith' operator implementation.
      protected java.lang.Object tryAssignOverload​(JexlNode node, JexlOperator operator, java.util.function.Consumer<java.lang.Object> assignFun, java.lang.Object... args)
      Evaluates an assign operator.
      protected java.lang.Object tryOverload​(JexlNode node, JexlOperator operator, java.lang.Object... args)
      Attempts to call an operator.
      • Methods inherited from class java.lang.Object

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

      • Operators

        protected Operators​(InterpreterBase owner)
        Constructs a new instance.
        Parameters:
        owner - the owning interpreter
    • Method Detail

      • isPostfix

        private static boolean isPostfix​(JexlOperator operator)
        Helper for postfix assignment operators.
        Parameters:
        operator - the operator
        Returns:
        true if operator is a postfix operator (x++, y--)
      • arguments

        private java.lang.Object[] arguments​(JexlOperator operator,
                                             java.lang.Object... args)
        Tidy arguments based on operator arity.

        The interpreter may add a null to the arguments of operator expecting only one parameter.

        Parameters:
        operator - the operator
        args - the arguements (as seen by the interpreter)
        Returns:
        the tidied arguments
      • contains

        protected boolean contains​(JexlNode node,
                                   java.lang.String op,
                                   java.lang.Object left,
                                   java.lang.Object right)
        The 'match'/'in' operator implementation.

        Note that 'x in y' or 'x matches y' means 'y contains x' ; the JEXL operator arguments order syntax is the reverse of this method call.

        Parameters:
        node - the node
        op - the calling operator, =~ or !~
        right - the left operand
        left - the right operand
        Returns:
        true if left matches right, false otherwise
      • controlNullOperands

        protected void controlNullOperands​(JexlArithmetic arithmetic,
                                           JexlOperator operator,
                                           java.lang.Object... args)
        Throw a NPE if operator is strict and one of the arguments is null.
        Parameters:
        arithmetic - the JEXL arithmetic instance
        operator - the operator to check
        args - the operands
        Throws:
        JexlArithmetic.NullOperand - if operator is strict and an operand is null
      • empty

        protected java.lang.Object empty​(JexlNode node,
                                         java.lang.Object object)
        Check for emptyness of various types: Collection, Array, Map, String, and anything that has a boolean isEmpty() method.

        Note that the result may not be a boolean.

        Parameters:
        node - the node holding the object
        object - the object to check the emptyness of
        Returns:
        the evaluation result
      • endsWith

        protected boolean endsWith​(JexlNode node,
                                   java.lang.String operator,
                                   java.lang.Object left,
                                   java.lang.Object right)
        The 'endsWith' operator implementation.
        Parameters:
        node - the node
        operator - the calling operator, ^= or ^!
        left - the left operand
        right - the right operand
        Returns:
        true if left ends with right, false otherwise
      • isArithmetic

        private boolean isArithmetic​(JexlMethod vm)
        Checks whether a method is a JexlArithmetic method.
        Parameters:
        vm - the JexlMethod (may be null)
        Returns:
        true of false
      • returnsBoolean

        private boolean returnsBoolean​(JexlMethod vm)
        Checks whether a method returns a boolean or a Boolean.
        Parameters:
        vm - the JexlMethod (may be null)
        Returns:
        true of false
      • returnsInteger

        private boolean returnsInteger​(JexlMethod vm)
        Checks whether a method returns an int or an Integer.
        Parameters:
        vm - the JexlMethod (may be null)
        Returns:
        true of false
      • size

        protected java.lang.Object size​(JexlNode node,
                                        java.lang.Object object)
        Calculate the size of various types: Collection, Array, Map, String, and anything that has a int size() method.

        Note that the result may not be an integer.

        Parameters:
        node - the node that gave the value to size
        object - the object to get the size of
        Returns:
        the evaluation result
      • startsWith

        protected boolean startsWith​(JexlNode node,
                                     java.lang.String operator,
                                     java.lang.Object left,
                                     java.lang.Object right)
        The 'startsWith' operator implementation.
        Parameters:
        node - the node
        operator - the calling operator, $= or $!
        left - the left operand
        right - the right operand
        Returns:
        true if left starts with right, false otherwise
      • tryAssignOverload

        protected java.lang.Object tryAssignOverload​(JexlNode node,
                                                     JexlOperator operator,
                                                     java.util.function.Consumer<java.lang.Object> assignFun,
                                                     java.lang.Object... args)
        Evaluates an assign operator.

        This takes care of finding and caching the operator method when appropriate. If an overloads returns Operator.ASSIGN, it means the side-effect is complete. Otherwise, a += b <=> a = a + b

        Parameters:
        node - the syntactic node
        operator - the operator
        args - the arguments, the first one being the target of assignment
        Returns:
        JexlOperator.ASSIGN if operation assignment has been performed, JexlEngine.TRY_FAILED if no operation was performed, the value to use as the side effect argument otherwise
      • tryOverload

        protected java.lang.Object tryOverload​(JexlNode node,
                                               JexlOperator operator,
                                               java.lang.Object... args)
        Attempts to call an operator.

        This performs the null argument control against the strictness of the operator.

        This takes care of finding and caching the operator method when appropriate.

        Parameters:
        node - the syntactic node
        operator - the operator
        args - the arguments
        Returns:
        the result of the operator evaluation or TRY_FAILED