KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > sort > DocumentOrderIterator


1 package net.sf.saxon.sort;
2 import net.sf.saxon.om.Item;
3 import net.sf.saxon.om.NodeInfo;
4 import net.sf.saxon.om.SequenceIterator;
5 import net.sf.saxon.trans.XPathException;
6 import net.sf.saxon.value.SequenceExtent;
7
8 /**
9 * DocumentOrderIterator takes as input an iteration of nodes in any order, and
10 * returns as output an iteration of the same nodes in document order, eliminating
11 * any duplicates.
12 */

13
14 public final class DocumentOrderIterator implements SequenceIterator, Sortable {
15
16     private SequenceIterator iterator;
17     private SequenceExtent sequence;
18     private NodeOrderComparer comparer;
19     private NodeInfo current = null;
20     private int position = 0;
21
22     /**
23     * Iterate over a sequence in document order.
24     */

25
26     public DocumentOrderIterator(SequenceIterator base, NodeOrderComparer comparer) throws XPathException {
27
28         this.comparer = comparer;
29
30         sequence = new SequenceExtent(base);
31         //System.err.println("sort into document order: sequence length = " + sequence.getLength());
32
if (sequence.getLength()>1) {
33             //QuickSort.sort(this, 0, sequence.getLength()-1);
34
GenericSorter.quickSort(0, sequence.getLength(), this);
35             //GenericSorter.mergeSort(0, sequence.getLength(), this);
36
}
37         iterator = sequence.iterate(null);
38     }
39
40     /**
41     * Private constructor used only by getAnother()
42     */

43
44     private DocumentOrderIterator() {}
45
46     /**
47     * Compare two nodes in document sequence
48     * (needed to implement the Sortable interface)
49     */

50
51     public int compare(int a, int b) {
52         //System.err.println("compare " + a + " with " + b);
53
return comparer.compare((NodeInfo)sequence.itemAt(a),
54                                 (NodeInfo)sequence.itemAt(b));
55     }
56
57     /**
58     * Swap two nodes (needed to implement the Sortable interface)
59     */

60
61     public void swap(int a, int b) {
62         sequence.swap(a, b);
63     }
64
65     // Implement the SequenceIterator as a wrapper around the underlying iterator
66
// over the sequenceExtent, but looking ahead to remove duplicates.
67

68     public Item next() throws XPathException {
69         while (true) {
70             NodeInfo next = (NodeInfo)iterator.next();
71             if (next == null) {
72                 current = null;
73                 position = -1;
74                 return null;
75             }
76             if (current != null && next.isSameNodeInfo(current)) {
77                 continue;
78             } else {
79                 position++;
80                 current = next;
81                 return current;
82             }
83         }
84     }
85
86     /**
87      * Get properties of this iterator, as a bit-significant integer.
88      *
89      * @return the properties of this iterator. This will be some combination of
90      * properties such as {@link GROUNDED}, {@link LAST_POSITION_FINDER},
91      * and {@link LOOKAHEAD}. It is always
92      * acceptable to return the value zero, indicating that there are no known special properties.
93      * It is acceptable for the properties of the iterator to change depending on its state.
94      */

95
96     public int getProperties() {
97         return 0;
98     }
99
100     public Item current() {
101         return current;
102     }
103
104     public int position() {
105         return position;
106     }
107
108     public SequenceIterator getAnother() throws XPathException {
109         DocumentOrderIterator another = new DocumentOrderIterator();
110         another.iterator = iterator.getAnother(); // don't need to sort it again
111
return another;
112     }
113
114 }
115
116 //
117
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
118
// you may not use this file except in compliance with the License. You may obtain a copy of the
119
// License at http://www.mozilla.org/MPL/
120
//
121
// Software distributed under the License is distributed on an "AS IS" basis,
122
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
123
// See the License for the specific language governing rights and limitations under the License.
124
//
125
// The Original Code is: all this file.
126
//
127
// The Initial Developer of the Original Code is Michael H. Kay.
128
//
129
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
130
//
131
// Contributor(s): none.
132
//
133
Popular Tags