1. /*
  2. * @(#)CacheMap.java 1.4 03/12/19
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package com.sun.jmx.remote.util;
  8. import java.lang.ref.SoftReference;
  9. import java.util.Iterator;
  10. import java.util.LinkedList;
  11. import java.util.List;
  12. import java.util.WeakHashMap;
  13. /**
  14. * <p>Like WeakHashMap, except that the keys of the <em>n</em> most
  15. * recently-accessed entries are kept as {@link SoftReference soft
  16. * references}. Accessing an element means creating it, or retrieving
  17. * it with {@link #get(Object) get}. Because these entries are kept
  18. * with soft references, they will tend to remain even if their keys
  19. * are not referenced elsewhere. But if memory is short, they will
  20. * be removed.</p>
  21. */
  22. public class CacheMap extends WeakHashMap {
  23. /**
  24. * <p>Create a <code>CacheMap</code> that can keep up to
  25. * <code>nSoftReferences</code> as soft references.</p>
  26. *
  27. * @param nSoftReferences Maximum number of keys to keep as soft
  28. * references. Access times for {@link #get(Object) get} and
  29. * {@link #put(Object, Object) put} have a component that scales
  30. * linearly with <code>nSoftReferences</code>, so this value
  31. * should not be too great.
  32. *
  33. * @throws IllegalArgumentException if
  34. * <code>nSoftReferences</code> is negative.
  35. */
  36. public CacheMap(int nSoftReferences) {
  37. if (nSoftReferences < 0) {
  38. throw new IllegalArgumentException("nSoftReferences = " +
  39. nSoftReferences);
  40. }
  41. this.nSoftReferences = nSoftReferences;
  42. }
  43. public Object put(Object key, Object value) {
  44. cache(key);
  45. return super.put(key, value);
  46. }
  47. public Object get(Object key) {
  48. cache(key);
  49. return super.get(key);
  50. }
  51. /* We don't override remove(Object) or try to do something with
  52. the map's iterators to detect removal. So we may keep useless
  53. entries in the soft reference list for keys that have since
  54. been removed. The assumption is that entries are added to the
  55. cache but never removed. But the behaviour is not wrong if
  56. they are in fact removed -- the caching is just less
  57. performant. */
  58. private void cache(Object key) {
  59. Iterator it = cache.iterator();
  60. while (it.hasNext()) {
  61. SoftReference sref = (SoftReference) it.next();
  62. Object key1 = sref.get();
  63. if (key1 == null)
  64. it.remove();
  65. else if (key.equals(key1)) {
  66. // Move this element to the head of the LRU list
  67. it.remove();
  68. cache.add(0, sref);
  69. return;
  70. }
  71. }
  72. int size = cache.size();
  73. if (size == nSoftReferences) {
  74. if (size == 0)
  75. return; // degenerate case, equivalent to WeakHashMap
  76. it.remove();
  77. }
  78. cache.add(0, new SoftReference(key));
  79. }
  80. /* List of soft references for the most-recently referenced keys.
  81. The list is in most-recently-used order, i.e. the first element
  82. is the most-recently referenced key. There are never more than
  83. nSoftReferences elements of this list.
  84. If we didn't care about J2SE 1.3 compatibility, we could use
  85. LinkedHashSet in conjunction with a subclass of SoftReference
  86. whose equals and hashCode reflect the referent. */
  87. private final LinkedList/*<SoftReference>*/ cache = new LinkedList();
  88. private final int nSoftReferences;
  89. }