Top Description Fields Constructors Methods
org.codehaus.groovy.runtime.memoize

public Class CommonCache<K, V>

extends Object
implements EvictableCache<K, V>, ValueConvertable<V, Object>, Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable, org.codehaus.groovy.runtime.memoize.ValueConvertable, org.codehaus.groovy.runtime.memoize.EvictableCache, java.util.Map, org.codehaus.groovy.runtime.memoize.MemoizeCache
Type Parameters
<K>
type of the keys
<V>
type of the values
Imports
java.io.Serializable, java.lang.ref.SoftReference, .WeakReference, java.util.Collection, .LinkedHashMap, .LinkedList, .List, .Map, .Set

Represents a simple key-value cache, which is NOT thread safe and backed by a java.util.Map instance
Since
2.5.0

Field Summary

Modifier and TypeField and Description
public static final int
DEFAULT_INITIAL_CAPACITY

The default initial capacity

public static final float
DEFAULT_LOAD_FACTOR

The default load factor

private final Map<K, V>
private static final long

Constructor Summary

AccessConstructor and Description
public
CommonCache()

Constructs a cache with unlimited size

public
CommonCache(final int
initial capacity of the cache
initialCapacity
,
final int
max size of the cache
maxSize
,
final EvictableCache.EvictionStrategy evictionStrategy)

Constructs a cache with limited size

public
CommonCache(int
initial capacity of the LRU cache
initialCapacity
,
int
max size of the LRU cache
maxSize
)

Constructs a LRU cache with the specified initial capacity and max size.

public
CommonCache(final int
max size of the LRU cache
maxSize
)

Constructs a LRU cache with the default initial capacity

public
CommonCache(Map<K, V>
the java.util.Map instance
map
)

Constructs a cache backed by the specified java.util.Map instance

Method Summary

Modifier and TypeMethod and Description
public void
cleanUpNullReferences()

Implements org.codehaus.groovy.runtime.memoize.MemoizeCache.cleanUpNullReferences.

Invoked when some of the held SoftReferences have been evicted by the garbage collector and so should be removed from the cache.
public Map<K, V>
public boolean
containsKey(Object
key whose presence in this cache is to be tested.
key
)

Implements org.codehaus.groovy.runtime.memoize.EvictableCache.containsKey.

Determines if the cache contains an entry for the specified key.
public boolean
containsValue(Object
value whose presence in this map is to be tested
value
)

Implements java.util.Map.containsValue.

Returns true if this map maps one or more keys to the specified value.
public Object
convertValue(V
the original value
value
)

Implements org.codehaus.groovy.runtime.memoize.ValueConvertable.convertValue.

convert the original value to the target value
public Set<Map.Entry<K, V>>
entrySet()

Implements java.util.Map.entrySet.

Returns a Set view of the mappings contained in this map.
public V
get(Object
the key whose associated value is to be returned
key
)

Implements org.codehaus.groovy.runtime.memoize.MemoizeCache.get, java.util.Map.get.

Gets a value from the cache
public V
getAndPut(K key, MemoizeCache.ValueProvider<? super K, ? extends V>
provide the value if the associated value not found
valueProvider
)

Overrides default org.codehaus.groovy.runtime.memoize.MemoizeCache.getAndPut.

Try to get the value from cache.
public V
getAndPut(K key, MemoizeCache.ValueProvider<? super K, ? extends V> valueProvider, boolean shouldCache)

public boolean
isEmpty()

Implements java.util.Map.isEmpty.

Returns true if this map contains no key-value mappings.
public Set<K>
keys()

Implements org.codehaus.groovy.runtime.memoize.EvictableCache.keys.

Get all keys associated to cached values
public Set<K>
keySet()

Implements java.util.Map.keySet.

Returns a Set view of the keys contained in this map.
public V
put(K
key with which the specified value is to be associated
key
,
V
value to be associated with the specified key
value
)

Implements org.codehaus.groovy.runtime.memoize.MemoizeCache.put, java.util.Map.put.

Associates the specified value with the specified key in the cache.
public void
putAll(Map<? extends K, ? extends V>
mappings to be stored in this map
m
)

Implements java.util.Map.putAll.

Copies all of the mappings from the specified map to this map (optional operation).
public V
remove(Object
of the cached value
key
)

Implements org.codehaus.groovy.runtime.memoize.EvictableCache.remove.

Remove the cached value by the key
public int
size()

Implements org.codehaus.groovy.runtime.memoize.EvictableCache.size.

Get the size of the cache
public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.
public Collection<V>
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

DEFAULT_INITIAL_CAPACITYback to summary
public static final int DEFAULT_INITIAL_CAPACITY

The default initial capacity

DEFAULT_LOAD_FACTORback to summary
public static final float DEFAULT_LOAD_FACTOR

The default load factor

mapback to summary
private final Map<K, V> map
serialVersionUIDback to summary
private static final long serialVersionUID

Constructor Detail

CommonCacheback to summary
public CommonCache()

Constructs a cache with unlimited size

CommonCacheback to summary
public CommonCache(final int initialCapacity, final int maxSize, final EvictableCache.EvictionStrategy evictionStrategy)

Constructs a cache with limited size

Parameters
initialCapacity:int

initial capacity of the cache

maxSize:int

max size of the cache

evictionStrategy:EvictableCache.EvictionStrategy

LRU or FIFO, see org.codehaus.groovy.runtime.memoize.EvictableCache.EvictionStrategy

CommonCacheback to summary
public CommonCache(int initialCapacity, int maxSize)

Constructs a LRU cache with the specified initial capacity and max size. The LRU cache is slower than LRUCache

Parameters
initialCapacity:int

initial capacity of the LRU cache

maxSize:int

max size of the LRU cache

CommonCacheback to summary
public CommonCache(final int maxSize)

Constructs a LRU cache with the default initial capacity

Parameters
maxSize:int

max size of the LRU cache

See Also
CommonCache(int, int)
CommonCacheback to summary
public CommonCache(Map<K, V> map)

Constructs a cache backed by the specified java.util.Map instance

Parameters
map:Map<K, V>

the java.util.Map instance

Method Detail

cleanUpNullReferencesback to summary
public void cleanUpNullReferences()

Implements org.codehaus.groovy.runtime.memoize.MemoizeCache.cleanUpNullReferences.

Doc from org.codehaus.groovy.runtime.memoize.MemoizeCache.cleanUpNullReferences.

Invoked when some of the held SoftReferences have been evicted by the garbage collector and so should be removed from the cache. The implementation must ensure that concurrent invocations of all methods on the cache may occur from other threads and thus should protect any shared resources.

Annotations
@Override
clearAllback to summary
public Map<K, V> clearAll()

Implements org.codehaus.groovy.runtime.memoize.EvictableCache.clearAll.

Doc from org.codehaus.groovy.runtime.memoize.EvictableCache.clearAll.

Clear the cache

Returns:Map<K, V>

returns the content of the cleared map

Annotations
@Override
containsKeyback to summary
public boolean containsKey(Object key)

Implements org.codehaus.groovy.runtime.memoize.EvictableCache.containsKey.

Doc from org.codehaus.groovy.runtime.memoize.EvictableCache.containsKey.

Determines if the cache contains an entry for the specified key.

Parameters
key:Object

key whose presence in this cache is to be tested.

Returns:boolean

true if the cache contains a mapping for the specified key

Annotations
@Override
containsValueback to summary
public boolean containsValue(Object value)

Implements java.util.Map.containsValue.

Doc from java.util.Map.containsValue.

Returns true if this map maps one or more keys to the specified value. More formally, returns true if and only if this map contains at least one mapping to a value v such that Objects.equals(value, v). This operation will probably require time linear in the map size for most implementations of the Map interface.

Parameters
value:Object

value whose presence in this map is to be tested

Returns:boolean

true if this map maps one or more keys to the specified value

Annotations
@Override
convertValueback to summary
public Object convertValue(V value)

Implements org.codehaus.groovy.runtime.memoize.ValueConvertable.convertValue.

Doc from org.codehaus.groovy.runtime.memoize.ValueConvertable.convertValue.

convert the original value to the target value

Parameters
value:V

the original value

Returns:Object

the converted value

Annotations
@Override
entrySetback to summary
public Set<Map.Entry<K, V>> entrySet()

Implements java.util.Map.entrySet.

Doc from java.util.Map.entrySet.

Returns a Set view of the mappings contained in this map. The set is backed by the map, so changes to the map are reflected in the set, and vice-versa. If the map is modified while an iteration over the set is in progress (except through the iterator's own remove operation, or through the setValue operation on a map entry returned by the iterator) the results of the iteration are undefined. The set supports element removal, which removes the corresponding mapping from the map, via the Iterator.remove, Set.remove, removeAll, retainAll and clear operations. It does not support the add or addAll operations.

Returns:Set<Map.Entry<K, V>>

a set view of the mappings contained in this map

Annotations
@Override
getback to summary
public V get(Object key)

Implements org.codehaus.groovy.runtime.memoize.MemoizeCache.get, java.util.Map.get.

Doc from org.codehaus.groovy.runtime.memoize.MemoizeCache.get.

Gets a value from the cache

Parameters
key:Object

the key whose associated value is to be returned

Returns:V

the value, or null, if it does not exist.

Annotations
@Override
getAndPutback to summary
public V getAndPut(K key, MemoizeCache.ValueProvider<? super K, ? extends V> valueProvider)

Overrides default org.codehaus.groovy.runtime.memoize.MemoizeCache.getAndPut.

Doc from org.codehaus.groovy.runtime.memoize.MemoizeCache.getAndPut.

Try to get the value from cache. If not found, create the value by ValueProvider and put it into the cache, at last return the value.

Parameters
valueProvider:MemoizeCache.ValueProvider<? super K, ? extends V>

provide the value if the associated value not found

Returns:V

the cached value

Annotations
@Override
getAndPutback to summary
public V getAndPut(K key, MemoizeCache.ValueProvider<? super K, ? extends V> valueProvider, boolean shouldCache)
isEmptyback to summary
public boolean isEmpty()

Implements java.util.Map.isEmpty.

Doc from java.util.Map.isEmpty.

Returns true if this map contains no key-value mappings.

Returns:boolean

true if this map contains no key-value mappings

Annotations
@Override
keysback to summary
public Set<K> keys()

Implements org.codehaus.groovy.runtime.memoize.EvictableCache.keys.

Doc from org.codehaus.groovy.runtime.memoize.EvictableCache.keys.

Get all keys associated to cached values

Returns:Set<K>

all keys

Annotations
@Override
keySetback to summary
public Set<K> keySet()

Implements java.util.Map.keySet.

Doc from java.util.Map.keySet.

Returns a Set view of the keys contained in this map. The set is backed by the map, so changes to the map are reflected in the set, and vice-versa. If the map is modified while an iteration over the set is in progress (except through the iterator's own remove operation), the results of the iteration are undefined. The set supports element removal, which removes the corresponding mapping from the map, via the Iterator.remove, Set.remove, removeAll, retainAll, and clear operations. It does not support the add or addAll operations.

Returns:Set<K>

a set view of the keys contained in this map

Annotations
@Override
putback to summary
public V put(K key, V value)

Implements org.codehaus.groovy.runtime.memoize.MemoizeCache.put, java.util.Map.put.

Doc from org.codehaus.groovy.runtime.memoize.MemoizeCache.put.

Associates the specified value with the specified key in the cache.

Parameters
key:K

key with which the specified value is to be associated

value:V

value to be associated with the specified key

Returns:V

null, or the old value if the key associated with the specified key.

Annotations
@Override
putAllback to summary
public void putAll(Map<? extends K, ? extends V> m)

Implements java.util.Map.putAll.

Doc from java.util.Map.putAll.

Copies all of the mappings from the specified map to this map (optional operation). The effect of this call is equivalent to that of calling put(k, v) on this map once for each mapping from key k to value v in the specified map. The behavior of this operation is undefined if the specified map is modified while the operation is in progress. If the specified map has a defined encounter order, processing of its mappings generally occurs in that order.

Parameters
m:Map<? extends K, ? extends V>

mappings to be stored in this map

Annotations
@Override
removeback to summary
public V remove(Object key)

Implements org.codehaus.groovy.runtime.memoize.EvictableCache.remove.

Doc from org.codehaus.groovy.runtime.memoize.EvictableCache.remove.

Remove the cached value by the key

Parameters
key:Object

of the cached value

Returns:V

returns the removed value

Annotations
@Override
sizeback to summary
public int size()

Implements org.codehaus.groovy.runtime.memoize.EvictableCache.size.

Doc from org.codehaus.groovy.runtime.memoize.EvictableCache.size.

Get the size of the cache

Returns:int

the size of the cache

Annotations
@Override
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object.

Returns:String

a string representation of the object.

Annotations
@Override
valuesback to summary
public Collection<V> values()

Implements org.codehaus.groovy.runtime.memoize.EvictableCache.values.

Doc from org.codehaus.groovy.runtime.memoize.EvictableCache.values.

Get all cached values

Returns:Collection<V>

all cached values

Annotations
@Override