Package com.singularsys.jep.functions
Class NaryBinaryFunction
- java.lang.Object
-
- com.singularsys.jep.functions.PostfixMathCommand
-
- com.singularsys.jep.functions.NaryBinaryFunction
-
- All Implemented Interfaces:
PostfixMathCommandI
,java.io.Serializable
public abstract class NaryBinaryFunction extends PostfixMathCommand
Convenient base class for n-ary functions backed by a binary operation. Defines anObject eval(Object l,Object r)
method for calculating the result. The operation must be associative.- Author:
- Richard Morris
- See Also:
- Serialized Form
-
-
Field Summary
-
Fields inherited from class com.singularsys.jep.functions.PostfixMathCommand
curNumberOfParameters, name, NaN, numberOfParameters
-
-
Constructor Summary
Constructors Constructor Description NaryBinaryFunction()
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description boolean
checkNumberOfParameters(int n)
Checks the number of parameters of the function.java.lang.Object
eval(java.lang.Object[] args)
Evaluate given an array of argumentsabstract java.lang.Object
eval(java.lang.Object l, java.lang.Object r)
static <T> NaryBinaryFunction
instanceOf(java.lang.Class<T> type, java.util.function.BiFunction<T,T,? super java.lang.Object> fun)
Create a NaryBinaryFunction from a lambda expression where both arguments are of a specified type.static NaryBinaryFunction
instanceOf(java.util.function.BiFunction<java.lang.Object,java.lang.Object,? super java.lang.Object> fun)
Create a NaryBinaryFunction from a lambda expression where arguments can be Objects.void
run(java.util.Stack<java.lang.Object> stack)
Callseval(Object, Object)
-
Methods inherited from class com.singularsys.jep.functions.PostfixMathCommand
asArray, asBool, asDouble, asInt, asLong, asStrictInt, asString, getName, getNumberOfParameters, setCurNumberOfParameters, setName, toString, toString
-
-
-
-
Method Detail
-
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 interfacePostfixMathCommandI
- Overrides:
checkNumberOfParameters
in classPostfixMathCommand
- Parameters:
n
- number of parameters function will be called with.- Returns:
- false if an illegal number of parameters is supplied, true otherwise.
-
run
public final void run(java.util.Stack<java.lang.Object> stack) throws EvaluationException
Callseval(Object, Object)
- Specified by:
run
in interfacePostfixMathCommandI
- Overrides:
run
in classPostfixMathCommand
- Parameters:
stack
- arguments for function- Throws:
EvaluationException
- if function cannot be evaluated
-
eval
public final java.lang.Object eval(java.lang.Object[] args) throws EvaluationException
Evaluate given an array of arguments- 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 java.lang.Object eval(java.lang.Object l, java.lang.Object r) throws EvaluationException
- Throws:
EvaluationException
-
instanceOf
public static NaryBinaryFunction instanceOf(java.util.function.BiFunction<java.lang.Object,java.lang.Object,? super java.lang.Object> fun)
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 <T> NaryBinaryFunction instanceOf(java.lang.Class<T> type, java.util.function.BiFunction<T,T,? super java.lang.Object> 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 anIllegalParameterException
if 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
-
-