Package com.singularsys.jep.misc.overloadedfunctions

# Class OverloadedNaryFunction

java.lang.Object
com.singularsys.jep.functions.PostfixMathCommand
com.singularsys.jep.functions.NaryFunction
com.singularsys.jep.misc.overloadedfunctions.OverloadedNaryFunction

All Implemented Interfaces:
  `JepComponent`, `PostfixMathCommandI`, `Serializable`

public class OverloadedNaryFunction extends NaryFunction implements JepComponent
Allows function to be overloaded with two candidate functions and a predicate that tests the values of the arguments to see which function should be applied. For example if we have a custom value type

            class MyVal {
                    double val;

                    public MyVal(double val) {
                            super();
                            this.val = val;
                    }

                    MyVal add(MyVal arg) {
                            return new MyVal(val+arg.val);
                    }
            }

The nary addition operator could be overload with a custom function that uses the `MyVal.add(MyVal)` method.

            var fun1 = new OverloadedUnaryFunction(
                    var fun1 =      new OverloadedNaryFunction(
                                    NaryBinaryFunction.instanceOf(MyVal.class, (x,y) -> x.add(y) ),
                                    new Add(),
                                    args -> args[0] instanceof MyVal);
            jep.getOperatorTable().getAdd().setPFMC(fun1);
            jep.reinitializeComponents();

In the constructor for the OverloadedFunction the first argument is our new function created by using a lambda expression and the static method `NaryBinaryFunction#instanceOf(Class,BiFunctionWithException)`. The second argument is the regular unary minus function and the third argument is the predicate that returns true when the argument is a MyVal.

Since:
  Jep 4.1
See Also:
  - Serialized Form

- ## Field Summary

  ### Fields inherited from class com.singularsys.jep.functions.PostfixMathCommand

  `curNumberOfParameters, description, name, NaN, numberOfParameters`

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `OverloadedNaryFunction(PostfixMathCommandI function1, PostfixMathCommandI function2, Predicate<Object[]> argTest)`

- ## Method Summary

  Modifier and Type
  Method
  Description
  `Object`
  `eval(Object[] args)`
  Evaluate the function
  `String`
  `getDescription()`
  Return a description of the function from the properties file.
  `Object`
  `getFunction1()`
  `PostfixMathCommandI`
  `getFunction2()`
  `JepComponent`
  `getLightWeightInstance()`
  Gets a light-weight instance suitable for using in multiple threads.
  `int`
  `getNumberOfParameters()`
  Return the required number of parameters.
  `Predicate<Object[]>`
  `getPredicate()`
  `void`
  `init(Jep jep)`
  Initialize the component.
  `void`
  `setCurNumberOfParameters(int n)`
  Sets the number of current number of parameters used in the next call of run().

  ### Methods inherited from class com.singularsys.jep.functions.NaryFunction

  `run`

  ### Methods inherited from class com.singularsys.jep.functions.PostfixMathCommand

  `asArray, asBool, asDouble, asInt, asLong, asStrictInt, asString, asType, checkNumberOfParameters, getDescription, getDescriptionWithType, getName, setDescription, setName, toString, toString`

  ### Methods inherited from class java.lang.Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

- ## Constructor Details

  - ### OverloadedNaryFunction

    public OverloadedNaryFunction(PostfixMathCommandI function1, PostfixMathCommandI function2, Predicate<Object[]> argTest)

- ## Method Details

  - ### setCurNumberOfParameters

    public void setCurNumberOfParameters(int n)
    Description copied from class: `PostfixMathCommand`
    Sets the number of current number of parameters used in the next call of run(). This method is only called when the reqNumberOfParameters is -1.
    Specified by:
      `setCurNumberOfParameters` in interface `PostfixMathCommandI`
    Overrides:
      `setCurNumberOfParameters` in class `PostfixMathCommand`
    Parameters:
      `n` - number of parameter for next call of function

  - ### getNumberOfParameters

    public int getNumberOfParameters()
    Description copied from class: `PostfixMathCommand`
    Return the required number of parameters.
    Specified by:
      `getNumberOfParameters` in interface `PostfixMathCommandI`
    Overrides:
      `getNumberOfParameters` in class `PostfixMathCommand`
    Returns:
      allowable number of parameters, -1 if a variable number is allowed

  - ### eval

    public Object eval(Object[] args) throws EvaluationException
    Description copied from class: `NaryFunction`
    Evaluate the function
    Specified by:
      `eval` in class `NaryFunction`
    Parameters:
      `args` - arguments to the function
    Returns:
      value returned by the function
    Throws:
      `EvaluationException` - if the calculation cannot be performed

  - ### init

    public void init(Jep jep)
    Description copied from interface: `JepComponent`
    Initialize the component. This method is called whenever a component is added to Jep. Hence, it allows components to keep track of the other components they may rely on.
    Specified by:
      `init` in interface `JepComponent`
    Parameters:
      `jep` - the current Jep instance

  - ### getLightWeightInstance

    public JepComponent getLightWeightInstance()
    Description copied from interface: `JepComponent`
    Gets a light-weight instance suitable for using in multiple threads.
    Specified by:
      `getLightWeightInstance` in interface `JepComponent`
    Returns:
      either a new instance, null or 'this'.

  - ### getFunction1

    public Object getFunction1()

  - ### getFunction2

    public PostfixMathCommandI getFunction2()

  - ### getPredicate

    public Predicate<Object[]> getPredicate()

  - ### getDescription

    public String getDescription()
    Description copied from class: `PostfixMathCommand`
    Return a description of the function from the properties file. The property name is the class name followed by ".description". The property is found using the `JepMessages` class, which can be configured to add additional properties files. If `PostfixMathCommand.setDescription(String)` has been called, return the description set by that method instead.
    Specified by:
      `getDescription` in interface `PostfixMathCommandI`
    Overrides:
      `getDescription` in class `PostfixMathCommand`
    Returns:
      the description of the function