Class WeakCache.Segment

  • All Implemented Interfaces:
    java.util.Map<java.lang.Object,​T>
    Enclosing class:
    WeakCache<T>

    private class WeakCache.Segment
    extends java.util.WeakHashMap<java.lang.Object,​T>
    The segment is effectively a synchronized weak hash map. If is used to store the key value pairs in such a way that they are kept only as long as the garbage collector does not collect the key. This ensures the cache does not cause memory issues.
    • Nested Class Summary

      • Nested classes/interfaces inherited from class java.util.AbstractMap

        java.util.AbstractMap.SimpleEntry<K extends java.lang.Object,​V extends java.lang.Object>, java.util.AbstractMap.SimpleImmutableEntry<K extends java.lang.Object,​V extends java.lang.Object>
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private Segment()  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void cache​(java.lang.Object key, T value)
      This method is used to insert a key value mapping in to the cache.
      boolean contains​(java.lang.Object key)
      This is used to determine whether the specified key exists with in the cache.
      T fetch​(java.lang.Object key)
      This method is used to get the value from the cache that is mapped to the specified key.
      T take​(java.lang.Object key)
      This is used to exclusively take the value mapped to the specified key from the cache.
      • Methods inherited from class java.util.WeakHashMap

        clear, containsKey, containsValue, entrySet, forEach, get, isEmpty, keySet, put, putAll, remove, replaceAll, size, values
      • Methods inherited from class java.util.AbstractMap

        clone, equals, hashCode, toString
      • Methods inherited from class java.lang.Object

        finalize, getClass, notify, notifyAll, wait, wait, wait
      • Methods inherited from interface java.util.Map

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

      • Segment

        private Segment()
    • Method Detail

      • cache

        public void cache​(java.lang.Object key,
                          T value)
        This method is used to insert a key value mapping in to the cache. The value can later be retrieved or removed from the cache if desired. If the value associated with the key is null then nothing is stored within the cache.
        Parameters:
        key - this is the key to cache the provided value to
        value - this is the value that is to be cached
      • fetch

        public T fetch​(java.lang.Object key)
        This method is used to get the value from the cache that is mapped to the specified key. If there is no value mapped to the specified key then this method will return a null.
        Parameters:
        key - this is the key to acquire the cache value with
        Returns:
        this returns the value mapped to the specified key
      • take

        public T take​(java.lang.Object key)
        This is used to exclusively take the value mapped to the specified key from the cache. Invoking this is effectively removing the value from the cache.
        Parameters:
        key - this is the key to acquire the cache value with
        Returns:
        this returns the value mapped to the specified key
      • contains

        public boolean contains​(java.lang.Object key)
        This is used to determine whether the specified key exists with in the cache. Typically this can be done using the fetch method, which will acquire the object.
        Parameters:
        key - this is the key to check within this segment
        Returns:
        true if the specified key is within the cache