1. /*
  2. * Copyright 2002-2004 The Apache Software Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.apache.commons.collections.map;
  17. import java.io.IOException;
  18. import java.io.ObjectInputStream;
  19. import java.io.ObjectOutputStream;
  20. import java.io.Serializable;
  21. /**
  22. * A <code>Map</code> implementation that allows mappings to be
  23. * removed by the garbage collector.
  24. * <p>
  25. * When you construct a <code>ReferenceMap</code>, you can specify what kind
  26. * of references are used to store the map's keys and values.
  27. * If non-hard references are used, then the garbage collector can remove
  28. * mappings if a key or value becomes unreachable, or if the JVM's memory is
  29. * running low. For information on how the different reference types behave,
  30. * see {@link Reference}.
  31. * <p>
  32. * Different types of references can be specified for keys and values.
  33. * The keys can be configured to be weak but the values hard,
  34. * in which case this class will behave like a
  35. * <a href="http://java.sun.com/j2se/1.4/docs/api/java/util/WeakHashMap.html">
  36. * <code>WeakHashMap</code></a>. However, you can also specify hard keys and
  37. * weak values, or any other combination. The default constructor uses
  38. * hard keys and soft values, providing a memory-sensitive cache.
  39. * <p>
  40. * This map is similar to
  41. * {@link org.apache.commons.collections.map.ReferenceIdentityMap ReferenceIdentityMap}.
  42. * It differs in that keys and values in this class are compared using <code>equals()</code>.
  43. * <p>
  44. * This {@link Map} implementation does <i>not</i> allow null elements.
  45. * Attempting to add a null key or value to the map will raise a <code>NullPointerException</code>.
  46. * <p>
  47. * This implementation is not synchronized.
  48. * You can use {@link java.util.Collections#synchronizedMap} to
  49. * provide synchronized access to a <code>ReferenceMap</code>.
  50. * Remember that synchronization will not stop the garbage collecter removing entries.
  51. * <p>
  52. * All the available iterators can be reset back to the start by casting to
  53. * <code>ResettableIterator</code> and calling <code>reset()</code>.
  54. * <p>
  55. * NOTE: As from Commons Collections 3.1 this map extends <code>AbstractReferenceMap</code>
  56. * (previously it extended AbstractMap). As a result, the implementation is now
  57. * extensible and provides a <code>MapIterator</code>.
  58. *
  59. * @see java.lang.ref.Reference
  60. *
  61. * @since Commons Collections 3.0 (previously in main package v2.1)
  62. * @version $Revision: 1.13 $ $Date: 2004/04/27 21:35:23 $
  63. *
  64. * @author Paul Jack
  65. * @author Stephen Colebourne
  66. */
  67. public class ReferenceMap extends AbstractReferenceMap implements Serializable {
  68. /** Serialization version */
  69. private static final long serialVersionUID = 1555089888138299607L;
  70. /**
  71. * Constructs a new <code>ReferenceMap</code> that will
  72. * use hard references to keys and soft references to values.
  73. */
  74. public ReferenceMap() {
  75. super(HARD, SOFT, DEFAULT_CAPACITY, DEFAULT_LOAD_FACTOR, false);
  76. }
  77. /**
  78. * Constructs a new <code>ReferenceMap</code> that will
  79. * use the specified types of references.
  80. *
  81. * @param keyType the type of reference to use for keys;
  82. * must be {@link #HARD}, {@link #SOFT}, {@link #WEAK}
  83. * @param valueType the type of reference to use for values;
  84. * must be {@link #HARD}, {@link #SOFT}, {@link #WEAK}
  85. */
  86. public ReferenceMap(int keyType, int valueType) {
  87. super(keyType, valueType, DEFAULT_CAPACITY, DEFAULT_LOAD_FACTOR, false);
  88. }
  89. /**
  90. * Constructs a new <code>ReferenceMap</code> that will
  91. * use the specified types of references.
  92. *
  93. * @param keyType the type of reference to use for keys;
  94. * must be {@link #HARD}, {@link #SOFT}, {@link #WEAK}
  95. * @param valueType the type of reference to use for values;
  96. * must be {@link #HARD}, {@link #SOFT}, {@link #WEAK}
  97. * @param purgeValues should the value be automatically purged when the
  98. * key is garbage collected
  99. */
  100. public ReferenceMap(int keyType, int valueType, boolean purgeValues) {
  101. super(keyType, valueType, DEFAULT_CAPACITY, DEFAULT_LOAD_FACTOR, purgeValues);
  102. }
  103. /**
  104. * Constructs a new <code>ReferenceMap</code> with the
  105. * specified reference types, load factor and initial
  106. * capacity.
  107. *
  108. * @param keyType the type of reference to use for keys;
  109. * must be {@link #HARD}, {@link #SOFT}, {@link #WEAK}
  110. * @param valueType the type of reference to use for values;
  111. * must be {@link #HARD}, {@link #SOFT}, {@link #WEAK}
  112. * @param capacity the initial capacity for the map
  113. * @param loadFactor the load factor for the map
  114. */
  115. public ReferenceMap(int keyType, int valueType, int capacity, float loadFactor) {
  116. super(keyType, valueType, capacity, loadFactor, false);
  117. }
  118. /**
  119. * Constructs a new <code>ReferenceMap</code> with the
  120. * specified reference types, load factor and initial
  121. * capacity.
  122. *
  123. * @param keyType the type of reference to use for keys;
  124. * must be {@link #HARD}, {@link #SOFT}, {@link #WEAK}
  125. * @param valueType the type of reference to use for values;
  126. * must be {@link #HARD}, {@link #SOFT}, {@link #WEAK}
  127. * @param capacity the initial capacity for the map
  128. * @param loadFactor the load factor for the map
  129. * @param purgeValues should the value be automatically purged when the
  130. * key is garbage collected
  131. */
  132. public ReferenceMap(int keyType, int valueType, int capacity,
  133. float loadFactor, boolean purgeValues) {
  134. super(keyType, valueType, capacity, loadFactor, purgeValues);
  135. }
  136. //-----------------------------------------------------------------------
  137. /**
  138. * Write the map out using a custom routine.
  139. */
  140. private void writeObject(ObjectOutputStream out) throws IOException {
  141. out.defaultWriteObject();
  142. doWriteObject(out);
  143. }
  144. /**
  145. * Read the map in using a custom routine.
  146. */
  147. private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
  148. in.defaultReadObject();
  149. doReadObject(in);
  150. }
  151. }