Class WeakList<E>

  • Type Parameters:
    E - generic typeto be used in this list
    All Implemented Interfaces:
    java.lang.Iterable<E>, java.util.Collection<E>, java.util.List<E>

    public class WeakList<E>
    extends java.util.AbstractList<E>
    This list stores objects automatically using weak references. Objects are added and removed from the list as normal, but may turn to null at any point (ie, indexOf(x) followed by get(x) may return null). The weak references are only removed when the trimToSize method is called so that the indices remain constant otherwise.
    • Field Summary

      • Fields inherited from class java.util.AbstractList

        modCount
    • Constructor Summary

      Constructors 
      Constructor Description
      WeakList()
      Create a weak random-access list.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void add​(int index, E element)
      Inserts the object at the specified position in the list.
      E get​(int index)  
      java.util.List<E> hardList()  
      E remove​(int index)
      Removes the object at the specified position and returns it or returns null if it was already collected.
      E set​(int index, E element)
      Sets the object at the specified position and returns the previous object at that position or null if it was already collected.
      int size()  
      void trimToSize()
      Compacts the list by removing references to collected objects.
      • Methods inherited from class java.util.AbstractList

        add, addAll, clear, equals, hashCode, indexOf, iterator, lastIndexOf, listIterator, listIterator, removeRange, subList
      • Methods inherited from class java.util.AbstractCollection

        addAll, contains, containsAll, isEmpty, remove, removeAll, 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.List

        addAll, contains, containsAll, isEmpty, remove, removeAll, replaceAll, retainAll, sort, spliterator, toArray, toArray
    • Constructor Detail

      • WeakList

        public WeakList()
        Create a weak random-access list.
    • Method Detail

      • get

        public E get​(int index)
        Specified by:
        get in interface java.util.List<E>
        Specified by:
        get in class java.util.AbstractList<E>
        Parameters:
        index - of field to get
        Returns:
        the object at the specified index, or null if the object has been collected.
      • size

        public int size()
        Specified by:
        size in interface java.util.Collection<E>
        Specified by:
        size in interface java.util.List<E>
        Specified by:
        size in class java.util.AbstractCollection<E>
        Returns:
        the size of the list, including already collected objects.
      • set

        public E set​(int index,
                     E element)
        Sets the object at the specified position and returns the previous object at that position or null if it was already collected.
        Specified by:
        set in interface java.util.List<E>
        Overrides:
        set in class java.util.AbstractList<E>
        Parameters:
        index - position where to place element
        element - data which to put on position
        Returns:
        previous object on that position
      • add

        public void add​(int index,
                        E element)
        Inserts the object at the specified position in the list. Automatically creates a weak reference to the object.
        Specified by:
        add in interface java.util.List<E>
        Overrides:
        add in class java.util.AbstractList<E>
        Parameters:
        index - position where to insert element
        element - data which to put on position
      • remove

        public E remove​(int index)
        Removes the object at the specified position and returns it or returns null if it was already collected.
        Specified by:
        remove in interface java.util.List<E>
        Overrides:
        remove in class java.util.AbstractList<E>
        Parameters:
        index - of element to be removed
        Returns:
        previous object on that position
      • hardList

        public java.util.List<E> hardList()
        Returns:
        a list of hard references to the objects. The returned list does not include the collected elements, so its indices do not necessarily correlate with those of this list.
      • trimToSize

        public void trimToSize()
        Compacts the list by removing references to collected objects.