
org.intermine.util.CacheMap Maven / Gradle / Ivy
package org.intermine.util;
/*
* Copyright (C) 2002-2022 FlyMine
*
* This code may be freely distributed and modified under the
* terms of the GNU Lesser General Public Licence. This should
* be distributed with the code. See the LICENSE file for more
* information or http://www.gnu.org/copyleft/lesser.html.
*
*/
import java.lang.ref.Reference;
import java.util.HashMap;
import java.util.Map;
/**
* This is a Map implementation designed specifically for people intending to create a cache.
* The keys are held strongly, but the values are held softly, so the values can be
* garbage-collected. When an entry is garbage-collected, its key is removed from the Map on
* the next Map activity.
*
* The entrySet() and values() methods of this class do not work.
*
* @see java.lang.ref.SoftReference
* @param the type of keys maintained by this map
* @param the type of mapped values
* @author Matthew Wakeling
*/
public class CacheMap extends SoftReferenceMap
{
/**
* Constructs a new, empty CacheMap with the given initial
* capacity and the given load factor.
*
* @param initialCapacity The initial capacity of the CacheMap
* @param loadFactor The load factor of the CacheMap
* @throws IllegalArgumentException If the initial capacity is negative,
* or if the load factor is nonpositive.
*/
public CacheMap(int initialCapacity, float loadFactor) {
subMap = new HashMap>(initialCapacity, loadFactor);
this.name = "unknown";
}
/**
* Constructs a new, empty CacheMap with the given initial
* capacity and the default load factor, which is 0.75.
*
* @param initialCapacity The initial capacity of the CacheMap
* @throws IllegalArgumentException If the initial capacity is negative.
*/
public CacheMap(int initialCapacity) {
subMap = new HashMap>(initialCapacity);
this.name = "unknown";
}
/**
* Constructs a new, empty CacheMap with the default initial
* capacity (16) and the default load factor (0.75).
*/
public CacheMap() {
subMap = new HashMap>();
this.name = "unknown";
}
/**
* Constructs a new, empty CacheMap with the default initial
* capacity (16) and the default load factor (0.75), and a name.
*
* @param name the name of the CacheMap - printed out in log messages
*/
public CacheMap(String name) {
subMap = new HashMap>();
this.name = name;
}
/**
* Constructs a new CacheMap with the same mappings as the
* specified Map. The CacheMap is created with
* default load factor, which is 0.75 and an initial capacity
* sufficient to hold the mappings in the specified Map.
*
* @param t the map whose mappings are to be placed in this map.
* @throws NullPointerException if the specified map is null.
*/
public CacheMap(Map t) {
subMap = new HashMap>();
this.name = "unknown";
putAll(t);
}
}