Class WeakCache

  • All Implemented Interfaces:
    java.util.Map

    public class WeakCache
    extends java.util.AbstractMap
    A HashMap implementation with weak references values and by default for the key. When the value is garbage collected, the key will also vanish from the map.
    Since:
    1.4
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      private static interface  WeakCache.Visitor  
      • 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>
      • Nested classes/interfaces inherited from interface java.util.Map

        java.util.Map.Entry<K extends java.lang.Object,​V extends java.lang.Object>
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.util.Map map  
    • Constructor Summary

      Constructors 
      Constructor Description
      WeakCache()
      Construct a WeakCache with weak keys.
      WeakCache​(java.util.Map map)
      Construct a WeakCache.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void clear()  
      boolean containsKey​(java.lang.Object key)  
      boolean containsValue​(java.lang.Object value)  
      protected java.lang.ref.Reference createReference​(java.lang.Object value)  
      java.util.Set entrySet()  
      boolean equals​(java.lang.Object o)  
      java.lang.Object get​(java.lang.Object key)  
      int hashCode()  
      private java.lang.Object iterate​(WeakCache.Visitor visitor, int type)  
      java.util.Set keySet()  
      java.lang.Object put​(java.lang.Object key, java.lang.Object value)  
      java.lang.Object remove​(java.lang.Object key)  
      int size()  
      java.lang.String toString()  
      java.util.Collection values()  
      • Methods inherited from class java.util.AbstractMap

        clone, isEmpty, putAll
      • Methods inherited from class java.lang.Object

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

        compute, computeIfAbsent, computeIfPresent, forEach, getOrDefault, merge, putIfAbsent, remove, replace, replace, replaceAll
    • Field Detail

      • map

        private final java.util.Map map
    • Constructor Detail

      • WeakCache

        public WeakCache()
        Construct a WeakCache with weak keys.

        Note, that the internally used WeakHashMap is not thread-safe.

        Parameters:
        map - the map to use
        Since:
        1.4
      • WeakCache

        public WeakCache​(java.util.Map map)
        Construct a WeakCache.
        Parameters:
        map - the map to use
        Since:
        1.4
    • Method Detail

      • get

        public java.lang.Object get​(java.lang.Object key)
        Specified by:
        get in interface java.util.Map
        Overrides:
        get in class java.util.AbstractMap
      • put

        public java.lang.Object put​(java.lang.Object key,
                                    java.lang.Object value)
        Specified by:
        put in interface java.util.Map
        Overrides:
        put in class java.util.AbstractMap
      • remove

        public java.lang.Object remove​(java.lang.Object key)
        Specified by:
        remove in interface java.util.Map
        Overrides:
        remove in class java.util.AbstractMap
      • createReference

        protected java.lang.ref.Reference createReference​(java.lang.Object value)
      • containsValue

        public boolean containsValue​(java.lang.Object value)
        Specified by:
        containsValue in interface java.util.Map
        Overrides:
        containsValue in class java.util.AbstractMap
      • size

        public int size()
        Specified by:
        size in interface java.util.Map
        Overrides:
        size in class java.util.AbstractMap
      • values

        public java.util.Collection values()
        Specified by:
        values in interface java.util.Map
        Overrides:
        values in class java.util.AbstractMap
      • entrySet

        public java.util.Set entrySet()
        Specified by:
        entrySet in interface java.util.Map
        Specified by:
        entrySet in class java.util.AbstractMap
      • iterate

        private java.lang.Object iterate​(WeakCache.Visitor visitor,
                                         int type)
      • containsKey

        public boolean containsKey​(java.lang.Object key)
        Specified by:
        containsKey in interface java.util.Map
        Overrides:
        containsKey in class java.util.AbstractMap
      • clear

        public void clear()
        Specified by:
        clear in interface java.util.Map
        Overrides:
        clear in class java.util.AbstractMap
      • keySet

        public java.util.Set keySet()
        Specified by:
        keySet in interface java.util.Map
        Overrides:
        keySet in class java.util.AbstractMap
      • equals

        public boolean equals​(java.lang.Object o)
        Specified by:
        equals in interface java.util.Map
        Overrides:
        equals in class java.util.AbstractMap
      • hashCode

        public int hashCode()
        Specified by:
        hashCode in interface java.util.Map
        Overrides:
        hashCode in class java.util.AbstractMap
      • toString

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