1 56 package org.enhydra.dods.util; 57 58 import java.io.Externalizable ; 59 import java.io.IOException ; 60 import java.io.ObjectInput ; 61 import java.io.ObjectOutput ; 62 import java.util.Iterator ; 63 import java.util.Map ; 64 65 90 public class LRUMap extends SequencedHashMap implements Externalizable { 91 92 private int maximumSize = 0; 93 94 100 public LRUMap() { 101 super(); 102 } 103 104 112 public LRUMap(int maxSize) { 113 this(); 114 maximumSize = maxSize; 115 } 116 117 124 public LRUMap(int initialCapacity, float loadFactor) { 125 super(initialCapacity, loadFactor); 126 } 127 128 138 139 public LRUMap(int initialCapacity, float loadFactor, int maxSize) { 140 this(initialCapacity, loadFactor); 141 maximumSize = maxSize; 142 } 143 144 152 153 public LRUMap(Map m) { 154 maximumSize = m.size()+1; 155 putAll(m); 157 } 158 159 169 public LRUMap(Map m, int maxSize) { 170 this(m); 171 if ( (maxSize < 0) || (maxSize>m.size()+1)) 172 maximumSize = maxSize; 173 } 174 175 182 public boolean containsKey(Object key) { 183 if(maximumSize == 0) 184 return false; 185 return super.containsKey(key); 186 } 187 188 200 public Object get(Object key) { 201 if(maximumSize == 0) 202 return null; 203 if(!containsKey(key)) 204 return null; 205 Object value = remove(key); 206 super.put(key,value); 207 return value; 208 } 209 210 223 224 225 public Object put( Object key, Object value ) { 226 if(maximumSize == 0) 227 return null; 228 int mapSize = size(); 229 Object retval = null; 230 231 if ((maximumSize > 0) && (mapSize >= maximumSize )) { 232 233 if (!containsKey(key)) { 236 removeLRU(); 238 } 239 } 240 retval = super.put(key,value); 241 return retval; 242 } 243 244 254 public void putAll(Map t) { 255 Iterator iter = t.entrySet().iterator(); 256 while(iter.hasNext()) { 257 Map.Entry entry = (Map.Entry )iter.next(); 258 this.put(entry.getKey(), entry.getValue()); 259 } 260 } 261 262 266 protected void removeLRU() { 267 Object key = getFirstKey(); 268 Object value = super.get(key); 271 remove(key); 272 processRemovedLRU(key,value); 273 } 274 275 284 protected void processRemovedLRU(Object key, Object value) { 285 } 286 287 public void readExternal( ObjectInput in ) throws IOException , ClassNotFoundException { 290 maximumSize = in.readInt(); 291 int size = in.readInt(); 292 293 for( int i = 0; i < size; i++ ) { 294 Object key = in.readObject(); 295 Object value = in.readObject(); 296 put(key,value); 297 } 298 } 299 300 public void writeExternal( ObjectOutput out ) throws IOException { 301 out.writeInt( maximumSize ); 302 out.writeInt( size() ); 303 for( Iterator iterator = keySet().iterator(); iterator.hasNext(); ) { 304 Object key = iterator.next(); 305 out.writeObject( key ); 306 Object value = super.get( key ); 309 out.writeObject( value ); 310 } 311 } 312 313 314 321 public int getMaximumSize() { 322 return maximumSize; 323 } 324 329 public void setMaximumSize(int maxSize) { 330 this.maximumSize = maxSize; 331 if(maxSize >= 0) { 332 while (size() > maximumSize) { 333 removeLRU(); 334 } 335 } 336 } 337 338 339 } 343 | Popular Tags |