1 22 23 24 package com.mchange.v1.identicator; 25 26 import java.lang.ref.*; 27 import java.util.*; 28 import com.mchange.v1.util.WrapperIterator; 29 30 33 public final class IdWeakHashMap extends IdMap implements Map 34 { 35 ReferenceQueue rq; 36 37 public IdWeakHashMap(Identicator id) 38 { 39 super ( new HashMap(), id ); 40 this.rq = new ReferenceQueue(); 41 } 42 43 public int size() 45 { 46 cleanCleared(); 52 return super.size(); 53 } 54 55 public boolean isEmpty() 56 { 57 try 58 { return super.isEmpty(); } 59 finally 60 { cleanCleared(); } 61 } 62 63 public boolean containsKey(Object o) 64 { 65 try 66 { return super.containsKey( o ); } 67 finally 68 { cleanCleared(); } 69 } 70 71 public boolean containsValue(Object o) 72 { 73 try 74 { return super.containsValue( o ); } 75 finally 76 { cleanCleared(); } 77 } 78 79 public Object get(Object o) 80 { 81 try 82 { return super.get( o ); } 83 finally 84 { cleanCleared(); } 85 } 86 87 public Object put(Object k, Object v) 88 { 89 try 90 { return super.put( k , v ); } 91 finally 92 { cleanCleared(); } 93 } 94 95 public Object remove(Object o) 96 { 97 try 98 { return super.remove( o ); } 99 finally 100 { cleanCleared(); } 101 } 102 103 public void putAll(Map m) 104 { 105 try 106 { super.putAll( m ); } 107 finally 108 { cleanCleared(); } 109 } 110 111 public void clear() 112 { 113 try 114 { super.clear(); } 115 finally 116 { cleanCleared(); } 117 } 118 119 public Set keySet() 120 { 121 try 122 { return super.keySet(); } 123 finally 124 { cleanCleared(); } 125 } 126 127 public Collection values() 128 { 129 try 130 { return super.values(); } 131 finally 132 { cleanCleared(); } 133 } 134 135 140 public Set entrySet() 141 { 142 try 143 { return new WeakUserEntrySet(); } 144 finally 145 { cleanCleared(); } 146 } 147 148 public boolean equals(Object o) 149 { 150 try 151 { return super.equals( o ); } 152 finally 153 { cleanCleared(); } 154 } 155 156 public int hashCode() 157 { 158 try 159 { return super.hashCode(); } 160 finally 161 { cleanCleared(); } 162 } 163 164 protected IdHashKey createIdKey(Object o) 166 { return new WeakIdHashKey( o, id, rq ); } 167 168 private void cleanCleared() 169 { 170 WeakIdHashKey.Ref ref; 171 while ((ref = (WeakIdHashKey.Ref) rq.poll()) != null) 172 this.removeIdHashKey( ref.getKey() ); 173 } 174 175 private final class WeakUserEntrySet extends AbstractSet 176 { 177 Set innerEntries = internalEntrySet(); 178 179 public Iterator iterator() 180 { 181 try 182 { 183 return new WrapperIterator(innerEntries.iterator(), true) 184 { 185 protected Object transformObject(Object o) 186 { 187 Entry innerEntry = (Entry) o; 188 final Object userKey = ((IdHashKey) innerEntry.getKey()).getKeyObj(); 189 if (userKey == null) 190 return WrapperIterator.SKIP_TOKEN; 191 else 192 return new UserEntry( innerEntry ) 193 { Object preventRefClear = userKey; }; 194 } 195 }; 196 } 197 finally 198 { cleanCleared(); } 199 } 200 201 public int size() 202 { 203 cleanCleared(); 209 return innerEntries.size(); 210 } 211 212 public boolean contains(Object o) 213 { 214 try 215 { 216 if (o instanceof Entry) 217 { 218 Entry entry = (Entry) o; 219 return innerEntries.contains( createIdEntry( entry ) ); 220 } 221 else 222 return false; 223 } 224 finally 225 { cleanCleared(); } 226 } 227 228 public boolean remove(Object o) 229 { 230 try 231 { 232 if (o instanceof Entry) 233 { 234 Entry entry = (Entry) o; 235 return innerEntries.remove( createIdEntry( entry ) ); 236 } 237 else 238 return false; 239 } 240 finally 241 { cleanCleared(); } 242 } 243 244 public void clear() 245 { 246 try 247 { inner.clear(); } 248 finally 249 { cleanCleared(); } 250 } 251 } 252 } 253 | Popular Tags |