Package com.singularsys.jep.functions
Class PostfixMathCommand
java.lang.Object
com.singularsys.jep.functions.PostfixMathCommand
- All Implemented Interfaces:
PostfixMathCommandI
,Serializable
- Direct Known Subclasses:
AbstractInc
,ArrayFunctionBase
,Assign
,BinaryFunction
,BugsTest.StackMessingFunc
,Case
,If
,JepTest.AddFractionsStack
,JepTest.Diff
,JepTest.PiFun
,JepTest.Product
,JepTest.Square
,LazyLogical
,List
,MacroFunction
,NaryBinaryFunction
,NaryFunction
,NullaryFunction
,NullWrappedLazyLogical
,NullWrappedPfmc
,OpEquals
,Sum
,Switch
,SwitchDefault
,TernaryConditional
,UnaryFunction
Function classes extend this class. It is an implementation of the
PostfixMathCommandI interface.
It includes a numberOfParameters member, that is checked when parsing the expression. This member should be initialized to an appropriate value for all classes extending this class. If an arbitrary number of parameters should be allowed, initialize this member to -1.
The asString(int, java.lang.Object)
and similar method are convenience
methods for use by subclasses to ease conversion of arguments to particular types.
- See Also:
-
Field Summary
Modifier and TypeFieldDescriptionprotected int
Number of parameters to be used for the next run() invocation.static final Double
Double.valueOf(Double.NaN)protected int
Number of parameters a function requires. -
Constructor Summary
ConstructorDescriptionCreates a new PostfixMathCommand class.PostfixMathCommand
(int nParam) Creates a new PostfixMathCommand class. -
Method Summary
Modifier and TypeMethodDescriptionprotected Object[]
Pop the arguments off the stack and put them in an arrayprotected boolean
Attempt to convert argument to a booleanprotected double
Converts an argument to a double, with error checking, floating point values will be rounded.protected int
Converts an argument to an int, floating point values will be rounded.protected long
Converts an argument to a long, with error checking, floating point values will be rounded.protected int
asStrictInt
(int position, Object value) Converts an argument to an int, floating point values must represent integers.protected String
Converts an argument to a string, with error checking.boolean
checkNumberOfParameters
(int n) Checks the number of parameters of the function.getName()
The name of the function used in error messagesint
Return the required number of parameters.void
Throws an exception because this method should never be called under normal circumstances.void
setCurNumberOfParameters
(int n) Sets the number of current number of parameters used in the next call of run().void
Sets the name of the function used for error messagestoString()
protected String
Return a string representation of the function with its arguments.
-
Field Details
-
NaN
Double.valueOf(Double.NaN) -
numberOfParameters
protected int numberOfParametersNumber of parameters a function requires. Initialize this value to -1 if any number of parameters should be allowed. -
curNumberOfParameters
protected transient int curNumberOfParametersNumber of parameters to be used for the next run() invocation. Applies only if the required number of parameters is variable (numberOfParameters = -1). -
name
-
-
Constructor Details
-
PostfixMathCommand
public PostfixMathCommand()Creates a new PostfixMathCommand class. -
PostfixMathCommand
public PostfixMathCommand(int nParam) Creates a new PostfixMathCommand class.- Parameters:
nParam
- number of parameters for the function
-
-
Method Details
-
getName
Description copied from interface:PostfixMathCommandI
The name of the function used in error messages- Specified by:
getName
in interfacePostfixMathCommandI
- Returns:
- the function name
-
setName
Description copied from interface:PostfixMathCommandI
Sets the name of the function used for error messages- Specified by:
setName
in interfacePostfixMathCommandI
- Parameters:
name
- the function name
-
getNumberOfParameters
public int getNumberOfParameters()Return the required number of parameters.- Specified by:
getNumberOfParameters
in interfacePostfixMathCommandI
- Returns:
- allowable number of parameters, -1 if a variable number is allowed
-
setCurNumberOfParameters
public void setCurNumberOfParameters(int n) 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 interfacePostfixMathCommandI
- Parameters:
n
- number of parameter for next call of function
-
checkNumberOfParameters
public boolean checkNumberOfParameters(int n) Checks the number of parameters of the function. Functions which set numberOfParameter=-1 should overload this method- Specified by:
checkNumberOfParameters
in interfacePostfixMathCommandI
- Parameters:
n
- number of parameters function will be called with.- Returns:
- false if an illegal number of parameters is supplied, true otherwise.
-
run
Throws an exception because this method should never be called under normal circumstances. Each function should use its ownrun()
method for evaluating the function. This includes popping off the parameters from the stack, and pushing the result back on the stack.- Specified by:
run
in interfacePostfixMathCommandI
- Parameters:
s
- arguments for function- Throws:
EvaluationException
- if function cannot be evaluated
-
asString
Converts an argument to a string, with error checking.- Parameters:
position
- argument number (starting from 0) used in error reportingvalue
- the argument to convert- Returns:
- value cast as a String
- Throws:
IllegalParameterException
- if the argument cannot be cast to a String
-
asInt
Converts an argument to an int, floating point values will be rounded.- Parameters:
position
- argument number (starting from 0) used in error reportingvalue
- the argument to convert- Returns:
- value as a int
- Throws:
IllegalParameterException
- if the argument cannot be cast to a Number
-
asStrictInt
Converts an argument to an int, floating point values must represent integers.- Parameters:
position
- argument number (starting from 0) used in error reportingvalue
- the argument to convert- Returns:
- value as a int
- Throws:
IllegalParameterException
- if the argument cannot be cast to a Number or intValue() != doubleValue()
-
asLong
Converts an argument to a long, with error checking, floating point values will be rounded.- Parameters:
position
- argument number (starting from 0) used in error reportingvalue
- the argument to convert- Returns:
- value as a long
- Throws:
IllegalParameterException
- if the argument cannot be cast to a NumberEvaluationException
-
asDouble
Converts an argument to a double, with error checking, floating point values will be rounded.- Parameters:
position
- argument number (starting from 0) used in error reportingvalue
- the argument to convert- Returns:
- value as a long
- Throws:
IllegalParameterException
- if the argument cannot be cast to a NumberEvaluationException
-
asBool
Attempt to convert argument to a boolean- Parameters:
pos
- position in argument listvalue
- actual value of objectallowNumbers
- if true non-zero numbers are true and zero numbers are false- Returns:
- value as a boolean
- Throws:
EvaluationException
- if value cannot be converted to a boolean, strings, fractional numbers
-
asArray
Pop the arguments off the stack and put them in an array- Parameters:
stack
- input- Returns:
- an array of the arguments of the function in correct order
-
toString
Return a string representation of the function with its arguments.- Parameters:
args
- the arguments to the function- Returns:
- string representation of the function with its arguments
-
toString
-