Class UnlimitedConcurrentCache<K,​V>

  • All Implemented Interfaces:
    java.io.Serializable, java.util.Map<K,​V>, EvictableCache<K,​V>, MemoizeCache<K,​V>

    @ThreadSafe
    public final class UnlimitedConcurrentCache<K,​V>
    extends java.lang.Object
    implements EvictableCache<K,​V>, java.io.Serializable
    A cache backed by a ConcurrentHashMap
    See Also:
    Serialized Form
    • Constructor Summary

      Constructors 
      Constructor Description
      UnlimitedConcurrentCache()
      Constructs a cache with unlimited size
      UnlimitedConcurrentCache​(int initialCapacity)
      Constructs a cache with unlimited size and set its initial capacity
      UnlimitedConcurrentCache​(java.util.Map<? extends K,​? extends V> m)
      Constructs a cache and initialize the cache with the specified map
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void cleanUpNullReferences()
      Replying on the ConcurrentHashMap thread-safe iteration implementation the method will remove all entries holding SoftReferences to gc-evicted objects.
      void clear()
      Clear the cache
      java.util.Map<K,​V> clearAll()
      Clear the cache
      boolean containsKey​(java.lang.Object key)
      Determines if the cache contains an entry for the specified key.
      boolean containsValue​(java.lang.Object value)  
      java.util.Set<java.util.Map.Entry<K,​V>> entrySet()  
      V get​(java.lang.Object key)
      Gets a value from the cache
      V getAndPut​(K key, MemoizeCache.ValueProvider<? super K,​? extends V> valueProvider)
      The implementation of `getAndPut` is not atomic
      boolean isEmpty()  
      java.util.Set<K> keys()
      Get all keys associated to cached values
      java.util.Set<K> keySet()  
      V put​(K key, V value)
      Associates the specified value with the specified key in the cache.
      void putAll​(java.util.Map<? extends K,​? extends V> m)  
      V remove​(java.lang.Object key)
      Remove the cached value by the key
      int size()
      Get the size of the cache
      java.util.Collection<V> values()
      Get all cached values
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
      • Methods inherited from interface java.util.Map

        compute, computeIfAbsent, computeIfPresent, equals, forEach, getOrDefault, hashCode, merge, putIfAbsent, remove, replace, replace, replaceAll
    • Constructor Detail

      • UnlimitedConcurrentCache

        public UnlimitedConcurrentCache()
        Constructs a cache with unlimited size
      • UnlimitedConcurrentCache

        public UnlimitedConcurrentCache​(int initialCapacity)
        Constructs a cache with unlimited size and set its initial capacity
        Parameters:
        initialCapacity - the initial capacity
      • UnlimitedConcurrentCache

        public UnlimitedConcurrentCache​(java.util.Map<? extends K,​? extends V> m)
        Constructs a cache and initialize the cache with the specified map
        Parameters:
        m - the map to initialize the cache
    • Method Detail

      • remove

        public V remove​(java.lang.Object key)
        Remove the cached value by the key
        Specified by:
        remove in interface EvictableCache<K,​V>
        Specified by:
        remove in interface java.util.Map<K,​V>
        Parameters:
        key -
        Returns:
        returns the removed value
      • putAll

        public void putAll​(java.util.Map<? extends K,​? extends V> m)
        Specified by:
        putAll in interface java.util.Map<K,​V>
      • keySet

        public java.util.Set<K> keySet()
        Specified by:
        keySet in interface java.util.Map<K,​V>
      • clearAll

        public java.util.Map<K,​V> clearAll()
        Clear the cache
        Specified by:
        clearAll in interface EvictableCache<K,​V>
        Returns:
        returns the content of the cleared map
      • clear

        public void clear()
        Clear the cache
        Specified by:
        clear in interface java.util.Map<K,​V>
        See Also:
        clearAll()
      • values

        public java.util.Collection<V> values()
        Get all cached values
        Specified by:
        values in interface EvictableCache<K,​V>
        Specified by:
        values in interface java.util.Map<K,​V>
        Returns:
        all cached values
      • entrySet

        public java.util.Set<java.util.Map.Entry<K,​V>> entrySet()
        Specified by:
        entrySet in interface java.util.Map<K,​V>
      • keys

        public java.util.Set<K> keys()
        Get all keys associated to cached values
        Specified by:
        keys in interface EvictableCache<K,​V>
        Returns:
        all keys
      • containsKey

        public boolean containsKey​(java.lang.Object key)
        Determines if the cache contains an entry for the specified key.
        Specified by:
        containsKey in interface EvictableCache<K,​V>
        Specified by:
        containsKey in 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
      • containsValue

        public boolean containsValue​(java.lang.Object value)
        Specified by:
        containsValue in interface java.util.Map<K,​V>
      • size

        public int size()
        Get the size of the cache
        Specified by:
        size in interface EvictableCache<K,​V>
        Specified by:
        size in interface java.util.Map<K,​V>
        Returns:
        the size of the cache
      • isEmpty

        public boolean isEmpty()
        Specified by:
        isEmpty in interface java.util.Map<K,​V>
      • put

        public V put​(K key,
                     V value)
        Associates the specified value with the specified key in the cache.
        Specified by:
        put in interface java.util.Map<K,​V>
        Specified by:
        put in 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.
      • get

        public V get​(java.lang.Object key)
        Gets a value from the cache
        Specified by:
        get in interface java.util.Map<K,​V>
        Specified by:
        get in interface MemoizeCache<K,​V>
        Parameters:
        key - the key whose associated value is to be returned
        Returns:
        the value, or null, if it does not exist.
      • cleanUpNullReferences

        public void cleanUpNullReferences()
        Replying on the ConcurrentHashMap thread-safe iteration implementation the method will remove all entries holding SoftReferences to gc-evicted objects.
        Specified by:
        cleanUpNullReferences in interface MemoizeCache<K,​V>