Debian Codegraph @

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