Top Description Inners Fields Constructors Methods
org.apache.commons.collections4.list

public Class CursorableLinkedList<E>

extends AbstractLinkedList<E>
implements Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable
Imports
java.io.IOException, .ObjectInputStream, .ObjectOutputStream, .Serializable, java.lang.ref.WeakReference, java.util.ArrayList, .Collection, .ConcurrentModificationException, .Iterator, .List, .ListIterator

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 java.util.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
java.util.LinkedList

Nested and Inner Type Summary

Modifier and TypeClass and Description
public 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.

Field Summary

Modifier and TypeField and Description
private transient List<WeakReference<CursorableLinkedList.Cursor<E>>>
cursors

A list of the cursor currently open on this list

private static final long
serialVersionUID

Ensure serialization compatibility

Inherited from org.apache.commons.collections4.list.AbstractLinkedList:
headermodCountsize

Constructor Summary

AccessConstructor and Description
public
CursorableLinkedList()

Constructor that creates.

public
CursorableLinkedList(final Collection<? extends E>
the collection to copy
coll
)

Constructor that copies the specified collection

Method Summary

Modifier and TypeMethod and Description
protected void
addNode(final AbstractLinkedList.Node<E>
new node to insert
nodeToInsert
,
final AbstractLinkedList.Node<E>
node to insert before
insertBeforeNode
)

Overrides org.apache.commons.collections4.list.AbstractLinkedList.addNode.

Inserts a new node into the list.
protected void
broadcastNodeChanged(final AbstractLinkedList.Node<E>
the node that was changed
node
)

Informs all of my registered cursors that the specified element was changed.

protected void
broadcastNodeInserted(final AbstractLinkedList.Node<E>
the node that was changed
node
)

Informs all of my registered cursors that the specified element was just added to my list.

protected void
broadcastNodeRemoved(final AbstractLinkedList.Node<E>
the node that was changed
node
)

Informs all of my registered cursors that the specified element was just removed from my list.

protected ListIterator<E>

Returns:

the list iterator for the sublist
createSubListListIterator
(final AbstractLinkedList.LinkedSubList<E>
the sublist to get an iterator for
subList
,
final int
the index to start from, relative to the sublist
fromIndex
)

Overrides org.apache.commons.collections4.list.AbstractLinkedList.createSubListListIterator.

Creates a list iterator for the sublist.
public CursorableLinkedList.Cursor<E>

Returns:

a new cursor iterator
cursor
()

Returns a Cursor for iterating through the elements of this list.

public CursorableLinkedList.Cursor<E>

Returns:

a new cursor iterator
cursor
(final int
the index to start from
fromIndex
)

Returns a Cursor for iterating through the elements of this list starting from a specified index.

protected void
init()

Overrides org.apache.commons.collections4.list.AbstractLinkedList.init.

The equivalent of a default constructor called by any constructor and by readObject.
public Iterator<E>

Returns:

a new iterator that does not support concurrent modification
iterator
()

Overrides org.apache.commons.collections4.list.AbstractLinkedList.iterator.

Implements java.util.List.iterator.

Returns an iterator that does not support concurrent modification.
public ListIterator<E>

Returns:

a new cursor iterator
listIterator
()

Overrides org.apache.commons.collections4.list.AbstractLinkedList.listIterator.

Implements java.util.List.listIterator.

Returns a cursor iterator that allows changes to the underlying list in parallel.
public ListIterator<E>

Returns:

a new cursor iterator
listIterator
(final int
the index to start from
fromIndex
)

Overrides org.apache.commons.collections4.list.AbstractLinkedList.listIterator.

Implements java.util.List.listIterator.

Returns a cursor iterator that allows changes to the underlying list in parallel.
private void
readObject(final ObjectInputStream
the input stream
in
)

Deserializes the data held in this object to the stream specified.

protected void
registerCursor(final CursorableLinkedList.Cursor<E>
the cursor to register
cursor
)

Registers a cursor to be notified of changes to this list.

protected void
protected void
removeNode(final AbstractLinkedList.Node<E>
the node to remove
node
)

Overrides org.apache.commons.collections4.list.AbstractLinkedList.removeNode.

Removes the specified node from the list.
protected void
unregisterCursor(final CursorableLinkedList.Cursor<E>
the cursor to deregister
cursor
)

Deregisters a cursor from the list to be notified of changes.

