Class Operator

java.lang.Object
net.objecthunter.exp4j.operator.Operator
All Implemented Interfaces:
Serializable

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

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

    Constructors
    Constructor
    Description
    Operator(String symbol, int numberOfOperands, boolean leftAssociative, int precedence)
    Create a new operator for use in expressions
  • Method Summary

    Modifier and Type
    Method
    Description
    abstract double
    apply(double... args)
    Apply the operation on the given operands
    int
    Get the number of operands
    int
    Check the precedence value for the operator
    Get the operator symbol
    static boolean
    Check if a character is an allowed operator char
    boolean
    Check if the operator is left associative

    Methods inherited from class java.lang.Object

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

    • PRECEDENCE_ADDITION

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

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

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

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

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

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

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

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

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

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

      public static final int PRECEDENCE_OR
      The precedence value for the boolean or (default: 100)
      See Also:
    • 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:
  • Constructor Details

    • Operator

      public Operator(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 Details

    • 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 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 String getSymbol()
      Get the operator symbol
      Returns:
      the symbol
    • getNumOperands

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