1 18 package org.apache.batik.util; 19 20 import java.lang.ref.SoftReference ; 21 import java.util.HashMap ; 22 23 36 37 public class SoftReferenceCache { 38 HashMap map = new HashMap (); 39 40 43 protected SoftReferenceCache() { } 44 45 46 51 public synchronized void flush() { 52 map.clear(); 53 this.notifyAll(); 54 } 55 56 64 protected final synchronized boolean isPresentImpl(Object key) { 65 if (!map.containsKey(key)) 66 return false; 67 68 Object o = map.get(key); 69 if (o == null) 70 return true; 72 73 SoftReference sr = (SoftReference )o; 76 o = sr.get(); 77 if (o != null) 78 return true; 79 80 clearImpl(key); 82 return false; 83 } 84 85 91 protected final synchronized boolean isDoneImpl(Object key) { 92 Object o = map.get(key); 93 if (o == null) return false; 94 SoftReference sr = (SoftReference )o; 95 o = sr.get(); 96 if (o != null) 97 return true; 98 99 clearImpl(key); 101 return false; 102 } 103 104 108 protected final synchronized Object requestImpl(Object key) { 109 if (map.containsKey(key)) { 110 111 Object o = map.get(key); 112 while(o == null) { 113 try { 114 wait(); 116 } 117 catch (InterruptedException ie) { } 118 119 if (!map.containsKey(key)) 122 break; 123 124 o = map.get(key); 126 } 127 if (o != null) { 128 SoftReference sr = (SoftReference )o; 129 o = sr.get(); 130 if (o != null) 131 return o; 132 } 133 } 134 135 map.put(key, null); 137 return null; 138 } 139 140 145 protected final synchronized void clearImpl(Object key) { 146 map.remove(key); 147 this.notifyAll(); 148 } 149 150 157 protected final synchronized void putImpl(Object key, Object object) { 158 if (map.containsKey(key)) { 159 SoftReference ref = new SoftRefKey(object, key); 160 map.put(key, ref); 161 this.notifyAll(); 162 } 163 } 164 165 class SoftRefKey extends CleanerThread.SoftReferenceCleared { 166 Object key; 167 public SoftRefKey(Object o, Object key) { 168 super(o); 169 this.key = key; 170 } 171 172 public void cleared() { 173 SoftReferenceCache cache = SoftReferenceCache.this; 174 if (cache == null) return; synchronized (cache) { 176 Object o = cache.map.remove(key); 177 if (this == o) { 178 cache.notifyAll(); 181 } else { 182 cache.map.put(key, o); 185 } 186 187 } 188 } 189 } 190 } 191 | Popular Tags |