protected void
updateNode(final AbstractLinkedList.Node<E>
node to update
node
,
final E
new value of the node
value
)

Overrides org.apache.commons.collections4.list.AbstractLinkedList.updateNode.

Updates the node with a new value.
private void
writeObject(final ObjectOutputStream
the output stream
out
)

Serializes the data held in this object to the stream specified.

Inherited from org.apache.commons.collections4.list.AbstractLinkedList:
addaddaddAlladdAlladdFirstaddLastaddNodeAfteraddNodeBeforeclearcontainscontainsAllcreateHeaderNodecreateNodecreateSubListIteratordoReadObjectdoWriteObjectequalsgetgetFirstgetLastgetNodehashCodeindexOfisEmptyisEqualValuelastIndexOfremoveremoveremoveAllremoveFirstremoveLastretainAllsetsizesubListtoArraytoArraytoString

Field Detail

cursorsback to summary
private transient List<WeakReference<CursorableLinkedList.Cursor<E>>> cursors

A list of the cursor currently open on this list

serialVersionUIDback to summary
private static final long serialVersionUID

Ensure serialization compatibility

Constructor Detail

CursorableLinkedListback to summary
public CursorableLinkedList()

Constructor that creates.

CursorableLinkedListback to summary
public CursorableLinkedList(final Collection<? extends E> coll)

Constructor that copies the specified collection

Parameters
coll:Collection<? extends E>

the collection to copy

Method Detail

addNodeback to summary
protected void addNode(final AbstractLinkedList.Node<E> nodeToInsert, final AbstractLinkedList.Node<E> insertBeforeNode)

Overrides org.apache.commons.collections4.list.AbstractLinkedList.addNode.

Inserts a new node into the list.

Parameters
nodeToInsert:AbstractLinkedList.Node<E>

new node to insert

insertBeforeNode:AbstractLinkedList.Node<E>

node to insert before

Annotations
@Override
Exceptions
NullPointerException:
if either node is null
broadcastNodeChangedback to summary
protected void broadcastNodeChanged(final AbstractLinkedList.Node<E> node)

Informs all of my registered cursors that the specified element was changed.

Parameters
node:AbstractLinkedList.Node<E>

the node that was changed

broadcastNodeInsertedback to summary
protected void broadcastNodeInserted(final AbstractLinkedList.Node<E> node)

Informs all of my registered cursors that the specified element was just added to my list.

Parameters
node:AbstractLinkedList.Node<E>

the node that was changed

broadcastNodeRemovedback to summary
protected void broadcastNodeRemoved(final AbstractLinkedList.Node<E> node)

Informs all of my registered cursors that the specified element was just removed from my list.

Parameters
node:AbstractLinkedList.Node<E>

the node that was changed

createSubListListIteratorback to summary
protected ListIterator<E> createSubListListIterator(final AbstractLinkedList.LinkedSubList<E> subList, final int fromIndex)

Overrides org.apache.commons.collections4.list.AbstractLinkedList.createSubListListIterator.

Creates a list iterator for the sublist.

Parameters
subList:AbstractLinkedList.LinkedSubList<E>

the sublist to get an iterator for

fromIndex:int

the index to start from, relative to the sublist

Returns:ListIterator<E>

the list iterator for the sublist

Annotations
@Override
cursorback to summary
public CursorableLinkedList.Cursor<E> cursor()

Returns a 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:CursorableLinkedList.Cursor<E>

a new cursor iterator

cursorback to summary
public CursorableLinkedList.Cursor<E> cursor(final int fromIndex)

Returns a 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:int

the index to start from

Returns:CursorableLinkedList.Cursor<E>

a new cursor iterator

Exceptions
IndexOutOfBoundsException:
if the index is out of range (index < 0 || index > size()).
initback to summary
protected void init()

Overrides org.apache.commons.collections4.list.AbstractLinkedList.init.

The equivalent of a default constructor called by any constructor and by readObject.

Annotations
@Override
iteratorback to summary
public Iterator<E> iterator()

Overrides org.apache.commons.collections4.list.AbstractLinkedList.iterator.

Implements java.util.List.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().

Returns:Iterator<E>

a new iterator that does not support concurrent modification

Annotations
@Override
listIteratorback to summary
public ListIterator<E> listIterator()

