Search in sources :

Example 1 with ScalarTypePromoter

use of org.eclipse.wst.xml.xpath2.processor.internal.utils.ScalarTypePromoter 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))));
}
Also used : XSFloat(org.eclipse.wst.xml.xpath2.processor.internal.types.XSFloat) ScalarTypePromoter(org.eclipse.wst.xml.xpath2.processor.internal.utils.ScalarTypePromoter) XSDouble(org.eclipse.wst.xml.xpath2.processor.internal.types.XSDouble) ResultSequence(org.eclipse.wst.xml.xpath2.api.ResultSequence) XSInteger(org.eclipse.wst.xml.xpath2.processor.internal.types.XSInteger) Iterator(java.util.Iterator) TypePromoter(org.eclipse.wst.xml.xpath2.processor.internal.utils.TypePromoter) ScalarTypePromoter(org.eclipse.wst.xml.xpath2.processor.internal.utils.ScalarTypePromoter) AnyAtomicType(org.eclipse.wst.xml.xpath2.processor.internal.types.AnyAtomicType)

Example 2 with ScalarTypePromoter

use of org.eclipse.wst.xml.xpath2.processor.internal.utils.ScalarTypePromoter 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);
}
Also used : XSFloat(org.eclipse.wst.xml.xpath2.processor.internal.types.XSFloat) ScalarTypePromoter(org.eclipse.wst.xml.xpath2.processor.internal.utils.ScalarTypePromoter) XSDouble(org.eclipse.wst.xml.xpath2.processor.internal.types.XSDouble) Iterator(java.util.Iterator) TypePromoter(org.eclipse.wst.xml.xpath2.processor.internal.utils.TypePromoter) ScalarTypePromoter(org.eclipse.wst.xml.xpath2.processor.internal.utils.ScalarTypePromoter) AnyAtomicType(org.eclipse.wst.xml.xpath2.processor.internal.types.AnyAtomicType)

Aggregations

Iterator (java.util.Iterator)2 AnyAtomicType (org.eclipse.wst.xml.xpath2.processor.internal.types.AnyAtomicType)2 XSDouble (org.eclipse.wst.xml.xpath2.processor.internal.types.XSDouble)2 XSFloat (org.eclipse.wst.xml.xpath2.processor.internal.types.XSFloat)2 ScalarTypePromoter (org.eclipse.wst.xml.xpath2.processor.internal.utils.ScalarTypePromoter)2 TypePromoter (org.eclipse.wst.xml.xpath2.processor.internal.utils.TypePromoter)2 ResultSequence (org.eclipse.wst.xml.xpath2.api.ResultSequence)1 XSInteger (org.eclipse.wst.xml.xpath2.processor.internal.types.XSInteger)1