ReferenceMap.java

  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. import java.io.IOException;
  19. import java.io.ObjectInputStream;
  20. import java.io.ObjectOutputStream;
  21. import java.io.Serializable;

  22. /**
  23.  * A {@code Map} implementation that allows mappings to be
  24.  * removed by the garbage collector.
  25.  * <p>
  26.  * When you construct a {@code ReferenceMap}, you can specify what kind
  27.  * of references are used to store the map's keys and values.
  28.  * If non-hard references are used, then the garbage collector can remove
  29.  * mappings if a key or value becomes unreachable, or if the JVM's memory is
  30.  * running low. For information on how the different reference types behave,
  31.  * see {@link java.lang.ref.Reference Reference}.
  32.  * </p>
  33.  * <p>
  34.  * Different types of references can be specified for keys and values.
  35.  * The keys can be configured to be weak but the values hard,
  36.  * in which case this class will behave like a
  37.  * <a href="https://docs.oracle.com/javase/8/docs/api/java/util/WeakHashMap.html">
  38.  * {@code WeakHashMap}</a>. However, you can also specify hard keys and
  39.  * weak values, or any other combination. The default constructor uses
  40.  * hard keys and soft values, providing a memory-sensitive cache.
  41.  * </p>
  42.  * <p>
  43.  * This map is similar to
  44.  * {@link org.apache.commons.collections4.map.ReferenceIdentityMap ReferenceIdentityMap}.
  45.  * It differs in that keys and values in this class are compared using {@code equals()}.
  46.  * </p>
  47.  * <p>
  48.  * This {@link java.util.Map Map} implementation does <em>not</em> allow null elements.
  49.  * Attempting to add a null key or value to the map will raise a {@code NullPointerException}.
  50.  * </p>
  51.  * <p>
  52.  * This implementation is not synchronized.
  53.  * You can use {@link java.util.Collections#synchronizedMap} to
  54.  * provide synchronized access to a {@code ReferenceMap}.
  55.  * Remember that synchronization will not stop the garbage collector removing entries.
  56.  * </p>
  57.  * <p>
  58.  * All the available iterators can be reset back to the start by casting to
  59.  * {@code ResettableIterator} and calling {@code reset()}.
  60.  * </p>
  61.  * <p>
  62.  * <strong>Note that ReferenceMap is not synchronized and is not thread-safe.</strong>
  63.  * If you wish to use this map from multiple threads concurrently, you must use
  64.  * appropriate synchronization. The simplest approach is to wrap this map
  65.  * using {@link java.util.Collections#synchronizedMap}. This class may throw
  66.  * exceptions when accessed by concurrent threads without synchronization.
  67.  * </p>
  68.  * <p>
  69.  * NOTE: As from Commons Collections 3.1 this map extends {@code AbstractReferenceMap}
  70.  * (previously it extended AbstractMap). As a result, the implementation is now
  71.  * extensible and provides a {@code MapIterator}.
  72.  * </p>
  73.  *
  74.  * @param <K> the type of the keys in the map
  75.  * @param <V> the type of the values in the map
  76.  * @see java.lang.ref.Reference
  77.  * @since 3.0 (previously in main package v2.1)
  78.  */
  79. public class ReferenceMap<K, V> extends AbstractReferenceMap<K, V> implements Serializable {

  80.     /** Serialization version */
  81.     private static final long serialVersionUID = 1555089888138299607L;

  82.     /**
  83.      * Constructs a new {@code ReferenceMap} that will
  84.      * use hard references to keys and soft references to values.
  85.      */
  86.     public ReferenceMap() {
  87.         super(ReferenceStrength.HARD, ReferenceStrength.SOFT, DEFAULT_CAPACITY,
  88.                 DEFAULT_LOAD_FACTOR, false);
  89.     }

  90.     /**
  91.      * Constructs a new {@code ReferenceMap} that will
  92.      * use the specified types of references.
  93.      *
  94.      * @param keyType  the type of reference to use for keys;
  95.      *   must be {@link AbstractReferenceMap.ReferenceStrength#HARD HARD},
  96.      *   {@link AbstractReferenceMap.ReferenceStrength#SOFT SOFT},
  97.      *   {@link AbstractReferenceMap.ReferenceStrength#WEAK WEAK}
  98.      * @param valueType  the type of reference to use for values;
  99.      *   must be {@link AbstractReferenceMap.ReferenceStrength#HARD HARD},
  100.      *   {@link AbstractReferenceMap.ReferenceStrength#SOFT SOFT},
  101.      *   {@link AbstractReferenceMap.ReferenceStrength#WEAK WEAK}
  102.      */
  103.     public ReferenceMap(final ReferenceStrength keyType, final ReferenceStrength valueType) {
  104.         super(keyType, valueType, DEFAULT_CAPACITY, DEFAULT_LOAD_FACTOR, false);
  105.     }

  106.     /**
  107.      * Constructs a new {@code ReferenceMap} that will
  108.      * use the specified types of references.
  109.      *
  110.      * @param keyType  the type of reference to use for keys;
  111.      *   must be {@link AbstractReferenceMap.ReferenceStrength#HARD HARD},
  112.      *   {@link AbstractReferenceMap.ReferenceStrength#SOFT SOFT},
  113.      *   {@link AbstractReferenceMap.ReferenceStrength#WEAK WEAK}
  114.      * @param valueType  the type of reference to use for values;
  115.      *   must be {@link AbstractReferenceMap.ReferenceStrength#HARD HARD},
  116.      *   {@link AbstractReferenceMap.ReferenceStrength#SOFT SOFT},
  117.      *   {@link AbstractReferenceMap.ReferenceStrength#WEAK WEAK}
  118.      * @param purgeValues should the value be automatically purged when the
  119.      *   key is garbage collected
  120.      */
  121.     public ReferenceMap(final ReferenceStrength keyType, final ReferenceStrength valueType, final boolean purgeValues) {
  122.         super(keyType, valueType, DEFAULT_CAPACITY, DEFAULT_LOAD_FACTOR, purgeValues);
  123.     }

  124.     /**
  125.      * Constructs a new {@code ReferenceMap} with the
  126.      * specified reference types, load factor and initial
  127.      * capacity.
  128.      *
  129.      * @param keyType  the type of reference to use for keys;
  130.      *   must be {@link AbstractReferenceMap.ReferenceStrength#HARD HARD},
  131.      *   {@link AbstractReferenceMap.ReferenceStrength#SOFT SOFT},
  132.      *   {@link AbstractReferenceMap.ReferenceStrength#WEAK WEAK}
  133.      * @param valueType  the type of reference to use for values;
  134.      *   must be {@link AbstractReferenceMap.ReferenceStrength#HARD HARD},
  135.      *   {@link AbstractReferenceMap.ReferenceStrength#SOFT SOFT},
  136.      *   {@link AbstractReferenceMap.ReferenceStrength#WEAK WEAK}
  137.      * @param capacity  the initial capacity for the map
  138.      * @param loadFactor  the load factor for the map
  139.      */
  140.     public ReferenceMap(final ReferenceStrength keyType, final ReferenceStrength valueType, final int capacity,
  141.             final float loadFactor) {
  142.         super(keyType, valueType, capacity, loadFactor, false);
  143.     }

  144.     /**
  145.      * Constructs a new {@code ReferenceMap} with the
  146.      * specified reference types, load factor and initial
  147.      * capacity.
  148.      *
  149.      * @param keyType  the type of reference to use for keys;
  150.      *   must be {@link AbstractReferenceMap.ReferenceStrength#HARD HARD},
  151.      *   {@link AbstractReferenceMap.ReferenceStrength#SOFT SOFT},
  152.      *   {@link AbstractReferenceMap.ReferenceStrength#WEAK WEAK}
  153.      * @param valueType  the type of reference to use for values;
  154.      *   must be {@link AbstractReferenceMap.ReferenceStrength#HARD HARD},
  155.      *   {@link AbstractReferenceMap.ReferenceStrength#SOFT SOFT},
  156.      *   {@link AbstractReferenceMap.ReferenceStrength#WEAK WEAK}
  157.      * @param capacity  the initial capacity for the map
  158.      * @param loadFactor  the load factor for the map
  159.      * @param purgeValues  should the value be automatically purged when the
  160.      *   key is garbage collected
  161.      */
  162.     public ReferenceMap(final ReferenceStrength keyType, final ReferenceStrength valueType, final int capacity,
  163.             final float loadFactor, final boolean purgeValues) {
  164.         super(keyType, valueType, capacity, loadFactor, purgeValues);
  165.     }

  166.     /**
  167.      * Deserializes the map in using a custom routine.
  168.      *
  169.      * @param in the input stream
  170.      * @throws IOException if an error occurs while reading from the stream
  171.      * @throws ClassNotFoundException if an object read from the stream cannot be loaded
  172.      */
  173.     private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException {
  174.         in.defaultReadObject();
  175.         doReadObject(in);
  176.     }

  177.     /**
  178.      * Serializes this object to an ObjectOutputStream.
  179.      *
  180.      * @param out the target ObjectOutputStream.
  181.      * @throws IOException thrown when an I/O errors occur writing to the target stream.
  182.      */
  183.     private void writeObject(final ObjectOutputStream out) throws IOException {
  184.         out.defaultWriteObject();
  185.         doWriteObject(out);
  186.     }

  187. }