Class WeakList<E>

java.lang.Object
java.util.AbstractCollection<E>
java.util.AbstractList<E>
net.sourceforge.jnlp.util.WeakList<E>
Type Parameters:
E - generic typeto be used in this list
All Implemented Interfaces:
Iterable<E>, Collection<E>, List<E>, SequencedCollection<E>

public class WeakList<E> extends 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.
  • Constructor Details

    • WeakList

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

    • get

      public E get(int index)
      Specified by:
      get in interface List<E>
      Specified by:
      get in class 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 Collection<E>
      Specified by:
      size in interface List<E>
      Specified by:
      size in class 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 List<E>
      Overrides:
      set in class 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 List<E>
      Overrides:
      add in class 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 List<E>
      Overrides:
      remove in class AbstractList<E>
      Parameters:
      index - of element to be removed
      Returns:
      previous object on that position
    • hardList

      public 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.