firstItem = ((UntypedAtomicValue) firstItem).castAs(DoubleType.DOUBLE, dynEnv);
}
if(!argItor.hasNext()) {
return firstItem;
}
final Type firstType = firstItem.getType();
if(firstItem instanceof XNumber) {
XNumber max = (XNumber) firstItem;
// If the converted sequence contains the value NaN, the value NaN is returned.
if(max.isNaN()) {
return max;
}
while(argItor.hasNext()) {
Item it = argItor.next();
if(it instanceof UntypedAtomicValue) {
it = ((UntypedAtomicValue) it).castAs(DoubleType.DOUBLE, dynEnv);
} else if(!(it instanceof XNumber)) {
throw new DynamicError("err:FORG0006", "fs:plus(" + max.getType() + ", "
+ it.getType() + ") is not defined");
}
XNumber cmp = (XNumber) it;
if(cmp.isNaN()) {
return cmp;
}
if(cmp.compareTo(max) > 0) {
max = cmp;
}
}
return max;
} else if(firstItem instanceof DurationValue) {
// Duration values must either all be xdt:yearMonthDuration values
// or must all be xdt:dayTimeDuration values.
DurationValue max = (DurationValue) firstItem;
while(argItor.hasNext()) {
Item it = argItor.next();
if(it instanceof DurationValue) {
throw new DynamicError("err:FORG0006", "Duration values must all be `"
+ firstType + "`, but found `" + it.getType() + "`");
}
DurationValue cmp = (DurationValue) it;
if(cmp.compareTo(max) > 0) {
max = cmp;
}
}
return max;
} else if(TypeUtil.subtypeOf(firstType, StringType.STRING)) {
AtomicValue max = (AtomicValue) firstItem;
final int arglen = argv.size();
final Collator collator;
if(arglen == 2) {
Item sec = argv.getItem(1);
Type secType = sec.getType();
if(!TypeUtil.subtypeOf(secType, StringType.STRING)) {
throw new DynamicError("err:FORG0006", "second argument is expected to be xs:string, but was "
+ secType);
}
String uri = sec.stringValue();
collator = CollationUtils.resolve(uri, dynEnv.getStaticContext());
} else {
collator = Collator.getInstance(); // compare in default locale
}
while(argItor.hasNext()) {
Item it = argItor.next();
Type cmpType = it.getType();
if(!TypeUtil.subtypeOf(cmpType, StringType.STRING)) {
throw new DynamicError("err:FORG0006", "imcomparable xs:string and " + cmpType);
}
if(collator.compare(it, firstItem) > 0) {
max = (AtomicValue) it;