Debian Codegraph @

net/sf/saxon/tree/iter/ManualIterator.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.tree.iter;
9
10 import net.sf.saxon.expr.LastPositionFinder;
11 import net.sf.saxon.om.*;
12 import net.sf.saxon.trans.XPathException;
13
14
15 /**
16 * ManualIterator: a pseudo-iterator used while streaming. It has a current node and a current position
17 * which are set manually. Calling last() is an error. Calling next() always returns null.
18 */
19
20 public class ManualIterator<T extends Item<?>> implements FocusIterator<T>, UnfailingIterator<T>,
21 ReversibleIterator<T>, LastPositionFinder, GroundedIterator<T>, LookaheadIterator<T> {
22
23 private T item;
24 private int position;
25 private LastPositionFinder lastPositionFinder;
26
27 /**
28 * Create an uninitialized ManualIterator: this is only usable after the context item, position, and size (if required)
29 * have been initialized using setter methods.
30 */
31
32 public ManualIterator() {
33 item = null;
34 position = 0;
35 }
36
37 /**
38 * Create a ManualIterator initializing the context item and position.
39 * The value of "last()" for such an iterator is unknown unless a LastPositionFinder is supplied.
40 * @param value the context item. May be null if the value is to be initialized later.
41 * @param position the context position
42 */
43
44 public ManualIterator(T value, int position) {
45 this.item = value;
46 this.position = position;
47 }
48
49 /**
50 * Create a ManualIterator supplying the context item, and setting the value of
51 * both "position()" and "last()" implicitly to 1.
52 * @param value the context item
53 */
54
55 public ManualIterator(T value) {
56 this.item = value;
57 this.position = 1;
58 this.lastPositionFinder = () -> 1;
59 }
60
61 public void setContextItem(T value) {
62 this.item = value;
63 }
64
65 public void setLastPositionFinder(LastPositionFinder finder) {
66 this.lastPositionFinder = finder;
67 }
68
69 public void incrementPosition() {
70 position++;
71 }
72
73 public void setPosition(int position) {
74 this.position = position;
75 }
76
77 /**
78 * Determine whether there are more items to come. Note that this operation
79 * is stateless and it is not necessary (or usual) to call it before calling
80 * next(). It is used only when there is an explicit need to tell if we
81 * are at the last element.
82 *
83 * @return true if there are more items
84 */
85
86 public boolean hasNext() {
87 try {
88 return position() != getLength();
89 } catch (XPathException e) {
90 return false; // should not happen
91 }
92 }
93
94 public T next() {
95 return null;
96 }
97
98 public T current() {
99 return item;
100 }
101
102 /**
103 * Return the current position in the sequence.
104 *
105 * @return 0 before the first call on next(); 1 before the second call on next(); -1 after the second
106 * call on next().
107 */
108 public int position() {
109 return position;
110 }
111
112 public int getLength() throws XPathException {
113 if (lastPositionFinder == null) {
114 throw new UnsupportedOperationException("last() cannot be used when streaming");
115 } else {
116 return lastPositionFinder.getLength();
117 }
118 }
119
120 public void close() {
121 }
122
123 public ManualIterator<T> getReverseIterator() {
124 return new ManualIterator<>(item);
125 }
126
127 /**
128 * Return a Value containing all the items in the sequence returned by this
129 * SequenceIterator
130 *
131 * @return the corresponding Value. If the value is a closure or a function call package, it will be
132 * evaluated and expanded.
133 */
134
135 /*@Nullable*/
136 public GroundedValue<T> materialize() {
137 return (GroundedValue<T>)item;
138 }
139
140 @Override
141 public GroundedValue<T> getResidue() {
142 return materialize();
143 }
144
145 /**
146 * Get properties of this iterator, as a bit-significant integer.
147 *
148 * @return the properties of this iterator. This will be some combination of
149 * properties such as {@link #GROUNDED}, {@link #LAST_POSITION_FINDER},
150 * and {@link #LOOKAHEAD}. It is always
151 * acceptable to return the value zero, indicating that there are no known special properties.
152 * It is acceptable for the properties of the iterator to change depending on its state.
153 */
154
155 public int getProperties() {
156 return GROUNDED | LAST_POSITION_FINDER | LOOKAHEAD;
157 }
158
159 }
160
161 // Copyright (c) 2009 - 2018 Saxonica Limited.