public final class CachedMap extends Object implements Map
This class provides cache access to Map
collections.
Instance of this class can be used as "proxy" for any collection
implementing the java.util.Map
interface.
Typically, CachedMap
are used to accelerate access to
large collections when the access to the collection is not evenly
distributed (associative cache). The performance gain is about
50% for the fastest hash map collections (e.g. FastMap
).
For slower collections such as java.util.TreeMap
,
non-resizable FastMap
(real-time) or database access,
performance can be of several orders of magnitude.
Note: The keys used to access elements of a CachedMap
do
not need to be immutable as they are not stored in the cache
(only keys specified by the put(java.lang.Object, java.lang.Object)
method are).
In other words, access can be performed using mutable keys as long
as these keys can be compared for equality with the real map's keys
(e.g. same hashCode
values).
This implementation is not synchronized. Multiple threads accessing or modifying the collection must be synchronized externally.
This class is public domain (not copyrighted).
Constructor and Description |
---|
CachedMap()
Creates a cached map backed by a
FastMap . |
CachedMap(int cacheSize)
Creates a cached map backed by a
FastMap and having the
specified cache size. |
CachedMap(int cacheSize,
Map backingMap)
Creates a cached map backed by the specified map and having the specified
cache size.
|
Modifier and Type | Method and Description |
---|---|
void |
clear()
Removes all mappings from this map (optional operation).
|
boolean |
containsKey(Object key)
Indicates if this map contains a mapping for the specified key.
|
boolean |
containsValue(Object value)
Returns
true if this map maps one or more keys to the
specified value. |
Set |
entrySet()
Returns an unmodifiable view of the mappings contained in this
map.
|
boolean |
equals(Object o)
Compares the specified object with this map for equality.
|
void |
flush()
Flushes the key/value pairs being cached.
|
Object |
get(Object key)
Returns the value to which this map maps the specified key.
|
Map |
getBackingMap()
Returns the backing map.
|
int |
getCacheSize()
Returns the actual cache size.
|
int |
hashCode()
Returns the hash code value for this map.
|
boolean |
isEmpty()
Returns
true if this map contains no key-value mappings. |
Set |
keySet()
Returns an unmodifiable view of the keys contained in this
map.
|
Object |
put(Object key,
Object value)
Associates the specified value with the specified key in this map.
|
void |
putAll(Map map)
Copies all of the mappings from the specified map to this map
(optional operation).
|
Object |
remove(Object key)
Removes the mapping for this key from this map if it is present.
|
int |
size()
Returns the number of key-value mappings in this map.
|
Collection |
values()
Returns an unmodifiable view of the values contained in this map.
|
public CachedMap()
FastMap
.
The default cache size and map capacity is set to 256
entries.public CachedMap(int cacheSize)
FastMap
and having the
specified cache size.cacheSize
- the cache size, the actual cache size is the
first power of 2 greater or equal to cacheSize
.
This is also the initial capacity of the backing map.public CachedMap(int cacheSize, Map backingMap)
CachedMap
instance; otherwise flush()
has to be called.cacheSize
- the cache size, the actual cache size is the
first power of 2 greater or equal to cacheSize
.backingMap
- the backing map to be "wrapped" in a cached map.public int getCacheSize()
public Map getBackingMap()
CachedMap
has to be flushed.flush()
public void flush()
public Object get(Object key)
get
in interface Map
key
- the key whose associated value is to be returned.null
if the map contains no mapping for this key.ClassCastException
- if the key is of an inappropriate type for
the backing map (optional).NullPointerException
- if the key is null
.public Object put(Object key, Object value)
put
in interface Map
key
- the key with which the specified value is to be associated.value
- the value to be associated with the specified key.null
if there was no mapping for the key.UnsupportedOperationException
- if the put
operation
is not supported by the backing map.ClassCastException
- if the class of the specified key or value
prevents it from being stored in this map.IllegalArgumentException
- if some aspect of this key or value
prevents it from being stored in this map.NullPointerException
- if the key is null
.public Object remove(Object key)
remove
in interface Map
key
- key whose mapping is to be removed from the map.null
if there was no mapping for key.ClassCastException
- if the key is of an inappropriate type for
the backing map (optional).NullPointerException
- if the key is null
.UnsupportedOperationException
- if the remove
method
is not supported by the backing map.public boolean containsKey(Object key)
containsKey
in interface Map
key
- the key whose presence in this map is to be tested.true
if this map contains a mapping for the
specified key; false
otherwise.public int size()
Integer.MAX_VALUE
elements,
returns Integer.MAX_VALUE
.public boolean isEmpty()
true
if this map contains no key-value mappings.public boolean containsValue(Object value)
true
if this map maps one or more keys to the
specified value.containsValue
in interface Map
value
- value whose presence in this map is to be tested.true
if this map maps one or more keys to the
specified value.ClassCastException
- if the value is of an inappropriate type for
the backing map (optional).NullPointerException
- if the value is null
and the
backing map does not not permit null
values.public void putAll(Map map)
putAll
in interface Map
map
- the mappings to be stored in this map.UnsupportedOperationException
- if the putAll
method
is not supported by the backing map.ClassCastException
- if the class of a key or value in the
specified map prevents it from being stored in this map.IllegalArgumentException
- some aspect of a key or value in the
specified map prevents it from being stored in this map.NullPointerException
- the specified map is null
, or
if the backing map does not permit null
keys or
values, and the specified map contains null
keys or
values.public void clear()
clear
in interface Map
UnsupportedOperationException
- if clear is not supported by the
backing map.public Set keySet()
public Collection values()
public Set entrySet()
Map.Entry
.public boolean equals(Object o)
Copyright © 2001-2014 Codehaus. All Rights Reserved.