Package net.sf.saxon.value
Class DurationValue
java.lang.Object
net.sf.saxon.value.AtomicValue
net.sf.saxon.value.DurationValue
- All Implemented Interfaces:
Comparable<XPathComparable>,Iterable<AtomicValue>,AtomicMatchKey,XPathComparable,AtomicSequence,GroundedValue,IdentityComparable,Item,Sequence,ConversionResult
- Direct Known Subclasses:
DayTimeDurationValue,YearMonthDurationValue
A value of type xs:duration. A duration is represented as an integer number of months (limited to 32 bits)
and a BigDecimal number of seconds (no limits). Both (if non-zero) must have the same sign.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classDurationValueComparable is a Comparable value that acts as a surrogate for a Duration, having ordering rules that implement the XML Schema specification. -
Field Summary
FieldsFields inherited from class net.sf.saxon.value.AtomicValue
metadata -
Constructor Summary
ConstructorsConstructorDescriptionDurationValue(int months, BigDecimal seconds, AtomicType typeLabel) Construct an xs:duration from the number of months and seconds -
Method Summary
Modifier and TypeMethodDescriptionadd(DurationValue other) Add two durationsprotected static ValidationFailurebadDuration(String msg, UnicodeString s) protected static ValidationFailurebadDuration(String msg, UnicodeString s, String errorCode) intdivide(double factor) Divide a duration by a numberdivide(DurationValue other) Divide a duration by a another durationbooleanTest if the two durations are of equal length.getComponent(AccessorFn.Component component) Get a component of the normalized valuelonggetDays()Get the days componentGet the days, hours, minute, and seconds components, in normalized form, as an array of four decimal values (the first three will always be integral).intgetHours()Get the hours componentintGet the minutes componentintGet the months componentConvert to stringDetermine the primitive type of the value.Get a Comparable value that implements the XML Schema ordering comparison semantics for this value.Get the seconds componentintGet the total number of months (ignoring the days/hours/minutes/seconds)Get the total number of seconds (ignoring the years/months)getXPathComparable(StringCollator collator, int implicitTimezone, int specVersion) Get an object value that implements the XPath equality and ordering comparison semantics for this value.getXPathMatchKey(StringCollator collator, int implicitTimezone, int specVersion) Get an object value that implements the XPath equality and ordering comparison semantics for this value.intgetYears()Get the year componentinthashCode()Returns a hash code value for the object.static ConversionResultStatic factory method: create a duration value from a supplied string, in ISO 8601 format [-]PnYnMnDTnHnMnSprotected static ConversionResultmakeDuration(UnicodeString s, boolean allowYM, boolean allowDT) multiply(double factor) Multiply a duration by a doublemultiply(long factor) Multiply a duration by an integermultiply(BigDecimal factor) Multiply a duration by a decimalnegate()Negate a duration (same as subtracting from zero, but it preserves the type of the original duration)intsignum()Return the signum of the valueprotected static longParse a simple unsigned integersubtract(DurationValue other) Subtract two durationswithMetadata(AtomicMetadata metadata) Create a copy of this atomic value, with a different type labelMethods inherited from class net.sf.saxon.value.AtomicValue
asAtomic, asMapKey, atomize, checkPermittedContents, checkValidInJavascript, effectiveBooleanValue, getCanonicalLexicalRepresentation, getCardinality, getGenre, getItemType, getLabel, getLength, getUnicodeStringValue, getUType, head, identityHashCode, isIdentical, isIdentical, isNaN, isUntypedAtomic, itemAt, iterate, iterator, show, toShortString, toStringMethods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface net.sf.saxon.expr.sort.AtomicMatchKey
asAtomic, longHashCodeMethods inherited from interface net.sf.saxon.om.GroundedValue
asIterable, concatenate, containsNode, materializeMethods inherited from interface net.sf.saxon.om.Item
getStringValue, isStreamed, reduce, subsequenceMethods inherited from interface java.lang.Iterable
forEach, spliteratorMethods inherited from interface net.sf.saxon.om.Sequence
makeRepeatable
-
Field Details
-
_months
protected final int _months -
_seconds
-
-
Constructor Details
-
DurationValue
Construct an xs:duration from the number of months and seconds- Parameters:
months- the integer number of months.seconds- the decimal number of seconds. Must be negative or zero if months is negative; must be positive or zero if months is positive.typeLabel- allows a subtype of xs:duration to be constructed - but not xs:dayTimeDuration or xs:yearMonthDuration, as these have separate subclasses.- Throws:
IllegalArgumentException- if the months and seconds have conflicting sign
-
-
Method Details
-
makeDuration
Static factory method: create a duration value from a supplied string, in ISO 8601 format [-]PnYnMnDTnHnMnS- Parameters:
s- a string in the lexical space of xs:duration- Returns:
- the constructed xs:duration value, or a
ValidationFailureif the supplied string is lexically invalid.
-
makeDuration
-
badDuration
-
badDuration
-
simpleInteger
Parse a simple unsigned integer- Parameters:
s- the string containing the sequence of digits. No sign or whitespace is allowed.- Returns:
- the integer. Return -1 if the string is not a sequence of digits, or -2 if it exceeds 2^63
-
withMetadata
Create a copy of this atomic value, with a different type label- Specified by:
withMetadatain classAtomicValue- Parameters:
metadata- the type label of the new copy. The caller is responsible for checking that the value actually conforms to this type. This method can't be used to create an instance of xs:dayTimeDuration or xs:yearMonthDuration.- Returns:
- the copied value
-
getPrimitiveType
Determine the primitive type of the value. This delivers the same answer as getItemType().getPrimitiveItemType(). The primitive types are the 19 primitive types of XML Schema, plus xs:integer, xs:dayTimeDuration and xs:yearMonthDuration, and xs:untypedAtomic. For external objects, the result is AnyAtomicType.- Specified by:
getPrimitiveTypein classAtomicValue- Returns:
- the primitive type
-
signum
public int signum()Return the signum of the value- Returns:
- -1 if the duration is negative, zero if it is zero-length, +1 if it is positive
-
getYears
public int getYears()Get the year component- Returns:
- the number of years in the normalized duration; always positive
-
getMonths
public int getMonths()Get the months component- Returns:
- the number of months in the normalized duration; always positive, in the range 0-11
-
getDays
public long getDays()Get the days component- Returns:
- the number of days in the normalized duration; always positive
-
getDaysHoursMinutesAndSeconds
Get the days, hours, minute, and seconds components, in normalized form, as an array of four decimal values (the first three will always be integral). All four values will have the same sign (negative for a negative duration, positive if positive). -
getHours
public int getHours()Get the hours component- Returns:
- the number of hours in the normalized duration; always positive, in the range 0-23
-
getMinutes
public int getMinutes()Get the minutes component- Returns:
- the number of minutes in the normalized duration; always positive, in the range 0-59
-
getSeconds
Get the seconds component- Returns:
- the number of seconds (including fractional seconds) in the normalized duration; always positive, in the range 0-59
-
getTotalMonths
public int getTotalMonths()Get the total number of months (ignoring the days/hours/minutes/seconds)- Returns:
- the total number of months, that is
getYears()*12 + getMonths(), as a positive or negative number according as the duration is positive or negative
-
getTotalSeconds
Get the total number of seconds (ignoring the years/months)- Returns:
- the total number of seconds (across the days, hours, minutes, and seconds components), as a positive or negative number according as the duration is positive or negative, with the fractional part indicating parts of a second
-
getPrimitiveStringValue
Convert to string- Specified by:
getPrimitiveStringValuein classAtomicValue- Returns:
- ISO 8601 representation.
-
getComponent
Get a component of the normalized value- Overrides:
getComponentin classAtomicValue- Parameters:
component- the required component. Component values are negative if the duration is negative- Returns:
- the value of the requested component of this value
-
getXPathMatchKey
public AtomicMatchKey getXPathMatchKey(StringCollator collator, int implicitTimezone, int specVersion) Get an object value that implements the XPath equality and ordering comparison semantics for this value. If the ordered parameter is set to true, the result will be a Comparable and will support a compareTo() method with the semantics of the XPath lt/gt operator, provided that the other operand is also obtained using the getXPathComparable() method. In all cases the result will support equals() and hashCode() methods that support the semantics of the XPath eq operator, again provided that the other operand is also obtained using the getXPathComparable() method. A context argument is supplied for use in cases where the comparison semantics are context-sensitive, for example where they depend on the implicit timezone or the default collation.- Specified by:
getXPathMatchKeyin classAtomicValue- Parameters:
collator- collation used for comparing string valuesimplicitTimezone- the XPath dynamic evaluation context, used in cases where the comparison is contextspecVersion- Durations (as distinct from subtypes thereof) are comparable only if version is 4.0 or greater.- Returns:
- an Object whose equals() and hashCode() methods implement the XPath comparison semantics with respect to this atomic value. If ordered is specified, the result will either be null if no ordering is defined, or will be a Comparable
-
getXPathComparable
public XPathComparable getXPathComparable(StringCollator collator, int implicitTimezone, int specVersion) throws NoDynamicContextException Description copied from class:AtomicValueGet an object value that implements the XPath equality and ordering comparison semantics for this value. A collation is supplied for comparing strings, and an implicit timezone for comparing date/time values that have no saved timezone. An atomic value may return itself as the result, provided that its ordering rules are independent of the collation and timezone, and provided that it implements the XPathComparable interface: which means that its compareTo, equals, and hashCode methods must be compatible with the rules for XPath value comparisons.- Specified by:
getXPathComparablein classAtomicValue- Parameters:
collator- the collation to be used when comparing stringsimplicitTimezone- the implicit timezone in the dynamic context, used when comparing dates/times with and without timezonespecVersion- the version of the XPath specification (31 for 3.1, 40 for 4.0)- Returns:
- an Object that implements the XPath value comparison semantics with respect to this atomic value. For an atomic type that is not ordered (according to XPath rules), return null.
- Throws:
NoDynamicContextException- if the supplied implicit timezone is "NO_TIMEZONE" (meaning unknown), and the implicit timezone is actually required because the value in question is a date/time value with no timezone. This can cause a failure to evaluate expressions statically (because the implicit timezone is not known statically), and it will then be caught, meaning that the expression has to be evaluated dynamically.
-
compareTo
- Specified by:
compareToin interfaceComparable<XPathComparable>
-
equals
Test if the two durations are of equal length.- Overrides:
equalsin classAtomicValue- Parameters:
other- the other value- Returns:
- true (in a subclass) if the other operand is an atomic value and the two values are equal as defined by the XPath eq operator
- Throws:
ClassCastException- if the other value is not an xs:duration or subtype thereof
-
hashCode
public int hashCode()Description copied from class:AtomicValueReturns a hash code value for the object.- Overrides:
hashCodein classAtomicValue
-
add
Add two durations- Parameters:
other- the duration to be added to this one- Returns:
- the sum of the two durations
- Throws:
XPathException- if an error is detected
-
subtract
Subtract two durations- Parameters:
other- the duration to be subtracted from this one- Returns:
- the difference of the two durations
- Throws:
XPathException- if an error is detected
-
negate
Negate a duration (same as subtracting from zero, but it preserves the type of the original duration)- Returns:
- the original duration with its sign reversed, retaining its type
-
multiply
Multiply a duration by an integer- Parameters:
factor- the number to multiply by- Returns:
- the result of the multiplication
- Throws:
XPathException- if an error is detected
-
multiply
Multiply a duration by a double- Parameters:
factor- the number to multiply by- Returns:
- the result of the multiplication
- Throws:
XPathException- if an error is detected
-
multiply
Multiply a duration by a decimal- Parameters:
factor- the number to multiply by- Returns:
- the result of the multiplication
- Throws:
XPathException- if an error is detected
-
divide
Divide a duration by a number- Parameters:
factor- the number to divide by- Returns:
- the result of the division
- Throws:
XPathException- if an error is detected
-
divide
Divide a duration by a another duration- Parameters:
other- the duration to divide by- Returns:
- the result of the division
- Throws:
XPathException- if an error is detected
-
getSchemaComparable
Get a Comparable value that implements the XML Schema ordering comparison semantics for this value. This implementation handles the ordering rules for durations in XML Schema. It is overridden for the two subtypes DayTimeDuration and YearMonthDuration.- Returns:
- a suitable Comparable
-