Debian Codegraph @

net/sf/saxon/expr/sort/DocumentOrderIterator.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.sort;
9
10 import net.sf.saxon.expr.LastPositionFinder;
11 import net.sf.saxon.om.Item;
12 import net.sf.saxon.om.NodeInfo;
13 import net.sf.saxon.om.SequenceIterator;
14 import net.sf.saxon.trans.Err;
15 import net.sf.saxon.trans.XPathException;
16 import net.sf.saxon.tree.iter.ListIterator;
17
18 import java.util.ArrayList;
19 import java.util.List;
20
21 /**
22 * DocumentOrderIterator takes as input an iteration of nodes in any order, and
23 * returns as output an iteration of the same nodes in document order, eliminating
24 * any duplicates. An error occurs if the input sequence contains items that are
25 * not nodes.
26 */
27
28 public final class DocumentOrderIterator implements SequenceIterator<NodeInfo>, Sortable {
29
30 private SequenceIterator<NodeInfo> iterator;
31 private List<NodeInfo> sequence;
32 private ItemOrderComparer comparer;
33 private NodeInfo current = null;
34
35 /**
36 * Iterate over a sequence in document order.
37 * @param base the input sequence to be sorted
38 * @param comparer the comparer used for comparing node positions
39 * @throws XPathException if the input sequence cannot be evaluated, or if it contains an item
40 * that is not a node
41 */
42
43 public DocumentOrderIterator(SequenceIterator<?> base, ItemOrderComparer comparer) throws XPathException {
44
45 this.comparer = comparer;
46
47 int len = (base.getProperties() & SequenceIterator.LAST_POSITION_FINDER) == 0
48 ? 50
49 : ((LastPositionFinder) base).getLength();
50 sequence = new ArrayList<>(len);
51 base.forEachOrFail(item -> {
52 if (item instanceof NodeInfo) {
53 sequence.add((NodeInfo) item);
54 } else {
55 throw new XPathException("Item in input for sorting is not a node: " + Err.depict(item), "XPTY0004");
56 }
57 });
58
59 //System.err.println("SORT into document order: sequence length = " + sequence.size());
60 if (sequence.size() > 1) {
61 GenericSorter.quickSort(0, sequence.size(), this);
62 }
63 iterator = new ListIterator<>(sequence);
64 }
65
66 /**
67 * Private constructor used only by getAnother()
68 */
69
70 private DocumentOrderIterator() {
71 }
72
73 /**
74 * Compare two nodes in document sequence
75 * (needed to implement the Sortable interface)
76 */
77
78 public int compare(int a, int b) {
79 //System.err.println("compare " + a + " with " + b + " = " + comparer.compare(sequence.get(a), sequence.get(b)));
80 return comparer.compare(sequence.get(a), sequence.get(b));
81 }
82
83 /**
84 * Swap two nodes (needed to implement the Sortable interface)
85 */
86
87 public void swap(int a, int b) {
88 NodeInfo temp = sequence.get(a);
89 sequence.set(a, sequence.get(b));
90 sequence.set(b, temp);
91 }
92
93 // Implement the SequenceIterator as a wrapper around the underlying iterator
94 // over the sequenceExtent, but looking ahead to remove duplicates.
95
96 public NodeInfo next() throws XPathException {
97 while (true) {
98 NodeInfo next = iterator.next();
99 if (next == null) {
100 current = null;
101 return null;
102 }
103 if (!next.equals(current)) {
104 current = next;
105 return current;
106 }
107 }
108 }
109
110 /**
111 * Get properties of this iterator, as a bit-significant integer.
112 *
113 * @return the properties of this iterator. This will be some combination of
114 * properties such as {@link #GROUNDED}, {@link #LAST_POSITION_FINDER},
115 * and {@link #LOOKAHEAD}. It is always
116 * acceptable to return the value zero, indicating that there are no known special properties.
117 * It is acceptable for the properties of the iterator to change depending on its state.
118 */
119
120 public int getProperties() {
121 return 0;
122 }
123
124 public void close() {
125 }
126
127 }
128