// javadoc inherited
public Value invoke(ExpressionContext context, Value[] arguments)
throws ExpressionException {
FunctionArgumentsValidationHelper.checkArgumentsCount(NAME, 1, arguments.length);
Sequence sequence = arguments[0].getSequence();
Value result;
if (sequence.getLength() == 0) {
result = Sequence.EMPTY;
} else {
NumericValuesSummator summator = new NumericValuesSummator();
summator.addAllValues(sequence);
double sum = summator.getSumAsDouble();
return context.getFactory().createDoubleValue(
Double.isNaN(sum) || Double.isInfinite(sum) ? sum :
new BigDecimal(Double.toString(sum)).divide(BigDecimal.valueOf(sequence.getLength()),
MAX_PRECISION, BigDecimal.ROUND_HALF_UP).doubleValue()
);
}
return result;
}