org.omnaest.utils.cache
Class ConcurrentWeakReferenceCache<K,V>

java.lang.Object
  extended by org.omnaest.utils.cache.ConcurrentWeakReferenceCache<K,V>
Type Parameters:
K -
V -
All Implemented Interfaces:
Serializable, Map<K,V>, Cache<K,V>

public class ConcurrentWeakReferenceCache<K,V>
extends Object

A ConcurrentWeakReferenceCache holds a given number of separated SynchronizedWeakReferenceCache instances, called segments.
Based on the number of segments the retrieval operation will block less long for a multithreaded call to get(Object), containsKey(Object), containsValue(Object), size() and isEmpty(), but any additional segment will increase the blocking time for any put(Object, Object), putAll(Map), remove(Object) and clear() call, as well as it is increasing the memory footprint since every segment will hold all the information within the Cache.

Author:
Omnaest
See Also:
Cache, Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from interface java.util.Map
Map.Entry<K,V>
 
Constructor Summary
ConcurrentWeakReferenceCache()
          This does create a ConcurrentWeakReferenceCache which uses as many segments as the Runtime.availableProcessors() returns
ConcurrentWeakReferenceCache(int numberOfSegments)
           
 
Method Summary
 void clear()
           
 boolean containsKey(Object key)
           
 boolean containsValue(Object value)
           
 Set<Map.Entry<K,V>> entrySet()
          Returns an unmodifiable Set of all Map.Entrys within the Cache which only represents a snapshot in moment
 boolean equals(Object o)
           
 V get(Object key)
           
 V getOrCreate(K key, Factory<V> factory)
          Gets a cached value or creates a new one using the given Factory and puts the new value into the cache.
 int hashCode()
           
 boolean isEmpty()
           
 Set<K> keySet()
          Returns an unmodifiable Set of all keys within the Cache which only represents a snapshot in moment
 V put(K key, V value)
           
 void putAll(Map<? extends K,? extends V> m)
           
 V remove(Object key)
           
 int size()
           
 Collection<V> values()
          Returns an unmodifiable Collection of all values within the Cache which only represents a snapshot in moment
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ConcurrentWeakReferenceCache

public ConcurrentWeakReferenceCache()
This does create a ConcurrentWeakReferenceCache which uses as many segments as the Runtime.availableProcessors() returns

See Also:
ConcurrentWeakReferenceCache

ConcurrentWeakReferenceCache

public ConcurrentWeakReferenceCache(int numberOfSegments)
Parameters:
numberOfSegments -
See Also:
ConcurrentWeakReferenceCache
Method Detail

size

public int size()

isEmpty

public boolean isEmpty()

containsKey

public boolean containsKey(Object key)

containsValue

public boolean containsValue(Object value)

get

public V get(Object key)

put

public V put(K key,
             V value)

remove

public V remove(Object key)

putAll

public void putAll(Map<? extends K,? extends V> m)

clear

public void clear()

keySet

public Set<K> keySet()
Description copied from interface: Cache
Returns an unmodifiable Set of all keys within the Cache which only represents a snapshot in moment


values

public Collection<V> values()
Description copied from interface: Cache
Returns an unmodifiable Collection of all values within the Cache which only represents a snapshot in moment


entrySet

public Set<Map.Entry<K,V>> entrySet()
Description copied from interface: Cache
Returns an unmodifiable Set of all Map.Entrys within the Cache which only represents a snapshot in moment


equals

public boolean equals(Object o)
Specified by:
equals in interface Map<K,V>
Overrides:
equals in class Object

hashCode

public int hashCode()
Specified by:
hashCode in interface Map<K,V>
Overrides:
hashCode in class Object

getOrCreate

public V getOrCreate(K key,
                     Factory<V> factory)
Description copied from interface: Cache
Gets a cached value or creates a new one using the given Factory and puts the new value into the cache.

Specified by:
getOrCreate in interface Cache<K,V>
Returns:


Copyright © 2013. All Rights Reserved.