return sum;
}
if (next instanceof UntypedAtomicValue) {
next = next.convert(BuiltInAtomicType.DOUBLE, true).asAtomic();
} else if (!(next instanceof NumericValue)) {
XPathException err = new XPathException("Input to sum() contains a mix of numeric and non-numeric values");
err.setXPathContext(context);
err.setErrorCode("FORG0006");
err.setLocator(location);
throw err;
}
//sum = ((NumericValue)sum).arithmetic(Token.PLUS, (NumericValue)next, context);
sum = ArithmeticExpression.compute(sum, Calculator.PLUS, next, context);
if (sum.isNaN() && sum instanceof DoubleValue) {
// take an early bath, once we've got a double NaN it's not going to change
return sum;
}
}
} else if (sum instanceof DurationValue) {
if (!((sum instanceof DayTimeDurationValue) || (sum instanceof YearMonthDurationValue))) {
XPathException err = new XPathException("Input to sum() contains a duration that is neither a dayTimeDuration nor a yearMonthDuration");
err.setXPathContext(context);
err.setErrorCode("FORG0006");
err.setLocator(location);
throw err;
}
while (true) {
AtomicValue next = (AtomicValue)iter.next();
if (next == null) {
return sum;
}
if (!(next instanceof DurationValue)) {
XPathException err = new XPathException("Input to sum() contains a mix of duration and non-duration values");
err.setXPathContext(context);
err.setErrorCode("FORG0006");
err.setLocator(location);
throw err;
}
sum = ((DurationValue)sum).add((DurationValue)next);
}
} else {
XPathException err = new XPathException(
"Input to sum() contains a value of type " +
sum.getPrimitiveType().getDisplayName() +
" which is neither numeric, nor a duration");
err.setXPathContext(context);
err.setErrorCode("FORG0006");
err.setLocator(location);
throw err;
}
}