Class CursorableLinkedList<E>

  • All Implemented Interfaces:
    java.io.Serializable, java.lang.Iterable<E>, java.util.Collection<E>, java.util.List<E>

    public class CursorableLinkedList<E>
    extends AbstractLinkedList<E>
    implements java.io.Serializable
    A List implementation with a ListIterator that allows concurrent modifications to the underlying list.

    This implementation supports all of the optional List operations. It extends AbstractLinkedList and thus provides the stack/queue/dequeue operations available in LinkedList.

    The main feature of this class is the ability to modify the list and the iterator at the same time. Both the listIterator() and cursor() methods provides access to a Cursor instance which extends ListIterator. The cursor allows changes to the list concurrent with changes to the iterator. Note that the iterator() method and sublists do not provide this cursor behaviour.

    The Cursor class is provided partly for backwards compatibility and partly because it allows the cursor to be directly closed. Closing the cursor is optional because references are held via a WeakReference. For most purposes, simply modify the iterator and list at will, and then let the garbage collector to the rest.

    Note that this implementation is not synchronized.

    Since:
    1.0
    See Also:
    LinkedList, Serialized Form
    • Constructor Detail

      • CursorableLinkedList

        public CursorableLinkedList()
        Constructor that creates.
      • CursorableLinkedList

        public CursorableLinkedList​(java.util.Collection<? extends E> coll)
        Constructor that copies the specified collection
        Parameters:
        coll - the collection to copy
    • Method Detail

      • iterator

        public java.util.Iterator<E> iterator()
        Returns an iterator that does not support concurrent modification.

        If the underlying list is modified while iterating using this iterator a ConcurrentModificationException will occur. The cursor behaviour is available via listIterator().

        Specified by:
        iterator in interface java.util.Collection<E>
        Specified by:
        iterator in interface java.lang.Iterable<E>
        Specified by:
        iterator in interface java.util.List<E>
        Overrides:
        iterator in class AbstractLinkedList<E>
        Returns:
        a new iterator that does not support concurrent modification
      • listIterator

        public java.util.ListIterator<E> listIterator()
        Returns a cursor iterator that allows changes to the underlying list in parallel.

        The cursor enables iteration and list changes to occur in any order without invalidating the iterator (from one thread). When elements are added to the list, an event is fired to all active cursors enabling them to adjust to the change in the list.

        When the "current" (i.e., last returned by ListIterator.next() or ListIterator.previous()) element of the list is removed, the cursor automatically adjusts to the change (invalidating the last returned value such that it cannot be removed).

        Specified by:
        listIterator in interface java.util.List<E>
        Overrides:
        listIterator in class AbstractLinkedList<E>
        Returns:
        a new cursor iterator
      • listIterator

        public java.util.ListIterator<E> listIterator​(int fromIndex)
        Returns a cursor iterator that allows changes to the underlying list in parallel.

        The cursor enables iteration and list changes to occur in any order without invalidating the iterator (from one thread). When elements are added to the list, an event is fired to all active cursors enabling them to adjust to the change in the list.

        When the "current" (i.e., last returned by ListIterator.next() or ListIterator.previous()) element of the list is removed, the cursor automatically adjusts to the change (invalidating the last returned value such that it cannot be removed).

        Specified by:
        listIterator in interface java.util.List<E>
        Overrides:
        listIterator in class AbstractLinkedList<E>
        Parameters:
        fromIndex - the index to start from
        Returns:
        a new cursor iterator
      • cursor

        public CursorableLinkedList.Cursor<E> cursor()
        Returns a CursorableLinkedList.Cursor for iterating through the elements of this list.

        A Cursor is a ListIterator with an additional close() method. Calling this method immediately discards the references to the cursor. If it is not called, then the garbage collector will still remove the reference as it is held via a WeakReference.

        The cursor enables iteration and list changes to occur in any order without invalidating the iterator (from one thread). When elements are added to the list, an event is fired to all active cursors enabling them to adjust to the change in the list.

        When the "current" (i.e., last returned by ListIterator.next() or ListIterator.previous()) element of the list is removed, the cursor automatically adjusts to the change (invalidating the last returned value such that it cannot be removed).

        The listIterator() method returns the same as this method, and can be cast to a Cursor if the close method is required.

        Returns:
        a new cursor iterator
      • cursor

        public CursorableLinkedList.Cursor<E> cursor​(int fromIndex)
        Returns a CursorableLinkedList.Cursor for iterating through the elements of this list starting from a specified index.

        A Cursor is a ListIterator with an additional close() method. Calling this method immediately discards the references to the cursor. If it is not called, then the garbage collector will still remove the reference as it is held via a WeakReference.

        The cursor enables iteration and list changes to occur in any order without invalidating the iterator (from one thread). When elements are added to the list, an event is fired to all active cursors enabling them to adjust to the change in the list.

        When the "current" (i.e., last returned by ListIterator.next() or ListIterator.previous()) element of the list is removed, the cursor automatically adjusts to the change (invalidating the last returned value such that it cannot be removed).

        The listIterator(int) method returns the same as this method, and can be cast to a Cursor if the close method is required.

        Parameters:
        fromIndex - the index to start from
        Returns:
        a new cursor iterator
        Throws:
        java.lang.IndexOutOfBoundsException - if the index is out of range (index < 0 || index > size()).