Debian Codegraph @

net/sf/saxon/tree/iter/SingleNodeIterator.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 import net.sf.saxon.value.EmptySequence;
14
15
16 /**
17 * SingleNodeIterator: an iterator over a sequence of zero or one nodes
18 */
19
20 public class SingleNodeIterator implements AxisIterator,
21 ReversibleIterator<NodeInfo>, LastPositionFinder, GroundedIterator<NodeInfo>, LookaheadIterator<NodeInfo> {
22
23 private NodeInfo item;
24 private int position = 0;
25
26 /**
27 * Private constructor: external classes should use the factory method
28 *
29 * @param value the item to iterate over
30 */
31
32 private SingleNodeIterator(NodeInfo value) {
33 this.item = value;
34 }
35
36 /**
37 * Factory method.
38 *
39 * @param item the item to iterate over
40 * @return a SingletonIterator over the supplied item, or an EmptyIterator
41 * if the supplied item is null.
42 */
43
44 /*@NotNull*/
45 public static AxisIterator makeIterator(/*@Nullable*/ NodeInfo item) {
46 if (item == null) {
47 return EmptyIterator.OfNodes.THE_INSTANCE;
48 } else {
49 return new SingleNodeIterator(item);
50 }
51 }
52
53 /**
54 * Determine whether there are more items to come. Note that this operation
55 * is stateless and it is not necessary (or usual) to call it before calling
56 * next(). It is used only when there is an explicit need to tell if we
57 * are at the last element.
58 *
59 * @return true if there are more items
60 */
61
62 public boolean hasNext() {
63 return position == 0;
64 }
65
66 /*@Nullable*/
67 public NodeInfo next() {
68 if (position == 0) {
69 position = 1;
70 return item;
71 } else if (position == 1) {
72 position = -1;
73 return null;
74 } else {
75 return null;
76 }
77 }
78
79 public int getLength() {
80 return 1;
81 }
82
83 public void close() {
84 }
85
86
87 /*@NotNull*/
88 public SequenceIterator<NodeInfo> getReverseIterator() {
89 return new SingleNodeIterator(item);
90 }
91
92 public NodeInfo getValue() {
93 return item;
94 }
95
96 /**
97 * Return a Value containing all the items in the sequence returned by this
98 * SequenceIterator
99 *
100 * @return the corresponding Value. If the value is a closure or a function call package, it will be
101 * evaluated and expanded.
102 */
103
104 /*@NotNull*/
105 public GroundedValue<NodeInfo> materialize() {
106 return new ZeroOrOne<>(item);
107 }
108
109 @Override
110 public GroundedValue<NodeInfo> getResidue() throws XPathException {
111 return item==null ? EmptySequence.getInstance() : new One<>(item);
112 }
113
114 /**
115 * Get properties of this iterator, as a bit-significant integer.
116 *
117 * @return the properties of this iterator. This will be some combination of
118 * properties such as {@link #GROUNDED}, {@link #LAST_POSITION_FINDER},
119 * and {@link #LOOKAHEAD}. It is always
120 * acceptable to return the value zero, indicating that there are no known special properties.
121 * It is acceptable for the properties of the iterator to change depending on its state.
122 */
123
124 public int getProperties() {
125 return GROUNDED | LAST_POSITION_FINDER | LOOKAHEAD;
126 }
127
128 }
129