Package com.singularsys.jep.functions
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
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:
-
Field Summary
Fields inherited from class com.singularsys.jep.functions.PostfixMathCommand
curNumberOfParameters, description, name, NaN, numberOfParameters -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleancheckNumberOfParameters(int n) Checks the number of parameters of the function.Evaluate given an array of arguments.abstract ObjectEvaluate given a pair of arguments.static NaryBinaryFunctionCreate a NaryBinaryFunction from a lambda expression where arguments can be Objects.static NaryBinaryFunctioninstanceOf(BinaryFunction.BiFunctionWithException<Object> fun, String name) Create a named NaryBinaryFunction from a lambda expression where arguments can be Objects.static <T> NaryBinaryFunctioninstanceOf(Class<T> type, BinaryFunction.BiFunctionWithException<T> fun) Create a NaryBinaryFunction from a lambda expression where both arguments are of a specified type.static <T> NaryBinaryFunctioninstanceOf(Class<T> type, BinaryFunction.BiFunctionWithException<T> fun, String name) Create a NaryBinaryFunction from a lambda expression where both arguments are of a specified type.voidCallseval(Object, Object)Methods inherited from class com.singularsys.jep.functions.PostfixMathCommand
asArray, asBool, asDouble, asInt, asLong, asStrictInt, asString, asType, getDescription, getDescription, getDescriptionWithType, getName, getNumberOfParameters, setCurNumberOfParameters, setDescription, setName, toString, toString
-
Constructor Details
-
NaryBinaryFunction
public NaryBinaryFunction()
-
-
Method Details
-
checkNumberOfParameters
public boolean checkNumberOfParameters(int n) Description copied from class:PostfixMathCommandChecks the number of parameters of the function. Functions which set numberOfParameter=-1 should overload this method- Specified by:
checkNumberOfParametersin interfacePostfixMathCommandI- Overrides:
checkNumberOfParametersin classPostfixMathCommand- Parameters:
n- number of parameters function will be called with.- Returns:
- false if an illegal number of parameters is supplied, true otherwise.
-
run
Callseval(Object, Object)- Specified by:
runin interfacePostfixMathCommandI- Overrides:
runin classPostfixMathCommand- Parameters:
stack- arguments for function- Throws:
EvaluationException- if function cannot be evaluated
-
eval
Evaluate given an array of arguments. Calls theeval(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
Evaluate given a pair of arguments. Must be specified for all implementing classes.- Parameters:
l- lhs argumentr- 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 exampleNaryBinaryFunction.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 NaryBinaryFunction instanceOf(BinaryFunction.BiFunctionWithException<Object> fun, String name) Create a named NaryBinaryFunction from a lambda expression where arguments can be Objects. For exampleNaryBinaryFunction.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 exampleNaryBinaryFunction.instanceOf(Integer.class, (x,y) -> x * y)Theeval(Object[])andeval(Object, Object)will throw anIllegalParameterExceptionif any argument is not of the correct type.- Type Parameters:
T- type of arguments and return value- Parameters:
type- type of the argumentsfun- 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 exampleNaryBinaryFunction.instanceOf(Integer.class, (x,y) -> x * y)Theeval(Object[])andeval(Object, Object)will throw anIllegalParameterExceptionif any argument is not of the correct type.- Type Parameters:
T- type of arguments and return value- Parameters:
type- type of the argumentsfun- the lambda function- Returns:
- a new NaryBinaryFunction instance
- Since:
- Jep 4.0
-