public class ReferenceMap
extends java.util.AbstractMap
Map
implementation that allows
mappings to be removed by the garbage collector.
When you construct a ReferenceMap
, you can
specify what kind of references are used to store the
map's keys and values. If non-hard references are
used, then the garbage collector can remove mappings
if a key or value becomes unreachable, or if the
JVM's memory is running low. For information on how
the different reference types behave, see
Reference
.
Different types of references can be specified for keys
and values. The keys can be configured to be weak but
the values hard, in which case this class will behave
like a
WeakHashMap
. However, you
can also specify hard keys and weak values, or any other
combination. The default constructor uses hard keys
and soft values, providing a memory-sensitive cache.
The algorithms used are basically the same as those
in HashMap
. In particular, you
can specify a load factor and capacity to suit your
needs. All optional Map
operations are
supported.
However, this Map
implementation does not
allow null elements. Attempting to add a null key or
or a null value to the map will raise a
NullPointerException
.
As usual, this implementation is not synchronized. You
can use Collections.synchronizedMap(java.util.Map<K, V>)
to
provide synchronized access to a ReferenceMap
.
Reference
Modifier and Type | Class and Description |
---|---|
private class |
ReferenceMap.Entry
Deprecated.
|
private class |
ReferenceMap.EntryIterator
Deprecated.
|
private class |
ReferenceMap.KeyIterator
Deprecated.
|
private static class |
ReferenceMap.SoftRef
Deprecated.
|
private class |
ReferenceMap.ValueIterator
Deprecated.
|
private static class |
ReferenceMap.WeakRef
Deprecated.
|
Modifier and Type | Field and Description |
---|---|
private java.util.Set |
entrySet
Deprecated.
Cached entry set.
|
static int |
HARD
Deprecated.
Constant indicating that hard references should be used.
|
private java.util.Set |
keySet
Deprecated.
Cached key set.
|
private int |
keyType
Deprecated.
The reference type for keys.
|
private float |
loadFactor
Deprecated.
The threshold variable is calculated by multiplying
table.length and loadFactor.
|
private int |
modCount
Deprecated.
Number of times this map has been modified.
|
private boolean |
purgeValues
Deprecated.
Should the value be automatically purged when the associated key has been collected?
|
private java.lang.ref.ReferenceQueue |
queue
Deprecated.
ReferenceQueue used to eliminate stale mappings.
|
private static long |
serialVersionUID
Deprecated.
For serialization.
|
private int |
size
Deprecated.
Number of mappings in this map.
|
static int |
SOFT
Deprecated.
Constant indicating that soft references should be used.
|
private ReferenceMap.Entry[] |
table
Deprecated.
The hash table.
|
private int |
threshold
Deprecated.
When size reaches threshold, the map is resized.
|
private java.util.Collection |
values
Deprecated.
Cached values.
|
private int |
valueType
Deprecated.
The reference type for values.
|
static int |
WEAK
Deprecated.
Constant indicating that weak references should be used.
|
Constructor and Description |
---|
ReferenceMap()
Deprecated.
Constructs a new
ReferenceMap that will
use hard references to keys and soft references to values. |
ReferenceMap(int keyType,
int valueType)
Deprecated.
Constructs a new
ReferenceMap that will
use the specified types of references. |
ReferenceMap(int keyType,
int valueType,
boolean purgeValues)
Deprecated.
Constructs a new
ReferenceMap that will
use the specified types of references. |
ReferenceMap(int keyType,
int valueType,
int capacity,
float loadFactor)
Deprecated.
Constructs a new
ReferenceMap with the
specified reference types, load factor and initial
capacity. |
ReferenceMap(int keyType,
int valueType,
int capacity,
float loadFactor,
boolean purgeValues)
Deprecated.
Constructs a new
ReferenceMap with the
specified reference types, load factor and initial
capacity. |
Modifier and Type | Method and Description |
---|---|
void |
clear()
Deprecated.
Clears this map.
|
boolean |
containsKey(java.lang.Object key)
Deprecated.
Returns
true if this map contains the given key. |
java.util.Set |
entrySet()
Deprecated.
Returns a set view of this map's entries.
|
java.lang.Object |
get(java.lang.Object key)
Deprecated.
Returns the value associated with the given key, if any.
|
private ReferenceMap.Entry |
getEntry(java.lang.Object key)
Deprecated.
Returns the entry associated with the given key.
|
private int |
indexFor(int hash)
Deprecated.
Converts the given hash code into an index into the
hash table.
|
boolean |
isEmpty()
Deprecated.
Returns
true if this map is empty. |
java.util.Set |
keySet()
Deprecated.
Returns a set view of this map's keys.
|
private void |
purge()
Deprecated.
Purges stale mappings from this map.
|
private void |
purge(java.lang.ref.Reference ref)
Deprecated.
|
java.lang.Object |
put(java.lang.Object key,
java.lang.Object value)
Deprecated.
Associates the given key with the given value.
|
private void |
readObject(java.io.ObjectInputStream inp)
Deprecated.
Reads the contents of this object from the given input stream.
|
java.lang.Object |
remove(java.lang.Object key)
Deprecated.
Removes the key and its associated value from this map.
|
private void |
resize()
Deprecated.
Resizes this hash table by doubling its capacity.
|
int |
size()
Deprecated.
Returns the size of this map.
|
private java.lang.Object |
toReference(int type,
java.lang.Object referent,
int hash)
Deprecated.
Constructs a reference of the given type to the given
referent.
|
java.util.Collection |
values()
Deprecated.
Returns a collection view of this map's values.
|
private static void |
verify(java.lang.String name,
int type)
Deprecated.
|
private void |
writeObject(java.io.ObjectOutputStream out)
Deprecated.
Writes this object to the given output stream.
|
clone, containsValue, equals, hashCode, putAll, toString
private static final long serialVersionUID
public static final int HARD
public static final int SOFT
public static final int WEAK
private int keyType
private int valueType
private float loadFactor
private boolean purgeValues
private transient java.lang.ref.ReferenceQueue queue
private transient ReferenceMap.Entry[] table
private transient int size
private transient int threshold
private transient volatile int modCount
private transient java.util.Set keySet
private transient java.util.Set entrySet
private transient java.util.Collection values
public ReferenceMap()
ReferenceMap
that will
use hard references to keys and soft references to values.public ReferenceMap(int keyType, int valueType, boolean purgeValues)
ReferenceMap
that will
use the specified types of references.public ReferenceMap(int keyType, int valueType)
ReferenceMap
that will
use the specified types of references.public ReferenceMap(int keyType, int valueType, int capacity, float loadFactor, boolean purgeValues)
ReferenceMap
with the
specified reference types, load factor and initial
capacity.keyType
- the type of reference to use for keys;
must be HARD
, SOFT
, WEAK
valueType
- the type of reference to use for values;
must be HARD
, SOFT
, WEAK
capacity
- the initial capacity for the maploadFactor
- the load factor for the mappurgeValues
- should the value be automatically purged when the
key is garbage collectedpublic ReferenceMap(int keyType, int valueType, int capacity, float loadFactor)
ReferenceMap
with the
specified reference types, load factor and initial
capacity.private static void verify(java.lang.String name, int type)
private void writeObject(java.io.ObjectOutputStream out) throws java.io.IOException
out
- the output stream to write tojava.io.IOException
- if the stream raises itprivate void readObject(java.io.ObjectInputStream inp) throws java.io.IOException, java.lang.ClassNotFoundException
inp
- the input stream to read fromjava.io.IOException
- if the stream raises itjava.lang.ClassNotFoundException
- if the stream raises itprivate java.lang.Object toReference(int type, java.lang.Object referent, int hash)
type
- HARD, SOFT or WEAKreferent
- the object to refer tohash
- the hash code of the key of the mapping;
this number might be different from referent.hashCode() if
the referent represents a value and not a keyprivate ReferenceMap.Entry getEntry(java.lang.Object key)
key
- the key of the entry to look upprivate int indexFor(int hash)
private void resize()
private void purge()
Ordinarily, stale mappings are only removed during a write operation, although this method is called for both read and write operations to maintain a consistent state.
Note that this method is not synchronized! Special care must be taken if, for instance, you want stale mappings to be removed on a periodic basis by some background thread.
private void purge(java.lang.ref.Reference ref)
public int size()
size
in interface java.util.Map
size
in class java.util.AbstractMap
public boolean isEmpty()
true
if this map is empty.isEmpty
in interface java.util.Map
isEmpty
in class java.util.AbstractMap
true
if this map is emptypublic boolean containsKey(java.lang.Object key)
true
if this map contains the given key.containsKey
in interface java.util.Map
containsKey
in class java.util.AbstractMap
public java.lang.Object get(java.lang.Object key)
get
in interface java.util.Map
get
in class java.util.AbstractMap
null
if the key maps to no valuepublic java.lang.Object put(java.lang.Object key, java.lang.Object value)
Neither the key nor the value may be null.
put
in interface java.util.Map
put
in class java.util.AbstractMap
key
- the key of the mappingvalue
- the value of the mappingjava.lang.NullPointerException
- if either the key or value
is nullpublic java.lang.Object remove(java.lang.Object key)
remove
in interface java.util.Map
remove
in class java.util.AbstractMap
key
- the key to removepublic void clear()
clear
in interface java.util.Map
clear
in class java.util.AbstractMap
public java.util.Set entrySet()
entrySet
in interface java.util.Map
entrySet
in class java.util.AbstractMap
public java.util.Set keySet()
keySet
in interface java.util.Map
keySet
in class java.util.AbstractMap
public java.util.Collection values()
values
in interface java.util.Map
values
in class java.util.AbstractMap