}
public Sequence eval(Sequence<? extends Item> contextSeq, ValueSequence argv, DynamicContext dynEnv)
throws XQueryException {
final int arglen = argv.size();
final Item zero;
if(arglen == 2) {
zero = argv.getItem(1);
} else if(arglen == 1) {
zero = XInteger.valueOf(0);
} else {
throw new IllegalStateException("Illefal args: " + arglen);
}
Item arg = argv.getItem(0);
Iterator<? extends Item> argItor = arg.iterator();
if(!argItor.hasNext()) {
// If the converted sequence is empty, then the single-argument form
// of the function returns the xs:integer value 0;
// the two-argument form returns the value of the argument $zero.
return zero;
}
Item firstItem = argItor.next();
if(firstItem instanceof UntypedAtomicValue) {
// Values of type xdt:untypedAtomic in $arg 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;
while(argItor.hasNext()) {
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.");
}
// TODO more efficient processing
final PlusOp op = new PlusOp();
op.staticAnalysis(dynEnv.getStaticContext(), sum, toadd);
sum = (XNumber) op.eval(dynEnv, sum, toadd);
}
return sum;
} 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);
while(argItor.hasNext()) {
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);
}