Debian Codegraph @

net/sf/saxon/s9api/XdmSequenceIterator.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.s9api;
9
10 import net.sf.saxon.om.SequenceIterator;
11 import net.sf.saxon.s9api.streams.XdmStream;
12 import net.sf.saxon.trans.XPathException;
13 import net.sf.saxon.tree.iter.AxisIterator;
14 import net.sf.saxon.tree.iter.SingletonIterator;
15 import net.sf.saxon.tree.iter.UnfailingIterator;
16 import net.sf.saxon.value.AtomicValue;
17
18 import java.util.Iterator;
19 import java.util.Spliterator;
20 import java.util.Spliterators;
21 import java.util.stream.Stream;
22 import java.util.stream.StreamSupport;
23
24 /**
25 * An iterator over an XPath sequence.
26 * <p>This class implements the standard Java Iterator interface.</p>
27 * <p>Because the <code>Iterator</code> interface does not define any checked
28 * exceptions, the <code>hasNext()</code> method of this iterator throws an unchecked
29 * exception if a dynamic error occurs while evaluating the expression. Applications
30 * wishing to control error handling should take care to catch this exception.</p>
31 */
32 public class XdmSequenceIterator<T extends XdmItem> implements Iterator<T> {
33
34 /*@Nullable*/ private T next = null;
35 private int state = BEFORE_ITEM;
36 private SequenceIterator base;
37
38 private final static int BEFORE_ITEM = 0;
39 private final static int ON_ITEM = 1;
40 private final static int FINISHED = 2;
41
42 protected XdmSequenceIterator(SequenceIterator base) {
43 this.base = base;
44 this.state = BEFORE_ITEM;
45 }
46
47 public XdmSequenceIterator(UnfailingIterator base) {
48 this.base = base;
49 this.state = BEFORE_ITEM;
50 }
51
52 public static XdmSequenceIterator<XdmNode> ofNodes(AxisIterator base) {
53 return new XdmSequenceIterator<>(base);
54 }
55
56 public static XdmSequenceIterator<XdmAtomicValue> ofAtomicValues(UnfailingIterator<? extends AtomicValue> base) {
57 return new XdmSequenceIterator<>(base);
58 }
59
60 protected static XdmSequenceIterator<XdmNode> ofNode(XdmNode node) {
61 return new XdmSequenceIterator<>(SingletonIterator.makeIterator(node.getUnderlyingNode()));
62 }
63
64 /**
65 * Returns <tt>true</tt> if the iteration has more elements. (In other
66 * words, returns <tt>true</tt> if <tt>next</tt> would return an element
67 * rather than throwing an exception.)
68 *
69 * @return <tt>true</tt> if the iterator has more elements.
70 * @throws SaxonApiUncheckedException if a dynamic error occurs during XPath evaluation that
71 * is detected at this point.
72 */
73 public boolean hasNext() throws SaxonApiUncheckedException {
74 switch (state) {
75 case ON_ITEM:
76 return true;
77 case FINISHED:
78 return false;
79 case BEFORE_ITEM:
80 try {
81 next = (T)XdmItem.wrapItem(base.next());
82 if (next == null) {
83 state = FINISHED;
84 return false;
85 } else {
86 state = ON_ITEM;
87 return true;
88 }
89 } catch (XPathException err) {
90 throw new SaxonApiUncheckedException(err);
91 }
92 default:
93 throw new IllegalStateException();
94 }
95 }
96
97 /**
98 * Returns the next element in the iteration. Calling this method
99 * repeatedly until the {@link #hasNext()} method returns false will
100 * return each element in the underlying collection exactly once.
101 *
102 * @return the next element in the iteration.
103 * @throws java.util.NoSuchElementException
104 * iteration has no more elements.
105 */
106 public T next() {
107 switch (state) {
108 case ON_ITEM:
109 state = BEFORE_ITEM;
110 return next;
111 case FINISHED:
112 throw new java.util.NoSuchElementException();
113 case BEFORE_ITEM:
114 if (hasNext()) {
115 state = BEFORE_ITEM;
116 return next;
117 } else {
118 throw new java.util.NoSuchElementException();
119 }
120 default:
121 throw new IllegalStateException();
122 }
123
124 }
125
126 /**
127 * Not supported on this implementation.
128 *
129 * @throws UnsupportedOperationException always
130 */
131
132 public void remove() {
133 throw new UnsupportedOperationException();
134 }
135
136 /**
137 * The close() method should be called to release resources if the caller wants to stop reading
138 * data before reaching the end. This is particularly relevant if the query uses saxon:stream()
139 * to read its input, since there will then be another thread supplying data, which will be left
140 * in suspended animation if no-one is consuming the data.
141 * @since 9.5.1.5 (see bug 2016)
142 */
143
144 public void close() {
145 base.close();
146 state = FINISHED;
147 }
148
149
150 /**
151 * Convert this iterator to a Stream
152 *
153 * @return a Stream delivering the same items as this iterator
154 * @since 9.9
155 */
156 public XdmStream<T> stream() {
157 Stream<T> base = StreamSupport.stream(Spliterators.spliteratorUnknownSize(
158 this, Spliterator.ORDERED), false);
159 base = base.onClose(new Runnable() {
160 @Override
161 public void run() {
162 XdmSequenceIterator.this.close();
163 }
164 });
165 return new XdmStream<T>(base);
166 }
167
168 }
169