Formula 1.0.0

Class FunctionDefinedInText

  extended by hirondelle.formula.function.FunctionDefinedInText
All Implemented Interfaces:

public final class FunctionDefinedInText
extends Object
implements Function

Define a custom function at runtime, using simple text.

The main reason this class exists is to let the end user define custom functions at runtime, without the need to write any code.

In addition, this class can also be used in regular code, which can be useful for quick experiments. Another advantage is that it's compact, and reads at a higher level of abstraction.

The main disadvantage of using this class, of course, is that the implementation uses the formula4j parser, and therefore may be a bit slower than an class which implements the Function interface directly. As usual, this may or may not be significant, depending on the context of your application.

Constructor Summary
FunctionDefinedInText(String aFunctionDefinition)
Method Summary
 Decimal calculate(Decimal... aArgs)
          Return the result of evaluating the function.
 String getName()
          Return the name of the function.
Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructor Detail


public FunctionDefinedInText(String aFunctionDefinition)

The single parameter passed to this constructor is a complete function definition. An example:

Function bmi = new FunctionDefinedInText("bmi(m,h) = m/h^2");

The left-hand side of the equation defines both the name of the function, and the ordered sequence of its expected parameters.

The right-hand side of the equation defines how the parameter values are used to calculate the function's result. The right-hand side uses the same syntax as used elsewhere by formula4j.

A function can have 0..N parameters.

Method Detail


public Decimal calculate(Decimal... aArgs)
Return the result of evaluating the function.

The number of arguments must match the number of arguments as defined in the function definition passed to the constructor. There can be no optional arguments.

As usual, the order of the passed arguments must have the exact same order as the arguments as defined in the function definition passed to the constructor.

In this case, no further validations are performed on the arguments.

Specified by:
calculate in interface Function
aArgs - 0 or more arguments to the function.


public String getName()
Return the name of the function.

The name is extracted from the left-hand side of the function definition passed to the constructor.

Formula 1.0.0

Copyright Hirondelle Systems. Published January 31, 2013