Calculator bound to a fixed list of resources, providing MVEL-expression-based filtering and summing for templates. An instance is obtained by calling calc(list) on the Formatter, so it is normally reached from a Velocity or JS template as formatter.calc(someList). Every method evaluates the same MVEL expression once against each resource in the bound list, so cost is proportional to the size of the list.
Methods
eval(String mvelExpr, Object r)
Returns: Object
Evaluates an MVEL expression once, against a single resource as the expression's root context. This is the primitive used internally by sum and filter, evaluating the expression for one item at a time, but it can also be called directly to compute an arbitrary value for a single resource.
| Parameter | Description |
|---|---|
mvelExpr | the MVEL expression to evaluate, in the syntax of the MVEL2 expression language |
r | the object made available to the expression as its root context, typically a CommonResource |
sum(String mvelExpr)
Returns: BigDecimal
Sums a numeric MVEL expression across every resource in the bound list, rounding the total to a whole number. Equivalent to calling sum(mvelExpr, 0).
| Parameter | Description |
|---|---|
mvelExpr | the MVEL expression to evaluate against each resource, expected to resolve to a number |
filter(String mvelExpr)
Returns: ResourceList
Filters the bound list with an MVEL boolean expression, keeping only the resources for which the expression evaluates to true. The expression is evaluated once per item in the list, so cost is proportional to the size of the list.
| Parameter | Description |
|---|---|
mvelExpr | the MVEL boolean expression to test against each resource |
sum(String mvelExpr, int decimals)
Returns: BigDecimal
Sums a numeric MVEL expression across every resource in the bound list, rounding the total to the given number of decimal places. Each resource's value is parsed with the Formatter this Calc was created with.
| Parameter | Description |
|---|---|
mvelExpr | the MVEL expression to evaluate against each resource, expected to resolve to a number |
decimals | the number of decimal places to round the total to |