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);
}
final DivOp op = new DivOp();
final XInteger divby = XInteger.valueOf(size);
op.staticAnalysis(dynEnv.getStaticContext(), sum, divby);
return op.eval(dynEnv, sum, divby);
} else {
throw new DynamicError("err:FORG0006", "Invalid argument type: " + firstItem.getType());
}
}