Class CommonCache<K,V>
- java.lang.Object
- 
- org.codehaus.groovy.runtime.memoize.CommonCache<K,V>
 
- 
- Type Parameters:
- K- type of the keys
- V- type of the values
 - All Implemented Interfaces:
- java.io.Serializable,- java.util.Map<K,V>,- EvictableCache<K,V>,- MemoizeCache<K,V>,- ValueConvertable<V,java.lang.Object>
 
 public class CommonCache<K,V> extends java.lang.Object implements EvictableCache<K,V>, ValueConvertable<V,java.lang.Object>, java.io.Serializable Represents a simple key-value cache, which is NOT thread safe and backed by aMapinstance- Since:
- 2.5.0
- See Also:
- Serialized Form
 
- 
- 
Nested Class Summary- 
Nested classes/interfaces inherited from interface org.codehaus.groovy.runtime.memoize.EvictableCacheEvictableCache.EvictionStrategy
 - 
Nested classes/interfaces inherited from interface java.util.Mapjava.util.Map.Entry<K extends java.lang.Object,V extends java.lang.Object>
 - 
Nested classes/interfaces inherited from interface org.codehaus.groovy.runtime.memoize.MemoizeCacheMemoizeCache.ValueProvider<K,V>
 
- 
 - 
Field SummaryFields Modifier and Type Field Description static intDEFAULT_INITIAL_CAPACITYThe default initial capacitystatic floatDEFAULT_LOAD_FACTORThe default load factor
 - 
Constructor SummaryConstructors Constructor Description CommonCache()Constructs a cache with unlimited sizeCommonCache(int maxSize)Constructs a LRU cache with the default initial capacityCommonCache(int initialCapacity, int maxSize)Constructs a LRU cache with the specified initial capacity and max size.CommonCache(int initialCapacity, int maxSize, EvictableCache.EvictionStrategy evictionStrategy)Constructs a cache with limited sizeCommonCache(java.util.Map<K,V> map)Constructs a cache backed by the specifiedMapinstance
 - 
Method SummaryAll Methods Instance Methods Concrete Methods Modifier and Type Method Description voidcleanUpNullReferences()Invoked when some of the held SoftReferences have been evicted by the garbage collector and so should be removed from the cache.voidclear()Clear the cachejava.util.Map<K,V>clearAll()Clear the cachebooleancontainsKey(java.lang.Object key)Determines if the cache contains an entry for the specified key.booleancontainsValue(java.lang.Object value)java.lang.ObjectconvertValue(V value)convert the original value to the target valuejava.util.Set<java.util.Map.Entry<K,V>>entrySet()Vget(java.lang.Object key)Gets a value from the cacheVgetAndPut(K key, MemoizeCache.ValueProvider<? super K,? extends V> valueProvider)Try to get the value from cache.VgetAndPut(K key, MemoizeCache.ValueProvider<? super K,? extends V> valueProvider, boolean shouldCache)booleanisEmpty()java.util.Set<K>keys()Get all keys associated to cached valuesjava.util.Set<K>keySet()Vput(K key, V value)Associates the specified value with the specified key in the cache.voidputAll(java.util.Map<? extends K,? extends V> m)Vremove(java.lang.Object key)Remove the cached value by the keyintsize()Get the size of the cachejava.lang.StringtoString()java.util.Collection<V>values()Get all cached values
 
- 
- 
- 
Field Detail- 
DEFAULT_LOAD_FACTORpublic static final float DEFAULT_LOAD_FACTOR The default load factor- See Also:
- Constant Field Values
 
 - 
DEFAULT_INITIAL_CAPACITYpublic static final int DEFAULT_INITIAL_CAPACITY The default initial capacity- See Also:
- Constant Field Values
 
 
- 
 - 
Constructor Detail- 
CommonCachepublic CommonCache() Constructs a cache with unlimited size
 - 
CommonCachepublic CommonCache(int initialCapacity, int maxSize, EvictableCache.EvictionStrategy evictionStrategy)Constructs a cache with limited size- Parameters:
- initialCapacity- initial capacity of the cache
- maxSize- max size of the cache
- evictionStrategy- LRU or FIFO, see- EvictableCache.EvictionStrategy
 
 - 
CommonCachepublic CommonCache(int initialCapacity, int maxSize)Constructs a LRU cache with the specified initial capacity and max size. The LRU cache is slower thanLRUCache- Parameters:
- initialCapacity- initial capacity of the LRU cache
- maxSize- max size of the LRU cache
 
 - 
CommonCachepublic CommonCache(int maxSize) Constructs a LRU cache with the default initial capacity- Parameters:
- maxSize- max size of the LRU cache
- See Also:
- CommonCache(int, int)
 
 
- 
 - 
Method Detail- 
getpublic V get(java.lang.Object key) Gets a value from the cache
 - 
putpublic V put(K key, V value) Associates the specified value with the specified key in the cache.- Specified by:
- putin interface- java.util.Map<K,V>
- Specified by:
- putin interface- MemoizeCache<K,V>
- Parameters:
- key- key with which the specified value is to be associated
- value- value to be associated with the specified key
- Returns:
- null, or the old value if the key associated with the specified key.
 
 - 
getAndPutpublic V getAndPut(K key, MemoizeCache.ValueProvider<? super K,? extends V> valueProvider) Try to get the value from cache. If not found, create the value byMemoizeCache.ValueProviderand put it into the cache, at last return the value- Specified by:
- getAndPutin interface- MemoizeCache<K,V>
- Returns:
- the cached value
 
 - 
getAndPutpublic V getAndPut(K key, MemoizeCache.ValueProvider<? super K,? extends V> valueProvider, boolean shouldCache) 
 - 
valuespublic java.util.Collection<V> values() Get all cached values
 - 
keyspublic java.util.Set<K> keys() Get all keys associated to cached values- Specified by:
- keysin interface- EvictableCache<K,V>
- Returns:
- all keys
 
 - 
containsValuepublic boolean containsValue(java.lang.Object value) 
 - 
containsKeypublic boolean containsKey(java.lang.Object key) Determines if the cache contains an entry for the specified key.- Specified by:
- containsKeyin interface- EvictableCache<K,V>
- Specified by:
- containsKeyin interface- java.util.Map<K,V>
- Parameters:
- key- key whose presence in this cache is to be tested.
- Returns:
- true if the cache contains a mapping for the specified key
 
 - 
sizepublic int size() Get the size of the cache
 - 
removepublic V remove(java.lang.Object key) Remove the cached value by the key
 - 
clearAllpublic java.util.Map<K,V> clearAll() Clear the cache- Specified by:
- clearAllin interface- EvictableCache<K,V>
- Returns:
- returns the content of the cleared map
 
 - 
clearpublic void clear() Clear the cache- Specified by:
- clearin interface- java.util.Map<K,V>
- See Also:
- clearAll()
 
 - 
cleanUpNullReferencespublic void 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.- Specified by:
- cleanUpNullReferencesin interface- MemoizeCache<K,V>
 
 - 
toStringpublic java.lang.String toString() - Overrides:
- toStringin class- java.lang.Object
 
 - 
convertValuepublic java.lang.Object convertValue(V value) convert the original value to the target value- Specified by:
- convertValuein interface- ValueConvertable<K,V>
- Parameters:
- value- the original value
- Returns:
- the converted value
 
 
- 
 
-