KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > expr > UnionEnumeration


1 package net.sf.saxon.expr;
2 import net.sf.saxon.om.Item;
3 import net.sf.saxon.om.LookaheadIterator;
4 import net.sf.saxon.om.NodeInfo;
5 import net.sf.saxon.om.SequenceIterator;
6 import net.sf.saxon.sort.NodeOrderComparer;
7 import net.sf.saxon.trans.XPathException;
8
9 /**
10 * An enumeration representing a nodeset that is a union of two other NodeSets.
11 */

12
13 public class UnionEnumeration implements SequenceIterator, LookaheadIterator {
14
15     private SequenceIterator e1;
16     private SequenceIterator e2;
17     private NodeInfo nextNode1 = null;
18     private NodeInfo nextNode2 = null;
19     private NodeOrderComparer comparer;
20     private NodeInfo current = null;
21     private int position = 0;
22
23     /**
24     * Create the iterator. The two input iterators must return nodes in document
25     * order for this to work.
26     */

27
28     public UnionEnumeration(SequenceIterator p1, SequenceIterator p2,
29                             NodeOrderComparer comparer) throws XPathException {
30         this.e1 = p1;
31         this.e2 = p2;
32         this.comparer = comparer;
33
34         nextNode1 = next(e1);
35         nextNode2 = next(e2);
36     }
37
38     /**
39     * Get the next item from one of the input sequences,
40     * checking that it is a node.
41     */

42
43     private NodeInfo next(SequenceIterator iter) throws XPathException {
44         return (NodeInfo)iter.next();
45         // we rely on the type-checking mechanism to prevent a ClassCastException here
46
}
47
48     public boolean hasNext() {
49         return nextNode1!=null || nextNode2!=null;
50     }
51
52     public Item next() throws XPathException {
53
54         // main merge loop: take a value from whichever set has the lower value
55

56         position++;
57         if (nextNode1 != null && nextNode2 != null) {
58             int c = comparer.compare(nextNode1, nextNode2);
59             if (c<0) {
60                 current = nextNode1;
61                 nextNode1 = next(e1);
62                 return current;
63
64             } else if (c>0) {
65                 current = nextNode2;
66                 nextNode2 = next(e2);
67                 return current;
68
69             } else {
70                 current = nextNode2;
71                 nextNode2 = next(e2);
72                 nextNode1 = next(e1);
73                 return current;
74             }
75         }
76
77         // collect the remaining nodes from whichever set has a residue
78

79         if (nextNode1!=null) {
80             current = nextNode1;
81             nextNode1 = next(e1);
82             return current;
83         }
84         if (nextNode2!=null) {
85             current = nextNode2;
86             nextNode2 = next(e2);
87             return current;
88         }
89         current = null;
90         position = -1;
91         return null;
92     }
93
94     public Item current() {
95         return current;
96     }
97
98     public int position() {
99         return position;
100     }
101
102     public SequenceIterator getAnother() throws XPathException {
103         return new UnionEnumeration(e1.getAnother(), e2.getAnother(), comparer);
104     }
105
106     /**
107      * Get properties of this iterator, as a bit-significant integer.
108      *
109      * @return the properties of this iterator. This will be some combination of
110      * properties such as {@link GROUNDED}, {@link LAST_POSITION_FINDER},
111      * and {@link LOOKAHEAD}. It is always
112      * acceptable to return the value zero, indicating that there are no known special properties.
113      * It is acceptable for the properties of the iterator to change depending on its state.
114      */

115
116     public int getProperties() {
117         return LOOKAHEAD;
118     }
119
120 }
121
122
123
124 //
125
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
126
// you may not use this file except in compliance with the License. You may obtain a copy of the
127
// License at http://www.mozilla.org/MPL/
128
//
129
// Software distributed under the License is distributed on an "AS IS" basis,
130
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
131
// See the License for the specific language governing rights and limitations under the License.
132
//
133
// The Original Code is: all this file.
134
//
135
// The Initial Developer of the Original Code is Michael H. Kay.
136
//
137
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
138
//
139
// Contributor(s): none.
140
//
141
Popular Tags