public class CursorableLinkedList<E> extends AbstractLinkedList<E> implements java.io.Serializable
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.
LinkedList
,
Serialized FormModifier and Type | Class and Description |
---|---|
static class |
CursorableLinkedList.Cursor<E>
An extended
ListIterator that allows concurrent changes to
the underlying list. |
protected static class |
CursorableLinkedList.SubCursor<E>
A cursor for the sublist based on LinkedSubListIterator.
|
AbstractLinkedList.LinkedListIterator<E>, AbstractLinkedList.LinkedSubList<E>, AbstractLinkedList.LinkedSubListIterator<E>, AbstractLinkedList.Node<E>
Modifier and Type | Field and Description |
---|---|
private java.util.List<java.lang.ref.WeakReference<CursorableLinkedList.Cursor<E>>> |
cursors
A list of the cursor currently open on this list
|
private static long |
serialVersionUID
Ensure serialization compatibility
|
header, modCount, size
Constructor and Description |
---|
CursorableLinkedList()
Constructor that creates.
|
CursorableLinkedList(java.util.Collection<? extends E> coll)
Constructor that copies the specified collection
|
Modifier and Type | Method and Description |
---|---|
protected void |
addNode(AbstractLinkedList.Node<E> nodeToInsert,
AbstractLinkedList.Node<E> insertBeforeNode)
Inserts a new node into the list.
|
protected void |
broadcastNodeChanged(AbstractLinkedList.Node<E> node)
Informs all of my registered cursors that the specified
element was changed.
|
protected void |
broadcastNodeInserted(AbstractLinkedList.Node<E> node)
Informs all of my registered cursors that the specified
element was just added to my list.
|
protected void |
broadcastNodeRemoved(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 a
CursorableLinkedList.Cursor for iterating through the elements of this list. |
CursorableLinkedList.Cursor<E> |
cursor(int fromIndex)
Returns a
CursorableLinkedList.Cursor for iterating through the elements of this list
starting from a specified index. |
protected void |
init()
The equivalent of a default constructor called
by any constructor and by
readObject . |
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.
|
private void |
readObject(java.io.ObjectInputStream in)
Deserializes the data held in this object to the stream specified.
|
protected void |
registerCursor(CursorableLinkedList.Cursor<E> cursor)
Registers a cursor to be notified of changes to this list.
|
protected void |
removeAllNodes()
Removes all nodes by iteration.
|
protected void |
removeNode(AbstractLinkedList.Node<E> node)
Removes the specified node from the list.
|
protected void |
unregisterCursor(CursorableLinkedList.Cursor<E> cursor)
Deregisters a cursor from the list to be notified of changes.
|
protected void |
updateNode(AbstractLinkedList.Node<E> node,
E value)
Updates the node with a new value.
|
private void |
writeObject(java.io.ObjectOutputStream out)
Serializes the data held in this object to the stream specified.
|
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
private static final long serialVersionUID
private transient java.util.List<java.lang.ref.WeakReference<CursorableLinkedList.Cursor<E>>> cursors
public CursorableLinkedList()
public CursorableLinkedList(java.util.Collection<? extends E> coll)
coll
- the collection to copyprotected void init()
readObject
.init
in class AbstractLinkedList<E>
public java.util.Iterator<E> iterator()
If the underlying list is modified while iterating using this iterator
a ConcurrentModificationException will occur.
The cursor behaviour is available via listIterator()
.
public java.util.ListIterator<E> listIterator()
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).
listIterator
in interface java.util.List<E>
listIterator
in class AbstractLinkedList<E>
public java.util.ListIterator<E> listIterator(int fromIndex)
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).
listIterator
in interface java.util.List<E>
listIterator
in class AbstractLinkedList<E>
fromIndex
- the index to start frompublic CursorableLinkedList.Cursor<E> cursor()
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.
public CursorableLinkedList.Cursor<E> cursor(int fromIndex)
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.
fromIndex
- the index to start fromjava.lang.IndexOutOfBoundsException
- if the index is out of range
(index < 0 || index > size()).protected void updateNode(AbstractLinkedList.Node<E> node, E value)
updateNode
in class AbstractLinkedList<E>
node
- node to updatevalue
- new value of the nodeprotected void addNode(AbstractLinkedList.Node<E> nodeToInsert, AbstractLinkedList.Node<E> insertBeforeNode)
addNode
in class AbstractLinkedList<E>
nodeToInsert
- new node to insertinsertBeforeNode
- node to insert beforejava.lang.NullPointerException
- if either node is nullprotected void removeNode(AbstractLinkedList.Node<E> node)
removeNode
in class AbstractLinkedList<E>
node
- the node to removejava.lang.NullPointerException
- if node
is nullprotected void removeAllNodes()
removeAllNodes
in class AbstractLinkedList<E>
protected void registerCursor(CursorableLinkedList.Cursor<E> cursor)
cursor
- the cursor to registerprotected void unregisterCursor(CursorableLinkedList.Cursor<E> cursor)
cursor
- the cursor to deregisterprotected void broadcastNodeChanged(AbstractLinkedList.Node<E> node)
node
- the node that was changedprotected void broadcastNodeRemoved(AbstractLinkedList.Node<E> node)
node
- the node that was changedprotected void broadcastNodeInserted(AbstractLinkedList.Node<E> node)
node
- the node that was changedprivate void writeObject(java.io.ObjectOutputStream out) throws java.io.IOException
java.io.IOException
private void readObject(java.io.ObjectInputStream in) throws java.io.IOException, java.lang.ClassNotFoundException
java.io.IOException
java.lang.ClassNotFoundException
protected java.util.ListIterator<E> createSubListListIterator(AbstractLinkedList.LinkedSubList<E> subList, int fromIndex)
createSubListListIterator
in class AbstractLinkedList<E>
subList
- the sublist to get an iterator forfromIndex
- the index to start from, relative to the sublist