use of org.eclipse.wst.xml.xpath2.processor.internal.utils.TypePromoter in project webtools.sourceediting by eclipse.
the class FnMax method max.
/**
* Max operation.
*
* @param args
* Result from the expressions evaluation.
* @param context
* Relevant dynamic context
* @throws DynamicError
* Dynamic error.
* @return Result of fn:max operation.
*/
public static ResultSequence max(Collection args, DynamicContext dynamicContext) throws DynamicError {
ResultSequence arg = get_arg(args, CmpGt.class);
if (arg.empty())
return ResultSequenceFactory.create_new();
CmpGt max = null;
TypePromoter tp = new ComparableTypePromoter();
tp.considerSequence(arg);
for (Iterator i = arg.iterator(); i.hasNext(); ) {
AnyAtomicType conv = tp.promote((AnyType) i.next());
if (conv != null) {
if (conv instanceof XSDouble && ((XSDouble) conv).nan() || conv instanceof XSFloat && ((XSFloat) conv).nan()) {
return ResultSequenceFactory.create_new(tp.promote(new XSFloat(Float.NaN)));
}
if (max == null || ((CmpGt) conv).gt((AnyType) max, dynamicContext)) {
max = (CmpGt) conv;
}
}
}
return ResultSequenceFactory.create_new((AnyType) max);
}
use of org.eclipse.wst.xml.xpath2.processor.internal.utils.TypePromoter in project webtools.sourceediting by eclipse.
the class FnMin method min.
/**
* Min operation.
*
* @param args
* Result from the expressions evaluation.
* @param dynamic
* Dynamic context
* @throws DynamicError
* Dynamic error.
* @return Result of fn:min operation.
*/
public static ResultSequence min(Collection args, DynamicContext context) throws DynamicError {
ResultSequence arg = FnMax.get_arg(args, CmpLt.class);
if (arg.empty())
return ResultSequenceFactory.create_new();
CmpLt max = null;
TypePromoter tp = new ComparableTypePromoter();
tp.considerSequence(arg);
for (Iterator i = arg.iterator(); i.hasNext(); ) {
AnyAtomicType conv = tp.promote((AnyType) i.next());
if (conv != null) {
if (conv instanceof XSDouble && ((XSDouble) conv).nan() || conv instanceof XSFloat && ((XSFloat) conv).nan()) {
return ResultSequenceFactory.create_new(tp.promote(new XSFloat(Float.NaN)));
}
if (max == null || ((CmpLt) conv).lt((AnyType) max, context)) {
max = (CmpLt) conv;
}
}
}
return ResultSequenceFactory.create_new((AnyType) max);
}
use of org.eclipse.wst.xml.xpath2.processor.internal.utils.TypePromoter in project webtools.sourceediting by eclipse.
the class FnAvg method avg.
/**
* Average value operation.
*
* @param args
* Result from the expressions evaluation.
* @throws DynamicError
* Dynamic error.
* @return Result of fn:avg operation.
*/
public static ResultSequence avg(Collection args) throws DynamicError {
ResultSequence arg = (ResultSequence) args.iterator().next();
if (arg.empty())
return ResultSequenceFactory.create_new();
int elems = 0;
MathPlus total = null;
TypePromoter tp = new ScalarTypePromoter();
tp.considerSequence(arg);
for (Iterator i = arg.iterator(); i.hasNext(); ) {
++elems;
AnyAtomicType conv = tp.promote((AnyType) i.next());
if (conv != null) {
if (conv instanceof XSDouble && ((XSDouble) conv).nan() || conv instanceof XSFloat && ((XSFloat) conv).nan()) {
return ResultSequenceFactory.create_new(tp.promote(new XSFloat(Float.NaN)));
}
if (total == null) {
total = (MathPlus) conv;
} else {
total = (MathPlus) total.plus(ResultSequenceFactory.create_new(conv)).first();
}
}
}
if (!(total instanceof MathDiv))
DynamicError.throw_type_error();
return ((MathDiv) total).div(ResultSequenceFactory.create_new(new XSInteger(BigInteger.valueOf(elems))));
}
use of org.eclipse.wst.xml.xpath2.processor.internal.utils.TypePromoter in project webtools.sourceediting by eclipse.
the class FnSum method sum.
/**
* Sum operation.
*
* @param args
* Result from the expressions evaluation.
* @throws DynamicError
* Dynamic error.
* @return Result of fn:sum operation.
*/
public static ResultSequence sum(ResultSequence arg, AnyAtomicType zero) throws DynamicError {
if (arg.empty())
return ResultSequenceFactory.create_new(zero);
MathPlus total = null;
TypePromoter tp = new ScalarTypePromoter();
tp.considerSequence(arg);
for (Iterator i = arg.iterator(); i.hasNext(); ) {
AnyAtomicType conv = tp.promote((AnyType) i.next());
if (conv == null) {
conv = zero;
}
if (conv instanceof XSDouble && ((XSDouble) conv).nan() || conv instanceof XSFloat && ((XSFloat) conv).nan()) {
return ResultSequenceFactory.create_new(tp.promote(new XSFloat(Float.NaN)));
}
if (total == null) {
total = (MathPlus) conv;
} else {
total = (MathPlus) total.plus(ResultSequenceFactory.create_new(conv)).first();
}
}
return ResultSequenceFactory.create_new((AnyType) total);
}
Aggregations