Search in sources :

Example 11 with XSDayTimeDuration

use of org.eclipse.wst.xml.xpath2.processor.internal.types.XSDayTimeDuration in project webtools.sourceediting by eclipse.

the class XSDateTime method plus.

/**
 * Mathematical addition operator between this XSDateTime and a supplied
 * result sequence (XDTYearMonthDuration and XDTDayTimeDuration are only
 * valid ones).
 *
 * @param arg
 *            The supplied ResultSequence that is on the right of the minus
 *            operator. If arg is an XDTYearMonthDuration or an
 *            XDTDayTimeDuration the result will be a XSDateTime of the
 *            result of the current date minus the duration of time
 *            supplied.
 * @return New ResultSequence consisting of the result of the mathematical
 *         minus operation.
 */
public ResultSequence plus(ResultSequence arg) throws DynamicError {
    if (arg.size() != 1)
        DynamicError.throw_type_error();
    Item at = arg.first();
    try {
        if (at instanceof XSYearMonthDuration) {
            XSYearMonthDuration val = (XSYearMonthDuration) at;
            XSDateTime res = (XSDateTime) clone();
            res.calendar().add(Calendar.MONTH, val.monthValue());
            return ResultSequenceFactory.create_new(res);
        } else if (at instanceof XSDayTimeDuration) {
            XSDuration val = (XSDuration) at;
            XSDateTime res = (XSDateTime) clone();
            XMLGregorianCalendar xmlCal = _datatypeFactory.newXMLGregorianCalendar((GregorianCalendar) calendar());
            Duration dtduration = _datatypeFactory.newDuration(val.getStringValue());
            xmlCal.add(dtduration);
            res = new XSDateTime(xmlCal.toGregorianCalendar(), res.tz());
            return ResultSequenceFactory.create_new(res);
        } else {
            DynamicError.throw_type_error();
            // unreach
            return null;
        }
    } catch (CloneNotSupportedException err) {
        assert false;
        return null;
    }
}
Also used : Item(org.eclipse.wst.xml.xpath2.api.Item) XMLGregorianCalendar(javax.xml.datatype.XMLGregorianCalendar) GregorianCalendar(java.util.GregorianCalendar) XMLGregorianCalendar(javax.xml.datatype.XMLGregorianCalendar) Duration(javax.xml.datatype.Duration)

Example 12 with XSDayTimeDuration

use of org.eclipse.wst.xml.xpath2.processor.internal.types.XSDayTimeDuration in project webtools.sourceediting by eclipse.

the class XSDayTimeDuration method div.

/**
 * Mathematical division between this duration stored and the supplied
 * duration of time (of type XSDayTimeDuration)
 *
 * @param arg
 *            The duration of time to divide by
 * @return New XSDayTimeDuration representing the resulting duration after
 *         the division
 * @throws DynamicError
 */
public ResultSequence div(ResultSequence arg) throws DynamicError {
    if (arg.size() != 1)
        DynamicError.throw_type_error();
    Item at = arg.first();
    if (at instanceof XSDouble) {
        XSDouble dt = (XSDouble) at;
        double retval = 0;
        if (dt.nan()) {
            throw DynamicError.nan();
        }
        if (!dt.zero()) {
            BigDecimal ret = new BigDecimal(0);
            if (dt.infinite()) {
                retval = value() / dt.double_value();
            } else {
                ret = new BigDecimal(value());
                ret = ret.divide(new BigDecimal(dt.double_value()), 18, BigDecimal.ROUND_HALF_EVEN);
                retval = ret.doubleValue();
            }
        } else {
            throw DynamicError.overflowUnderflow();
        }
        return ResultSequenceFactory.create_new(new XSDayTimeDuration(retval));
    } else if (at instanceof XSDecimal) {
        XSDecimal dt = (XSDecimal) at;
        BigDecimal ret = new BigDecimal(0);
        if (!dt.zero()) {
            ret = new BigDecimal(value());
            ret = ret.divide(dt.getValue(), 18, BigDecimal.ROUND_HALF_EVEN);
        } else {
            throw DynamicError.overflowUnderflow();
        }
        return ResultSequenceFactory.create_new(new XSDayTimeDuration(ret.intValue()));
    } else if (at instanceof XSDayTimeDuration) {
        XSDuration md = (XSDuration) at;
        BigDecimal res = null;
        res = new BigDecimal(this.value());
        BigDecimal l = new BigDecimal(md.value());
        res = res.divide(l, 18, BigDecimal.ROUND_HALF_EVEN);
        return ResultSequenceFactory.create_new(new XSDecimal(res));
    } else {
        DynamicError.throw_type_error();
        // unreach
        return null;
    }
}
Also used : Item(org.eclipse.wst.xml.xpath2.api.Item) BigDecimal(java.math.BigDecimal)

Example 13 with XSDayTimeDuration

use of org.eclipse.wst.xml.xpath2.processor.internal.types.XSDayTimeDuration in project webtools.sourceediting by eclipse.

the class XSDayTimeDuration method times.

/**
 * Mathematical multiplication between this duration stored and the supplied
 * duration of time (of type XSDayTimeDuration)
 *
 * @param arg
 *            The duration of time to multiply by
 * @return New XSDayTimeDuration representing the resulting duration after
 *         the multiplication
 * @throws DynamicError
 */
public ResultSequence times(ResultSequence arg) throws DynamicError {
    ResultSequence convertedRS = arg;
    if (arg.size() == 1) {
        Item argValue = arg.first();
        if (argValue instanceof XSDecimal) {
            convertedRS = ResultSequenceFactory.create_new(new XSDouble(argValue.getStringValue()));
        }
    }
    XSDouble val = (XSDouble) NumericType.get_single_type(convertedRS, XSDouble.class);
    if (val.nan()) {
        throw DynamicError.nan();
    }
    double res = value() * val.double_value();
    return ResultSequenceFactory.create_new(new XSDayTimeDuration(res));
}
Also used : Item(org.eclipse.wst.xml.xpath2.api.Item) ResultSequence(org.eclipse.wst.xml.xpath2.api.ResultSequence)

Aggregations

XSDayTimeDuration (org.eclipse.wst.xml.xpath2.processor.internal.types.XSDayTimeDuration)9 Duration (javax.xml.datatype.Duration)6 XMLGregorianCalendar (javax.xml.datatype.XMLGregorianCalendar)4 Item (org.eclipse.wst.xml.xpath2.api.Item)4 ResultSequence (org.eclipse.wst.xml.xpath2.api.ResultSequence)4 ArrayList (java.util.ArrayList)3 Collection (java.util.Collection)3 Iterator (java.util.Iterator)3 SeqType (org.eclipse.wst.xml.xpath2.processor.internal.SeqType)3 AnyType (org.eclipse.wst.xml.xpath2.processor.internal.types.AnyType)3 XSDate (org.eclipse.wst.xml.xpath2.processor.internal.types.XSDate)3 XSDateTime (org.eclipse.wst.xml.xpath2.processor.internal.types.XSDateTime)3 XSTime (org.eclipse.wst.xml.xpath2.processor.internal.types.XSTime)3 GregorianCalendar (java.util.GregorianCalendar)2 BigDecimal (java.math.BigDecimal)1 CalendarType (org.eclipse.wst.xml.xpath2.processor.internal.types.CalendarType)1