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