KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icl > saxon > expr > UnionEnumeration


1 package com.icl.saxon.expr;
2 import com.icl.saxon.Controller;
3 import com.icl.saxon.om.NodeInfo;
4 import com.icl.saxon.om.NodeEnumeration;
5 import com.icl.saxon.sort.Comparer;
6
7 /**
8 * An enumeration representing a nodeset that is a union of two other NodeSets.
9 */

10
11 public class UnionEnumeration implements NodeEnumeration {
12
13     private NodeEnumeration p1;
14     private NodeEnumeration p2;
15     private NodeEnumeration e1;
16     private NodeEnumeration e2;
17     private NodeInfo nextNode1 = null;
18     private NodeInfo nextNode2 = null;
19     private Controller controller;
20
21     public UnionEnumeration(NodeEnumeration p1, NodeEnumeration p2,
22                             Controller controller) throws XPathException {
23         this.p1 = p1;
24         this.p2 = p2;
25         this.controller = controller;
26         e1 = p1;
27         e2 = p2;
28
29         if (!e1.isSorted()) {
30             e1 = (new NodeSetExtent(e1, controller)).sort().enumerate();
31         }
32         if (!e2.isSorted()) {
33             e2 = (new NodeSetExtent(e2, controller)).sort().enumerate();
34         }
35         
36         if (e1.hasMoreElements()) {
37             nextNode1 = e1.nextElement();
38         }
39         if (e2.hasMoreElements()) {
40             nextNode2 = e2.nextElement();
41         }
42     }
43
44     public boolean hasMoreElements() {
45         return nextNode1!=null || nextNode2!=null;
46     }
47
48     public NodeInfo nextElement() throws XPathException {
49
50         // main merge loop: take a value from whichever set has the lower value
51

52         if (nextNode1 != null && nextNode2 != null) {
53             int c = controller.compare(nextNode1, nextNode2);
54             if (c<0) {
55                 NodeInfo next = nextNode1;
56                 if (e1.hasMoreElements()) {
57                     nextNode1 = e1.nextElement();
58                 } else {
59                     nextNode1 = null;
60                 }
61                 return next;
62             
63             } else if (c>0) {
64                 NodeInfo next = nextNode2;
65                 if (e2.hasMoreElements()) {
66                     nextNode2 = e2.nextElement();
67                 } else {
68                     nextNode2 = null;
69                 }
70                 return next;
71             
72             } else {
73                 NodeInfo next = nextNode2;
74                 if (e2.hasMoreElements()) {
75                     nextNode2 = e2.nextElement();
76                 } else {
77                     nextNode2 = null;
78                 }
79                 if (e1.hasMoreElements()) {
80                     nextNode1 = e1.nextElement();
81                 } else {
82                     nextNode1 = null;
83                 }
84                 return next;
85             }
86         }
87
88         // collect the remaining nodes from whichever set has a residue
89

90         if (nextNode1!=null) {
91             NodeInfo next = nextNode1;
92             if (e1.hasMoreElements()) {
93                 nextNode1 = e1.nextElement();
94             } else {
95                 nextNode1 = null;
96             }
97             return next;
98         }
99         if (nextNode2!=null) {
100             NodeInfo next = nextNode2;
101             if (e2.hasMoreElements()) {
102                 nextNode2 = e2.nextElement();
103             } else {
104                 nextNode2 = null;
105             }
106             return next;
107         }
108         return null;
109     }
110
111     public boolean isSorted() {
112         return true;
113     }
114
115     public boolean isReverseSorted() {
116         return false;
117     }
118
119     public boolean isPeer() {
120         return false;
121     }
122
123 }
124
125
126
127 //
128
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
129
// you may not use this file except in compliance with the License. You may obtain a copy of the
130
// License at http://www.mozilla.org/MPL/
131
//
132
// Software distributed under the License is distributed on an "AS IS" basis,
133
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
134
// See the License for the specific language governing rights and limitations under the License.
135
//
136
// The Original Code is: all this file.
137
//
138
// The Initial Developer of the Original Code is
139
// Michael Kay of International Computers Limited (mhkay@iclway.co.uk).
140
//
141
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
142
//
143
// Contributor(s): none.
144
//
145
Popular Tags