Class WeakHashLRUImpl<K>

  • All Implemented Interfaces:
    WeakHashLRU<K>

    public class WeakHashLRUImpl<K>
    extends java.lang.Object
    implements WeakHashLRU<K>
    An implementation of the WeakHashLRU as needed by the CAR algorithm
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.util.WeakHashMap<K,​DoubleNode<K,​java.lang.Object>> byKey  
      private java.util.concurrent.ConcurrentHashMap<K,​DoubleNode<K,​java.lang.Object>> byKeyNotWeak  
      private boolean isWeak  
      private DoubleNode<K,​java.lang.Object> lru  
      private DoubleNode<K,​java.lang.Object> mru  
      private java.lang.ref.ReferenceQueue<? super K> myQueue  
      private static java.lang.Object VALUE  
    • Constructor Summary

      Constructors 
      Constructor Description
      WeakHashLRUImpl​(boolean isWeak)  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void add​(K key)
      Adds the given key to the LRU.
      private DoubleNode<K,​java.lang.Object> addToHead​(K key)  
      void clear()
      Removes all entries from this LRU
      private void clearStale()  
      void clearStaleReferences()
      Causes stale references to be cleared from the data structures.
      boolean contains​(K key)
      Tells if the given key is in the LRU
      void releaseMatching​(CacheKeyFilter<K> filter)
      Releases all keys that match the filter
      K remove()
      Removes the key that was Least Recently Used
      boolean remove​(K key)
      Removes the given key from the LRU, if found
      private K remove​(DoubleNode<K,​java.lang.Object> removeMe)  
      private boolean removeNoClear​(K key)  
      int size()
      Returns the number of elements currently in the clock.
      java.lang.String toString()  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Field Detail

      • VALUE

        private static final java.lang.Object VALUE
      • isWeak

        private final boolean isWeak
      • byKey

        private final java.util.WeakHashMap<K,​DoubleNode<K,​java.lang.Object>> byKey
      • byKeyNotWeak

        private final java.util.concurrent.ConcurrentHashMap<K,​DoubleNode<K,​java.lang.Object>> byKeyNotWeak
      • myQueue

        private final java.lang.ref.ReferenceQueue<? super K> myQueue
    • Constructor Detail

      • WeakHashLRUImpl

        public WeakHashLRUImpl​(boolean isWeak)
    • Method Detail

      • addToHead

        private DoubleNode<K,​java.lang.Object> addToHead​(K key)
      • remove

        private K remove​(DoubleNode<K,​java.lang.Object> removeMe)
      • add

        public void add​(K key)
        Description copied from interface: WeakHashLRU
        Adds the given key to the LRU. It will be placed at the MRU of the LRU. If this key already exists in the LRU it will be moved to the MRU
        Specified by:
        add in interface WeakHashLRU<K>
        Parameters:
        key - Must not be null
      • contains

        public boolean contains​(K key)
        Description copied from interface: WeakHashLRU
        Tells if the given key is in the LRU
        Specified by:
        contains in interface WeakHashLRU<K>
        Parameters:
        key - The key to search for, may not be null
        Returns:
        true if found, false otherwise
      • remove

        public boolean remove​(K key)
        Description copied from interface: WeakHashLRU
        Removes the given key from the LRU, if found
        Specified by:
        remove in interface WeakHashLRU<K>
        Parameters:
        key - The key to remove, may not be null
        Returns:
        true if removed, false otherwise
      • removeNoClear

        private boolean removeNoClear​(K key)
      • size

        public int size()
        Description copied from interface: WeakHashLRU
        Returns the number of elements currently in the clock. References that have gone away because they were weakly referenced will not be counted in the size
        Specified by:
        size in interface WeakHashLRU<K>
        Returns:
        The number of entries currently in the LRU
      • remove

        public K remove()
        Description copied from interface: WeakHashLRU
        Removes the key that was Least Recently Used
        Specified by:
        remove in interface WeakHashLRU<K>
        Returns:
        The key that was removed, or null if the list is empty
      • releaseMatching

        public void releaseMatching​(CacheKeyFilter<K> filter)
        Description copied from interface: WeakHashLRU
        Releases all keys that match the filter
        Specified by:
        releaseMatching in interface WeakHashLRU<K>
        Parameters:
        filter - A non-null filter that can be used to delete every key that matches the filter
      • clear

        public void clear()
        Description copied from interface: WeakHashLRU
        Removes all entries from this LRU
        Specified by:
        clear in interface WeakHashLRU<K>
      • clearStaleReferences

        public void clearStaleReferences()
        Description copied from interface: WeakHashLRU
        Causes stale references to be cleared from the data structures. Since this is a weak clock the references can go away at any time, which happens whenever any operation has been performed. However, it may be the case that no operation will be performed for a while and so this method is provided to have a no-op operation to call in order to clear out any stale references
        Specified by:
        clearStaleReferences in interface WeakHashLRU<K>
      • clearStale

        private void clearStale()
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object