Class Operator

  • All Implemented Interfaces:
    java.io.Serializable

    public abstract class Operator
    extends java.lang.Object
    implements java.io.Serializable
    Class representing operators that can be used in an expression
    See Also:
    Serialized Form
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static double BOOLEAN_THRESHOLD
      This is the threshold used to consider false values false, it has a default value of 1.0E-12
      static int PRECEDENCE_ADDITION
      The precedence value for the addition operation (default: 500)
      static int PRECEDENCE_AND
      The precedence value for the boolean and (default: 200)
      static int PRECEDENCE_DIVISION
      The precedence value for the division operation (default: 1000)
      static int PRECEDENCE_MODULO
      The precedence value for the modulo operation (default: 1000)
      static int PRECEDENCE_MULTIPLICATION
      The precedence value for the multiplication operation (default: 1000)
      static int PRECEDENCE_NOT
      The precedence value for the boolean not (default: 300)
      static int PRECEDENCE_OR
      The precedence value for the boolean or (default: 100)
      static int PRECEDENCE_POWER
      The precedence value for the power operation (default: 10000)
      static int PRECEDENCE_SUBTRACTION
      The precedence value for the subtraction operation (default: 500)
      static int PRECEDENCE_UNARY_MINUS
      The precedence value for the unary minus operation (default: 5000)
      static int PRECEDENCE_UNARY_PLUS
      The precedence value for the unary plus operation (default: 5000)
    • Constructor Summary

      Constructors 
      Constructor Description
      Operator​(java.lang.String symbol, int numberOfOperands, boolean leftAssociative, int precedence)
      Create a new operator for use in expressions
    • Field Detail

      • PRECEDENCE_ADDITION

        public static final int PRECEDENCE_ADDITION
        The precedence value for the addition operation (default: 500)
        See Also:
        Constant Field Values
      • PRECEDENCE_SUBTRACTION

        public static final int PRECEDENCE_SUBTRACTION
        The precedence value for the subtraction operation (default: 500)
        See Also:
        Constant Field Values
      • PRECEDENCE_MULTIPLICATION

        public static final int PRECEDENCE_MULTIPLICATION
        The precedence value for the multiplication operation (default: 1000)
        See Also:
        Constant Field Values
      • PRECEDENCE_DIVISION

        public static final int PRECEDENCE_DIVISION
        The precedence value for the division operation (default: 1000)
        See Also:
        Constant Field Values
      • PRECEDENCE_MODULO

        public static final int PRECEDENCE_MODULO
        The precedence value for the modulo operation (default: 1000)
        See Also:
        Constant Field Values
      • PRECEDENCE_POWER

        public static final int PRECEDENCE_POWER
        The precedence value for the power operation (default: 10000)
        See Also:
        Constant Field Values
      • PRECEDENCE_UNARY_MINUS

        public static final int PRECEDENCE_UNARY_MINUS
        The precedence value for the unary minus operation (default: 5000)
        See Also:
        Constant Field Values
      • PRECEDENCE_UNARY_PLUS

        public static final int PRECEDENCE_UNARY_PLUS
        The precedence value for the unary plus operation (default: 5000)
        See Also:
        Constant Field Values
      • PRECEDENCE_NOT

        public static final int PRECEDENCE_NOT
        The precedence value for the boolean not (default: 300)
        See Also:
        Constant Field Values
      • PRECEDENCE_AND

        public static final int PRECEDENCE_AND
        The precedence value for the boolean and (default: 200)
        See Also:
        Constant Field Values
      • PRECEDENCE_OR

        public static final int PRECEDENCE_OR
        The precedence value for the boolean or (default: 100)
        See Also:
        Constant Field Values
      • BOOLEAN_THRESHOLD

        public static final double BOOLEAN_THRESHOLD
        This is the threshold used to consider false values false, it has a default value of 1.0E-12
        See Also:
        Constant Field Values
    • Constructor Detail

      • Operator

        public Operator​(java.lang.String symbol,
                        int numberOfOperands,
                        boolean leftAssociative,
                        int precedence)
        Create a new operator for use in expressions
        Parameters:
        symbol - the symbol of the operator
        numberOfOperands - the number of operands the operator takes (1 or 2)
        leftAssociative - set to true if the operator is left associative, false if it is right associative
        precedence - the precedence value of the operator
    • Method Detail

      • isAllowedOperatorChar

        public static boolean isAllowedOperatorChar​(char ch)
        Check if a character is an allowed operator char
        Parameters:
        ch - the char to check
        Returns:
        true if the char is allowed an an operator symbol, false otherwise
      • isLeftAssociative

        public boolean isLeftAssociative()
        Check if the operator is left associative
        Returns:
        true os the operator is left associative, false otherwise
      • getPrecedence

        public int getPrecedence()
        Check the precedence value for the operator
        Returns:
        the precedence value
      • apply

        public abstract double apply​(double... args)
        Apply the operation on the given operands
        Parameters:
        args - the operands for the operation
        Returns:
        the calculated result of the operation
      • getSymbol

        public java.lang.String getSymbol()
        Get the operator symbol
        Returns:
        the symbol
      • getNumOperands

        public int getNumOperands()
        Get the number of operands
        Returns:
        the number of operands