Class WeakReferenceList<E>

  • All Implemented Interfaces:
    java.lang.Iterable<E>

    public class WeakReferenceList<E>
    extends java.lang.Object
    implements java.lang.Iterable<E>
    Manages a list of WeakReferences, and providing an iterator over the objects which are still strongly referenced.

    Useful for avoiding memory leaks of the type where a class (X) adds itself to a listener of another class (Y, which might be a permanent class which is never intended to be freed). When nothing else refers to class X, it normally wouldn't be freed, because it is still a listener of class Y. Using a weak reference list means that class X can be freed if the only reference to it is a weak reference inside class Y's listener list.

    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void add​(E objectToAddReferenceTo)
      Adds a weak reference to the given object.
      java.util.Iterator<E> iterator()
      Gets an iterator over all objects added to this class that are still strongly referenced.
      void remove​(E objectToRemoveReferenceTo)
      Removes a reference to an object previously added using add(Object).
      java.util.List<E> toList()
      Creates a list of all objects added to this class that are still strongly referenced.
      • Methods inherited from class java.lang.Object

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

        forEach, spliterator
    • Constructor Detail

      • WeakReferenceList

        public WeakReferenceList​(WeakReferenceList<E> weakReferenceListToCopy)
        Constructs a copy of another WeakReferenceList
        Parameters:
        weakReferenceListToCopy - the WeakReferenceList to copy
      • WeakReferenceList

        public WeakReferenceList()
        Constructs an empty WeakReferenceList
    • Method Detail

      • add

        public void add​(E objectToAddReferenceTo)
        Adds a weak reference to the given object.
        Parameters:
        objectToAddReferenceTo - the object to add a weak reference to.
      • remove

        public void remove​(E objectToRemoveReferenceTo)
        Removes a reference to an object previously added using add(Object).
        Parameters:
        objectToRemoveReferenceTo - the object to remove a reference to
      • iterator

        public java.util.Iterator<E> iterator()
        Gets an iterator over all objects added to this class that are still strongly referenced.
        Specified by:
        iterator in interface java.lang.Iterable<E>
        Returns:
        an iterator over all objects added to this class that are still strongly referenced.
      • toList

        public java.util.List<E> toList()
        Creates a list of all objects added to this class that are still strongly referenced.
        Returns:
        a list of all objects added to this class that are still strongly referenced.