Goal of this Library is to make calculation codes with BigDecimals much more readable and a lot of Java Code. By defining the formula with a string expression, the code should be better understandable and maintainable.
Simple usage:
BigDecimal result = FormulaEvaluator.create("IF(a >= 8, a * betta ^ 3, a / gamma)")
.with("a", a).and("beta", beta).and("gamma", gamma)
.eval();
Define with which precision the calculations should be performed.
Methods:
setPrecision(int)
: Sets calculation and result precisionsetResultPrecision(int)
: Sets result precisionsetCalculationPrecision(int)
: Sets calculation precision (see MathContext)
Define how values should be rounded.
Method:
setRoundingMode(RoundingMode)
: Sets the rounding Mode (see RoundingMode)
Define the generic behavior of null
values.
Method:
setDefaultNullHandling(DefaultNullHandling)
Available options:
EXCEPTION
: Throw an exception if one value is null (default).NULL
: Return null if at least one value of an operation or function is nullZERO
: Null is equal to 0
Example:
BigDecimal result = FormulaEvaluator.create("a / b")
.setDefaultNullHandling(NULL)
.with("a", null)
.with("b", ONE)
.eval();
Define for some basic operations their handling of null
values.
Methods:
setPlusMinusNullHandling(BasicOperationsNullHandling)
setMultiplicationNullHandling(BasicOperationsNullHandling)
setDivisionNullHandling(BasicOperationsNullHandling)
Available options:
INHERIT
: Use default handling (default).IDENTITY
: Operation should return the same value (+-: 0, */: 1)
Define how the calculation should behave on division by zero.
Method:
setDivisionByZeroHandling(DivisionByZeroHandling)
Available options:
INHERIT
: Inherit checks from default.ONE
: Zero is treated as 1. (a / 0 = a)NULL
: When denominator is zero it returns null.
+
(Addition)-
(Subtraction)*
(Multiplication)/
(Division)%
(Modulo)^
(Exponentiation)&&
(logical and)||
(logical or)>
(greater)>=
(greater equal)<
(smaller)<=
(smaller equal)==
or=
(greater)!=
or<>
(not equal)
NOT(term)
IF(condTerm, trueTerm, falseTerm)
RANDOM()
SIN(term)
,COS(term)
,TAN(term)
,SINH(term)
,COSH(term)
,TANH(term)
DEG(term)
,RAD(term)
MIN(term1, term2)
,MAX(term1, term2)
LOG(term)
,LOG10(term)
ROUND(valueTerm, precisionTerm)
FLOOR(term)
CEILING(term)
ABS(term)
SQRT(term)
It is possible to extend the FormulaEvaluator with custom Operators and Functions by registering them in BigDecimalTermFactory
- In a first step the formula will be parsed and converted into PRN notation
- As next based on the PRN a calculation tree is built up which will later be used for the calculations
- Now this tree (Term) can be evaluated by passing all variables
Already evaluated and built up terms will be cached to improve future calculations.