Class ExpressionBuilder

java.lang.Object
net.objecthunter.exp4j.ExpressionBuilder

public class ExpressionBuilder extends Object
Factory class for Expression instances. This class is the main API entrypoint. Users should create new Expression instances using this factory class.
  • Constructor Details

    • ExpressionBuilder

      public ExpressionBuilder(String expression)
      Create a new ExpressionBuilder instance and initialize it with a given expression string.
      Parameters:
      expression - the expression to be parsed
  • Method Details

    • disableBuiltInFunctions

      public ExpressionBuilder disableBuiltInFunctions()
      Removes all the built-in functions
      Returns:
      the ExpressionBuilder instance
    • function

      public ExpressionBuilder function(Function function)
      Add a Function implementation available for use in the expression.
      Parameters:
      function - the custom Function implementation that should be available for use in the expression.
      Returns:
      the ExpressionBuilder instance
    • functions

      public ExpressionBuilder functions(Function... functions)
      Add multiple Function implementations available for use in the expression.
      Parameters:
      functions - the custom Function implementations
      Returns:
      the ExpressionBuilder instance
    • functions

      public ExpressionBuilder functions(List<Function> functions)
      Add multiple Function implementations available for use in the expression
      Parameters:
      functions - A List of custom Function implementations
      Returns:
      the ExpressionBuilder instance
    • variables

      public ExpressionBuilder variables(Set<String> variableNames)
      Add multiple variables that must be used in the expression.

      Note: the "must" part of that statement will change on future versions.
      Parameters:
      variableNames - variables to use
      Returns:
      the ExpressionBuilder instance
      Throws:
      IllegalArgumentException - if the variable name contains spaces or operator characters
    • variables

      public ExpressionBuilder variables(String... variableNames)
      Add multiple variables that must be used in the expression.

      Note: the "must" part of that statement will change on future versions.
      Parameters:
      variableNames - variables to use
      Returns:
      the ExpressionBuilder instance
      Throws:
      IllegalArgumentException - if the variable name contains spaces or operator characters
    • variable

      public ExpressionBuilder variable(String variableName)
      Add a variable that must be used in the expression.

      Note: the "must" part of that statement will change on future versions.
      Parameters:
      variableName - variable to use
      Returns:
      the ExpressionBuilder instance
      Throws:
      IllegalArgumentException - if the variable name contains spaces or operator characters
    • operator

      public ExpressionBuilder operator(Operator operator)
      Add an Operator which should be available for use in the expression
      Parameters:
      operator - the custom Operator to add
      Returns:
      the ExpressionBuilder instance
    • operators

      public ExpressionBuilder operators(Operator... operators)
      Add multiple Operator implementations which should be available for use in the expression
      Parameters:
      operators - the set of custom Operator implementations to add
      Returns:
      the ExpressionBuilder instance
    • operators

      public ExpressionBuilder operators(List<Operator> operators)
      Add multiple Operator implementations which should be available for use in the expression
      Parameters:
      operators - the List of custom Operator implementations to add
      Returns:
      the ExpressionBuilder instance
    • build

      public Expression build()
      Build the Expression instance using the custom operators and functions set.
      Returns:
      an Expression instance which can be used to evaluate the result of the expression
    • build

      public Expression build(boolean simplify)
      Build the Expression instance using the custom operators and functions set.
      Parameters:
      simplify - true if you want to attempt to simplify constants false otherwise
      Returns:
      an Expression instance which can be used to evaluate the result of the expression
    • toString

      public String toString()
      Overrides:
      toString in class Object