|
http://www.eteks.com | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
Interface of functions defined by user. These functions may be of one the two following types :
compileFunction ()
method of a instance of
FunctionParser
: This method returns an instance of CompiledFunction
that implements the Function
interface.Function
interface : This
enables to create some functions with a variable number of parameters (like the function
AND ()
of Jeks spreadsheet), that may run faster than functions of class
CompiledFunction
and that may use parameters of special type (like arrays
of values for the SUM ()
method of Jeks spreadsheet).getFunction ()
method of
the syntax of a parser, if you want to call it in the definition of an other function or an expression.Serializable
, com.eteks.parser
classes can run on a JVM 1.0
(as long as you don't process any serialization of course).
FunctionParser
,
Syntax
Method Summary | |
java.lang.Object |
computeFunction(Interpreter interpreter,
java.lang.Object[] parametersValue)
Returns the result of this function computed with the value of its parameters parametersValue . |
java.lang.String |
getName()
Returns the name of this function. |
boolean |
isValidParameterCount(int count)
Returns true if the number of parameters count
required at runtime by this function is valid. |
Method Detail |
public java.lang.String getName()
FunctionParser
or ExpressionParser
.Syntax.getFunction(java.lang.String)
public boolean isValidParameterCount(int count)
true
if the number of parameters count
required at runtime by this function is valid. This method is called
by the parser to check if the call to a function has the good parameter count.count
- Number of parameters (may be equal to 0).public java.lang.Object computeFunction(Interpreter interpreter, java.lang.Object[] parametersValue)
parametersValue
. The type of the parameters and of the returned value depends
on the interpreter used at runtime. This allows to apply computations on a wide range of
parameter types, from number types (with the classes Long
,
Double
,...) to strings (with the String
class) and also booleans
(Boolean
). Other classes like java.math.BigDecimal
or javax.vecmath.GVector
and javax.vecmath.GMatrix
can be also used.IllegalArgumentException
) if its parameters
have a wrong type to perform the resquested function.AVERAGE ()
function that computes the average value of
a variable number of parameters can be implemented in either following ways :
orclass FunctionAverage implements Function { public String getName () { return "AVERAGE"; } public boolean isValidParameterCount (int parameterCount) { return parameterCount > 0; // At least one parameter } public Object computeFunction (Interpreter interpreter, Object [] parametersValue) { Object sum = parametersValue [0]; for (int i = 1; i < parametersValue.length; i++) // Add the parameters value with the binary operator ADD of the interpreter sum = interpreter.getBinaryOperatorValue (Syntax.OPERATOR_ADD, sum, parametersValue [i]); // Get the value of the literal parametersValue.length in the type used by interpreter Object parameterCount = interpreter.getLiteralValue (new Integer (parametersValue.length)); // Divide the sum by parameterCount with the binary operator DIVIDE of the interpreter return interpreter.getBinaryOperatorValue (Syntax.OPERATOR_DIVIDE, sum, parameterCount); } }
Note that the second way is faster but is less generic because it supports onlyclass FunctionAverage implements Function { public String getName () { return "AVERAGE"; } public boolean isValidParameterCount (int parameterCount) { return parameterCount > 0; // At least one parameter } public Object computeFunction (Interpreter interpreter, Object [] parametersValue) { double sum = 0; for (int i = 0; i < parametersValue.length; i++) if (parametersValue [i] instanceof Number) // Add all numbers parametersValue [i] sum += ((Number)parametersValue [i]).doubleValue (); else throw new IllegalArgumentException (String.valueOf (parametersValue [i]) + " not a number"); return new Double (sum / parametersValue.length); } }
Number
parameters. According to the implementation of interpreter
, the first
way may accept and return different type of values.interpreter
- the runtime interpreter to perform the operations of Syntax
.parametersValue
- the value of parameters (already evaluated if they are expressions).Syntax
,
Interpreter
|
|
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |