View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.collections4.map;
18  
19  import java.io.IOException;
20  import java.io.ObjectInputStream;
21  import java.io.ObjectOutputStream;
22  import java.io.Serializable;
23  
24  /**
25   * A {@code Map} implementation that allows mappings to be
26   * removed by the garbage collector.
27   * <p>
28   * When you construct a {@code ReferenceMap}, you can specify what kind
29   * of references are used to store the map's keys and values.
30   * If non-hard references are used, then the garbage collector can remove
31   * mappings if a key or value becomes unreachable, or if the JVM's memory is
32   * running low. For information on how the different reference types behave,
33   * see {@link java.lang.ref.Reference Reference}.
34   * </p>
35   * <p>
36   * Different types of references can be specified for keys and values.
37   * The keys can be configured to be weak but the values hard,
38   * in which case this class will behave like a
39   * <a href="https://docs.oracle.com/javase/8/docs/api/java/util/WeakHashMap.html">
40   * {@code WeakHashMap}</a>. However, you can also specify hard keys and
41   * weak values, or any other combination. The default constructor uses
42   * hard keys and soft values, providing a memory-sensitive cache.
43   * </p>
44   * <p>
45   * This map is similar to
46   * {@link org.apache.commons.collections4.map.ReferenceIdentityMap ReferenceIdentityMap}.
47   * It differs in that keys and values in this class are compared using {@code equals()}.
48   * </p>
49   * <p>
50   * This {@link java.util.Map Map} implementation does <em>not</em> allow null elements.
51   * Attempting to add a null key or value to the map will raise a {@code NullPointerException}.
52   * </p>
53   * <p>
54   * This implementation is not synchronized.
55   * You can use {@link java.util.Collections#synchronizedMap} to
56   * provide synchronized access to a {@code ReferenceMap}.
57   * Remember that synchronization will not stop the garbage collector removing entries.
58   * </p>
59   * <p>
60   * All the available iterators can be reset back to the start by casting to
61   * {@code ResettableIterator} and calling {@code reset()}.
62   * </p>
63   * <p>
64   * <strong>Note that ReferenceMap is not synchronized and is not thread-safe.</strong>
65   * If you wish to use this map from multiple threads concurrently, you must use
66   * appropriate synchronization. The simplest approach is to wrap this map
67   * using {@link java.util.Collections#synchronizedMap}. This class may throw
68   * exceptions when accessed by concurrent threads without synchronization.
69   * </p>
70   * <p>
71   * NOTE: As from Commons Collections 3.1 this map extends {@code AbstractReferenceMap}
72   * (previously it extended AbstractMap). As a result, the implementation is now
73   * extensible and provides a {@code MapIterator}.
74   * </p>
75   *
76   * @param <K> the type of the keys in the map
77   * @param <V> the type of the values in the map
78   * @see java.lang.ref.Reference
79   * @since 3.0 (previously in main package v2.1)
80   */
81  public class ReferenceMap<K, V> extends AbstractReferenceMap<K, V> implements Serializable {
82  
83      /** Serialization version */
84      private static final long serialVersionUID = 1555089888138299607L;
85  
86      /**
87       * Constructs a new {@code ReferenceMap} that will
88       * use hard references to keys and soft references to values.
89       */
90      public ReferenceMap() {
91          super(ReferenceStrength.HARD, ReferenceStrength.SOFT, DEFAULT_CAPACITY,
92                  DEFAULT_LOAD_FACTOR, false);
93      }
94  
95      /**
96       * Constructs a new {@code ReferenceMap} that will
97       * use the specified types of references.
98       *
99       * @param keyType  the type of reference to use for keys;
100      *   must be {@link AbstractReferenceMap.ReferenceStrength#HARD HARD},
101      *   {@link AbstractReferenceMap.ReferenceStrength#SOFT SOFT},
102      *   {@link AbstractReferenceMap.ReferenceStrength#WEAK WEAK}
103      * @param valueType  the type of reference to use for values;
104      *   must be {@link AbstractReferenceMap.ReferenceStrength#HARD HARD},
105      *   {@link AbstractReferenceMap.ReferenceStrength#SOFT SOFT},
106      *   {@link AbstractReferenceMap.ReferenceStrength#WEAK WEAK}
107      */
108     public ReferenceMap(final ReferenceStrength keyType, final ReferenceStrength valueType) {
109         super(keyType, valueType, DEFAULT_CAPACITY, DEFAULT_LOAD_FACTOR, false);
110     }
111 
112     /**
113      * Constructs a new {@code ReferenceMap} that will
114      * use the specified types of references.
115      *
116      * @param keyType  the type of reference to use for keys;
117      *   must be {@link AbstractReferenceMap.ReferenceStrength#HARD HARD},
118      *   {@link AbstractReferenceMap.ReferenceStrength#SOFT SOFT},
119      *   {@link AbstractReferenceMap.ReferenceStrength#WEAK WEAK}
120      * @param valueType  the type of reference to use for values;
121      *   must be {@link AbstractReferenceMap.ReferenceStrength#HARD HARD},
122      *   {@link AbstractReferenceMap.ReferenceStrength#SOFT SOFT},
123      *   {@link AbstractReferenceMap.ReferenceStrength#WEAK WEAK}
124      * @param purgeValues should the value be automatically purged when the
125      *   key is garbage collected
126      */
127     public ReferenceMap(final ReferenceStrength keyType, final ReferenceStrength valueType, final boolean purgeValues) {
128         super(keyType, valueType, DEFAULT_CAPACITY, DEFAULT_LOAD_FACTOR, purgeValues);
129     }
130 
131     /**
132      * Constructs a new {@code ReferenceMap} with the
133      * specified reference types, load factor and initial
134      * capacity.
135      *
136      * @param keyType  the type of reference to use for keys;
137      *   must be {@link AbstractReferenceMap.ReferenceStrength#HARD HARD},
138      *   {@link AbstractReferenceMap.ReferenceStrength#SOFT SOFT},
139      *   {@link AbstractReferenceMap.ReferenceStrength#WEAK WEAK}
140      * @param valueType  the type of reference to use for values;
141      *   must be {@link AbstractReferenceMap.ReferenceStrength#HARD HARD},
142      *   {@link AbstractReferenceMap.ReferenceStrength#SOFT SOFT},
143      *   {@link AbstractReferenceMap.ReferenceStrength#WEAK WEAK}
144      * @param capacity  the initial capacity for the map
145      * @param loadFactor  the load factor for the map
146      */
147     public ReferenceMap(final ReferenceStrength keyType, final ReferenceStrength valueType, final int capacity,
148             final float loadFactor) {
149         super(keyType, valueType, capacity, loadFactor, false);
150     }
151 
152     /**
153      * Constructs a new {@code ReferenceMap} with the
154      * specified reference types, load factor and initial
155      * capacity.
156      *
157      * @param keyType  the type of reference to use for keys;
158      *   must be {@link AbstractReferenceMap.ReferenceStrength#HARD HARD},
159      *   {@link AbstractReferenceMap.ReferenceStrength#SOFT SOFT},
160      *   {@link AbstractReferenceMap.ReferenceStrength#WEAK WEAK}
161      * @param valueType  the type of reference to use for values;
162      *   must be {@link AbstractReferenceMap.ReferenceStrength#HARD HARD},
163      *   {@link AbstractReferenceMap.ReferenceStrength#SOFT SOFT},
164      *   {@link AbstractReferenceMap.ReferenceStrength#WEAK WEAK}
165      * @param capacity  the initial capacity for the map
166      * @param loadFactor  the load factor for the map
167      * @param purgeValues  should the value be automatically purged when the
168      *   key is garbage collected
169      */
170     public ReferenceMap(final ReferenceStrength keyType, final ReferenceStrength valueType, final int capacity,
171             final float loadFactor, final boolean purgeValues) {
172         super(keyType, valueType, capacity, loadFactor, purgeValues);
173     }
174 
175     /**
176      * Deserializes the map in using a custom routine.
177      *
178      * @param in the input stream
179      * @throws IOException if an error occurs while reading from the stream
180      * @throws ClassNotFoundException if an object read from the stream cannot be loaded
181      */
182     private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException {
183         in.defaultReadObject();
184         doReadObject(in);
185     }
186 
187     /**
188      * Serializes this object to an ObjectOutputStream.
189      *
190      * @param out the target ObjectOutputStream.
191      * @throws IOException thrown when an I/O errors occur writing to the target stream.
192      */
193     private void writeObject(final ObjectOutputStream out) throws IOException {
194         out.defaultWriteObject();
195         doWriteObject(out);
196     }
197 
198 }