Class LazySequence

  • All Implemented Interfaces:
    Sequence

    public class LazySequence
    extends java.lang.Object
    implements Sequence
    A sequence that wraps an iterator, without being materialized. It can only be used once.
    • Constructor Summary

      Constructors 
      Constructor Description
      LazySequence​(SequenceIterator iterator)
      Create a virtual sequence backed by an iterator
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      Item head()
      Get the first item in the sequence.
      SequenceIterator iterate()
      Iterate over all the items in the sequence.
      Sequence makeRepeatable()
      Ensure that the sequence is in a form where it can be evaluated more than once.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • LazySequence

        public LazySequence​(SequenceIterator iterator)
        Create a virtual sequence backed by an iterator
        Parameters:
        iterator - the iterator that delivers the items in the sequence
    • Method Detail

      • head

        public Item head()
                  throws XPathException
        Get the first item in the sequence. This calls iterate() internally, which means it can only be called once; the method should only be used if the client requires only the first item in the sequence (or knows that the sequence has length one).
        Specified by:
        head in interface Sequence
        Returns:
        the first item, or null if the sequence is empty
        Throws:
        XPathException - if an error occurs evaluating the underlying expression
      • iterate

        public SequenceIterator iterate()
                                 throws XPathException
        Iterate over all the items in the sequence. The first time this is called it returns the original iterator backing the sequence. On subsequent calls it clones the original iterator.
        Specified by:
        iterate in interface Sequence
        Returns:
        an iterator over the items in the sequence
        Throws:
        XPathException - if evaluation of the iterator fails.
        java.lang.IllegalStateException - if iterate() has already been called
      • makeRepeatable

        public Sequence makeRepeatable()
                                throws XPathException
        Description copied from interface: Sequence
        Ensure that the sequence is in a form where it can be evaluated more than once. Some sequences (for example LazySequence and Closure can only be evaluated once, and this operation causes these to be grounded. However, making it repeatable is not the same as making it grounded; it does not flush out all errors. Indeed, lazy evaluation relies on this property, because an expression that has been lifted out of a loop must not be evaluated unless the loop is executed at least once, to prevent spurious errors.
        Specified by:
        makeRepeatable in interface Sequence
        Returns:
        An equivalent sequence that can be repeatedly evaluated
        Throws:
        XPathException - if evaluation fails