/** * Wrapper for {@link #evaluate(Expression, ProcessingContext)} that substitutes a replacement value and emits a * runtime warning if the result was NULL. */ public int evaluateNotNull(final Expression expression, final ProcessingContext context, final String messageOnNull, final int replacementOnNull) { final Value evaluated = evaluate(expression, context); int result; if (evaluated.isNull()) { context.fireRuntimeWarning(expression, messageOnNull); result = replacementOnNull; } else { result = ((IntegerValue) evaluated).intValue(); } return result; } }
@Override protected Value evaluateValidSelfAndChildren(final ProcessingContext context, final int depth) { /* Check runtime value of numberRepeats */ final Value numberRepeatsValue = getNumberRepeats().evaluate(this, context); if (numberRepeatsValue.isNull()) { context.fireRuntimeWarning(this, "numberRepeats evaluated to NULL. Returning NULL"); return NullValue.INSTANCE; } final int numberRepeats = ((IntegerValue) numberRepeatsValue).intValue(); if (numberRepeats < 1) { context.fireRuntimeWarning(this, "numberRepeats ended up being less than 1. Returning NULL"); return NullValue.INSTANCE; } /* Now evaluate child expression repeatedly and build up result */ final List<SingleValue> resultList = new ArrayList<SingleValue>(); for (int i=0; i<numberRepeats; i++) { final Value[] childValues = evaluateChildren(context, depth); if (isAnyChildNull(childValues)) { return NullValue.INSTANCE; } for (int j=0; j<childValues.length; j++) { resultList.add((SingleValue) childValues[j]); } } return OrderedValue.createOrderedValue(resultList); } }
@Override protected Value evaluateValidSelf(final ProcessingContext context, final Value[] childValues, final int depth) { if (isAnyChildNull(childValues)) { return NullValue.INSTANCE; } final OrderedValue childOrderedValue = (OrderedValue) childValues[0]; final Value computedIndex = getIndex().evaluate(this, context); if (computedIndex.isNull()) { return NullValue.INSTANCE; } final int index = ((IntegerValue) computedIndex).intValue(); if (index < 1 || index > childOrderedValue.size()) { return NullValue.INSTANCE; } return childOrderedValue.get(index - 1); } }
@Override protected Value evaluateValidSelf(final ProcessingContext context, final Value[] childValues, final int depth) { final Value childValue = childValues[0]; if (childValue.isNull()) { return NullValue.INSTANCE; } final double childNumber = ((NumberValue) childValue).doubleValue(); if (Double.isInfinite(childNumber) || Double.isNaN(childNumber)) { return childValue; } final Value computedFigures = getFigures().evaluate(this, context); if (computedFigures.isNull()) { context.fireRuntimeWarning(this, "Computed value of figures is NULL. Returning NULL"); return NullValue.INSTANCE; } final RoundingMode roundingMode = getRoundingMode(); final int figures = ((IntegerValue) computedFigures).intValue(); if (!roundingMode.isFiguresValid(figures)) { context.fireRuntimeWarning(this, "The computed value of figures (" + figures + ") was not compatible with the constraints of the rounding mode. Returning NULL"); return NullValue.INSTANCE; } final BigDecimal rounded = getRoundingMode().round(childNumber, figures); return new FloatValue(rounded.doubleValue()); } }
@Override protected Value evaluateValidSelf(final ProcessingContext context, final Value[] childValues, final int depth) { if (isAnyChildNull(childValues)) { return NullValue.INSTANCE; } final double firstNumber = ((NumberValue) childValues[0]).doubleValue(); final double secondNumber = ((NumberValue) childValues[1]).doubleValue(); /* Handle NaN or infinite cases by just doing normal double comparison */ if (RoundingMode.isNaNOrInfinite(firstNumber) || RoundingMode.isNaNOrInfinite(secondNumber)) { return BooleanValue.valueOf(firstNumber==secondNumber); } final Value computedFigures = getFigures().evaluate(this, context); if (computedFigures.isNull()) { context.fireRuntimeWarning(this, "Computed value of figures was NULL. Returning NULL"); return NullValue.INSTANCE; } final RoundingMode roundingMode = getRoundingMode(); final int figures = ((IntegerValue) computedFigures).intValue(); if (!roundingMode.isFiguresValid(figures)) { context.fireRuntimeWarning(this, "The computed value of figures (" + figures + ") was not compatible with the constraints of the rounding mode. Returning NULL"); return NullValue.INSTANCE; } return BooleanValue.valueOf(roundingMode.isEqual(firstNumber, secondNumber, figures)); } }