net.sf.saxon.value

## Class DurationValue

- java.lang.Object
- - net.sf.saxon.om.AbstractItem
  - - net.sf.saxon.value.AtomicValue
    - - net.sf.saxon.value.DurationValue

- All Implemented Interfaces:
    PullEvent, AtomicSequence, GroundedValue, IdentityComparable, Item, Sequence, ConversionResult
  Direct Known Subclasses:
    DayTimeDurationValue, YearMonthDurationValue

      public class DurationValue
      extends AtomicValue

  A value of type xs:duration

- - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `protected int` | `microseconds`
    `protected int` | `months`
    `protected boolean` | `negative`
    `protected long` | `seconds`

    - ### Fields inherited from class net.sf.saxon.value.AtomicValue

      `typeLabel`

  - ### Constructor Summary

    Constructors
    Modifier | Constructor and Description
    --- | ---
    `protected` | `DurationValue()` Private constructor for internal use
    `` | `DurationValue(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.

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `DurationValue` | `add(DurationValue other)` Add two durations
    `protected static ValidationFailure` | `badDuration(String msg, CharSequence s)`
    `protected static ValidationFailure` | `badDuration(String msg, CharSequence s, String errorCode)`
    `AtomicValue` | `copyAsSubType(AtomicType typeLabel)` Create a copy of this atomic value, with a different type label
    `DecimalValue` | `divide(DurationValue other)` Divide a duration by a another duration
    `boolean` | `equals(Object other)` Test if the two durations are of equal length.
    `AtomicValue` | `getComponent(int component)` Get a component of the normalized value
    `int` | `getDays()` Get the days component
    `int` | `getHours()` Get the hours component
    `double` | `getLengthInSeconds()` Get length of duration in seconds, assuming an average length of month.
    `int` | `getMicroseconds()` Get the microseconds component
    `int` | `getMinutes()` Get the minutes component
    `int` | `getMonths()` Get the months component
    `CharSequence` | `getPrimitiveStringValue()` Convert to string
    `BuiltInAtomicType` | `getPrimitiveType()` Determine the primitive type of the value.
    `Comparable` | `getSchemaComparable()` Get a Comparable value that implements the XML Schema ordering comparison semantics for this value.
    `static Comparable` | `getSchemaComparable(DurationValue value)` Get a Comparable value that implements the XML Schema ordering comparison semantics for this value.
    `int` | `getSeconds()` Get the seconds component
    `int` | `getTotalMonths()` Get the total number of months (ignoring the days/hours/minutes/seconds)
    `BigDecimal` | `getTotalSeconds()` Get the total number of seconds (ignoring the years/months)
    `Object` | `getXPathComparable(boolean ordered, StringCollator collator, XPathContext context)` Get an object value that implements the XPath equality and ordering comparison semantics for this value.
    `int` | `getYears()` Get the year component
    `int` | `hashCode()`
    `static ConversionResult` | `makeDuration(CharSequence s)` Static factory method: create a duration value from a supplied string, in ISO 8601 format [-]PnYnMnDTnHnMnS
    `protected static ConversionResult` | `makeDuration(CharSequence s, boolean allowYM, boolean allowDT)`
    `DurationValue` | `multiply(double factor)` Multiply a duration by a number
    `DurationValue` | `negate()` Negate a duration (same as subtracting from zero, but it preserves the type of the original duration)
    `DurationValue` | `normalizeDuration()` Deprecated. since 9.0 - the method does nothing
    `protected void` | `normalizeZeroDuration()` Ensure that a zero duration is considered positive
    `int` | `signum()` Return the signum of the value
    `protected static int` | `simpleInteger(String s)` Parse a simple unsigned integer
    `DurationValue` | `subtract(DurationValue other)` Subtract two durations

    - ### Methods inherited from class net.sf.saxon.value.AtomicValue

      `asAtomic, checkPermittedContents, effectiveBooleanValue, getCanonicalLexicalRepresentation, getCardinality, getItemType, getStringValue, getStringValueCS, head, isIdentical, isIdentical, isNaN, itemAt, iterate, process, setTypeLabel, toString`

    - ### Methods inherited from class net.sf.saxon.om.AbstractItem

      `getLength, reduce, subsequence`

    - ### Methods inherited from class java.lang.Object

      `clone, finalize, getClass, notify, notifyAll, wait, wait, wait`

    - ### Methods inherited from interface net.sf.saxon.om.AtomicSequence

      `getLength`

    - ### Methods inherited from interface net.sf.saxon.om.GroundedValue

      `reduce, subsequence`

- - ### Field Detail

    - #### negative

          protected boolean negative

    - #### months

          protected int months

    - #### seconds

          protected long seconds

    - #### microseconds

          protected int microseconds

  - ### 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 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.
      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 years
        `months` - the number of months
        `days` - the number of days
        `hours` - the number of hours
        `minutes` - the number of minutes
        `seconds` - the number of seconds
        `microseconds` - the number of microseconds
      Throws:
        `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.
      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 years
        `months` - the number of months
        `days` - the number of days
        `hours` - the number of hours
        `minutes` - the number of minutes
        `seconds` - the number of seconds (long to allow copying)
        `microseconds` - the number of microseconds
        `type` - 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:
        `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

    - #### normalizeZeroDuration

          protected void normalizeZeroDuration()

      Ensure that a zero duration is considered positive

    - #### makeDuration

          public static ConversionResult makeDuration(CharSequence 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(CharSequence s,
                                      boolean allowYM,
                                      boolean allowDT)

    - #### badDuration

          protected static ValidationFailure badDuration(String msg,
                                      CharSequence s)

    - #### badDuration

          protected static ValidationFailure badDuration(String msg,
                                      CharSequence s,
                                      String errorCode)

    - #### simpleInteger

          protected static int simpleInteger(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 class `AtomicValue`
      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 class `AtomicValue`
      Returns:
        the primitive type

    - #### normalizeDuration

          public DurationValue normalizeDuration()

      Deprecated. since 9.0 - the method does nothing
      Normalize the duration, so that months<12, hours<24, minutes<60, seconds<60. Since durations are now always normalized, this method has become a no-op, but is retained for backwards compatibility
      Returns:
        the duration unchanged

    - #### 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 microseconds in the normalized duration; always positive, in the range 0-999999

    - #### 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 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

    - #### getPrimitiveStringValue

          public CharSequence getPrimitiveStringValue()

      Convert to string
      Specified by:
        `getPrimitiveStringValue` in class `AtomicValue`
      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(int component)
                                   throws XPathException

      Get a component of the normalized value
      Overrides:
        `getComponent` in class `AtomicValue`
      Parameters:
        `component` - identifies the required component, as a constant defined in class `Component`, for example `Component.HOURS`
      Returns:
        the value of the requested component of this value
      Throws:
        `XPathException` - if a dynamic error occurs

    - #### getXPathComparable

          public Object getXPathComparable(boolean ordered,
                                  StringCollator collator,
                                  XPathContext context)

      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:
        `getXPathComparable` in class `AtomicValue`
      Parameters:
        `ordered` - true if an ordered comparison is required. In this case the result is null if the type is unordered; in other cases the returned value will be a Comparable.
        `collator` - collation used for comparing string values
        `context` - the XPath dynamic evaluation context, used in cases where the comparison is context sensitive @return an Object whose equals() and hashCode() methods implement the XPath comparison semantics
      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

    - #### equals

          public boolean equals(Object other)

      Test if the two durations are of equal length.
      Specified by:
        `equals` in class `AtomicValue`
      Parameters:
        `other` - the other value
      Returns:
        true 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()

      Overrides:
        `hashCode` in class `Object`

    - #### 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`

    - #### 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`

    - #### 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(double factor)
                                 throws XPathException

      Multiply a duration by a number
      Parameters:
        `factor` - the number to multiply by
      Returns:
        the result of the multiplication
      Throws:
        `XPathException`

    - #### divide

          public DecimalValue 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`

    - #### getSchemaComparable

          public Comparable 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.
      Specified by:
        `getSchemaComparable` in interface `AtomicSequence`
      Specified by:
        `getSchemaComparable` in class `AtomicValue`
      Returns:
        a suitable Comparable

    - #### getSchemaComparable

          public static Comparable getSchemaComparable(DurationValue value)

      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.
      Parameters:
        `value` - the duration for which a comparison key is required
      Returns:
        a suitable Comparable

Copyright (c) 2004-2013 Saxonica Limited. All rights reserved.