public class WeakHashMap extends AbstractMap implements Map, Rollbackable
A weak hash map makes most sense when the keys doesn't override the
equals
method: If there is no other reference to the
key nobody can ever look up the key in this table and so the entry
can be removed. This table also works when the equals
method is overloaded, such as String keys, but you should be prepared
to deal with some entries disappearing spontaneously.
Other strange behaviors to be aware of: The size of this map may
spontaneously shrink (even if you use a synchronized map and synchronize
it); it behaves as if another thread removes entries from this table
without synchronization. The entry set returned by entrySet
has similar phenomenons: The size may spontaneously shrink, or an
entry, that was in the set before, suddenly disappears.
A weak hash map is not meant for caches; use a normal map, with soft references as values instead, or try {
HashMap
,
WeakReference
,
LinkedHashMap
$CHECKPOINT, $RECORD$$CHECKPOINT
Constructor and Description |
---|
WeakHashMap()
Creates a new weak hash map with default load factor and default
capacity.
|
WeakHashMap(int initialCapacity)
Creates a new weak hash map with default load factor and the given
capacity.
|
WeakHashMap(int initialCapacity,
float loadFactor)
Creates a new weak hash map with the given initial capacity and
load factor.
|
WeakHashMap(Map m)
Construct a new WeakHashMap with the same mappings as the given map.
|
Modifier and Type | Method and Description |
---|---|
void |
$COMMIT(long timestamp)
Commit changes up to the given timestamp, but not including changes
made at timestamp and afterward.
|
void |
$RESTORE(long timestamp,
boolean trim)
Restore a previous state to all the private fields of this rollbackable
object.
|
void |
clear()
Clears all entries from this map.
|
boolean |
containsKey(java.lang.Object key)
Tells if the map contains the given key.
|
boolean |
containsValue(java.lang.Object value)
Returns true if the map contains at least one key which points to
the specified object as a value.
|
Set |
entrySet()
Returns a set representation of the entries in this map.
|
java.lang.Object |
get(java.lang.Object key)
Gets the value the key is mapped to.
|
boolean |
isEmpty()
Tells if the map is empty.
|
Set |
keySet()
Returns a set representation of the keys in this map.
|
java.lang.Object |
put(java.lang.Object key,
java.lang.Object value)
Adds a new key/value mapping to this map.
|
void |
putAll(Map m)
Puts all of the mappings from the given map into this one.
|
java.lang.Object |
remove(java.lang.Object key)
Removes the key and the corresponding value from this map.
|
int |
size()
Returns the size of this hash map.
|
Collection |
values()
Returns a collection representation of the values in this map.
|
$GET$CHECKPOINT, $SET$CHECKPOINT, clone, equals, hashCode, toString
finalize, getClass, notify, notifyAll, wait, wait, wait
$GET$CHECKPOINT, $SET$CHECKPOINT, equals, hashCode
public WeakHashMap()
public WeakHashMap(int initialCapacity)
initialCapacity
- the initial capacityjava.lang.IllegalArgumentException
- if initialCapacity is negativepublic WeakHashMap(int initialCapacity, float loadFactor)
initialCapacity
- the initial capacity.loadFactor
- the load factor (see class description of HashMap).java.lang.IllegalArgumentException
- if initialCapacity is negative, or
loadFactor is non-positivepublic WeakHashMap(Map m)
m
- the map to copyjava.lang.NullPointerException
- if m is nullpublic int size()
size
in interface Map
size
in class AbstractMap
Set.size()
public boolean isEmpty()
isEmpty
in interface Map
isEmpty
in class AbstractMap
AbstractMap.size()
public boolean containsKey(java.lang.Object key)
containsKey
in interface Map
containsKey
in class AbstractMap
key
- the key to look forAbstractMap.containsValue(Object)
public java.lang.Object get(java.lang.Object key)
get
in interface Map
get
in class AbstractMap
key
- the key to look upAbstractMap.containsKey(Object)
public java.lang.Object put(java.lang.Object key, java.lang.Object value)
put
in interface Map
put
in class AbstractMap
key
- the key, may be nullvalue
- the value, may be nullAbstractMap.containsKey(Object)
public java.lang.Object remove(java.lang.Object key)
remove
in interface Map
remove
in class AbstractMap
key
- the key. This may be null.Iterator.remove()
public Set entrySet()
entrySet
in interface Map
entrySet
in class AbstractMap
Map.Entry
public void clear()
clear
in interface Map
clear
in class AbstractMap
Set.clear()
public boolean containsValue(java.lang.Object value)
containsValue
in interface Map
containsValue
in class AbstractMap
value
- the value to search forAbstractMap.containsKey(Object)
public Set keySet()
keySet
in interface Map
keySet
in class AbstractMap
Set.iterator()
,
AbstractMap.size()
,
AbstractMap.containsKey(Object)
,
AbstractMap.values()
public void putAll(Map m)
putAll
in interface Map
putAll
in class AbstractMap
m
- the map to copy inAbstractMap.put(Object, Object)
public Collection values()
values
in interface Map
values
in class AbstractMap
Collection.iterator()
,
AbstractMap.size()
,
AbstractMap.containsValue(Object)
,
AbstractMap.keySet()
public void $COMMIT(long timestamp)
Rollbackable
$COMMIT
in interface Rollbackable
$COMMIT
in interface Map
$COMMIT
in class AbstractMap
timestamp
- The timestamp.public void $RESTORE(long timestamp, boolean trim)
Rollbackable
$RESTORE
in interface Rollbackable
$RESTORE
in interface Map
$RESTORE
in class AbstractMap
timestamp
- The timestamp taken at the time when the previous
state was recorded.trim
- Whether to delete the records used for the rollback.Checkpoint.rollback(long, boolean)