|
Formula 1.0.0 | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
public interface Function
The fundamental interface implemented by all functions.
All functions present in your formulas, either built-in or custom, must be represented by an implementation of this interface.
Implementations must:
Most functions are not hard to implement. It's highly recommended that you implement your custom functions as immutable objects.
Here are some examples. Note that these classes have no fields, and are declared as final. Therefore, these classes are immutable.
Example 1.
public final class Abs implements Function { public Decimal calculate(Decimal... aArgs) { Check.oneArg(aArgs); //ensure there's a single arg return aArgs[0].abs(); //return its absolute value } }
Example 2.
public final class LoanPayment implements Function { public Decimal calculate(Decimal... aArgs) { Check.numArgs(3, aArgs); // principal (>0), annual interest rate (>0), num payments (int, > 0) Decimal principal = aArgs[0]; Decimal interest = aArgs[1]; Decimal numPayments = aArgs[2]; Check.positive(principal, "Principal"); Check.positive(interest, "Annual interest rate"); Check.positiveInteger(numPayments, "Number of monthly payments"); Decimal monthlyInterest = interest.div(12); Decimal a = Decimal.ONE.plus(monthlyInterest).pow(numPayments.intValue()); Decimal result = a.times(principal).times(monthlyInterest); result = result.div(a.minus(1)); return result; } }
Method Summary | |
---|---|
Decimal |
calculate(Decimal... aArgs)
Calculate the value of a function. |
Method Detail |
---|
Decimal calculate(Decimal... aArgs)
It's highly recommended that the implementation validate the number of arguments
passed to the function. The Check
class can be used for doing the most common
argument validations.
aArgs
- 0 or more arguments to the function.
|
Formula 1.0.0 | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |