|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object java.util.AbstractMap<K,V> net.sourceforge.rcache.BaseCache<K,V>
K
- the type of keys maintained by this cacheV
- the type of cached valuespublic abstract class BaseCache<K,V>
Memory-Sensitive Cache base implementation.
Thread-safety of this class will depend on the selected implementation for the internal map. If the convenience constructors are used, resulting instances will be thread-safe, unless a concurrency level of -1 (or less) is specified.
Nested Class Summary |
---|
Nested classes/interfaces inherited from class java.util.AbstractMap |
---|
java.util.AbstractMap.SimpleEntry<K,V>, java.util.AbstractMap.SimpleImmutableEntry<K,V> |
Nested classes/interfaces inherited from interface net.sourceforge.rcache.Cache |
---|
Cache.Operation |
Nested classes/interfaces inherited from interface java.util.Map |
---|
java.util.Map.Entry<K,V> |
Field Summary |
---|
Fields inherited from interface net.sourceforge.rcache.Cache |
---|
DEFAULT_CONCURRENCY_LEVEL, DEFAULT_INITIAL_CAPACITY, DEFAULT_LOAD_FACTOR |
Constructor Summary | |
---|---|
BaseCache(int initCapacity,
float loadFactor,
int concurrencyLevel)
Creates an instance with custom capacity and concurrency level. |
|
BaseCache(java.util.Map<K,KeyedReference<K,V>> map)
Advanced constructor. |
Method Summary | |
---|---|
void |
clear()
Removes all entries from this cache. |
protected abstract KeyedReference<K,V> |
createRef(K key,
V value,
java.lang.ref.ReferenceQueue<V> rqueue)
Factory method for creating a new reference instance. |
java.util.Set<java.util.Map.Entry<K,V>> |
entrySet()
|
boolean |
equals(java.lang.Object obj)
Cache instances are always different by nature, even if they contain exactly the same entries. |
V |
get(java.lang.Object key)
Returns the value to which this cache maps the specified key. |
int |
hashCode()
The superclass implementation of this method relies on the entrySet() method, which always throws an exception. |
protected void |
purge()
Removes keys for References that have been enqueued. |
V |
put(K key,
V value)
Associates the specified value with the specified key in this cache, but only if no value is associated yet. |
V |
remove(java.lang.Object key)
Removes the entry for this key from this cache if present. |
int |
size()
|
java.util.Collection<V> |
values()
|
Methods inherited from class java.util.AbstractMap |
---|
clone, containsKey, containsValue, isEmpty, keySet, putAll, toString |
Methods inherited from class java.lang.Object |
---|
finalize, getClass, notify, notifyAll, wait, wait, wait |
Constructor Detail |
---|
public BaseCache(int initCapacity, float loadFactor, int concurrencyLevel)
Creates an instance with custom capacity and concurrency level.
The concurrency level is used as a hint to select the internal Map implementation:
HashMap
will be used.Hashtable
will be selected.ConcurrentHashMap
will be
created with the specified concurrency level.
initCapacity
- The map initial capacityloadFactor
- The map load factorconcurrencyLevel
- The concurrency level.public BaseCache(java.util.Map<K,KeyedReference<K,V>> map)
Advanced constructor.
With this constructor, the programmer can specify the Map to be used to hold the values. Useful for those cases in which the default implementations do not have the correct performance profile.
map
- The map to use internallyMethod Detail |
---|
public final V get(java.lang.Object key)
Returns the value to which this cache maps the specified key. Returns null if the cache contains no entry for this key. A return value of null does not necessarily indicate that the cache contains no entry for the key; it's also possible that the cache explicitly maps the key to null
.Although this method should use K for the key type, it
is left as Object to keep this interface compatible with
Map
.
get
in interface java.util.Map<K,V>
get
in interface Cache<K,V>
get
in class java.util.AbstractMap<K,V>
key
- key whose associated value is to be returned.
public final V put(K key, V value)
Associates the specified value with the specified key in this cache, but only if no value is associated yet. If the cache previously contained an entry for this key, the old value is returned, but not replaced.
This method is equivalent to the putIfAbsent from the
ConcurrentMap
class, as it is more
appropriate for a cache than the original one from Map
.
put
in interface java.util.Map<K,V>
put
in interface Cache<K,V>
put
in class java.util.AbstractMap<K,V>
key
- key with which the specified value is to be associated.value
- value to be associated with the specified key.
public final V remove(java.lang.Object key)
Removes the entry for this key from this cache if present.
Although this method should use K for the key type, it
is left as Object to keep this interface compatible with
Map
.
remove
in interface java.util.Map<K,V>
remove
in interface Cache<K,V>
remove
in class java.util.AbstractMap<K,V>
key
- key whose entry is to be removed from the cache.
public final int size()
size
in interface java.util.Map<K,V>
size
in class java.util.AbstractMap<K,V>
public final void clear()
Removes all entries from this cache.
This method must be handled very carefully, as it will break the assumption of unique instances for cached factories.
clear
in interface java.util.Map<K,V>
clear
in interface Cache<K,V>
clear
in class java.util.AbstractMap<K,V>
public final java.util.Set<java.util.Map.Entry<K,V>> entrySet()
entrySet
in interface java.util.Map<K,V>
entrySet
in class java.util.AbstractMap<K,V>
public final java.util.Collection<V> values()
values
in interface java.util.Map<K,V>
values
in class java.util.AbstractMap<K,V>
public final int hashCode()
The superclass implementation of this method relies on the
entrySet()
method, which always throws an exception. This
re-implementation removes the issue.
hashCode
in interface java.util.Map<K,V>
hashCode
in class java.util.AbstractMap<K,V>
public final boolean equals(java.lang.Object obj)
Cache instances are always different by nature, even if they contain exactly the same entries.
equals
in interface java.util.Map<K,V>
equals
in class java.util.AbstractMap<K,V>
protected abstract KeyedReference<K,V> createRef(K key, V value, java.lang.ref.ReferenceQueue<V> rqueue)
key
- The keyvalue
- The valuerqueue
- A reference queue
protected final void purge()
Removes keys for References that have been enqueued.
This method is not public because applications should never need to manually purge the cache. However, it is left protected to allow invocation from tests.
This method is synchronised because ReferenceQueue
is not thread-safe.
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |