1. /*
  2. * @(#)WeakHashMap.java 1.6 01/11/29
  3. *
  4. * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.util;
  8. import java.util.Iterator;
  9. import java.util.Map;
  10. import java.util.AbstractMap;
  11. import java.util.HashMap;
  12. import java.util.Set;
  13. import java.util.AbstractSet;
  14. import java.util.NoSuchElementException;
  15. import java.lang.ref.WeakReference;
  16. import java.lang.ref.ReferenceQueue;
  17. /**
  18. * A hashtable-based <code>Map</code> implementation with <em>weak keys</em>.
  19. * An entry in a <code>WeakHashMap</code> will automatically be removed when
  20. * its key is no longer in ordinary use. More precisely, the presence of a
  21. * mapping for a given key will not prevent the key from being discarded by the
  22. * garbage collector, that is, made finalizable, finalized, and then reclaimed.
  23. * When a key has been discarded its entry is effectively removed from the map,
  24. * so this class behaves somewhat differently than other <code>Map</code>
  25. * implementations.
  26. *
  27. * <p> Both null values and the null key are supported. This class has
  28. * performance characteristics similar to those of the <code>HashMap</code>
  29. * class, and has the same efficiency parameters of <em>initial capacity</em>
  30. * and <em>load factor</em>.
  31. *
  32. * <p> Like most collection classes, this class is not synchronized. A
  33. * synchronized <code>WeakHashMap</code> may be constructed using the
  34. * <code>Collections.synchronizedMap</code> method.
  35. *
  36. * <p> This class is intended primarily for use with key objects whose
  37. * <code>equals</code> methods test for object identity using the
  38. * <code>==</code> operator. Once such a key is discarded it can never be
  39. * recreated, so it is impossible to do a lookup of that key in a
  40. * <code>WeakHashMap</code> at some later time and be surprised that its entry
  41. * has been removed. This class will work perfectly well with key objects
  42. * whose <code>equals</code> methods are not based upon object identity, such
  43. * as <code>String</code> instances. With such recreatable key objects,
  44. * however, the automatic removal of <code>WeakHashMap</code> entries whose
  45. * keys have been discarded may prove to be confusing.
  46. *
  47. * <p> The behavior of the <code>WeakHashMap</code> class depends in part upon
  48. * the actions of the garbage collector, so several familiar (though not
  49. * required) <code>Map</code> invariants do not hold for this class. Because
  50. * the garbage collector may discard keys at any time, a
  51. * <code>WeakHashMap</code> may behave as though an unknown thread is silently
  52. * removing entries. In particular, even if you synchronize on a
  53. * <code>WeakHashMap</code> instance and invoke none of its mutator methods, it
  54. * is possible for the <code>size</code> method to return smaller values over
  55. * time, for the <code>isEmpty</code> method to return <code>false</code> and
  56. * then <code>true</code>, for the <code>containsKey</code> method to return
  57. * <code>true</code> and later <code>false</code> for a given key, for the
  58. * <code>get</code> method to return a value for a given key but later return
  59. * <code>null</code>, for the <code>put</code> method to return
  60. * <code>null</code> and the <code>remove</code> method to return
  61. * <code>false</code> for a key that previously appeared to be in the map, and
  62. * for successive examinations of the key set, the value set, and the entry set
  63. * to yield successively smaller numbers of elements.
  64. *
  65. * <p> Each key object in a <code>WeakHashMap</code> is stored indirectly as
  66. * the referent of a weak reference. Therefore a key will automatically be
  67. * removed only after the weak references to it, both inside and outside of the
  68. * map, have been cleared by the garbage collector.
  69. *
  70. * <p> <strong>Implementation note:</strong> The value objects in a
  71. * <code>WeakHashMap</code> are held by ordinary strong references. Thus care
  72. * should be taken to ensure that value objects do not strongly refer to their
  73. * own keys, either directly or indirectly, since that will prevent the keys
  74. * from being discarded. Note that a value object may refer indirectly to its
  75. * key via the <code>WeakHashMap</code> itself; that is, a value object may
  76. * strongly refer to some other key object whose associated value object, in
  77. * turn, strongly refers to the key of the first value object. This problem
  78. * may be fixed in a future release.
  79. *
  80. * @version 1.6, 01/11/29
  81. * @author Mark Reinhold
  82. * @since JDK1.2
  83. * @see java.util.HashMap
  84. * @see java.lang.ref.WeakReference
  85. */
  86. public class WeakHashMap extends AbstractMap implements Map {
  87. /* A WeakHashMap is implemented as a HashMap that maps WeakKeys to values.
  88. Because we don't have access to the innards of the HashMap, the various
  89. query methods must create a temporary WeakKey every time a lookup is
  90. done. Fortunately these are small, short-lived objects, so the added
  91. allocation overhead is tolerable. */
  92. static private class WeakKey extends WeakReference {
  93. private int hash; /* Hashcode of key, stored here since the key
  94. may be tossed by the GC */
  95. private WeakKey(Object k) {
  96. super(k);
  97. hash = k.hashCode();
  98. }
  99. private static WeakKey create(Object k) {
  100. if (k == null) return null;
  101. else return new WeakKey(k);
  102. }
  103. private WeakKey(Object k, ReferenceQueue q) {
  104. super(k, q);
  105. hash = k.hashCode();
  106. }
  107. private static WeakKey create(Object k, ReferenceQueue q) {
  108. if (k == null) return null;
  109. else return new WeakKey(k, q);
  110. }
  111. /* A WeakKey is equal to another WeakKey iff they both refer to objects
  112. that are, in turn, equal according to their own equals methods */
  113. public boolean equals(Object o) {
  114. if (this == o) return true;
  115. if (!(o instanceof WeakKey)) return false;
  116. Object t = this.get();
  117. Object u = ((WeakKey)o).get();
  118. if ((t == null) || (u == null)) return false;
  119. if (t == u) return true;
  120. return t.equals(u);
  121. }
  122. public int hashCode() {
  123. return hash;
  124. }
  125. }
  126. /* Hash table mapping WeakKeys to values */
  127. private Map hash;
  128. /* Reference queue for cleared WeakKeys */
  129. private ReferenceQueue queue = new ReferenceQueue();
  130. /* Remove all invalidated entries from the map, that is, remove all entries
  131. whose keys have been discarded. This method should be invoked once by
  132. each public mutator in this class. We don't invoke this method in
  133. public accessors because that can lead to surprising
  134. ConcurrentModificationExceptions. */
  135. private void processQueue() {
  136. WeakKey wk;
  137. while ((wk = (WeakKey)queue.poll()) != null) {
  138. hash.remove(wk);
  139. }
  140. }
  141. /* -- Constructors -- */
  142. /**
  143. * Constructs a new, empty <code>WeakHashMap</code> with the given
  144. * initial capacity and the given load factor.
  145. *
  146. * @param initialCapacity The initial capacity of the
  147. * <code>WeakHashMap</code>
  148. *
  149. * @param loadFactor The load factor of the <code>WeakHashMap</code>
  150. *
  151. * @throws IllegalArgumentException If the initial capacity is less than
  152. * zero, or if the load factor is
  153. * nonpositive
  154. */
  155. public WeakHashMap(int initialCapacity, float loadFactor) {
  156. hash = new HashMap(initialCapacity, loadFactor);
  157. }
  158. /**
  159. * Constructs a new, empty <code>WeakHashMap</code> with the given
  160. * initial capacity and the default load factor, which is
  161. * <code>0.75</code>.
  162. *
  163. * @param initialCapacity The initial capacity of the
  164. * <code>WeakHashMap</code>
  165. *
  166. * @throws IllegalArgumentException If the initial capacity is less than
  167. * zero
  168. */
  169. public WeakHashMap(int initialCapacity) {
  170. hash = new HashMap(initialCapacity);
  171. }
  172. /**
  173. * Constructs a new, empty <code>WeakHashMap</code> with the default
  174. * capacity and the default load factor, which is <code>0.75</code>.
  175. */
  176. public WeakHashMap() {
  177. hash = new HashMap();
  178. }
  179. /* -- Simple queries -- */
  180. /**
  181. * Returns the number of key-value mappings in this map.
  182. * <strong>Note:</strong> <em>In contrast with most implementations of the
  183. * <code>Map</code> interface, the time required by this operation is
  184. * linear in the size of the map.</em>
  185. */
  186. public int size() {
  187. return entrySet().size();
  188. }
  189. /**
  190. * Returns <code>true</code> if this map contains no key-value mappings.
  191. */
  192. public boolean isEmpty() {
  193. return entrySet().isEmpty();
  194. }
  195. /**
  196. * Returns <code>true</code> if this map contains a mapping for the
  197. * specified key.
  198. *
  199. * @param key The key whose presence in this map is to be tested
  200. */
  201. public boolean containsKey(Object key) {
  202. return hash.containsKey(WeakKey.create(key));
  203. }
  204. /* -- Lookup and modification operations -- */
  205. /**
  206. * Returns the value to which this map maps the specified <code>key</code>.
  207. * If this map does not contain a value for this key, then return
  208. * <code>null</code>.
  209. *
  210. * @param key The key whose associated value, if any, is to be returned
  211. */
  212. public Object get(Object key) {
  213. return hash.get(WeakKey.create(key));
  214. }
  215. /**
  216. * Updates this map so that the given <code>key</code> maps to the given
  217. * <code>value</code>. If the map previously contained a mapping for
  218. * <code>key</code> then that mapping is replaced and the previous value is
  219. * returned.
  220. *
  221. * @param key The key that is to be mapped to the given
  222. * <code>value</code>
  223. * @param value The value to which the given <code>key</code> is to be
  224. * mapped
  225. *
  226. * @return The previous value to which this key was mapped, or
  227. * <code>null</code> if if there was no mapping for the key
  228. */
  229. public Object put(Object key, Object value) {
  230. processQueue();
  231. return hash.put(WeakKey.create(key, queue), value);
  232. }
  233. /**
  234. * Removes the mapping for the given <code>key</code> from this map, if
  235. * present.
  236. *
  237. * @param key The key whose mapping is to be removed
  238. *
  239. * @return The value to which this key was mapped, or <code>null</code> if
  240. * there was no mapping for the key
  241. */
  242. public Object remove(Object key) {
  243. processQueue();
  244. return hash.remove(WeakKey.create(key));
  245. }
  246. /**
  247. * Removes all mappings from this map.
  248. */
  249. public void clear() {
  250. processQueue();
  251. hash.clear();
  252. }
  253. /* -- Views -- */
  254. /* Internal class for entries */
  255. static private class Entry implements Map.Entry {
  256. private Map.Entry ent;
  257. private Object key; /* Strong reference to key, so that the GC
  258. will leave it alone as long as this Entry
  259. exists */
  260. Entry(Map.Entry ent, Object key) {
  261. this.ent = ent;
  262. this.key = key;
  263. }
  264. public Object getKey() {
  265. return key;
  266. }
  267. public Object getValue() {
  268. return ent.getValue();
  269. }
  270. public Object setValue(Object value) {
  271. return ent.setValue(value);
  272. }
  273. private static boolean valEquals(Object o1, Object o2) {
  274. return (o1 == null) ? (o2 == null) : o1.equals(o2);
  275. }
  276. public boolean equals(Object o) {
  277. if (! (o instanceof Map.Entry)) return false;
  278. Map.Entry e = (Map.Entry)o;
  279. return (valEquals(key, e.getKey())
  280. && valEquals(getValue(), e.getValue()));
  281. }
  282. public int hashCode() {
  283. Object v;
  284. return (((key == null) ? 0 : key.hashCode())
  285. ^ (((v = getValue()) == null) ? 0 : v.hashCode()));
  286. }
  287. }
  288. /* Internal class for entry sets */
  289. private class EntrySet extends AbstractSet {
  290. Set hashEntrySet = hash.entrySet();
  291. public Iterator iterator() {
  292. return new Iterator() {
  293. Iterator hashIterator = hashEntrySet.iterator();
  294. Entry next = null;
  295. public boolean hasNext() {
  296. while (hashIterator.hasNext()) {
  297. Map.Entry ent = (Map.Entry)hashIterator.next();
  298. WeakKey wk = (WeakKey)ent.getKey();
  299. Object k = null;
  300. if ((wk != null) && ((k = wk.get()) == null)) {
  301. /* Weak key has been cleared by GC */
  302. continue;
  303. }
  304. next = new Entry(ent, k);
  305. return true;
  306. }
  307. return false;
  308. }
  309. public Object next() {
  310. if ((next == null) && !hasNext())
  311. throw new NoSuchElementException();
  312. Entry e = next;
  313. next = null;
  314. return e;
  315. }
  316. public void remove() {
  317. hashIterator.remove();
  318. }
  319. };
  320. }
  321. public boolean isEmpty() {
  322. return !(iterator().hasNext());
  323. }
  324. public int size() {
  325. int j = 0;
  326. for (Iterator i = iterator(); i.hasNext(); i.next()) j++;
  327. return j;
  328. }
  329. public boolean remove(Object o) {
  330. processQueue();
  331. if (!(o instanceof Map.Entry)) return false;
  332. Map.Entry e = (Map.Entry)o;
  333. Object ev = e.getValue();
  334. WeakKey wk = WeakKey.create(e.getKey());
  335. Object hv = hash.get(wk);
  336. if ((hv == null)
  337. ? ((ev == null) && hash.containsKey(wk)) : hv.equals(ev)) {
  338. hash.remove(wk);
  339. return true;
  340. }
  341. return false;
  342. }
  343. public int hashCode() {
  344. int h = 0;
  345. for (Iterator i = hashEntrySet.iterator(); i.hasNext();) {
  346. Map.Entry ent = (Map.Entry)i.next();
  347. WeakKey wk = (WeakKey)ent.getKey();
  348. Object v;
  349. if (wk == null) continue;
  350. h += (wk.hashCode()
  351. ^ (((v = ent.getValue()) == null) ? 0 : v.hashCode()));
  352. }
  353. return h;
  354. }
  355. }
  356. private Set entrySet = null;
  357. /**
  358. * Returns a <code>Set</code> view of the mappings in this map.
  359. */
  360. public Set entrySet() {
  361. if (entrySet == null) entrySet = new EntrySet();
  362. return entrySet;
  363. }
  364. }