Package net.sf.saxon.value
Class DurationValue
- java.lang.Object
-
- net.sf.saxon.value.AtomicValue
-
- net.sf.saxon.value.DurationValue
-
- All Implemented Interfaces:
java.lang.Iterable<AtomicValue>
,AtomicMatchKey
,AtomicSequence
,GroundedValue
,IdentityComparable
,Item
,Sequence
,ConversionResult
- Direct Known Subclasses:
DayTimeDurationValue
,YearMonthDurationValue
public class DurationValue extends AtomicValue implements AtomicMatchKey
A value of type xs:duration
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
DurationValue.DurationComparable
DurationValueComparable is a Comparable value that acts as a surrogate for a Duration, having ordering rules that implement the XML Schema specification.
-
Field Summary
Fields Modifier and Type Field Description protected int
_months
protected int
_nanoseconds
protected boolean
_negative
protected long
_seconds
-
Fields inherited from class net.sf.saxon.value.AtomicValue
typeLabel
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
DurationValue()
Private constructor for internal useDurationValue(boolean positive, int years, int months, int days, int hours, int minutes, long seconds, int microseconds)
Constructor for xs:duration taking the components of the duration.DurationValue(boolean positive, int years, int months, int days, int hours, int minutes, long seconds, int microseconds, AtomicType type)
Constructor for xs:duration taking the components of the duration, plus a user-specified type which must be a subtype of xs:duration.DurationValue(int years, int months, int days, int hours, int minutes, long seconds, int nanoseconds, AtomicType type)
Constructor for xs:duration taking the components of the duration, plus a user-specified type which must be a subtype of xs:duration.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description DurationValue
add(DurationValue other)
Add two durationsprotected static ValidationFailure
badDuration(java.lang.String msg, UnicodeString s)
protected static ValidationFailure
badDuration(java.lang.String msg, UnicodeString s, java.lang.String errorCode)
AtomicValue
copyAsSubType(AtomicType typeLabel)
Create a copy of this atomic value, with a different type labelDurationValue
divide(double factor)
Divide a duration by a numberBigDecimalValue
divide(DurationValue other)
Divide a duration by a another durationboolean
equals(java.lang.Object other)
Test if the two durations are of equal length.protected static void
formatFractionalSeconds(UnicodeBuilder sb, int seconds, long nanosecs)
AtomicValue
getComponent(AccessorFn.Component component)
Get a component of the normalized valueint
getDays()
Get the days componentint
getHours()
Get the hours componentdouble
getLengthInSeconds()
Get length of duration in seconds, assuming an average length of month.int
getMicroseconds()
Get the microseconds componentint
getMinutes()
Get the minutes componentint
getMonths()
Get the months componentint
getNanoseconds()
Get the nanoseconds componentUnicodeString
getPrimitiveStringValue()
Convert to stringBuiltInAtomicType
getPrimitiveType()
Determine the primitive type of the value.DurationValue.DurationComparable
getSchemaComparable()
Get a Comparable value that implements the XML Schema ordering comparison semantics for this value.int
getSeconds()
Get the seconds componentint
getTotalMonths()
Get the total number of months (ignoring the days/hours/minutes/seconds)java.math.BigDecimal
getTotalSeconds()
Get the total number of seconds (ignoring the years/months)XPathComparable
getXPathComparable(StringCollator collator, int implicitTimezone)
Get an object value that implements the XPath equality and ordering comparison semantics for this value.AtomicMatchKey
getXPathMatchKey(StringCollator collator, int implicitTimezone)
Get an object value that implements the XPath equality and ordering comparison semantics for this value.int
getYears()
Get the year componentint
hashCode()
Returns a hash code value for the object.static ConversionResult
makeDuration(UnicodeString s)
Static factory method: create a duration value from a supplied string, in ISO 8601 format [-]PnYnMnDTnHnMnSprotected static ConversionResult
makeDuration(UnicodeString s, boolean allowYM, boolean allowDT)
DurationValue
multiply(double factor)
Multiply a duration by a doubleDurationValue
multiply(long factor)
Multiply a duration by an integerDurationValue
multiply(java.math.BigDecimal factor)
Multiply a duration by a decimalDurationValue
negate()
Negate a duration (same as subtracting from zero, but it preserves the type of the original duration)protected void
normalizeZeroDuration()
Ensure that a zero duration is considered positiveint
signum()
Return the signum of the valueprotected static int
simpleInteger(java.lang.String s)
Parse a simple unsigned integerDurationValue
subtract(DurationValue other)
Subtract two durations-
Methods inherited from class net.sf.saxon.value.AtomicValue
asAtomic, asMapKey, atomize, checkPermittedContents, checkValidInJavascript, effectiveBooleanValue, getCanonicalLexicalRepresentation, getCardinality, getGenre, getItemType, getLength, getUnicodeStringValue, getUType, head, identityHashCode, isIdentical, isIdentical, isNaN, isUntypedAtomic, itemAt, iterate, iterator, setTypeLabel, show, toShortString, toString
-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface net.sf.saxon.expr.sort.AtomicMatchKey
asAtomic
-
Methods inherited from interface net.sf.saxon.om.GroundedValue
asIterable, concatenate, containsNode, materialize
-
Methods inherited from interface net.sf.saxon.om.Item
getStringValue, isStreamed, reduce, subsequence
-
Methods inherited from interface net.sf.saxon.om.Sequence
makeRepeatable
-
-
-
-
Constructor Detail
-
DurationValue
protected DurationValue()
Private constructor for internal use
-
DurationValue
public DurationValue(boolean positive, int years, int months, int days, int hours, int minutes, long seconds, int microseconds) throws java.lang.IllegalArgumentException
Constructor for xs:duration taking the components of the duration. There is no requirement that the values are normalized, for example it is acceptable to specify months=18. The values of the individual components must all be non-negative.Note: For historic reasons this constructor only supports microsecond precision. To get nanosecond precision, use the constructor
DurationValue(int, int, int, int, int, long, int, AtomicType)
.- Parameters:
positive
- true if the duration is positive, false if negative. For a negative duration the components are all supplied as positive integers (or zero).years
- the number of yearsmonths
- the number of monthsdays
- the number of dayshours
- the number of hoursminutes
- the number of minutesseconds
- the number of secondsmicroseconds
- the number of microseconds- Throws:
java.lang.IllegalArgumentException
- if the size of the duration exceeds implementation-defined limits: specifically, if the total number of months exceeds 2^31, or if the total number of seconds exceeds 2^63.
-
DurationValue
public DurationValue(boolean positive, int years, int months, int days, int hours, int minutes, long seconds, int microseconds, AtomicType type)
Constructor for xs:duration taking the components of the duration, plus a user-specified type which must be a subtype of xs:duration. There is no requirement that the values are normalized, for example it is acceptable to specify months=18. The values of the individual components must all be non-negative.Note: for historic reasons this constructor was written to expect microseconds rather than nanoseconds. To supply nanoseconds, use the alternative constructor
DurationValue(int, int, int, int, int, long, int, AtomicType)
.- Parameters:
positive
- true if the duration is positive, false if negative. For a negative duration the components are all supplied as positive integers (or zero).years
- the number of yearsmonths
- the number of monthsdays
- the number of dayshours
- the number of hoursminutes
- the number of minutesseconds
- the number of seconds (long to allow copying)microseconds
- the number of microsecondstype
- the user-defined subtype of xs:duration. Note that this constructor cannot be used to create an instance of xs:dayTimeDuration or xs:yearMonthDuration.- Throws:
java.lang.IllegalArgumentException
- if the size of the duration exceeds implementation-defined limits: specifically, if the total number of months exceeds 2^31, or if the total number of seconds exceeds 2^63.
-
DurationValue
public DurationValue(int years, int months, int days, int hours, int minutes, long seconds, int nanoseconds, AtomicType type)
Constructor for xs:duration taking the components of the duration, plus a user-specified type which must be a subtype of xs:duration. There is no requirement that the values are normalized, for example it is acceptable to specify months=18. The values of the individual components must all be non-negative.If the duration is positive, all the components must be supplied as positive (or zero) integers. If the duration is negative, all the components must be supplied as negative (or zero) integers.
- Parameters:
years
- the number of yearsmonths
- the number of monthsdays
- the number of dayshours
- the number of hoursminutes
- the number of minutesseconds
- the number of seconds (long to allow copying)nanoseconds
- the number of nanosecondstype
- the user-defined subtype of xs:duration. Note that this constructor cannot be used to create an instance of xs:dayTimeDuration or xs:yearMonthDuration.- Throws:
java.lang.IllegalArgumentException
- if the size of the duration exceeds implementation-defined limits: specifically, if the total number of months exceeds 2^31, or if the total number of seconds exceeds 2^63.
-
-
Method Detail
-
formatFractionalSeconds
protected static void formatFractionalSeconds(UnicodeBuilder sb, int seconds, long nanosecs)
-
normalizeZeroDuration
protected void normalizeZeroDuration()
Ensure that a zero duration is considered positive
-
makeDuration
public static ConversionResult makeDuration(UnicodeString s)
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
ValidationFailure
if the supplied string is lexically invalid.
-
makeDuration
protected static ConversionResult makeDuration(UnicodeString s, boolean allowYM, boolean allowDT)
-
badDuration
protected static ValidationFailure badDuration(java.lang.String msg, UnicodeString s)
-
badDuration
protected static ValidationFailure badDuration(java.lang.String msg, UnicodeString s, java.lang.String errorCode)
-
simpleInteger
protected static int simpleInteger(java.lang.String s)
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^31
-
copyAsSubType
public AtomicValue copyAsSubType(AtomicType typeLabel)
Create a copy of this atomic value, with a different type label- Specified by:
copyAsSubType
in classAtomicValue
- Parameters:
typeLabel
- the type label of the new copy. The caller is responsible for checking that the value actually conforms to this type.- Returns:
- the copied value
-
getPrimitiveType
public BuiltInAtomicType 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:
getPrimitiveType
in 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 int getDays()
Get the days component- Returns:
- the number of days in the normalized duration; always 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
public int getSeconds()
Get the seconds component- Returns:
- the number of whole seconds in the normalized duration; always positive, in the range 0-59
-
getMicroseconds
public int getMicroseconds()
Get the microseconds component- Returns:
- the number of nanoseconds in the normalized duration, divided by one thousand and rounded down; always positive, in the range 0-999999
-
getNanoseconds
public int getNanoseconds()
Get the nanoseconds component- Returns:
- the number of nanoseconds in the normalized duration; always positive, in the range 0-999,999,999
-
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
public java.math.BigDecimal getTotalSeconds()
Get the total number of seconds (ignoring the years/months)- Returns:
- the total number of seconds, as a positive or negative number according as the duration is positive or negative, with the fractional part indicating parts of a second to nanosecond precision
-
getPrimitiveStringValue
public UnicodeString getPrimitiveStringValue()
Convert to string- Specified by:
getPrimitiveStringValue
in classAtomicValue
- Returns:
- ISO 8601 representation.
-
getLengthInSeconds
public double getLengthInSeconds()
Get length of duration in seconds, assuming an average length of month. (Note, this defines a total ordering on durations which is different from the partial order defined in XML Schema; XPath 2.0 currently avoids defining an ordering at all. But the ordering here is consistent with the ordering of the two duration subtypes in XPath 2.0.)- Returns:
- the duration in seconds, as a double
-
getComponent
public AtomicValue getComponent(AccessorFn.Component component)
Get a component of the normalized value- Overrides:
getComponent
in classAtomicValue
- Parameters:
component
- the required component- Returns:
- the value of the requested component of this value
-
getXPathMatchKey
public AtomicMatchKey getXPathMatchKey(StringCollator collator, int implicitTimezone)
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:
getXPathMatchKey
in classAtomicValue
- Parameters:
collator
- collation used for comparing string valuesimplicitTimezone
- the XPath dynamic evaluation context, used in cases where the comparison is context- 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) throws NoDynamicContextException
Description copied from class:AtomicValue
Get 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:
getXPathComparable
in 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 timezone- 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.
-
equals
public boolean equals(java.lang.Object other)
Test if the two durations are of equal length.- Overrides:
equals
in 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:
java.lang.ClassCastException
- if the other value is not an xs:duration or subtype thereof
-
hashCode
public int hashCode()
Description copied from class:AtomicValue
Returns a hash code value for the object.- Overrides:
hashCode
in classAtomicValue
-
add
public DurationValue add(DurationValue other) throws XPathException
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
public DurationValue subtract(DurationValue other) throws XPathException
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
public DurationValue 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
public DurationValue multiply(long factor) throws XPathException
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
public DurationValue multiply(double factor) throws XPathException
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
public DurationValue multiply(java.math.BigDecimal factor) throws XPathException
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
public DurationValue divide(double factor) throws XPathException
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
public BigDecimalValue divide(DurationValue other) throws XPathException
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
public DurationValue.DurationComparable 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
-
-