KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jofti > btree > ResultNode


1 /*
2  * Created on 07-Oct-2004
3  *
4  */

5 package com.jofti.btree;
6
7 import java.util.ArrayList JavaDoc;
8 import java.util.Collection JavaDoc;
9 import java.util.List JavaDoc;
10
11 import com.jofti.exception.JoftiException;
12 import com.jofti.oswego.concurrent.ReadWriteLock;
13
14
15
16 /**
17  * The implementation class for the IResultNode.
18  *
19  * @author Steve Woodcock
20  * @version 1.0<br>
21  */

22 public class ResultNode implements INode,IResultNode{
23
24     private Object JavaDoc[] entries =null;
25     private Comparable JavaDoc rightValue;
26     private int entryNumber;
27     boolean isDeleted;
28     NodeLink link;
29     private INode node;
30     
31     ResultNode (INode node){
32         entries = node.getEntries();
33         if (entries == null){
34             throw new RuntimeException JavaDoc("entries are null for "+node.getRightValue());
35     }
36         
37         entryNumber = node.getEntryNumber();
38         rightValue = node.getRightValue();
39         isDeleted = node.isDeleted();
40         link = node.getLinkNode();
41         this.node =node;
42     }
43     /* (non-Javadoc)
44      * @see com.jofti.btree.Node#getRightValue()
45      */

46     public Comparable JavaDoc getRightValue()
47     {
48         return rightValue;
49     }
50
51     /* (non-Javadoc)
52      * @see com.jofti.btree.Node#setRightValue(java.lang.Comparable)
53      */

54     public void setRightValue(Comparable JavaDoc value)
55     {
56         throw new UnsupportedOperationException JavaDoc("Modification of node not supported");
57         
58     }
59
60     /* (non-Javadoc)
61      * @see com.jofti.btree.Node#contains(java.lang.Comparable)
62      */

63     public boolean contains(Comparable JavaDoc value)
64     {
65         if (value == null)
66         {
67             return false;
68         }
69         
70         return rightValue.compareTo(value) >=0;
71     }
72
73     /* (non-Javadoc)
74      * @see com.jofti.btree.Node#insertEntry(com.jofti.btree.NodeEntry)
75      */

76     public Object JavaDoc[] insertEntry(NodeEntry entry) throws JoftiException
77     {
78         throw new UnsupportedOperationException JavaDoc("Modification of node not supported");
79
80     }
81
82     /* (non-Javadoc)
83      * @see com.jofti.btree.Node#deleteEntry(com.jofti.btree.NodeEntry)
84      */

85     public boolean deleteEntry(NodeEntry entry)
86     {
87         throw new UnsupportedOperationException JavaDoc("Modification of node not supported");
88
89     }
90
91     /* (non-Javadoc)
92      * @see com.jofti.btree.Node#split()
93      */

94     public List JavaDoc split()
95     {
96         throw new UnsupportedOperationException JavaDoc("Modification of node not supported");
97
98     }
99
100     /* (non-Javadoc)
101      * @see com.jofti.btree.Node#splitNode()
102      */

103     public Node splitNode(Object JavaDoc[] temp)
104     {
105         
106         throw new UnsupportedOperationException JavaDoc("Modification of node not supported");
107
108     }
109
110     /* (non-Javadoc)
111      * @see com.jofti.btree.Node#getEntryNumber()
112      */

113     public int getEntryNumber()
114     {
115         
116         return entryNumber;
117     }
118
119     /* (non-Javadoc)
120      * @see com.jofti.btree.Node#isUnderFull()
121      */

122     public boolean isUnderFull()
123     {
124         
125         throw new UnsupportedOperationException JavaDoc("Modification of node not supported");
126
127     }
128
129     /* (non-Javadoc)
130      * @see com.jofti.btree.Node#isEmpty()
131      */

132     public boolean isEmpty()
133     {
134         
135         return entryNumber == 0;
136     }
137
138     /* (non-Javadoc)
139      * @see com.jofti.btree.Node#isDeleted()
140      */

141     public boolean isDeleted()
142     {
143         
144         return isDeleted;
145     }
146
147     /* (non-Javadoc)
148      * @see com.jofti.btree.Node#setDeleted(boolean)
149      */

150     public void setDeleted(boolean deleted)
151     {
152         throw new UnsupportedOperationException JavaDoc("Modification of node not supported");
153
154         
155     }
156
157     /* (non-Javadoc)
158      * @see com.jofti.btree.Node#getEntries()
159      */

160     public Object JavaDoc[] getEntries()
161     {
162         
163         return entries;
164
165     }
166
167     /* (non-Javadoc)
168      * @see com.jofti.btree.Node#setEntries(java.util.List)
169      */

170     public void setEntries(List JavaDoc entries)
171     {
172         throw new UnsupportedOperationException JavaDoc("Modification of node not supported");
173
174         
175     }
176
177     /* (non-Javadoc)
178      * @see com.jofti.btree.Node#getLinkNode()
179      */

180     public NodeLink getLinkNode()
181     {
182         
183         return new ResultNodeLink(link);
184     }
185
186     public IResultNode getNextNode(){
187         return new ResultNode(link.getNode());
188     }
189     /* (non-Javadoc)
190      * @see com.jofti.btree.Node#setLinkNode(com.jofti.btree.NodeLink)
191      */

192     public void setLinkNode(NodeLink node)
193     {
194         throw new UnsupportedOperationException JavaDoc("Modification of node not supported");
195
196         
197     }
198     /* (non-Javadoc)
199      * @see com.jofti.btree.INode#getNodeLock()
200      */

201     public ReadWriteLock getNodeLock()
202     {
203         
204         throw new UnsupportedOperationException JavaDoc("Modification of node not supported");
205
206     }
207     /* (non-Javadoc)
208      * @see com.jofti.btree.IResultNode#getEntry(java.lang.Comparable)
209      */

210     public LeafNodeEntry getEntry(Comparable JavaDoc value)
211     {
212
213         if (entries.length ==0)
214         {
215             return null;
216         }
217         // look through list and see if we have a match
218

219         return indexedBinaryRetrieve(entries, value);
220         
221         
222     }
223
224     INode getDelegate(){
225         return node;
226     }
227
228      protected int indexedBinarySearch(Object JavaDoc[] arr1, Object JavaDoc obj)
229         {
230          int low = 0;
231             int high = entryNumber-1;
232             
233             LeafNodeEntry entry =null;
234             while (low <= high) {
235                 int mid = (low + high) >> 1;
236                 
237                 entry = (LeafNodeEntry)arr1[mid];
238                 int cmp = entry.getValue().compareTo(obj);
239
240                 if (cmp < 0)
241                 low = mid + 1;
242                 else if (cmp > 0)
243                 high = mid - 1;
244                 else
245                 return mid; // key found
246
}
247             return -(low + 1); // key not found
248
}
249     
250      
251      protected LeafNodeEntry indexedBinaryRetrieve(Object JavaDoc[] list1, Object JavaDoc obj) {
252             int i = 0;
253             int size = entryNumber;
254             for (int j = size- 1; i <= j;) {
255                 int k = i + j >> 1;
256                 LeafNodeEntry obj1 = (LeafNodeEntry)list1[k];
257                 int l = obj1.getValue().compareTo(obj);
258                 if (l < 0)
259                     i = k + 1;
260                 else if (l > 0)
261                     j = k - 1;
262                 else
263                     return obj1;
264             }
265
266             return null;
267         }
268      
269     public int hashCode(){
270         return node.hashCode();
271     }
272
273     /* (non-Javadoc)
274      * @see com.jofti.btree.IResultNode#getEntriesBefore(java.lang.Comparable)
275      */

276     public Collection JavaDoc getEntriesBefore(Comparable JavaDoc value)
277     {
278
279     
280         return null;
281     }
282     
283     
284      
285
286      public boolean equals(Object JavaDoc obj){
287         if (obj instanceof IResultNode){
288             return getDelegate() == ((ResultNode)obj).getDelegate();
289         }
290         return false;
291      }
292     public void setEntries(Object JavaDoc[] entries) {
293         throw new UnsupportedOperationException JavaDoc("Modification of node not supported");
294         
295     }
296     
297     public boolean isLeaf(){
298         return true;
299     }
300 }
301
Popular Tags