Class SaxonDuration
- java.lang.Object
-
- javax.xml.datatype.Duration
-
- net.sf.saxon.value.SaxonDuration
-
public class SaxonDuration extends javax.xml.datatype.DurationSaxon implementation of the JAXP class javax.xml.datatype.Duration. This is currently used only by the XQJ interface for XQuery: the normal representation of a duration in Saxon is the classDurationValue.The JAXP specification for this class defines it in terms of XML Schema 1.0 semantics. This defines a structure with six independent components (year, month, day, hour, minute, second). This implementation is more aligned to the XPath 2.0 semantics of the data type, which essentially defines duration as an integer number of months plus a decimal number of seconds.
-
-
Constructor Summary
Constructors Constructor Description SaxonDuration(DurationValue duration)Create a SaxonDuration that wraps a supplied DurationValue
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description javax.xml.datatype.Durationadd(javax.xml.datatype.Duration rhs)Computes a new duration whose value isthis+rhs.voidaddTo(java.util.Calendar calendar)Adds this duration to aCalendarobject.intcompare(javax.xml.datatype.Duration rhs)Partial order relation comparison with thisDurationinstance.DurationValuegetDurationValue()Get the underlying DurationValuejava.lang.NumbergetField(javax.xml.datatype.DatatypeConstants.Field field)Gets the value of a field.intgetSign()Returns the sign of this duration in -1,0, or 1.javax.xml.namespace.QNamegetXMLSchemaType()Get the type of this duration, as one of the values xs:duration, xs:dayTimeDuration, or xs:yearMonthDuration.inthashCode()Returns a hash code consistent with the definition of the equals method.booleanisSet(javax.xml.datatype.DatatypeConstants.Field field)Checks if a field is set.javax.xml.datatype.Durationmultiply(java.math.BigDecimal factor)Computes a new duration whose value isfactortimes longer than the value of this duration.javax.xml.datatype.Durationnegate()Returns a newDurationobject whose value is-this.javax.xml.datatype.DurationnormalizeWith(java.util.Calendar startTimeInstant)Converts the years and months fields into the days field by using a specific time instant as the reference point.javax.xml.datatype.Durationsubtract(javax.xml.datatype.Duration rhs)Computes a new duration whose value isthis-rhs.
-
-
-
Constructor Detail
-
SaxonDuration
public SaxonDuration(DurationValue duration)
Create a SaxonDuration that wraps a supplied DurationValue- Parameters:
duration- the value to be wrapped.
-
-
Method Detail
-
getDurationValue
public DurationValue getDurationValue()
Get the underlying DurationValue- Returns:
- the underlying DurationValue
-
getXMLSchemaType
public javax.xml.namespace.QName getXMLSchemaType()
Get the type of this duration, as one of the values xs:duration, xs:dayTimeDuration, or xs:yearMonthDuration. (Note that the XML Schema namespace URI is used, whereas the current implementation of the superclass uses a provisional URI allocated in a 2003 W3C working draft)- Overrides:
getXMLSchemaTypein classjavax.xml.datatype.Duration- Returns:
- the type of this duration, as one of the values xs:duration, xs:dayTimeDuration, or xs:yearMonthDuration
-
getSign
public int getSign()
Returns the sign of this duration in -1,0, or 1.- Specified by:
getSignin classjavax.xml.datatype.Duration- Returns:
- -1 if this duration is negative, 0 if the duration is zero, and 1 if the duration is positive.
-
getField
public java.lang.Number getField(javax.xml.datatype.DatatypeConstants.Field field)
Gets the value of a field.Fields of a duration object may contain arbitrary large value. Therefore this method is designed to return a
Numberobject.In case of YEARS, MONTHS, DAYS, HOURS, and MINUTES, the returned number will be a non-negative integer. In case of seconds, the returned number may be a non-negative decimal value.
The Saxon implementation of duration uses normalized values. This means that the YEARS and DAYS fields may be arbitrarily large, but other components will be limited in size: for example MINUTES will never exceed 60 and MONTHS will never exceed 12.
- Specified by:
getFieldin classjavax.xml.datatype.Duration- Parameters:
field- one of the six Field constants (YEARS, MONTHS, DAYS, HOURS, MINUTES, or SECONDS.)- Returns:
- If the specified field is present, this method returns
a non-null non-negative
Numberobject that represents its value. If it is not present, return null. For YEARS, MONTHS, DAYS, HOURS, and MINUTES, this method returns aBigIntegerobject. For SECONDS, this method returns aBigDecimal. - Throws:
java.lang.NullPointerException- If thefieldisnull.
-
isSet
public boolean isSet(javax.xml.datatype.DatatypeConstants.Field field)
Checks if a field is set. In this implementation, all fields are always set.- Specified by:
isSetin classjavax.xml.datatype.Duration- Parameters:
field- one of the six Field constants (YEARS, MONTHS, DAYS, HOURS, MINUTES, or SECONDS.)- Returns:
- This implementation always returns true.
-
add
public javax.xml.datatype.Duration add(javax.xml.datatype.Duration rhs)
Computes a new duration whose value is
this+rhs.This implementation follows the XPath semantics. This means that the operation will fail if the duration is not a yearMonthDuration or a dayTimeDuration
- Specified by:
addin classjavax.xml.datatype.Duration- Parameters:
rhs-Durationto add to thisDuration- Returns:
- non-null valid Duration object.
- Throws:
java.lang.NullPointerException- If the rhs parameter is null.java.lang.IllegalStateException- If the durations are not both dayTimeDurations, or both yearMonthDurations.- See Also:
subtract(javax.xml.datatype.Duration)
-
subtract
public javax.xml.datatype.Duration subtract(javax.xml.datatype.Duration rhs)
Computes a new duration whose value is
this-rhs.This implementation follows the XPath semantics. This means that the operation will fail if the duration is not a yearMonthDuration or a dayTimeDuration.
- Overrides:
subtractin classjavax.xml.datatype.Duration- Parameters:
rhs-Durationto subtract from thisDuration- Returns:
- non-null valid Duration object.
- Throws:
java.lang.NullPointerException- If the rhs parameter is null.java.lang.IllegalStateException- If the durations are not both dayTimeDurations, or both yearMonthDurations.- See Also:
add(javax.xml.datatype.Duration)
-
addTo
public void addTo(java.util.Calendar calendar)
Adds this duration to aCalendarobject.Calls
Calendar.add(int, int)in the order of YEARS, MONTHS, DAYS, HOURS, MINUTES, SECONDS, and MILLISECONDS if those fields are present. Because theCalendarclass uses int to hold values, there are cases where this method won't work correctly (for example if values of fields exceed the range of int.)Also, since this duration class is a Gregorian duration, this method will not work correctly if the given
Calendarobject is based on some other calendar systems.Any fractional parts of this
Durationobject beyond milliseconds will be simply ignored. For example, if this duration is "P1.23456S", then 1 is added to SECONDS, 234 is added to MILLISECONDS, and the rest will be unused.Note that because
Calendar.add(int, int)is using int,Durationwith values beyond the range of int in its fields will cause overflow/underflow to the givenCalendar.XMLGregorianCalendar.add(javax.xml.datatype.Duration)provides the same basic operation as this method while avoiding the overflow/underflow issues.- Specified by:
addToin classjavax.xml.datatype.Duration- Parameters:
calendar- A calendar object whose value will be modified.- Throws:
java.lang.NullPointerException- if the calendar parameter is null.
-
multiply
public javax.xml.datatype.Duration multiply(java.math.BigDecimal factor)
Computes a new duration whose value isfactortimes longer than the value of this duration.This implementation follows the XPath semantics. This means that it is defined only on yearMonthDuration and dayTimeDuration. Other cases produce an IllegalStateException.
- Specified by:
multiplyin classjavax.xml.datatype.Duration- Parameters:
factor- to multiply by- Returns:
- returns a non-null valid
Durationobject - Throws:
java.lang.IllegalStateException- if operation produces fraction in the months field.java.lang.NullPointerException- if thefactorparameter isnull.
-
negate
public javax.xml.datatype.Duration negate()
Returns a newDurationobject whose value is-this.Since the
Durationclass is immutable, this method doesn't change the value of this object. It simply computes a new Duration object and returns it.- Specified by:
negatein classjavax.xml.datatype.Duration- Returns:
- always return a non-null valid
Durationobject.
-
normalizeWith
public javax.xml.datatype.Duration normalizeWith(java.util.Calendar startTimeInstant)
Converts the years and months fields into the days field by using a specific time instant as the reference point.
This implementation does not support this method
- Specified by:
normalizeWithin classjavax.xml.datatype.Duration- Parameters:
startTimeInstant-Calendarreference point.- Returns:
Durationof years and months of thisDurationas days.- Throws:
java.lang.NullPointerException- If the startTimeInstant parameter is null.java.lang.UnsupportedOperationException- Always thrown by this implementation.
-
compare
public int compare(javax.xml.datatype.Duration rhs)
Partial order relation comparison with this
Durationinstance.This implementation follows the XPath semantics. This means that the result is defined only for dayTimeDuration and yearMonthDuration values, and the result is never indeterminate.
Return:
DatatypeConstants.LESSERif thisDurationis shorter thandurationparameterDatatypeConstants.EQUALif thisDurationis equal todurationparameterDatatypeConstants.GREATERif thisDurationis longer thandurationparameterDatatypeConstants.INDETERMINATEif a conclusive partial order relation cannot be determined
- Specified by:
comparein classjavax.xml.datatype.Duration- Parameters:
rhs- duration to compare- Returns:
- the relationship between
thisDurationanddurationparameter asDatatypeConstants.LESSER,DatatypeConstants.EQUAL,DatatypeConstants.GREATERorDatatypeConstants.INDETERMINATE. - Throws:
java.lang.UnsupportedOperationException- If the underlying implementation cannot reasonably process the request, e.g. W3C XML Schema allows for arbitrarily large/small/precise values, the request may be beyond the implementations capability.java.lang.NullPointerException- ifdurationisnull.java.lang.IllegalArgumentException- if the operands are not dayTimeDuration or yearMonthDuration values.- See Also:
Duration.isShorterThan(javax.xml.datatype.Duration),Duration.isLongerThan(javax.xml.datatype.Duration)
-
hashCode
public int hashCode()
Returns a hash code consistent with the definition of the equals method.- Specified by:
hashCodein classjavax.xml.datatype.Duration- See Also:
Object.hashCode()
-
-