1 package com.daffodilwoods.database.general; 2 3 import java.util.Comparator ; 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 key = wk.getKey(); 26 if(super.containsKey(key)) 27 super.delete(key); 28 } 29 }catch(Exception e) {} 30 } 31 32 public boolean containsKey(Object key) throws DException { 33 processQueue(); 34 return super.containsKey(key); 35 } 36 37 public boolean containsValue(Object value) throws DException { 38 return false; 39 } 40 41 public void put(Object key, Object value) throws DException { 42 processQueue(); 43 WeakKey wk = WeakKey.create(value,queue); wk.setKey(key); 45 super.insert(key,wk); 46 } 47 48 public Object getKey() throws DException { 49 processQueue(); 50 return super.getKey(); 51 } 52 53 public void remove(Object key) throws DException { 54 try{ 55 processQueue(); 56 if(super.size() != 0) 57 super.delete(key); 58 }catch(Exception ex){} 59 } 60 61 62 public Object 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 get(Object 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; 79 80 private Object key; 81 public WeakKey(Object k) throws DException { 82 super(k); 83 hash = k.hashCode(); 84 } 85 86 public static WeakKey create(Object k) throws DException { 87 if (k == null) return null; 88 else return new WeakKey(k); 89 } 90 91 public WeakKey(Object k, ReferenceQueue q) throws DException { 92 super(k, q); 93 hash = k.hashCode(); 94 } 95 96 public static WeakKey create(Object 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 key) throws DException { 102 this.key = key; 103 } 104 105 public Object getKey() throws DException { 106 return key; 107 } 108 109 111 public boolean equals(Object o) { 112 if (this == o) return true; 113 if (!(o instanceof WeakKey)) return false; 114 Object t = this.get(); 115 Object 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 current; 131 132 public WeakOrderedKeyListIterator() throws DException { 133 current = getTopKey(); 134 } 135 136 137 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 162 163 public boolean previous() throws DException { 164 Object 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 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 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 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 key) throws DException 208 { 209 if(key == null) 210 throw new DException("DSE982",null); 211 212 current = key; 213 } 214 215 public Object getKey() throws DException { 216 return current; 217 } 218 219 public Object getObject() throws DException { 220 return WeakOrderedKeyList.this.get(current); 221 } 222 223 public Comparator getComparator() throws DException { 224 return getComparator(); 225 } 226 } 227 public void moveToKey(Object key) { 228 super.moveToKey( key); 229 } 230 public int getRowCount() throws DException { 231 return super.getRowCount(); 232 } 233 public Object getBottomKey() throws DException { 234 return super.getBottomKey(); 235 } 236 public Object getPreviousKey(Object key) throws DException { 237 return super.getPreviousKey( key); 238 } 239 public Object getRootKey() throws DException { 240 return super.getRootKey(); 241 } 242 public Object getTopKey() throws DException { 243 return super.getTopKey(); 244 } 245 public Object getNextKey(Object key) throws DException { 246 return super.getNextKey( key); 247 } 248 249 } 250 | Popular Tags |