{result = Sequence.EMPTY_SEQUENCE;}
else {
//TODO : test if a range index is defined *iff* it is compatible with the collator
final Collator collator = getCollator(contextSequence, contextItem, 2);
final SequenceIterator iter = arg.unorderedIterator();
AtomicValue min = null;
while (iter.hasNext()) {
final Item item = iter.nextItem();
if (item instanceof QNameValue)
{throw new XPathException(this, ErrorCodes.FORG0006, "Cannot compare " + Type.getTypeName(item.getType()), arg);}
AtomicValue value = item.atomize();
//Duration values must either all be xs:yearMonthDuration values or must all be xs:dayTimeDuration values.
if (Type.subTypeOf(value.getType(), Type.DURATION)) {
value = ((DurationValue)value).wrap();
if (value.getType() == Type.YEAR_MONTH_DURATION) {
if (min != null && min.getType() != Type.YEAR_MONTH_DURATION)
{throw new XPathException(this, ErrorCodes.FORG0006, "Cannot compare " + Type.getTypeName(min.getType()) +
" and " + Type.getTypeName(value.getType()), value);}
} else if (value.getType() == Type.DAY_TIME_DURATION) {
if (min != null && min.getType() != Type.DAY_TIME_DURATION)
{throw new XPathException(this, ErrorCodes.FORG0006, "Cannot compare " + Type.getTypeName(min.getType()) +
" and " + Type.getTypeName(value.getType()), value);}
} else
{throw new XPathException(this, ErrorCodes.FORG0006, "Cannot compare " + Type.getTypeName(value.getType()), value);}
//Any value of type xdt:untypedAtomic is cast to xs:double
} else if (value.getType() == Type.UNTYPED_ATOMIC)
{value = value.convertTo(Type.DOUBLE);}
if (min == null)
{min = value;}
else {
if (Type.getCommonSuperType(min.getType(), value.getType()) == Type.ATOMIC) {
throw new XPathException(this, ErrorCodes.FORG0006, "Cannot compare " + Type.getTypeName(min.getType()) +
" and " + Type.getTypeName(value.getType()), value);
}
//Any value of type xdt:untypedAtomic is cast to xs:double
if (value.getType() == Type.ATOMIC)
{value = value.convertTo(Type.DOUBLE);}
//Numeric tests
if (Type.subTypeOf(value.getType(), Type.NUMBER)) {
//Don't mix comparisons
if (!Type.subTypeOf(min.getType(), Type.NUMBER))
{throw new XPathException(this, ErrorCodes.FORG0006, "Cannot compare " + Type.getTypeName(min.getType()) +
" and " + Type.getTypeName(value.getType()), min);}
if (((NumericValue) value).isNaN()) {
//Type NaN correctly
value = value.promote(min);
if (value.getType() == Type.FLOAT)
{min = FloatValue.NaN;}
else
{min = DoubleValue.NaN;}
//although result will be NaN, we need to continue on order to type correctly
continue;
}
min = min.promote(value);
}
//Ugly test
if (value instanceof ComputableValue) {
if (!(min instanceof ComputableValue))
{throw new XPathException(this, ErrorCodes.FORG0006, "Cannot compare " + Type.getTypeName(min.getType()) +
" and " + Type.getTypeName(value.getType()), min);}
//Type value correctly
value = value.promote(min);
min = min.min(collator, value);
computableProcessing = true;
} else {
if (computableProcessing)
{throw new XPathException(this, ErrorCodes.FORG0006, "Cannot compare " + Type.getTypeName(min.getType()) +
" and " + Type.getTypeName(value.getType()), value);}
if (Collations.compare(collator, value.getStringValue(), min.getStringValue()) < 0)
{min = value;}
}
}
}
result = min;