Formula 1.0.0

hirondelle.formula.function
Interface Function

All Known Implementing Classes:
Abs, Arccosine, Arcsine, Arctangent, Arctangent2, ArithmeticMean, Bearing, Ceil, Combination, Cosine, CubeRoot, DayFromJD, DaysBetween, DaysMinus, DaysPlus, DaysSince, DaysUntil, Degrees, Distance, Exp, Factorial, Floor, FunctionDefinedInText, HyperbolicCosine, HyperbolicSine, HyperbolicTangent, Investment, JulianDate, LoanPayment, LogBase10, Max, Median, Min, ModifiedJulianDate, NaturalLog, Permutation, PopulationStandardDeviation, Power, Radians, RandomGaussian, RandomNumber, Round, SampleStandardDeviation, Sign, Sine, SquareRoot, Sum, Tangent, Weekday

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

calculate

Decimal calculate(Decimal... aArgs)
Calculate the value of a function.

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.

Parameters:
aArgs - 0 or more arguments to the function.

Formula 1.0.0

Copyright Hirondelle Systems. Published January 31, 2013