throws XQueryException {
// If $arg is the empty sequence, the empty sequence is returned.
if(argv.isEmpty()) {
return ValueSequence.EMPTY_SEQUENCE;
}
Item arg = argv.getItem(0);
Iterator<? extends Item> argItor = arg.iterator();
Item firstItem = argItor.next();
if(firstItem instanceof UntypedAtomicValue) {
// If $arg contains values of type xdt:untypedAtomic they are cast to xs:double.
firstItem = ((UntypedAtomicValue) firstItem).castAs(DoubleType.DOUBLE, dynEnv);
}
if(!argItor.hasNext()) {
return firstItem;
}
if(firstItem instanceof XNumber) {
XNumber sum = (XNumber) firstItem;
int size;
for(size = 1; argItor.hasNext(); size++) {
Item toadd = argItor.next();
if(toadd instanceof UntypedAtomicValue) {
toadd = ((UntypedAtomicValue) toadd).castAs(DoubleType.DOUBLE, dynEnv);
} else if(!(toadd instanceof XNumber)) {
throw new DynamicError("err:FORG0006", "fs:plus(" + sum.getType() + ", "
+ toadd.getType() + ") is not defined.");
}
final PlusOp op = new PlusOp();
op.staticAnalysis(dynEnv.getStaticContext(), sum, toadd);
sum = (XNumber) op.eval(dynEnv, sum, toadd);
}
final DivOp op = new DivOp();
final XInteger divby = XInteger.valueOf(size);
op.staticAnalysis(dynEnv.getStaticContext(), sum, divby);
return op.eval(dynEnv, sum, divby);
} else if(firstItem instanceof DurationValue) {
// Duration values must either all be xdt:yearMonthDuration values
// or must all be xdt:dayTimeDuration values.
DurationValue sum = (DurationValue) firstItem;
Type firstType = firstItem.getType();
assert (firstType != null);
int size;
for(size = 1; argItor.hasNext(); size++) {
Item toadd = argItor.next();
if(toadd instanceof DurationValue) {
throw new DynamicError("err:FORG0006", "Duration values must all be `"
+ firstType + "`, but found `" + toadd.getType() + "`");
}
final PlusOp op = new PlusOp();
op.staticAnalysis(dynEnv.getStaticContext(), sum, toadd);
sum = (DurationValue) op.eval(dynEnv, sum, toadd);
}