Class WeakObjectRegistry<K,​E extends K>

  • All Implemented Interfaces:
    java.lang.Iterable<E>, java.util.Collection<E>, java.util.Set<E>

    public class WeakObjectRegistry<K,​E extends K>
    extends java.util.AbstractSet<E>
    An object registry that uses weak references to keep track of the stored objects. The registry can be used to retrieve stored objects using another, equivalent object. As such, it can be used to prevent the use of duplicates in another data structure, reducing memory usage. The objects that are being stored should properly implement the Object.equals(java.lang.Object) and Object.hashCode() methods.
    • Constructor Summary

      Constructors 
      Constructor Description
      WeakObjectRegistry()
      Constructs a new, empty object registry.
      WeakObjectRegistry​(int cacheSize, java.util.Collection<? extends E> c)
      Constructs a new WeakObjectRegistry containing the elements in the specified collection.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean add​(E object)  
      void clear()  
      WeakObjectRegistry.AutoCloseableIterator<E> closeableIterator()  
      boolean contains​(java.lang.Object key)  
      E get​(K key)
      Retrieves the stored object that is equal to the supplied key object.
      private int getIndex​(java.lang.Object key)  
      E getOrAdd​(K key, java.util.function.Supplier<E> supplier)  
      java.util.Iterator<E> iterator()  
      boolean remove​(java.lang.Object object)  
      int size()  
      • Methods inherited from class java.util.AbstractSet

        equals, hashCode, removeAll
      • Methods inherited from class java.util.AbstractCollection

        addAll, containsAll, isEmpty, retainAll, toArray, toArray, toString
      • Methods inherited from class java.lang.Object

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

        parallelStream, removeIf, stream, toArray
      • Methods inherited from interface java.lang.Iterable

        forEach
      • Methods inherited from interface java.util.Set

        addAll, containsAll, isEmpty, retainAll, spliterator, toArray, toArray
    • Field Detail

      • logger

        private static final org.slf4j.Logger logger
      • objectMap

        private final java.util.Map<E extends K,​java.lang.ref.WeakReference<E extends K>>[] objectMap
        The hash map that is used to store the objects.
    • Constructor Detail

      • WeakObjectRegistry

        public WeakObjectRegistry()
        Constructs a new, empty object registry.
      • WeakObjectRegistry

        public WeakObjectRegistry​(int cacheSize,
                                  java.util.Collection<? extends E> c)
        Constructs a new WeakObjectRegistry containing the elements in the specified collection.
        Parameters:
        c - The collection whose elements are to be placed into this object registry.
        Throws:
        java.lang.NullPointerException - If the specified collection is null.
    • Method Detail

      • get

        public E get​(K key)
        Retrieves the stored object that is equal to the supplied key object.
        Parameters:
        key - The object that should be used as the search key for the operation.
        Returns:
        A stored object that is equal to the supplied key, or null if no such object was found.
      • getIndex

        private int getIndex​(java.lang.Object key)
      • iterator

        public java.util.Iterator<E> iterator()
        Specified by:
        iterator in interface java.util.Collection<K>
        Specified by:
        iterator in interface java.lang.Iterable<K>
        Specified by:
        iterator in interface java.util.Set<K>
        Specified by:
        iterator in class java.util.AbstractCollection<E extends K>
      • size

        public int size()
        Specified by:
        size in interface java.util.Collection<K>
        Specified by:
        size in interface java.util.Set<K>
        Specified by:
        size in class java.util.AbstractCollection<E extends K>
      • contains

        public boolean contains​(java.lang.Object key)
        Specified by:
        contains in interface java.util.Collection<K>
        Specified by:
        contains in interface java.util.Set<K>
        Overrides:
        contains in class java.util.AbstractCollection<E extends K>
      • add

        public boolean add​(E object)
        Specified by:
        add in interface java.util.Collection<K>
        Specified by:
        add in interface java.util.Set<K>
        Overrides:
        add in class java.util.AbstractCollection<E extends K>
      • getOrAdd

        public E getOrAdd​(K key,
                          java.util.function.Supplier<E> supplier)
      • remove

        public boolean remove​(java.lang.Object object)
        Specified by:
        remove in interface java.util.Collection<K>
        Specified by:
        remove in interface java.util.Set<K>
        Overrides:
        remove in class java.util.AbstractCollection<E extends K>
      • clear

        public void clear()
        Specified by:
        clear in interface java.util.Collection<K>
        Specified by:
        clear in interface java.util.Set<K>
        Overrides:
        clear in class java.util.AbstractCollection<E extends K>