Class Chain

  • All Implemented Interfaces:
    GroundedValue, Sequence

    public class Chain
    extends java.lang.Object
    implements GroundedValue
    A chain is an implementation of Sequence that represents the concatenation of a number of subsequences.

    The most common use case is a recursive function that appends one item to a sequence each time it is called. Each call of this function will create a Chain with two subsequences, the first being a Chain and the second an individual item. The design of the class is constrained by the need to handle this extreme case.

    Firstly, the iterator for the class cannot use simple recursion to navigate the tree because it will often be too deep, causing StackOverflow. So it maintains its own Stack (on the Java heap).

    Secondly, even using the heap will run out of space at about a million entries. To prevent this, any Chains of size less than thirty items are amalgamated into chunks of 30. Building larger chunks than this would cause insertion operations to have linear performance (and thus the total cost of sequence construction would be quadratic). The figure of 30 was chosen because elapsed time is almost as good as with smaller chunks, and memory use during navigation is substantially reduced.

    A Chain has two phases in its life-cycle. In the first phase, the Chain is mutable; it can be extended using append() calls. In the second phase, the Chain is immutable; further append() operations are not allowed. The transition from the first to the second phase occurs when any of the methods itemAt(), reduce(), or subsequence() is called.

    • Constructor Detail

      • Chain

        public Chain​(java.util.List<GroundedValue> children)
        Create a chain from a list of grounded values
        Parameters:
        children - the list of grounded values. The implementation may or may not copy this list, and it may or may not modify the list during execution of the append(Item) method. The caller must not attempt to modify the list after return from this constructor.
    • Method Detail

      • head

        public Item head()
        Description copied from interface: GroundedValue
        Get the first item of the sequence. This differs from the parent interface in not allowing an exception
        Specified by:
        head in interface GroundedValue
        Specified by:
        head in interface Sequence
        Returns:
        the first item of the sequence, or null if the sequence is empty
      • iterate

        public UnfailingIterator iterate()
        Description copied from interface: GroundedValue
        Get an iterator over all the items in the sequence. This differs from the superclass method in not allowing an exception, either during this method call, or in the subsequent processing of the returned iterator.
        Specified by:
        iterate in interface GroundedValue
        Specified by:
        iterate in interface Sequence
        Returns:
        an iterator (meaning a Saxon SequenceIterator rather than a Java Iterator) over all the items in this Sequence.
      • append

        public void append​(Item item)
        Add a single item to the end of this sequence. This method must only be called while the value is being constructed, since the sequence thereafter is immutable.
        Parameters:
        item - the item to be added
      • itemAt

        public Item itemAt​(int n)
        Get the n'th item in the value, counting from 0
        Specified by:
        itemAt in interface GroundedValue
        Parameters:
        n - the index of the required item, with 0 representing the first item in the sequence
        Returns:
        the n'th item if it exists, or null otherwise
      • subsequence

        public GroundedValue subsequence​(int start,
                                         int length)
        Get a subsequence of the value
        Specified by:
        subsequence in interface GroundedValue
        Parameters:
        start - the index of the first item to be included in the result, counting from zero. A negative value is taken as zero. If the value is beyond the end of the sequence, an empty sequence is returned
        length - the number of items to be included in the result. Specify Integer.MAX_VALUE to get the subsequence up to the end of the base sequence. If the value is negative, an empty sequence is returned. If the value goes off the end of the sequence, the result returns items up to the end of the sequence
        Returns:
        the required subsequence.
      • getLength

        public int getLength()
        Get the size of the value (the number of items)
        Specified by:
        getLength in interface GroundedValue
        Returns:
        the number of items in the sequence
      • effectiveBooleanValue

        public boolean effectiveBooleanValue()
                                      throws XPathException
        Get the effective boolean value of this sequence
        Specified by:
        effectiveBooleanValue in interface GroundedValue
        Returns:
        the effective boolean value
        Throws:
        XPathException - if the sequence has no effective boolean value (for example a sequence of two integers)
      • getStringValue

        public java.lang.String getStringValue()
                                        throws XPathException
        Get the string value of this sequence. The string value of an item is the result of applying the string() function. The string value of a sequence is the space-separated result of applying the string-join() function using a single space as the separator
        Specified by:
        getStringValue in interface GroundedValue
        Returns:
        the string value of the sequence.
        Throws:
        XPathException - if the sequence contains items that have no string value (for example, function items)
      • getStringValueCS

        public java.lang.CharSequence getStringValueCS()
                                                throws XPathException
        Get the string value of this sequence. The string value of an item is the result of applying the string() function. The string value of a sequence is the space-separated result of applying the string-join() function using a single space as the separator
        Specified by:
        getStringValueCS in interface GroundedValue
        Returns:
        the string value of the sequence.
        Throws:
        XPathException - if the sequence contains items that have no string value (for example, function items)
      • reduce

        public GroundedValue reduce()
        Reduce the sequence to its simplest form. If the value is an empty sequence, the result will be EmptySequence.getInstance(). If the value is a single atomic value, the result will be an instance of AtomicValue. If the value is a single item of any other kind, the result will be an instance of SingletonItem. Otherwise, the result will typically be unchanged.
        Specified by:
        reduce in interface GroundedValue
        Returns:
        the simplified sequence