Introduction
BigNumbers Core provides the core classes and interfaces reused by most of the other packages. Basically, it provides a set of abstractions for the construction and usage of objects that represent an expression and know how to evaluate this expression for certain set of values passed in. This is how it works:
/** * Calculates (a / 20) * (b + 123.90) */ public void calculateIt(BigDecimal a, BigDecimal b) { ExpressionBuilder builder = ....; Expression expression = builder.create("(a / 20) * (b + 123.90)"); VariableMap map = new VariableMap(); return expression.evaluate(map.assign(a, "a").assign(b, "b")); }
There are three key classes and interfaces, and one additional convenience class.
- Expression
The interface to be implemented by objects that represent expressions. Expressions are required to have the ability to evaluate themselves against a number of variables being passed in.
- ExpressionBuilder
The abstract base class of factories with the ability to create Expression instances. The ExpressionBuilder allows Expression clients to abstract themselves from the way expressions are evaluated. Instead of creating Expressions by calling their constructors, they are able to rely on a common interface that will invoke the appropriate constructor.)
Note that the ExpressionBuilder is an abstract base class and not an interface. This way implementers will get a couple of convenience operations for free. At this stage, there are two known implementations of the ExpressionBuilder: bignumbers-jit. and bignumbers-interpreter.
- VariableResolver
The abstraction of classes that are able to resolve variable values. The example above uses the VariableMap class as the implementation; this is a convenience implementation that holds the variable values itself.
- Function
A convenience wrapper of Expression implementations, allowing you to evaluate the expression in a way that may be more convenient in some situations. Evaluation of a Function results in evaluation of the underlying expression, passing in a VariableResolver that resolves variables from the list of additional arguments passed to the Functions evaluate method.