/** * Negate a duration (same as subtracting from zero, but it preserves the type of the original duration) * * @return the original duration with its sign reversed, retaining its type */ public DurationValue negate() { if (negative) { return new DurationValue(0, months, 0, 0, 0, seconds, nanoseconds, typeLabel); } else { return new DurationValue(0, -months, 0, 0, 0, -seconds, -nanoseconds, typeLabel); } }
public AtomicValue compute(AtomicValue a, AtomicValue b, XPathContext c) throws XPathException { return ((DurationValue)a).add((DurationValue)b); } public AtomicType getResultType(AtomicType typeA, AtomicType typeB) {
/** * Multiply a duration by an integer * * @param factor the number to multiply by * @return the result of the multiplication */ public DurationValue multiply(long factor) throws XPathException { return multiply((double)factor); }
public DayTimeDurationValue convert(AtomicValue duration) { DurationValue d = (DurationValue)duration; if (d.signum() < 0) { return new DayTimeDurationValue(-d.getDays(), -d.getHours(), -d.getMinutes(), -d.getSeconds(), -d.getNanoseconds()); } else { return new DayTimeDurationValue(d.getDays(), d.getHours(), d.getMinutes(), d.getSeconds(), d.getNanoseconds()); } } }
/** * Static factory: create a year-month duration value from a supplied string, in * ISO 8601 format [+|-]PnYnM * * @param s a string in the lexical space of xs:yearMonthDuration. * @return either a YearMonthDurationValue, or a ValidationFailure if the string was * not in the lexical space of xs:yearMonthDuration. */ public static ConversionResult makeYearMonthDurationValue(CharSequence s) { ConversionResult d = DurationValue.makeDuration(s, true, false); if (d instanceof ValidationFailure) { return d; } DurationValue dv = (DurationValue) d; return YearMonthDurationValue.fromMonths((dv.getYears() * 12 + dv.getMonths()) * dv.signum()); }
/** * Get a component of the normalized value */ public AtomicValue getComponent(int component) throws XPathException { switch (component) { case Component.YEAR: return Int64Value.makeIntegerValue((negative ? -getYears() : getYears())); case Component.MONTH: return Int64Value.makeIntegerValue((negative ? -getMonths() : getMonths())); case Component.DAY: return Int64Value.makeIntegerValue((negative ? -getDays() : getDays())); case Component.HOURS: return Int64Value.makeIntegerValue((negative ? -getHours() : getHours())); case Component.MINUTES: return Int64Value.makeIntegerValue((negative ? -getMinutes() : getMinutes())); case Component.SECONDS: FastStringBuffer sb = new FastStringBuffer(16); String ms = ("000000" + microseconds); ms = ms.substring(ms.length() - 6); sb.append((negative ? "-" : "") + getSeconds() + '.' + ms); return (AtomicValue)DecimalValue.makeDecimalValue(sb, false); case Component.WHOLE_SECONDS: return Int64Value.makeIntegerValue((negative ? -seconds : seconds)); case Component.MICROSECONDS: return new Int64Value((negative ? -microseconds : microseconds)); default: throw new IllegalArgumentException("Unknown component for duration: " + component); } }
int components = 0; if (!tok.hasMoreElements()) { return badDuration("empty string", s); return badDuration("+ sign not allowed in a duration", s); } else if ("-".equals(part)) { negative = true; return badDuration("missing 'P'", s); state = 4; if (!tok.hasMoreElements()) { return badDuration("T must be followed by time components", s); int value = simpleInteger(part); if (value < 0) { if (part.length() > 8) { return badDuration("component invalid or too large", s); } else { return badDuration("non-numeric component", s); return badDuration("missing unit letter at end", s); case'Y': if (state > 0) { return badDuration("Y is out of sequence", s); break; } else {
public AtomicValue compute(AtomicValue a, AtomicValue b, XPathContext c) throws XPathException { return ((DurationValue)a).divide((DurationValue)b); } public AtomicType getResultType(AtomicType typeA, AtomicType typeB) {
public int hashCode() { return Double.valueOf(getLengthInSeconds()).hashCode(); }
/** * Get a component of the normalized value */ public AtomicValue getComponent(int component) throws XPathException { switch (component) { case Component.YEAR: return Int64Value.makeIntegerValue((negative ? -getYears() : getYears())); case Component.MONTH: return Int64Value.makeIntegerValue((negative ? -getMonths() : getMonths())); case Component.DAY: return Int64Value.makeIntegerValue((negative ? -getDays() : getDays())); case Component.HOURS: return Int64Value.makeIntegerValue((negative ? -getHours() : getHours())); case Component.MINUTES: return Int64Value.makeIntegerValue((negative ? -getMinutes() : getMinutes())); case Component.SECONDS: FastStringBuffer sb = new FastStringBuffer(16); String ms = ("000000" + microseconds); ms = ms.substring(ms.length() - 6); sb.append((negative ? "-" : "") + getSeconds() + '.' + ms); return (AtomicValue)DecimalValue.makeDecimalValue(sb, false); case Component.WHOLE_SECONDS: return Int64Value.makeIntegerValue((negative ? -seconds : seconds)); case Component.MICROSECONDS: return new Int64Value((negative ? -microseconds : microseconds)); default: throw new IllegalArgumentException("Unknown component for duration: " + component); } }
public DayTimeDurationValue convert(AtomicValue duration) { DurationValue d = (DurationValue)duration; if (d.signum() < 0) { return new DayTimeDurationValue(-d.getDays(), -d.getHours(), -d.getMinutes(), -d.getSeconds(), -d.getNanoseconds()); } else { return new DayTimeDurationValue(d.getDays(), d.getHours(), d.getMinutes(), d.getSeconds(), d.getNanoseconds()); } } }
int components = 0; if (!tok.hasMoreElements()) { return badDuration("empty string", s); return badDuration("+ sign not allowed in a duration", s); } else if ("-".equals(part)) { negative = true; return badDuration("missing 'P'", s); state = 4; if (!tok.hasMoreElements()) { return badDuration("T must be followed by time components", s); int value = simpleInteger(part); if (value < 0) { if (value == -2) { return badDuration("component of duration exceeds Saxon limits", s, "FODT0002"); } else { return badDuration("invalid or non-numeric component", s); return badDuration("missing unit letter at end", s); case 'Y': if (state > 0) { return badDuration("Y is out of sequence", s); return badDuration("Year component is not allowed in dayTimeDuration", s);
/** * Static factory: create a year-month duration value from a supplied string, in * ISO 8601 format [+|-]PnYnM * * @param s a string in the lexical space of xs:yearMonthDuration. * @return either a YearMonthDurationValue, or a ValidationFailure if the string was * not in the lexical space of xs:yearMonthDuration. */ public static ConversionResult makeYearMonthDurationValue(CharSequence s) { ConversionResult d = DurationValue.makeDuration(s, true, false); if (d instanceof ValidationFailure) { return d; } DurationValue dv = (DurationValue) d; return YearMonthDurationValue.fromMonths((dv.getYears() * 12 + dv.getMonths()) * dv.signum()); }
public AtomicValue compute(AtomicValue a, AtomicValue b, XPathContext c) throws XPathException { return ((DurationValue)a).divide((DurationValue)b); } public AtomicType getResultType(AtomicType typeA, AtomicType typeB) {
public int hashCode() { return new Double(getLengthInSeconds()).hashCode(); }
switch (component) { case YEAR: return Int64Value.makeIntegerValue(negative ? -getYears() : getYears()); case MONTH: return Int64Value.makeIntegerValue(negative ? -getMonths() : getMonths()); case DAY: return Int64Value.makeIntegerValue(negative ? -getDays() : getDays()); case HOURS: return Int64Value.makeIntegerValue(negative ? -getHours() : getHours()); case MINUTES: return Int64Value.makeIntegerValue(negative ? -getMinutes() : getMinutes()); case SECONDS: FastStringBuffer sb = new FastStringBuffer(FastStringBuffer.C16); String ms = "000000000" + nanoseconds; ms = ms.substring(ms.length() - 9); sb.append((negative ? "-" : "") + getSeconds() + '.' + ms); return BigDecimalValue.parse(sb); case WHOLE_SECONDS:
int components = 0; if (!tok.hasMoreElements()) { return badDuration("empty string", s); return badDuration("+ sign not allowed in a duration", s); } else if ("-".equals(part)) { negative = true; return badDuration("missing 'P'", s); state = 4; if (!tok.hasMoreElements()) { return badDuration("T must be followed by time components", s); int value = simpleInteger(part); if (value < 0) { if (value == -2) { return badDuration("component of duration exceeds Saxon limits", s, "FODT0002"); } else { return badDuration("invalid or non-numeric component", s); return badDuration("missing unit letter at end", s); case 'Y': if (state > 0) { return badDuration("Y is out of sequence", s); return badDuration("Year component is not allowed in dayTimeDuration", s);
/** * Multiply a duration by an integer * * @param factor the number to multiply by * @return the result of the multiplication */ public DurationValue multiply(long factor) throws XPathException { return multiply((double)factor); }