net.sf.saxon.value

## Class AtomicValue

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

- All Implemented Interfaces:
    PullEvent, AtomicSequence, GroundedValue, IdentityComparable, Item, Sequence, ConversionResult
  Direct Known Subclasses:
    Base64BinaryValue, BooleanValue, CalendarValue, DurationValue, HexBinaryValue, NumericValue, QualifiedNameValue, StringValue

      public abstract class AtomicValue
      extends AbstractItem
      implements AtomicSequence, ConversionResult, IdentityComparable

  The AtomicValue class corresponds to the concept of an atomic value in the XPath 2.0 data model. Atomic values belong to one of the 19 primitive types defined in XML Schema; or they are of type xs:untypedAtomic; or they are "external objects", representing a Saxon extension to the XPath 2.0 type system.

  The AtomicValue class contains some methods that are suitable for applications to use, and many others that are designed for internal use by Saxon itself. These have not been fully classified. At present, therefore, none of the methods on this class should be considered to be part of the public Saxon API.

  Author:
    Michael H. Kay

- - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `protected AtomicType` | `typeLabel`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `AtomicValue()`

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `AtomicValue` | `asAtomic()` Calling this method on a ConversionResult returns the AtomicValue that results from the conversion if the conversion was successful, and throws a ValidationException explaining the conversion error otherwise.
    `void` | `checkPermittedContents(SchemaType parentType, StaticContext env, boolean whole)` Check statically that the results of the expression are capable of constructing the content of a given schema type.
    `abstract AtomicValue` | `copyAsSubType(AtomicType typeLabel)` Create a copy of this atomic value, with a different type label
    `boolean` | `effectiveBooleanValue()` Get the effective boolean value of the value
    `abstract boolean` | `equals(Object o)` The equals() methods on atomic values is defined to follow the semantics of eq when applied to two atomic values.
    `CharSequence` | `getCanonicalLexicalRepresentation()` Get the canonical lexical representation as defined in XML Schema.
    `int` | `getCardinality()` Determine the static cardinality
    `AtomicValue` | `getComponent(int component)` Method to extract components of a value.
    `AtomicType` | `getItemType()` Determine the data type of the value
    `protected abstract CharSequence` | `getPrimitiveStringValue()` Convert the value to a string, using the serialization rules for the primitive type.
    `abstract BuiltInAtomicType` | `getPrimitiveType()` Determine the primitive type of the value.
    `abstract Comparable` | `getSchemaComparable()` Get a Comparable value that implements the XML Schema ordering comparison semantics for this value.
    `String` | `getStringValue()` Convert the value to a string, using the serialization rules.
    `CharSequence` | `getStringValueCS()` Get the value of the item as a CharSequence.
    `abstract Object` | `getXPathComparable(boolean ordered, StringCollator collator, XPathContext context)` Get an object value that implements the XPath equality and ordering comparison semantics for this value.
    `AtomicValue` | `head()` To implement `Sequence`, this method returns the item itself
    `boolean` | `isIdentical(AtomicValue v)` Determine whether two atomic values are identical, as determined by XML Schema rules.
    `boolean` | `isIdentical(IdentityComparable other)` Determine whether two IdentityComparable values are identical.
    `boolean` | `isNaN()` Test whether the value is the special value NaN
    `AtomicValue` | `itemAt(int n)` Get the n'th item in the sequence (starting from 0).
    `SequenceIterator<AtomicValue>` | `iterate()` To implement `Sequence`, this method returns a singleton iterator that delivers this item in the form of a sequence
    `void` | `process(XPathContext context)` Process the instruction, without returning any tail calls
    `void` | `setTypeLabel(AtomicType type)` Set the type label on this atomic value.
    `String` | `toString()` Get string value.

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

      `getLength, reduce, subsequence`

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

      `clone, finalize, getClass, hashCode, 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

    - #### typeLabel

          protected AtomicType typeLabel

  - ### Constructor Detail

    - #### AtomicValue

          public AtomicValue()

  - ### Method Detail

    - #### head

          public final AtomicValue head()

      To implement `Sequence`, this method returns the item itself
      Specified by:
        `head` in interface `AtomicSequence`
      Specified by:
        `head` in interface `Sequence`
      Overrides:
        `head` in class `AbstractItem`
      Returns:
        this item

    - #### iterate

          public final SequenceIterator<AtomicValue> iterate()

      To implement `Sequence`, this method returns a singleton iterator that delivers this item in the form of a sequence
      Specified by:
        `iterate` in interface `AtomicSequence`
      Specified by:
        `iterate` in interface `Sequence`
      Overrides:
        `iterate` in class `AbstractItem`
      Returns:
        a singleton iterator that returns this item

    - #### setTypeLabel

          public void setTypeLabel(AtomicType type)

      Set the type label on this atomic value. Note that this modifies the value, so it must only called if the caller is confident that the value is not shared. In other cases, use `copyAsSubType(net.sf.saxon.type.AtomicType)`
      Parameters:
        `type` - the type label to be set

    - #### getSchemaComparable

          public abstract Comparable getSchemaComparable()

      Get a Comparable value that implements the XML Schema ordering comparison semantics for this value. An implementation must be provided for all atomic types.

      In the case of data types that are partially ordered, the returned Comparable extends the standard semantics of the compareTo() method by returning the value `SequenceTool.INDETERMINATE_ORDERING` when there is no defined order relationship between two given values. This value is also returned when two values of different types are compared.

      Specified by:
        `getSchemaComparable` in interface `AtomicSequence`
      Returns:
        a Comparable that follows XML Schema comparison rules

    - #### getXPathComparable

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

      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.
      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` - the collation to be used when comparing strings
        `context` - the XPath dynamic evaluation context, used in cases where the comparison is context sensitive
      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
      Throws:
        `NoDynamicContextException` - if the comparison depends on dynamic context information that is not available, for example implicit timezone

    - #### equals

          public abstract boolean equals(Object o)

      The equals() methods on atomic values is defined to follow the semantics of eq when applied to two atomic values. When the other operand is not an atomic value, the result is undefined (may be false, may be an exception). When the other operand is an atomic value that cannot be compared with this one, the method must throw a ClassCastException.

      The hashCode() method is consistent with equals().

      Overrides:
        `equals` in class `Object`
      Parameters:
        `o` - 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

    - #### isIdentical

          public boolean isIdentical(AtomicValue v)

      Determine whether two atomic values are identical, as determined by XML Schema rules. This is a stronger test than equality (even schema-equality); for example two dateTime values are not identical unless they are in the same timezone.

      Note that even this check ignores the type annotation of the value. The integer 3 and the short 3 are considered identical, even though they are not fully interchangeable. "Identical" means the same point in the value space, regardless of type annotation.

      NaN is identical to itself.

      Parameters:
        `v` - the other value to be compared with this one
      Returns:
        true if the two values are identical, false otherwise.

    - #### isIdentical

          public boolean isIdentical(IdentityComparable other)

      Determine whether two IdentityComparable values are identical. This is a stronger test than equality (even schema-equality); for example two dateTime values are not identical unless they are in the same timezone.
      Specified by:
        `isIdentical` in interface `IdentityComparable`
      Parameters:
        `other` -
      Returns:
        true if the two values are identical, false otherwise

    - #### getStringValueCS

          public final CharSequence getStringValueCS()

      Get the value of the item as a CharSequence. This is in some cases more efficient than the version of the method that returns a String.
      Specified by:
        `getStringValueCS` in interface `AtomicSequence`
      Specified by:
        `getStringValueCS` in interface `GroundedValue`
      Specified by:
        `getStringValueCS` in interface `Item`
      Returns:
        the string value of the sequence.
      See Also:
        `Item.getStringValue()`

    - #### getCanonicalLexicalRepresentation

          public CharSequence getCanonicalLexicalRepresentation()

      Get the canonical lexical representation as defined in XML Schema. This is not always the same as the result of casting to a string according to the XPath rules.
      Specified by:
        `getCanonicalLexicalRepresentation` in interface `AtomicSequence`
      Returns:
        the canonical lexical representation if defined in XML Schema; otherwise, the result of casting to string according to the XPath 2.0 rules

    - #### process

          public void process(XPathContext context)
                       throws XPathException

      Process the instruction, without returning any tail calls
      Parameters:
        `context` - The dynamic context, giving access to the current node, the current variables, etc.
      Throws:
        `XPathException` - if the current receiver fails for any reason, for example with a serialization error due to invalid characters in the content

    - #### itemAt

          public final AtomicValue itemAt(int n)

      Get the n'th item in the sequence (starting from 0). This is defined for all Values, but its real benefits come for a sequence Value stored extensionally (or for a MemoClosure, once all the values have been read)
      Specified by:
        `itemAt` in interface `AtomicSequence`
      Specified by:
        `itemAt` in interface `GroundedValue`
      Overrides:
        `itemAt` in class `AbstractItem`
      Parameters:
        `n` - position of the required item, counting from zero.
      Returns:
        the n'th item in the sequence, where the first item in the sequence is numbered zero. If n is negative or >= the length of the sequence, returns null.

    - #### getItemType

          public final AtomicType getItemType()

      Determine the data type of the value
      Returns:
        the type annotation of the atomic value

    - #### getPrimitiveType

          public abstract 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 xs:anyAtomicType.
      Returns:
        the primitive type

    - #### getCardinality

          public final int getCardinality()

      Determine the static cardinality
      Returns:
        code identifying the cardinality
      See Also:
        `Cardinality`

    - #### copyAsSubType

          public abstract AtomicValue copyAsSubType(AtomicType typeLabel)

      Create a copy of this atomic value, with a different type label
      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

    - #### isNaN

          public boolean isNaN()

      Test whether the value is the special value NaN
      Returns:
        true if the value is float NaN or double NaN or precisionDecimal NaN; otherwise false

    - #### getStringValue

          public final String getStringValue()

      Convert the value to a string, using the serialization rules. For atomic values this is the same as a cast; for sequence values it gives a space-separated list. This method is refined for AtomicValues so that it never throws an Exception.
      Specified by:
        `getStringValue` in interface `AtomicSequence`
      Specified by:
        `getStringValue` in interface `GroundedValue`
      Specified by:
        `getStringValue` in interface `Item`
      Returns:
        the string value of the sequence.
      See Also:
        `Item.getStringValueCS()`

    - #### getPrimitiveStringValue

          protected abstract CharSequence getPrimitiveStringValue()

      Convert the value to a string, using the serialization rules for the primitive type. This is the result of conversion to a string except that postprocessing defined by the saxon:preprocess facet is not (yet) applied.
      Returns:
        the value converted to a string according to the rules for the primitive type

    - #### effectiveBooleanValue

          public boolean effectiveBooleanValue()
                                        throws XPathException

      Get the effective boolean value of the value
      Specified by:
        `effectiveBooleanValue` in interface `GroundedValue`
      Returns:
        true, unless the value is boolean false, numeric zero, or zero-length string
      Throws:
        `XPathException` - if effective boolean value is not defined for this type (the default behaviour)

    - #### getComponent

          public AtomicValue getComponent(int component)
                                   throws XPathException

      Method to extract components of a value. Implemented by some subclasses, but defined at this level for convenience
      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
        `UnsupportedOperationException` - if applied to a value of a type that has no components

    - #### checkPermittedContents

          public void checkPermittedContents(SchemaType parentType,
                                    StaticContext env,
                                    boolean whole)
                                      throws XPathException

      Check statically that the results of the expression are capable of constructing the content of a given schema type.
      Parameters:
        `parentType` - The schema type
        `env` - the static context
        `whole` - true if this atomic value accounts for the entire content of the containing node
      Throws:
        `XPathException` - if the expression doesn't match the required content type

    - #### asAtomic

          public AtomicValue asAtomic()

      Calling this method on a ConversionResult returns the AtomicValue that results from the conversion if the conversion was successful, and throws a ValidationException explaining the conversion error otherwise.

      Use this method if you are calling a conversion method that returns a ConversionResult, and if you want to throw an exception if the conversion fails.

      Specified by:
        `asAtomic` in interface `ConversionResult`
      Returns:
        the atomic value that results from the conversion if the conversion was successful

    - #### toString

          public String toString()

      Get string value. In general toString() for an atomic value displays the value as it would be written in XPath: that is, as a literal if available, or as a call on a constructor function otherwise.
      Overrides:
        `toString` in class `Object`

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