1 20 package org.enhydra.dods.cache.lru; 21 22 import java.util.HashSet ; 23 import java.util.Iterator ; 24 import java.util.Map ; 25 import java.util.Set ; 26 import java.util.Collection ; 27 import org.enhydra.dods.util.LRUMap; 28 29 36 37 public class LRUCache implements Map { 38 39 private Map impl; 40 private static Map createMap(int initialCapacity, float loadFactor) { 41 Map ret = null; 42 try { 43 ret = (Map ) Class.forName("java.util.LinkedHashMap") 44 .getConstructor(new Class [] {int.class, float.class, boolean.class}) 45 .newInstance(new Object [] {new Integer (initialCapacity), new Float (loadFactor), new Boolean (true)}); 46 } catch (Throwable t) { 47 ret = new LRUMap(initialCapacity, loadFactor, -1); 48 } 49 return ret; 50 } 51 55 protected int maxEntries = 1024; 56 57 67 public LRUCache(int initialCapacity, float loadFactor, int maxEnt) { 68 impl = createMap(initialCapacity, loadFactor); 69 maxEntries = maxEnt; 70 } 71 72 80 public LRUCache(int initialCapacity, float loadFactor) { 81 impl = createMap(initialCapacity, loadFactor); 82 } 83 84 92 public LRUCache(int maxEnt) { 93 impl = createMap(16, (float) 0.75); 94 maxEntries = maxEnt; 95 } 96 97 102 public LRUCache() { 103 impl = createMap(16, (float) 0.75); 104 } 105 106 116 public LRUCache(Map m, int maxEnt) { 117 impl = createMap(16, (float) 0.75); 118 maxEntries = maxEnt; 119 impl.putAll(m); 120 } 121 122 131 public LRUCache(Map m) { 132 impl = createMap(16, (float) 0.75); 133 impl.putAll(m); 134 } 135 136 147 public Object add(Object key, Object value) { 148 Object ret = impl.put(key, value); 149 150 if (ret == null) { 151 if (maxEntries < size() && maxEntries > 0) { 152 Iterator iter = impl.keySet().iterator(); 153 Object rem = iter.next(); 154 155 ret = impl.remove(rem); 156 } 157 } 158 return ret; 159 } 160 161 166 public int getMaxEntries() { 167 return maxEntries; 168 } 169 170 175 public void setMaxEntries(int max) { 176 if (max < size() && max > 0) { 177 Iterator iter = (new HashSet (impl.keySet())).iterator(); 178 int dif = size() - max; 179 180 for (int i = 0; i < dif; i++) { 181 impl.remove(iter.next()); 182 } 183 } 184 maxEntries = max; 185 } 186 187 190 public String toString() { 191 Object [] v = impl.entrySet().toArray(); 192 String ret = "LRU content:\n"; 193 194 for (int i = 0; i < v.length; i++) { 195 Map.Entry map = (Map.Entry ) v[i]; 196 197 ret += i + ". key = " + map.getKey() + " value = " + map.getValue() 198 + "\n"; 199 } 200 return ret; 201 } 202 203 public int size() { 204 return impl.size(); 205 } 206 public boolean isEmpty() { 207 return impl.isEmpty(); 208 } 209 public boolean containsKey(Object key) throws ClassCastException , NullPointerException { 210 return impl.containsKey(key); 211 } 212 public boolean containsValue(Object value) throws ClassCastException , NullPointerException { 213 return impl.containsValue(value); 214 } 215 public synchronized Object get(Object key) throws ClassCastException , NullPointerException { 216 return impl.get(key); 217 } 218 public Object put(Object key, Object value) throws ClassCastException , NullPointerException , UnsupportedOperationException , IllegalArgumentException { 219 return impl.put(key, value); 220 } 221 public synchronized Object remove(Object key) throws ClassCastException , NullPointerException , UnsupportedOperationException { 222 return impl.remove(key); 223 } 224 public void putAll(Map t) throws ClassCastException , NullPointerException , UnsupportedOperationException , IllegalArgumentException { 225 impl.putAll(t); 226 } 227 public void clear() throws UnsupportedOperationException { 228 impl.clear(); 229 } 230 public Set keySet() { 231 return impl.keySet(); 232 } 233 public Collection values() { 234 return impl.values(); 235 } 236 public Set entrySet() { 237 return impl.entrySet(); 238 } 239 public boolean equals(Object o) { 240 return (o instanceof LRUCache) && impl.equals(((LRUCache)o).impl); 241 } 242 public int hashCode() { 243 return impl.hashCode(); 244 } 245 } 246 | Popular Tags |