Class CursorableLinkedList<E>
- java.lang.Object
-
- org.apache.commons.collections4.list.AbstractLinkedList<E>
-
- org.apache.commons.collections4.list.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
AListimplementation with aListIteratorthat allows concurrent modifications to the underlying list.This implementation supports all of the optional
Listoperations. It extendsAbstractLinkedListand thus provides the stack/queue/dequeue operations available inLinkedList.The main feature of this class is the ability to modify the list and the iterator at the same time. Both the
listIterator()andcursor()methods provides access to aCursorinstance which extendsListIterator. The cursor allows changes to the list concurrent with changes to the iterator. Note that theiterator()method and sublists do not provide this cursor behaviour.The
Cursorclass 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 aWeakReference. 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
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classCursorableLinkedList.Cursor<E>An extendedListIteratorthat allows concurrent changes to the underlying list.protected static classCursorableLinkedList.SubCursor<E>A cursor for the sublist based on LinkedSubListIterator.-
Nested classes/interfaces inherited from class org.apache.commons.collections4.list.AbstractLinkedList
AbstractLinkedList.LinkedListIterator<E>, AbstractLinkedList.LinkedSubList<E>, AbstractLinkedList.LinkedSubListIterator<E>, AbstractLinkedList.Node<E>
-
-
Constructor Summary
Constructors Constructor Description CursorableLinkedList()Constructor that creates.CursorableLinkedList(java.util.Collection<? extends E> coll)Constructor that copies the specified collection
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected voidaddNode(AbstractLinkedList.Node<E> nodeToInsert, AbstractLinkedList.Node<E> insertBeforeNode)Inserts a new node into the list.protected voidbroadcastNodeChanged(AbstractLinkedList.Node<E> node)Informs all of my registered cursors that the specified element was changed.protected voidbroadcastNodeInserted(AbstractLinkedList.Node<E> node)Informs all of my registered cursors that the specified element was just added to my list.protected voidbroadcastNodeRemoved(AbstractLinkedList.Node<E> node)Informs all of my registered cursors that the specified element was just removed from my list.protected java.util.ListIterator<E>createSubListListIterator(AbstractLinkedList.LinkedSubList<E> subList, int fromIndex)Creates a list iterator for the sublist.CursorableLinkedList.Cursor<E>cursor()Returns aCursorableLinkedList.Cursorfor iterating through the elements of this list.CursorableLinkedList.Cursor<E>cursor(int fromIndex)Returns aCursorableLinkedList.Cursorfor iterating through the elements of this list starting from a specified index.protected voidinit()The equivalent of a default constructor called by any constructor and byreadObject.java.util.Iterator<E>iterator()Returns an iterator that does not support concurrent modification.java.util.ListIterator<E>listIterator()Returns a cursor iterator that allows changes to the underlying list in parallel.java.util.ListIterator<E>listIterator(int fromIndex)Returns a cursor iterator that allows changes to the underlying list in parallel.protected voidregisterCursor(CursorableLinkedList.Cursor<E> cursor)Registers a cursor to be notified of changes to this list.protected voidremoveAllNodes()Removes all nodes by iteration.protected voidremoveNode(AbstractLinkedList.Node<E> node)Removes the specified node from the list.protected voidunregisterCursor(CursorableLinkedList.Cursor<E> cursor)Deregisters a cursor from the list to be notified of changes.protected voidupdateNode(AbstractLinkedList.Node<E> node, E value)Updates the node with a new value.-
Methods inherited from class org.apache.commons.collections4.list.AbstractLinkedList
add, add, addAll, addAll, addFirst, addLast, addNodeAfter, addNodeBefore, clear, contains, containsAll, createHeaderNode, createNode, createSubListIterator, doReadObject, doWriteObject, equals, get, getFirst, getLast, getNode, hashCode, indexOf, isEmpty, isEqualValue, lastIndexOf, remove, remove, removeAll, removeFirst, removeLast, retainAll, set, size, subList, toArray, toArray, toString
-
-
-
-
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
-
init
protected void init()
The equivalent of a default constructor called by any constructor and byreadObject.- Overrides:
initin classAbstractLinkedList<E>
-
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().
-
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()orListIterator.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:
listIteratorin interfacejava.util.List<E>- Overrides:
listIteratorin classAbstractLinkedList<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()orListIterator.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:
listIteratorin interfacejava.util.List<E>- Overrides:
listIteratorin classAbstractLinkedList<E>- Parameters:
fromIndex- the index to start from- Returns:
- a new cursor iterator
-
cursor
public CursorableLinkedList.Cursor<E> cursor()
Returns aCursorableLinkedList.Cursorfor iterating through the elements of this list.A
Cursoris aListIteratorwith an additionalclose()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 aWeakReference.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()orListIterator.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 aCursorif theclosemethod is required.- Returns:
- a new cursor iterator
-
cursor
public CursorableLinkedList.Cursor<E> cursor(int fromIndex)
Returns aCursorableLinkedList.Cursorfor iterating through the elements of this list starting from a specified index.A
Cursoris aListIteratorwith an additionalclose()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 aWeakReference.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()orListIterator.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 aCursorif theclosemethod 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()).
-
updateNode
protected void updateNode(AbstractLinkedList.Node<E> node, E value)
Updates the node with a new value. This implementation sets the value on the node. Subclasses can override this to record the change.- Overrides:
updateNodein classAbstractLinkedList<E>- Parameters:
node- node to updatevalue- new value of the node
-
addNode
protected void addNode(AbstractLinkedList.Node<E> nodeToInsert, AbstractLinkedList.Node<E> insertBeforeNode)
Inserts a new node into the list.- Overrides:
addNodein classAbstractLinkedList<E>- Parameters:
nodeToInsert- new node to insertinsertBeforeNode- node to insert before- Throws:
java.lang.NullPointerException- if either node is null
-
removeNode
protected void removeNode(AbstractLinkedList.Node<E> node)
Removes the specified node from the list.- Overrides:
removeNodein classAbstractLinkedList<E>- Parameters:
node- the node to remove- Throws:
java.lang.NullPointerException- ifnodeis null
-
removeAllNodes
protected void removeAllNodes()
Removes all nodes by iteration.- Overrides:
removeAllNodesin classAbstractLinkedList<E>
-
registerCursor
protected void registerCursor(CursorableLinkedList.Cursor<E> cursor)
Registers a cursor to be notified of changes to this list.- Parameters:
cursor- the cursor to register
-
unregisterCursor
protected void unregisterCursor(CursorableLinkedList.Cursor<E> cursor)
Deregisters a cursor from the list to be notified of changes.- Parameters:
cursor- the cursor to deregister
-
broadcastNodeChanged
protected void broadcastNodeChanged(AbstractLinkedList.Node<E> node)
Informs all of my registered cursors that the specified element was changed.- Parameters:
node- the node that was changed
-
broadcastNodeRemoved
protected void broadcastNodeRemoved(AbstractLinkedList.Node<E> node)
Informs all of my registered cursors that the specified element was just removed from my list.- Parameters:
node- the node that was changed
-
broadcastNodeInserted
protected void broadcastNodeInserted(AbstractLinkedList.Node<E> node)
Informs all of my registered cursors that the specified element was just added to my list.- Parameters:
node- the node that was changed
-
createSubListListIterator
protected java.util.ListIterator<E> createSubListListIterator(AbstractLinkedList.LinkedSubList<E> subList, int fromIndex)
Creates a list iterator for the sublist.- Overrides:
createSubListListIteratorin classAbstractLinkedList<E>- Parameters:
subList- the sublist to get an iterator forfromIndex- the index to start from, relative to the sublist- Returns:
- the list iterator for the sublist
-
-