Class WeakArrayList<T>

  • Type Parameters:
    T - is the type of the array's elements.
    All Implemented Interfaces:
    Iterable<T>, Collection<T>, List<T>

    public class WeakArrayList<T>
    extends AbstractList<T>
    A array-based List implementation with weak keys. An entry in a WeakArrayList will automatically be removed when it is no longer in ordinary use.

    The behavior of the WeakArrayList class depends in part upon the actions of the garbage collector, so several familiar (though not required) List invariants do not hold for this class. Because the garbage collector may discard values at any time, a WeakArrayList may behave as though an unknown thread is silently removing entries. In particular, even if you synchronize on a WeakArrayList instance and invoke none of its mutator methods, it is possible for the size method to return smaller values over time, for the isEmpty method to return false and then true, for the contains method to return true and later false for a given value, for the get method to return a value for a given key but later return null, for the add method to return null and the remove method to return false for a value that previously appeared to be in the list.

    If this map does not use a "deep expurge" of the released references, it could contains null values that corresponds to values that are released by the garbage collector. If a "deep expurge" is used, all the values released by the garbage collector will be removed from the map.

    "Deep expurge" consumes much more time that "No deep expurge". This is the reason why this feature is not activated by default.

    The "deep expurge" feature was added to fix the uncoherent behavior of the garbage collector which seems to not always enqueued the released values (sometimes the queue is empty even if a value was released).

    Version:
    17.0 2020-01-04 14:41:38
    Author:
    Stéphane GALLAND
    Maven Group Id:
    org.arakhne.afc.core
    Maven Artifact Id:
    references
    • Constructor Detail

      • WeakArrayList

        public WeakArrayList​(int initialCapacity)
        Constructs an empty list with the specified initial capacity.
        Parameters:
        initialCapacity - the initial capacity of the list
        Throws:
        IllegalArgumentException - if the specified initial capacity is negative
      • WeakArrayList

        public WeakArrayList()
        Constructs an empty list with an initial capacity of ten.
      • WeakArrayList

        public WeakArrayList​(Collection<? extends T> collection)
        Constructs a list containing the elements of the specified collection, in the order they are returned by the collection's iterator.
        Parameters:
        collection - the collection whose elements are to be placed into this list
        Throws:
        NullPointerException - if the specified collection is null
    • Method Detail

      • ensureCapacity

        public void ensureCapacity​(int minCapacity)
        Increases the capacity of this WeakArrayList instance, if necessary, to ensure that it can hold at least the number of elements specified by the minimum capacity argument.
        Parameters:
        minCapacity - the desired minimum capacity
      • trimToSize

        public void trimToSize()
        Trims the capacity of this WeakArrayList instance to be the list's current size. An application can use this operation to minimize the storage of an WeakArrayList instance.
      • expurge

        public int expurge()
        Clean the references that was released.

        Notifies the listeners if a reference was released.

        Returns:
        the size
      • assertRange

        protected void assertRange​(int index,
                                   boolean allowLast)
        Verify if the specified index is inside the array.
        Parameters:
        index - is the index totest
        allowLast - indicates if the last elements is assumed to be valid or not.
      • get

        @Pure
        public T get​(int index)
        Specified by:
        get in interface List<T>
        Specified by:
        get in class AbstractList<T>
      • set

        public T set​(int index,
                     T element)
        Specified by:
        set in interface List<T>
        Overrides:
        set in class AbstractList<T>
      • add

        public void add​(int index,
                        T element)
        Specified by:
        add in interface List<T>
        Overrides:
        add in class AbstractList<T>
      • addReferenceListener

        public void addReferenceListener​(ReferenceListener listener)
        Add listener on reference's release.
        Parameters:
        listener - the listener.
      • removeReferenceListener

        public void removeReferenceListener​(ReferenceListener listener)
        Remove listener on reference's release.
        Parameters:
        listener - the listener.
      • fireReferenceRelease

        protected void fireReferenceRelease​(int released)
        Fire the reference release event.
        Parameters:
        released - is the count of released objects.