Overrides org.apache.commons.collections4.list.AbstractLinkedList.listIterator.

Implements java.util.List.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).

Returns:ListIterator<E>

a new cursor iterator

Annotations
@Override
listIteratorback to summary
public ListIterator<E> listIterator(final int fromIndex)

Overrides org.apache.commons.collections4.list.AbstractLinkedList.listIterator.

Implements java.util.List.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).

Parameters
fromIndex:int

the index to start from

Returns:ListIterator<E>

a new cursor iterator

Annotations
@Override
readObjectback to summary
private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException

Deserializes the data held in this object to the stream specified.

Parameters
in:ObjectInputStream

the input stream

Exceptions
IOException:
if an error occurs while reading from the stream
ClassNotFoundException:
if an object read from the stream can not be loaded
registerCursorback to summary
protected void registerCursor(final CursorableLinkedList.Cursor<E> cursor)

Registers a cursor to be notified of changes to this list.

Parameters
cursor:CursorableLinkedList.Cursor<E>

the cursor to register

removeAllNodesback to summary
protected void removeAllNodes()

Overrides org.apache.commons.collections4.list.AbstractLinkedList.removeAllNodes.

Removes all nodes by iteration.

Annotations
@Override
removeNodeback to summary
protected void removeNode(final AbstractLinkedList.Node<E> node)

Overrides org.apache.commons.collections4.list.AbstractLinkedList.removeNode.

Removes the specified node from the list.

Parameters
node:AbstractLinkedList.Node<E>

the node to remove

Annotations
@Override
Exceptions
NullPointerException:
if node is null
unregisterCursorback to summary
protected void unregisterCursor(final CursorableLinkedList.Cursor<E> cursor)

Deregisters a cursor from the list to be notified of changes.

Parameters
cursor:CursorableLinkedList.Cursor<E>

the cursor to deregister

updateNodeback to summary
protected void updateNode(final AbstractLinkedList.Node<E> node, final E value)

Overrides org.apache.commons.collections4.list.AbstractLinkedList.updateNode.

Updates the node with a new value. This implementation sets the value on the node. Subclasses can override this to record the change.

Parameters
node:AbstractLinkedList.Node<E>

node to update

value:E

new value of the node

Annotations
@Override
writeObjectback to summary
private void writeObject(final ObjectOutputStream out) throws IOException

Serializes the data held in this object to the stream specified.

Parameters
out:ObjectOutputStream

the output stream

Exceptions
IOException:
if an error occurs while writing to the stream
org.apache.commons.collections4.list back to summary

public Class CursorableLinkedList.Cursor<E>

extends AbstractLinkedList.LinkedListIterator<E>
Class Inheritance
Known Direct Subclasses
org.apache.commons.collections4.list.CursorableLinkedList.SubCursor

An extended ListIterator that allows concurrent changes to the underlying list.

Field Summary

Modifier and TypeField and Description
pack-priv boolean
currentRemovedByAnother

Flag to indicate if the current element was removed by another object.

pack-priv boolean
nextIndexValid

Is the next index valid

pack-priv boolean
valid

Is the cursor valid (not closed)

Inherited from org.apache.commons.collections4.list.AbstractLinkedList.LinkedListIterator:
currentexpectedModCountnextnextIndexparent

Constructor Summary

AccessConstructor and Description
protected
Cursor(final CursorableLinkedList<E>
the parent list
parent
,
final int
the index to start from
index
)

Constructs a new cursor.

Method Summary

Modifier and TypeMethod and Description
public void
add(final E
the object to add
obj
)

Overrides org.apache.commons.collections4.list.AbstractLinkedList.LinkedListIterator.add.

Implements java.util.ListIterator.add.

Adds an object to the list.
protected void
checkModCount()

Overrides org.apache.commons.collections4.list.AbstractLinkedList.LinkedListIterator.checkModCount.

Override superclass modCount check, and replace it with our valid flag.
public void
close()

Mark this cursor as no longer being needed.

public int

Returns:

the next index
nextIndex
()

Overrides org.apache.commons.collections4.list.AbstractLinkedList.LinkedListIterator.nextIndex.

Implements java.util.ListIterator.nextIndex.

Gets the index of the next element to be returned.
protected void
nodeChanged(final AbstractLinkedList.Node<E>
the node that changed
node
)

Handle event from the list when a node has changed.

