1 2 3 4 package net.nutch.util; 5 6 import java.lang.ref.SoftReference ; 7 import java.util.AbstractMap ; 8 import java.util.ArrayList ; 9 import java.util.Collections ; 10 import java.util.Collection ; 11 import java.util.HashMap ; 12 import java.util.Map ; 13 import java.util.Set ; 14 15 38 public class SoftHashMap extends AbstractMap implements Map { 39 Map hashMap; 40 ArrayList keysToDelete; 41 42 46 public interface FinalizationListener { 47 54 public void finalizationOccurring(); 55 } 56 57 63 public interface FinalizationNotifier { 64 67 public void addFinalizationListener(FinalizationListener listener); 68 } 69 70 private class MyFinalizationListener implements FinalizationListener { 71 Object key; 72 73 MyFinalizationListener(Object key, FinalizationNotifier value) { 74 this.key= key; 75 value.addFinalizationListener(this); 76 } 77 78 public void finalizationOccurring() { 79 SoftHashMap.this.queueKeyForDeletion(key); 80 } 81 82 } 83 84 public SoftHashMap() { 85 hashMap= Collections.synchronizedMap(new HashMap ()); 86 keysToDelete= new ArrayList (128); 87 } 88 89 public void clear() { 90 hashMap.clear(); 91 } 92 93 protected void queueKeyForDeletion(Object key) { 96 synchronized (keysToDelete) { 97 purgeQueuedKeys(); 98 keysToDelete.add(key); 99 } 100 } 101 102 protected void purgeQueuedKeys() { 104 synchronized (keysToDelete) { 105 for (int i= keysToDelete.size() - 1; i >= 0 ; i--) { 106 remove(keysToDelete.get(i)); 107 } 108 keysToDelete.clear(); 109 } 110 } 111 112 120 public boolean containsKey(Object key) { 121 return hashMap.containsKey(key); 122 } 123 124 130 public boolean containsValue(Object value) 131 throws UnsupportedOperationException { 132 throw new UnsupportedOperationException ("SoftHashMap.containsValue is " 133 + "not implemented"); 134 } 135 136 139 public Set entrySet() throws UnsupportedOperationException { 140 throw new 141 UnsupportedOperationException ("SoftHashMap.entrySet() not implemented"); 142 } 143 144 public Object get(Object key) { 145 SoftReference ref= (SoftReference ) hashMap.get(key); 146 if (ref == null) { 147 return null; 148 } 149 return ref.get(); 150 } 151 152 public boolean isEmpty() { 153 purgeQueuedKeys(); 154 return hashMap.isEmpty(); 155 } 156 157 public Set keySet() { 158 purgeQueuedKeys(); 159 return hashMap.keySet(); 160 } 161 162 163 173 public Object put(Object key, Object value) { 174 purgeQueuedKeys(); 175 SoftReference oldRef= 176 (SoftReference ) hashMap.put(key, new SoftReference (value)); 177 178 try { 179 new MyFinalizationListener(key, (FinalizationNotifier) value); 180 } catch (ClassCastException e) { 181 } 183 184 if (oldRef == null) 185 return null; 186 return oldRef.get(); 187 } 188 189 public Object remove(Object key) { 190 SoftReference ref= (SoftReference ) hashMap.remove(key); 191 if (ref == null) 192 return null; 193 return ref.get(); 194 } 195 196 public int size() { 197 purgeQueuedKeys(); 198 return hashMap.size(); 199 } 200 201 204 public Collection values() throws UnsupportedOperationException { 205 throw new 206 UnsupportedOperationException ("SoftHashMap: values() not implemnted"); 207 } 208 209 } 210 | Popular Tags |