Class OverloadedUnaryFunction
java.lang.Object
com.singularsys.jep.functions.PostfixMathCommand
com.singularsys.jep.functions.UnaryFunction
com.singularsys.jep.misc.overloadedfunctions.OverloadedUnaryFunction
- All Implemented Interfaces:
JepComponent,PostfixMathCommandI,Serializable
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 neg() {
return new MyVal(-val);
}
}
The addition operator could be overload
with a custom function that uses the MyVal.add(MyVal) method.
var fun1 = new OverloadedUnaryFunction(
UnaryFunction.instanceOf(MyVal.class, x -> x.neg() ),
new Add(),
l -> l instanceof MyVal );
jep.getOperatorTable().getUminus().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
UnaryFunction.instanceOf(Class, FunctionWithException) .
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.
With this setup jep.evaluate(jep.parse("'abc' * 3")) returns abcabcabc
and jep.evaluate(jep.parse("4 * 3")) returns 12.
Note in practice you would not want to test on the value of node as it will
not be able to test the value when it is the result of a sub-expression, like
('ab'*2)*2.
- Since:
- Jep 4.1
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from class com.singularsys.jep.functions.UnaryFunction
UnaryFunction.FunctionWithException<T> -
Field Summary
Fields inherited from class com.singularsys.jep.functions.PostfixMathCommand
curNumberOfParameters, description, name, NaN, numberOfParameters -
Constructor Summary
ConstructorsConstructorDescriptionOverloadedUnaryFunction(PostfixMathCommandI function1, PostfixMathCommandI function2, Predicate<Object> argTest) Constructor. -
Method Summary
Modifier and TypeMethodDescriptionEvaluate the functionReturn a description of the function from the properties file.Gets a light-weight instance suitable for using in multiple threads.voidInitialize the component.Methods inherited from class com.singularsys.jep.functions.UnaryFunction
instanceOf, instanceOf, instanceOf, instanceOf, runMethods inherited from class com.singularsys.jep.functions.PostfixMathCommand
asArray, asBool, asDouble, asInt, asLong, asStrictInt, asString, asType, checkNumberOfParameters, getDescription, getDescriptionWithType, getName, getNumberOfParameters, setCurNumberOfParameters, setDescription, setName, toString, toString
-
Constructor Details
-
OverloadedUnaryFunction
public OverloadedUnaryFunction(PostfixMathCommandI function1, PostfixMathCommandI function2, Predicate<Object> argTest) Constructor. Each function can be aUnaryFunctionor any otherPostfixMathCommandIthat uses thePostfixMathCommandI.run(Stack)method.- Parameters:
function1- the first functionfunction2- the second functionargTest- a predicate that tests the arguments
-
-
Method Details
-
eval
Description copied from class:UnaryFunctionEvaluate the function- Specified by:
evalin classUnaryFunction- Parameters:
l- the single argument passed in- Returns:
- the result of the function
- Throws:
EvaluationException- on error
-
init
Description copied from interface:JepComponentInitialize 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:
initin interfaceJepComponent- Parameters:
jep- the current Jep instance
-
getLightWeightInstance
Description copied from interface:JepComponentGets a light-weight instance suitable for using in multiple threads.- Specified by:
getLightWeightInstancein interfaceJepComponent- Returns:
- either a new instance, null or 'this'.
-
getFunction1
-
getFunction2
-
getPredicate
-
getDescription
Description copied from class:PostfixMathCommandReturn a description of the function from the properties file. The property name is the class name followed by ".description". The property is found using theJepMessagesclass, which can be configured to add additional properties files. IfPostfixMathCommand.setDescription(String)has been called, return the description set by that method instead.- Specified by:
getDescriptionin interfacePostfixMathCommandI- Overrides:
getDescriptionin classPostfixMathCommand- Returns:
- the description of the function
-