KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > daffodilwoods > database > general > WeakOrderedKeyList


1 package com.daffodilwoods.database.general;
2
3 import java.util.Comparator JavaDoc;
4 import com.daffodilwoods.database.resource.*;
5 import java.lang.ref.*;
6 import com.daffodilwoods.daffodildb.utils.comparator.SuperComparator;
7
8 public class WeakOrderedKeyList extends OrderedKeyList
9 {
10    private SuperComparator comparator = null;
11
12    public ReferenceQueue queue = new ReferenceQueue();
13
14    public WeakOrderedKeyList(SuperComparator comparator) {
15       super(comparator);
16       this.comparator = comparator;
17    }
18
19    private void processQueue() throws DException {
20        try {
21           WeakKey wk;
22           while ((wk = (WeakKey)queue.poll()) != null) {
23             if(super.size() == 0)
24                 return;
25             Object JavaDoc key = wk.getKey();
26             if(super.containsKey(key))
27                 super.delete(key);
28          }
29        }catch(Exception JavaDoc e) {}
30    }
31
32    public boolean containsKey(Object JavaDoc key) throws DException {
33       processQueue();
34       return super.containsKey(key);
35    }
36
37    public boolean containsValue(Object JavaDoc value) throws DException {
38       return false;
39    }
40
41    public void put(Object JavaDoc key, Object JavaDoc value) throws DException {
42       processQueue();
43       WeakKey wk = WeakKey.create(value,queue);//
44
wk.setKey(key);
45       super.insert(key,wk);
46    }
47
48    public Object JavaDoc getKey() throws DException {
49       processQueue();
50       return super.getKey();
51    }
52
53    public void remove(Object JavaDoc key) throws DException {
54       try{
55         processQueue();
56         if(super.size() != 0)
57               super.delete(key);
58       }catch(Exception JavaDoc ex){}
59    }
60
61
62    public Object JavaDoc get() throws DException {
63       return ((WeakKey) super.getObject()).get();
64    }
65
66    public WeakOrderedKeyListIterator getWeakOrderedKeyListIterator() throws DException {
67       WeakOrderedKeyListIterator w = new WeakOrderedKeyListIterator();
68       return w;
69    }
70
71    public Object JavaDoc get(Object JavaDoc key) throws DException {
72       WeakKey weakKey1 = (WeakKey) super.getObject(key);
73       return weakKey1 == null ? null : weakKey1.get();
74    }
75
76    public static class WeakKey extends WeakReference {
77       private int hash; /* Hashcode of key, stored here since the key
78                    may be tossed by the GC */

79
80       private Object JavaDoc key;
81        public WeakKey(Object JavaDoc k) throws DException {
82           super(k);
83           hash = k.hashCode();
84        }
85
86        public static WeakKey create(Object JavaDoc k) throws DException {
87           if (k == null) return null;
88              else return new WeakKey(k);
89        }
90
91        public WeakKey(Object JavaDoc k, ReferenceQueue q) throws DException {
92           super(k, q);
93           hash = k.hashCode();
94        }
95
96        public static WeakKey create(Object JavaDoc k, ReferenceQueue q) throws DException {
97           if (k == null) return null;
98           else return new WeakKey(k, q);
99        }
100
101       public void setKey(Object JavaDoc key) throws DException {
102          this.key = key;
103       }
104
105       public Object JavaDoc getKey() throws DException {
106          return key;
107       }
108
109         /* A WeakKey is equal to another WeakKey iff they both refer to objects
110        that are, in turn, equal according to their own equals methods */

111        public boolean equals(Object JavaDoc o) {
112           if (this == o) return true;
113           if (!(o instanceof WeakKey)) return false;
114           Object JavaDoc t = this.get();
115           Object JavaDoc u = ((WeakKey)o).get();
116           if ((t == null) || (u == null)) return false;
117           if (t == u) return true;
118           return t.equals(u);
119        }
120
121 public int hashCode() {
122           return hash;
123        }
124
125       public void clear() {}
126    }
127
128
129  public class WeakOrderedKeyListIterator {
130     Object JavaDoc current;
131
132     public WeakOrderedKeyListIterator() throws DException {
133       current = getTopKey();
134     }
135
136
137 /*
138     public Object locateNearestRecord(Object value) throws DException {
139       return WeakOrderedKeyList.this.locateNearestRecord(value);
140     }*/

141
142      public int getRowCount() throws DException {
143       return size();
144      }
145
146     public boolean top() throws DException {
147        current = getTopKey();
148        return current != null;
149     }
150
151     public boolean bottom() throws DException {
152        current = getBottomKey();
153        return current != null;
154     }
155 /*
156    public void changeCurrentKey(Object object,Object oldKey) throws DException {
157       Entry entry = getEntry(object);
158       if(entry == null)
159          return;
160       moveToKey(entry.key);
161    }*/

162
163     public boolean previous() throws DException {
164       Object JavaDoc p = getPreviousKey(current);
165       if(p == null)
166          return false;
167       current = p;
168       return true;
169     }
170
171     public boolean next() throws DException {
172       Object JavaDoc p = getNextKey(current);
173       if(p == null)
174          return false;
175       current = p;
176       return true;
177     }
178
179     public boolean isTop() throws DException {
180       if( current == null )
181          return false;
182       else {
183          Object JavaDoc p = getTopKey();
184          return (p != null && current == p);
185       }
186     }
187
188     public boolean isBottom() throws DException {
189       if( current == null )
190          return false;
191       else {
192          Object JavaDoc p = getBottomKey();
193          return (p != null && current == p) ;
194       }
195     }
196
197     public boolean isPrevious() throws DException {
198       return current == null ? false :
199                                getPreviousKey(current) != null ;
200     }
201
202     public boolean isNext() throws DException {
203       return current == null ? false :
204                                getNextKey(current) != null ;
205     }
206
207     public void moveToKey(Object JavaDoc key) throws DException
208     {
209       if(key == null)
210          throw new DException("DSE982",null);
211
212       current = key;
213     }
214
215     public Object JavaDoc getKey() throws DException {
216        return current;
217     }
218
219     public Object JavaDoc getObject() throws DException {
220       return WeakOrderedKeyList.this.get(current);
221     }
222
223     public Comparator JavaDoc getComparator() throws DException {
224       return getComparator();
225     }
226  }
227    public void moveToKey(Object JavaDoc key) {
228       super.moveToKey( key);
229    }
230    public int getRowCount() throws DException {
231       return super.getRowCount();
232    }
233    public Object JavaDoc getBottomKey() throws DException {
234       return super.getBottomKey();
235    }
236    public Object JavaDoc getPreviousKey(Object JavaDoc key) throws DException {
237       return super.getPreviousKey( key);
238    }
239    public Object JavaDoc getRootKey() throws DException {
240       return super.getRootKey();
241    }
242    public Object JavaDoc getTopKey() throws DException {
243       return super.getTopKey();
244    }
245    public Object JavaDoc getNextKey(Object JavaDoc key) throws DException {
246       return super.getNextKey( key);
247    }
248
249 }
250
Popular Tags