Class YearMonthDurationValue

All Implemented Interfaces:
Comparable<XPathComparable>, Iterable<AtomicValue>, AtomicMatchKey, XPathComparable, AtomicSequence, GroundedValue, IdentityComparable, Item, Sequence, ConversionResult, ContextFreeAtomicValue

public final class YearMonthDurationValue extends DurationValue implements XPathComparable, ContextFreeAtomicValue
A value of type xs:yearMonthDuration (or a subtype thereof).

The state retained by this class is essentially a signed 32-bit integer representing the number of months: that is, year*12 + month; plus a type label allowing subtypes of xs:yearMonthDuration to be represented.

  • Constructor Details

    • YearMonthDurationValue

      public YearMonthDurationValue(int months, AtomicType typeLabel)
  • Method Details

    • makeYearMonthDurationValue

      public static ConversionResult makeYearMonthDurationValue(UnicodeString s)
      Static factory: create a year-month duration value from a supplied string, in ISO 8601 format [+|-]PnYnM
      Parameters:
      s - a string in the lexical space of xs:yearMonthDuration.
      Returns:
      either a YearMonthDurationValue, or a ValidationFailure if the string was not in the lexical space of xs:yearMonthDuration.
    • copyAsSubType

      public AtomicValue copyAsSubType(AtomicType typeLabel)
      Create a copy of this atomic value, with a different type label
      Overrides:
      copyAsSubType in class DurationValue
      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.
      Overrides:
      getPrimitiveType in class DurationValue
      Returns:
      the primitive type
    • getPrimitiveStringValue

      public UnicodeString getPrimitiveStringValue()
      Convert to string
      Overrides:
      getPrimitiveStringValue in class DurationValue
      Returns:
      ISO 8601 representation.
    • getLengthInMonths

      public int getLengthInMonths()
      Get the number of months in the duration
      Returns:
      the number of months in the duration
    • fromMonths

      public static YearMonthDurationValue fromMonths(int months)
      Construct a duration value as a number of months.
      Parameters:
      months - the number of months (may be negative)
      Returns:
      the corresponding xs:yearMonthDuration value
    • multiply

      public YearMonthDurationValue multiply(long factor) throws XPathException
      Multiply a duration by an integer
      Overrides:
      multiply in class DurationValue
      Parameters:
      factor - the number to multiply by
      Returns:
      the result of the multiplication
      Throws:
      XPathException - if an error is detected
    • multiply

      public YearMonthDurationValue multiply(double n) throws XPathException
      Multiply duration by a number.
      Overrides:
      multiply in class DurationValue
      Parameters:
      n - the number to multiply by
      Returns:
      the result of the multiplication
      Throws:
      XPathException - if an error is detected
    • multiply

      public YearMonthDurationValue multiply(BigDecimal n) throws XPathException
      Multiply duration by a decimal.
      Overrides:
      multiply in class DurationValue
      Parameters:
      n - the number to multiply by
      Returns:
      the result of the multiplication
      Throws:
      XPathException - if an error is detected
    • divide

      public DurationValue divide(double n) throws XPathException
      Divide duration by a number.
      Overrides:
      divide in class DurationValue
      Parameters:
      n - 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
      Find the ratio between two durations
      Overrides:
      divide in class DurationValue
      Parameters:
      other - the dividend
      Returns:
      the ratio, as a decimal
      Throws:
      XPathException - if an error occurs, for example division by zero or dividing durations of different type
    • add

      public DurationValue add(DurationValue other) throws XPathException
      Add two year-month-durations
      Overrides:
      add in class DurationValue
      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 year-month-durations
      Overrides:
      subtract in class DurationValue
      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)
      Overrides:
      negate in class DurationValue
      Returns:
      the original duration with its sign reversed, retaining its type
    • compareTo

      public int compareTo(XPathComparable other)
      Compare the value to another duration value
      Specified by:
      compareTo in interface Comparable<XPathComparable>
      Parameters:
      other - The other dateTime value
      Returns:
      negative value if this one is the earler, 0 if they are chronologically equal, positive value if this one is the later. For this purpose, dateTime values with an unknown timezone are considered to be UTC values (the Comparable interface requires a total ordering).
      Throws:
      ClassCastException - if the other value is not a DateTimeValue (the parameter is declared as Object to satisfy the Comparable interface)
    • getXPathComparable

      public XPathComparable getXPathComparable(StringCollator collator, int implicitTimezone)
      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.
      Overrides:
      getXPathComparable in class DurationValue
      Parameters:
      collator - the collation to be used when comparing strings
      implicitTimezone - 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.
    • getXPathComparable

      public XPathComparable getXPathComparable()
      Description copied from interface: ContextFreeAtomicValue
      Get an XPathComparable object that supports the semantics of context-free eq and lt comparisons between atomic values. Note that in many cases the returned XPathComparable will be the AtomicValue itself; however because of the constraints of the generic Comparable interface, this cannot be assumed.
      Specified by:
      getXPathComparable in interface ContextFreeAtomicValue
      Returns:
      an XPathComparable that can be used in comparisons with other atomic values.
    • getXPathMatchKey

      public AtomicMatchKey getXPathMatchKey(StringCollator collator, int implicitTimezone)
      Get a Comparable value that implements the XPath ordering comparison semantics for this value. Returns null if the value is not comparable according to XPath rules. The default implementation returns the value itself. This is modified for types such as xs:duration which allow ordering comparisons in XML Schema, but not in XPath.
      Overrides:
      getXPathMatchKey in class DurationValue
      Parameters:
      collator - for comparing strings - not used
      implicitTimezone - implicit timezone in the dynamic context - not used
      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