protected void
nodeInserted(final AbstractLinkedList.Node<E>
the node that was added
node
)

Handle event from the list when a node has been added.

protected void
nodeRemoved(final AbstractLinkedList.Node<E>
the node that was removed
node
)

Handle event from the list when a node has been removed.

public void
remove()

Overrides org.apache.commons.collections4.list.AbstractLinkedList.LinkedListIterator.remove.

Overrides default java.util.Iterator.remove.

Implements java.util.ListIterator.remove.

Removes the item last returned by this iterator.
Inherited from org.apache.commons.collections4.list.AbstractLinkedList.LinkedListIterator:
getLastNodeReturnedhasNexthasPreviousnextpreviouspreviousIndexset

Field Detail

currentRemovedByAnotherback to summary
pack-priv boolean currentRemovedByAnother

Flag to indicate if the current element was removed by another object.

nextIndexValidback to summary
pack-priv boolean nextIndexValid

Is the next index valid

validback to summary
pack-priv boolean valid

Is the cursor valid (not closed)

Constructor Detail

Cursorback to summary
protected Cursor(final CursorableLinkedList<E> parent, final int index)

Constructs a new cursor.

Parameters
parent:CursorableLinkedList<E>

the parent list

index:int

the index to start from

Method Detail

addback to summary
public void add(final E obj)

Overrides org.apache.commons.collections4.list.AbstractLinkedList.LinkedListIterator.add.

Implements java.util.ListIterator.add.

Adds an object to the list. The object added here will be the new 'previous' in the iterator.

Parameters
obj:E

the object to add

Annotations
@Override
checkModCountback to summary
protected void checkModCount()

Overrides org.apache.commons.collections4.list.AbstractLinkedList.LinkedListIterator.checkModCount.

Override superclass modCount check, and replace it with our valid flag.

Annotations
@Override
closeback to summary
public void close()

Mark this cursor as no longer being needed. Any resources associated with this cursor are immediately released. In previous versions of this class, it was mandatory to close all cursor objects to avoid memory leaks. It is no longer necessary to call this close method; an instance of this class can now be treated exactly like a normal iterator.

nextIndexback to summary
public int nextIndex()

Overrides org.apache.commons.collections4.list.AbstractLinkedList.LinkedListIterator.nextIndex.

Implements java.util.ListIterator.nextIndex.

Gets the index of the next element to be returned.

Returns:int

the next index

Annotations
@Override
nodeChangedback to summary
protected void nodeChanged(final AbstractLinkedList.Node<E> node)

Handle event from the list when a node has changed.

Parameters
node:AbstractLinkedList.Node<E>

the node that changed

nodeInsertedback to summary
protected void nodeInserted(final AbstractLinkedList.Node<E> node)

Handle event from the list when a node has been added.

Parameters
node:AbstractLinkedList.Node<E>

the node that was added

nodeRemovedback to summary
protected void nodeRemoved(final AbstractLinkedList.Node<E> node)

Handle event from the list when a node has been removed.

Parameters
node:AbstractLinkedList.Node<E>

the node that was removed

removeback to summary
public void remove()

Overrides org.apache.commons.collections4.list.AbstractLinkedList.LinkedListIterator.remove.

Overrides default java.util.Iterator.remove.

Implements java.util.ListIterator.remove.

Removes the item last returned by this iterator.

There may have been subsequent alterations to the list since you obtained this item, however you can still remove it. You can even remove it if the item is no longer in the main list. However, you can't call this method on the same iterator more than once without calling next() or previous().

Annotations
@Override
Exceptions
IllegalStateException:
if there is no item to remove
org.apache.commons.collections4.list back to summary

protected Class CursorableLinkedList.SubCursor<E>

extends CursorableLinkedList.Cursor<E>
Class Inheritance

A cursor for the sublist based on LinkedSubListIterator.
Since
3.2

Field Summary

Modifier and TypeField and Description
protected final AbstractLinkedList.LinkedSubList<E>
sub

The parent list

Inherited from org.apache.commons.collections4.list.CursorableLinkedList.Cursor:
currentRemovedByAnothernextIndexValidvalid

Constructor Summary

AccessConstructor and Description
protected
SubCursor(final AbstractLinkedList.LinkedSubList<E>
the sub list
sub
,
final int
the index to start from
index
)

Constructs a new cursor.

Method Summary

