Debian Codegraph @

net/sf/saxon/expr/UnionEnumeration.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.expr;
9
10 import net.sf.saxon.expr.sort.ItemOrderComparer;
11 import net.sf.saxon.om.NodeInfo;
12 import net.sf.saxon.om.SequenceIterator;
13 import net.sf.saxon.trans.XPathException;
14 import net.sf.saxon.tree.iter.LookaheadIterator;
15
16 /**
17 * An enumeration representing a nodeset that is a union of two other NodeSets.
18 */
19
20 public class UnionEnumeration implements SequenceIterator<NodeInfo>, LookaheadIterator<NodeInfo> {
21
22 private SequenceIterator<? extends NodeInfo> e1;
23 private SequenceIterator<? extends NodeInfo> e2;
24 /*@Nullable*/ private NodeInfo nextNode1 = null;
25 private NodeInfo nextNode2 = null;
26 private ItemOrderComparer comparer;
27
28 /**
29 * Create the iterator. The two input iterators must return nodes in document
30 * order for this to work.
31 *
32 * @param p1 iterator over the first operand sequence (in document order)
33 * @param p2 iterator over the second operand sequence
34 * @param comparer used to test whether nodes are in document order. Different versions
35 * are used for intra-document and cross-document operations
36 * @throws XPathException if an error occurs reading the first item of either operand
37 */
38
39 public UnionEnumeration(SequenceIterator<? extends NodeInfo> p1, SequenceIterator<? extends NodeInfo> p2,
40 ItemOrderComparer comparer) throws XPathException {
41 this.e1 = p1;
42 this.e2 = p2;
43 this.comparer = comparer;
44
45 nextNode1 = next(e1);
46 nextNode2 = next(e2);
47 }
48
49 /**
50 * Get the next item from one of the input sequences,
51 * checking that it is a node.
52 *
53 * @param iter the sequence from which a node is to be read
54 * @return the node that was read
55 * @throws XPathException if reading from either of the input sequences fails
56 */
57
58 private NodeInfo next(SequenceIterator iter) throws XPathException {
59 return (NodeInfo) iter.next();
60 // we rely on the type-checking mechanism to prevent a ClassCastException here
61 }
62
63 public boolean hasNext() {
64 return nextNode1 != null || nextNode2 != null;
65 }
66
67 public NodeInfo next() throws XPathException {
68
69 // main merge loop: take a value from whichever set has the lower value
70
71 if (nextNode1 != null && nextNode2 != null) {
72 int c = comparer.compare(nextNode1, nextNode2);
73 if (c < 0) {
74 NodeInfo current = nextNode1;
75 nextNode1 = next(e1);
76 return current;
77
78 } else if (c > 0) {
79 NodeInfo current = nextNode2;
80 nextNode2 = next(e2);
81 return current;
82
83 } else {
84 NodeInfo current = nextNode2;
85 nextNode2 = next(e2);
86 nextNode1 = next(e1);
87 return current;
88 }
89 }
90
91 // collect the remaining nodes from whichever set has a residue
92
93 if (nextNode1 != null) {
94 NodeInfo current = nextNode1;
95 nextNode1 = next(e1);
96 return current;
97 }
98 if (nextNode2 != null) {
99 NodeInfo current = nextNode2;
100 nextNode2 = next(e2);
101 return current;
102 }
103 return null;
104 }
105
106 public void close() {
107 e1.close();
108 e2.close();
109 }
110
111 /**
112 * Get properties of this iterator, as a bit-significant integer.
113 *
114 * @return the properties of this iterator. This will be some combination of
115 * properties such as {@link #GROUNDED}, {@link #LAST_POSITION_FINDER},
116 * and {@link #LOOKAHEAD}. It is always
117 * acceptable to return the value zero, indicating that there are no known special properties.
118 * It is acceptable for the properties of the iterator to change depending on its state.
119 */
120
121 public int getProperties() {
122 return LOOKAHEAD;
123 }
124
125 }
126