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