Modifier and TypeMethod and Description
public void
add(final E
the object to add
obj
)

Overrides org.apache.commons.collections4.list.CursorableLinkedList.Cursor.add.

Implements java.util.ListIterator.add.

Adds an object to the list.
public boolean
hasNext()

Overrides org.apache.commons.collections4.list.AbstractLinkedList.LinkedListIterator.hasNext.

Implements java.util.ListIterator.hasNext, java.util.Iterator.hasNext.

Returns true if this list iterator has more elements when traversing the list in the forward direction.
public boolean
hasPrevious()

Overrides org.apache.commons.collections4.list.AbstractLinkedList.LinkedListIterator.hasPrevious.

Implements java.util.ListIterator.hasPrevious, org.apache.commons.collections4.OrderedIterator.hasPrevious.

Returns true if this list iterator has more elements when traversing the list in the reverse direction.
public int
nextIndex()

Overrides org.apache.commons.collections4.list.CursorableLinkedList.Cursor.nextIndex.

Implements java.util.ListIterator.nextIndex.

Gets the index of the next element to be returned.
public void
remove()

Overrides org.apache.commons.collections4.list.CursorableLinkedList.Cursor.remove.

Overrides default java.util.Iterator.remove.

Implements java.util.ListIterator.remove.

Removes the item last returned by this iterator.
Inherited from org.apache.commons.collections4.list.CursorableLinkedList.Cursor:
checkModCountclosenodeChangednodeInsertednodeRemoved

Field Detail

subback to summary
protected final AbstractLinkedList.LinkedSubList<E> sub

The parent list

Constructor Detail

SubCursorback to summary
protected SubCursor(final AbstractLinkedList.LinkedSubList<E> sub, final int index)

Constructs a new cursor.

Parameters
sub:AbstractLinkedList.LinkedSubList<E>

the sub list

index:int

the index to start from

Method Detail

addback to summary
public void add(final E obj)

Overrides org.apache.commons.collections4.list.CursorableLinkedList.Cursor.add.

Implements java.util.ListIterator.add.

Doc from org.apache.commons.collections4.list.CursorableLinkedList.Cursor.add.

Adds an object to the list. The object added here will be the new 'previous' in the iterator.

Parameters
obj:E

the object to add

Annotations
@Override
hasNextback to summary
public boolean hasNext()

Overrides org.apache.commons.collections4.list.AbstractLinkedList.LinkedListIterator.hasNext.

Implements java.util.ListIterator.hasNext, java.util.Iterator.hasNext.

Doc from java.util.ListIterator.hasNext.

Returns true if this list iterator has more elements when traversing the list in the forward direction. (In other words, returns true if next would return an element rather than throwing an exception.)

Returns:boolean

true if the list iterator has more elements when traversing the list in the forward direction

Annotations
@Override
hasPreviousback to summary
public boolean hasPrevious()

Overrides org.apache.commons.collections4.list.AbstractLinkedList.LinkedListIterator.hasPrevious.

Implements java.util.ListIterator.hasPrevious, org.apache.commons.collections4.OrderedIterator.hasPrevious.

Doc from java.util.ListIterator.hasPrevious.

Returns true if this list iterator has more elements when traversing the list in the reverse direction. (In other words, returns true if previous would return an element rather than throwing an exception.)

Returns:boolean

true if the list iterator has more elements when traversing the list in the reverse direction

Annotations
@Override
nextIndexback to summary
public int nextIndex()

Overrides org.apache.commons.collections4.list.CursorableLinkedList.Cursor.nextIndex.

Implements java.util.ListIterator.nextIndex.

Doc from org.apache.commons.collections4.list.CursorableLinkedList.Cursor.nextIndex.

Gets the index of the next element to be returned.

Returns:int

the next index

Annotations
@Override
removeback to summary
public void remove()

Overrides org.apache.commons.collections4.list.CursorableLinkedList.Cursor.remove.

Overrides default java.util.Iterator.remove.

Implements java.util.ListIterator.remove.

Doc from org.apache.commons.collections4.list.CursorableLinkedList.Cursor.remove.

Removes the item last returned by this iterator.

There may have been subsequent alterations to the list since you obtained this item, however you can still remove it. You can even remove it if the item is no longer in the main list. However, you can't call this method on the same iterator more than once without calling next() or previous().

Annotations
@Override