Class NaryBinaryFunction

java.lang.Object
com.singularsys.jep.functions.PostfixMathCommand
com.singularsys.jep.functions.NaryBinaryFunction
All Implemented Interfaces:
PostfixMathCommandI, Serializable
Direct Known Subclasses:
Add, Concat, JepTest.MyNaryBinary, Multiply, NullWrapTest.MyNaryBinary

public abstract class NaryBinaryFunction extends PostfixMathCommand
Convenient base class for n-ary functions backed by a binary operation. Defines an Object eval(Object l,Object r) method for calculating the result. The operation must be associative.
Author:
Richard Morris
See Also:
  • Constructor Details

    • NaryBinaryFunction

      public NaryBinaryFunction()
  • Method Details

    • checkNumberOfParameters

      public boolean checkNumberOfParameters(int n)
      Description copied from class: PostfixMathCommand
      Checks the number of parameters of the function. Functions which set numberOfParameter=-1 should overload this method
      Specified by:
      checkNumberOfParameters in interface PostfixMathCommandI
      Overrides:
      checkNumberOfParameters in class PostfixMathCommand
      Parameters:
      n - number of parameters function will be called with.
      Returns:
      false if an illegal number of parameters is supplied, true otherwise.
    • run

      public void run(Stack<Object> stack) throws EvaluationException
      Specified by:
      run in interface PostfixMathCommandI
      Overrides:
      run in class PostfixMathCommand
      Parameters:
      stack - arguments for function
      Throws:
      EvaluationException - if function cannot be evaluated
    • eval

      public Object eval(Object[] args) throws EvaluationException
      Evaluate given an array of arguments. Calls the eval(Object, Object) method for the first pair of arguments, then repeatedly with the current result and the next argument.
      Parameters:
      args - array of arguments to function, must have at least one element
      Returns:
      result of function
      Throws:
      EvaluationException - if empty array
    • eval

      public abstract Object eval(Object l, Object r) throws EvaluationException
      Evaluate given a pair of arguments. Must be specified for all implementing classes.
      Parameters:
      l - lhs argument
      r - rhs argument
      Returns:
      result of applying the function to the arguments
      Throws:
      EvaluationException - on error.
    • instanceOf

      Create a NaryBinaryFunction from a lambda expression where arguments can be Objects. For example NaryBinaryFunction.instanceOf((x,y) -> ((Number) x).doubleValue() + ((Number) y).doubleValue())
      Parameters:
      fun - the lambda function
      Returns:
      a new NaryBinaryFunction instance
      Since:
      Jep 4.0
    • instanceOf

      Create a named NaryBinaryFunction from a lambda expression where arguments can be Objects. For example NaryBinaryFunction.instanceOf((x,y) -> ((Number) x).doubleValue() + ((Number) y).doubleValue())
      Parameters:
      fun - the lambda function
      Returns:
      a new NaryBinaryFunction instance
      Since:
      Jep 4.0
    • instanceOf

      public static <T> NaryBinaryFunction instanceOf(Class<T> type, BinaryFunction.BiFunctionWithException<T> fun)
      Create a NaryBinaryFunction from a lambda expression where both arguments are of a specified type. For example NaryBinaryFunction.instanceOf(Integer.class, (x,y) -> x * y) The eval(Object[]) and eval(Object, Object) will throw an IllegalParameterException if any argument is not of the correct type.
      Type Parameters:
      T - type of arguments and return value
      Parameters:
      type - type of the arguments
      fun - the lambda function
      Returns:
      a new NaryBinaryFunction instance
      Since:
      Jep 4.0
    • instanceOf

      public static <T> NaryBinaryFunction instanceOf(Class<T> type, BinaryFunction.BiFunctionWithException<T> fun, String name)
      Create a NaryBinaryFunction from a lambda expression where both arguments are of a specified type. For example NaryBinaryFunction.instanceOf(Integer.class, (x,y) -> x * y) The eval(Object[]) and eval(Object, Object) will throw an IllegalParameterException if any argument is not of the correct type.
      Type Parameters:
      T - type of arguments and return value
      Parameters:
      type - type of the arguments
      fun - the lambda function
      Returns:
      a new NaryBinaryFunction instance
      Since:
      Jep 4.0