Debian Codegraph @

net/sf/saxon/om/SequenceIterator.java

raw symbols SCIP
1 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2018 Saxonica Limited.
3 // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
4 // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0.
6 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
7
8 package net.sf.saxon.om;
9
10 import net.sf.saxon.trans.XPathException;
11 import net.sf.saxon.value.SequenceExtent;
12
13 import java.io.Closeable;
14
15 /**
16 * A SequenceIterator is used to iterate over any XPath 2 sequence (of values or nodes).
17 * To get the next item in a sequence, call next(); if this returns null, you've
18 * reached the end of the sequence.
19 * <p>The objects returned by the SequenceIterator will generally be either nodes
20 * (class NodeInfo), singleton values (class AtomicValue), or function items: these are represented
21 * collectively by the interface {@link Item}.</p>
22 * <p>The interface to SequenceIterator is changed in Saxon 9.6 to drop support for the
23 * current() and position() methods. Internal iterators no longer need to maintain the values
24 * of the current item or the current position. This information is needed (in general) only
25 * for an iterator that acts as the current focus; that is, an iterator stored as the current
26 * iterator in an XPathContext. SequenceIterators than maintain the value of position()
27 * and last() are represented by the interface {@link FocusIterator}.</p>
28 *
29 * @author Michael H. Kay
30 * @since 8.4; significant changes in 9.6.
31 */
32
33 public interface SequenceIterator<T extends Item<?>> extends Closeable {
34
35 /**
36 * Get the next item in the sequence. This method changes the state of the
37 * iterator.
38 *
39 * @return the next item, or null if there are no more items. Once a call
40 * on next() has returned null, no further calls should be made. The preferred
41 * action for an iterator if subsequent calls on next() are made is to return
42 * null again, and all implementations within Saxon follow this rule.
43 * @throws XPathException if an error occurs retrieving the next item
44 * @since 8.4
45 */
46
47 /*@Nullable*/
48 T next() throws XPathException;
49
50 /**
51 * Close the iterator. This indicates to the supplier of the data that the client
52 * does not require any more items to be delivered by the iterator. This may enable the
53 * supplier to release resources. After calling close(), no further calls on the
54 * iterator should be made; if further calls are made, the effect of such calls is undefined.
55 * <p>For example, the iterator returned by the unparsed-text-lines() function has a close() method
56 * that causes the underlying input stream to be closed, whether or not the file has been read
57 * to completion.</p>
58 * <p>Closing an iterator is important when the data is being "pushed" in
59 * another thread. Closing the iterator terminates that thread and means that it needs to do
60 * no additional work. Indeed, failing to close the iterator may cause the push thread to hang
61 * waiting for the buffer to be emptied.</p>
62 *
63 * @since 9.1. Default implementation added in 9.9.
64 */
65
66 default void close() {}
67
68 /**
69 * Get properties of this iterator, as a bit-significant integer.
70 *
71 * @return the properties of this iterator. This will be some combination of
72 * properties such as {@link #GROUNDED}, {@link #LAST_POSITION_FINDER},
73 * and {@link #LOOKAHEAD}. It is always
74 * acceptable to return the value zero, indicating that there are no known special properties.
75 * It is acceptable for the properties of the iterator to change depending on its state.
76 * @since 8.6. Default implementation added in 9.9.
77 */
78
79 default int getProperties() {
80 return 0;
81 }
82
83 /**
84 * Property value: the iterator is "grounded". This means that (a) the
85 * iterator must be an instance of {@link net.sf.saxon.tree.iter.GroundedIterator}, and (b) the
86 * implementation of the materialize() method must be efficient (in particular,
87 * it should not involve the creation of new objects)
88 */
89
90 int GROUNDED = 1;
91
92 /**
93 * Property value: the iterator knows the number of items that it will deliver.
94 * This means that (a) the iterator must be an instance of {@link net.sf.saxon.expr.LastPositionFinder},
95 * and (b) the implementation of the getLastPosition() method must be efficient (in particular,
96 * it should take constant time, rather than time proportional to the length of the sequence)
97 */
98
99 int LAST_POSITION_FINDER = 1 << 1;
100
101 /**
102 * Property value: the iterator knows whether there are more items still to come. This means
103 * that (a) the iterator must be an instance of {@link net.sf.saxon.tree.iter.LookaheadIterator}, and (b) the
104 * implementation of the hasNext() method must be efficient (more efficient than the client doing
105 * it)
106 */
107
108 int LOOKAHEAD = 1 << 2;
109
110 /**
111 * Property value: the iterator can deliver an atomized result. This means that the iterator
112 * must be an instance of AtomizingIterator.
113 */
114
115 int ATOMIZING = 1 << 3;
116
117 /**
118 * Process all the remaining items delivered by the SequenceIterator using a supplied consumer function.
119 *
120 * @param consumer the supplied consumer function
121 * @throws XPathException if either (a) an error occurs obtaining an item from the input sequence,
122 * or (b) the consumer throws an exception.
123 */
124
125 default void forEachOrFail(ItemConsumer<T> consumer) throws XPathException {
126 T item;
127 while ((item = next()) != null) {
128 consumer.accept(item);
129 }
130 }
131
132 /**
133 * Create a GroundedValue (a sequence materialized in memory) containing all the values delivered
134 * by this SequenceIterator. The method must only be used when the SequenceIterator is positioned
135 * at the start. If it is not positioned at the start, then it is implementation-dependant whether
136 * the returned sequence contains all the nodes delivered by the SequenceIterator from the beginning,
137 * or only those delivered starting at the current position.
138 * <p>It is implementation-dependant whether this method consumes the SequenceIterator. (More specifically,
139 * in the current implementation: if the iterator is backed by a {@link GroundedValue}, then that
140 * value is returned and the iterator is not consumed; otherwise, the iterator is consumed and the
141 * method returns the remaining items after the current position only).</p>
142 *
143 * @return a sequence containing all the items delivered by this SequenceIterator.
144 * @throws XPathException if reading the SequenceIterator throws an error
145 */
146
147 default GroundedValue<T> materialize() throws XPathException {
148 //noinspection deprecation
149 return SequenceExtent.fromIterator(this);
150 }
151
152
153
154 }
155