Class CursorableLinkedList<E>
- Type Parameters:
E
- the type of elements held in this collection
- All Implemented Interfaces:
Serializable
,Iterable<E>
,Collection<E>
,List<E>
- Direct Known Subclasses:
CursorableSubList
This class has been copied from Commons Collections, version 3.1 in order to eliminate the dependency of pool on collections. It has package scope to prevent its inclusion in the pool public API. The class declaration below should *not* be changed to public.
A doubly-linked list implementation of theList
interface,
supporting a ListIterator
that allows concurrent modifications
to the underlying list.
Implements all of the optional List
operations, the
stack/queue/dequeue operations available in LinkedList
and supports a ListIterator
that allows concurrent modifications
to the underlying list (see cursor()
).
Note that this implementation is not synchronized.
- Version:
- $Revision: 480452 $ $Date: 2006-11-29 00:45:14 -0700 (Wed, 29 Nov 2006) $
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionclass
(package private) static class
(package private) class
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected List
<WeakReference<CursorableLinkedList<E>.Cursor>> A list of the currentlyCursorableLinkedList.Cursor
s currently open in this list.protected CursorableLinkedList.Listable
<E> A sentry node.protected int
Tracks the number of structural modifications to me.protected int
The number of elements in me.private static final long
Ensure serialization compatibility -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
Inserts the specified element at the specified position in this list.boolean
Appends the specified element to the end of this list.boolean
addAll
(int index, Collection<? extends E> c) Inserts all of the elements in the specified collection into this list at the specified position.boolean
addAll
(Collection<? extends E> c) Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specifiedCollection
'sIterator
.boolean
Inserts the specified element at the beginning of this list.boolean
Inserts the specified element at the end of this list.protected void
Informs all of my registered cursors that the specified element was changed.protected void
Informs all of my registered cursors that the specified element was just added to my list.protected void
Informs all of my registered cursors that the specified element was just removed from my list.void
clear()
Removes all of the elements from this list.boolean
Returns true if this list contains the specified element.boolean
containsAll
(Collection<?> c) Returns true if this list contains all of the elements of the specified collection.cursor()
Returns aListIterator
for iterating through the elements of this list.cursor
(int i) Returns aListIterator
for iterating through the elements of this list, initialized such thatListIterator.next()
will return the element at the specified index (if any) andListIterator.previous()
will return the element immediately preceding it (if any).boolean
Compares the specified object with this list for equality.get
(int index) Returns the element at the specified position in this list.getFirst()
Returns the element at the beginning of this list.getLast()
Returns the element at the end of this list.protected CursorableLinkedList.Listable
<E> getListableAt
(int index) Returns theinvalid reference
org.apache.commons.collections.CursorableLinkedList.Listable
int
hashCode()
Returns the hash code value for this list.int
Returns the index in this list of the first occurrence of the specified element, or -1 if this list does not contain this element.protected CursorableLinkedList.Listable
<E> insertListable
(CursorableLinkedList.Listable<E> before, CursorableLinkedList.Listable<E> after, E value) Inserts a new value into my list, after the specified before element, and before the specified after elementprotected void
Informs all of my registered cursors that they are now invalid.boolean
isEmpty()
Returns true if this list contains no elements.iterator()
Returns a fail-fast iterator.int
Returns the index in this list of the last occurrence of the specified element, or -1 if this list does not contain this element.Returns a fail-fast ListIterator.listIterator
(int index) Returns a fail-fast ListIterator.private void
protected void
Registers aCursorableLinkedList.Cursor
to be notified of changes to this list.remove
(int index) Removes the element at the specified position in this list (optional operation).boolean
Removes the first occurrence in this list of the specified element.boolean
removeAll
(Collection<?> c) Removes from this list all the elements that are contained in the specified collection.Removes the first element of this list, if any.Removes the last element of this list, if any.protected void
Removes the giveninvalid reference
org.apache.commons.collections.CursorableLinkedList.Listable
boolean
retainAll
(Collection<?> c) Retains only the elements in this list that are contained in the specified collection.Replaces the element at the specified position in this list with the specified element.int
size()
Returns the number of elements in this list.subList
(int i, int j) Returns a fail-fast sublist.Object[]
toArray()
Returns an array containing all of the elements in this list in proper sequence.<T> T[]
toArray
(T[] a) Returns an array containing all of the elements in this list in proper sequence; the runtime type of the returned array is that of the specified array.toString()
Returns aString
representation of this list, suitable for debugging.protected void
Removes aCursorableLinkedList.Cursor
from the set of cursors to be notified of changes to this list.private void
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.util.Collection
parallelStream, removeIf, stream
Methods inherited from interface java.util.List
replaceAll, sort, spliterator
-
Field Details
-
serialVersionUID
private static final long serialVersionUIDEnsure serialization compatibility- See Also:
-
_size
protected transient int _sizeThe number of elements in me. -
_head
A sentry node._head.next() points to the first element in the list, _head.prev() to the last. Note that it is possible for _head.next().prev() and _head.prev().next() to be non-null, as when I am a sublist for some larger list. Use == _head.next() and == _head.prev() to determine if a given
invalid reference
org.apache.commons.collections.CursorableLinkedList.Listable
-
_modCount
protected transient int _modCountTracks the number of structural modifications to me. -
_cursors
A list of the currentlyCursorableLinkedList.Cursor
s currently open in this list.
-
-
Constructor Details
-
CursorableLinkedList
CursorableLinkedList()
-
-
Method Details
-
add
Appends the specified element to the end of this list. -
add
Inserts the specified element at the specified position in this list. Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices).- Specified by:
add
in interfaceList<E>
- Parameters:
index
- index at which the specified element is to be inserted.element
- element to be inserted.- Throws:
ClassCastException
- if the class of the specified element prevents it from being added to this list.IllegalArgumentException
- if some aspect of the specified element prevents it from being added to this list.IndexOutOfBoundsException
- if the index is out of range (index < 0 || index > size()).
-
addAll
Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specifiedCollection
'sIterator
. The behavior of this operation is unspecified if the specified collection is modified while the operation is in progress. (Note that this will occur if the specified collection is this list, and it's nonempty.)- Specified by:
addAll
in interfaceCollection<E>
- Specified by:
addAll
in interfaceList<E>
- Parameters:
c
- collection whose elements are to be added to this list.- Returns:
- true if this list changed as a result of the call.
- Throws:
ClassCastException
- if the class of an element in the specified collection prevents it from being added to this list.IllegalArgumentException
- if some aspect of an element in the specified collection prevents it from being added to this list.
-
addAll
Inserts all of the elements in the specified collection into this list at the specified position. Shifts the element currently at that position (if any) and any subsequent elements to the right (increases their indices). The new elements will appear in this list in the order that they are returned by the specifiedCollection
'sIterator
. The behavior of this operation is unspecified if the specified collection is modified while the operation is in progress. (Note that this will occur if the specified collection is this list, and it's nonempty.)- Specified by:
addAll
in interfaceList<E>
- Parameters:
index
- index at which to insert first element from the specified collection.c
- elements to be inserted into this list.- Returns:
- true if this list changed as a result of the call.
- Throws:
ClassCastException
- if the class of one of elements of the specified collection prevents it from being added to this list.IllegalArgumentException
- if some aspect of one of elements of the specified collection prevents it from being added to this list.IndexOutOfBoundsException
- if the index is out of range (index < 0 || index > size()).
-
addFirst
Inserts the specified element at the beginning of this list. (Equivalent toadd(0,o)
).- Parameters:
o
- element to be prepended to this list.- Returns:
- true
-
addLast
Inserts the specified element at the end of this list. (Equivalent toadd(java.lang.Object)
).- Parameters:
o
- element to be appended to this list.- Returns:
- true
-
clear
public void clear()Removes all of the elements from this list. This list will be empty after this call returns (unless it throws an exception). -
contains
Returns true if this list contains the specified element. More formally, returns true if and only if this list contains at least one element e such that (o==null ? e==null : o.equals(e)). -
containsAll
Returns true if this list contains all of the elements of the specified collection.- Specified by:
containsAll
in interfaceCollection<E>
- Specified by:
containsAll
in interfaceList<E>
- Parameters:
c
- collection to be checked for containment in this list.- Returns:
- true if this list contains all of the elements of the specified collection.
-
cursor
Returns aListIterator
for iterating through the elements of this list. Unlikeiterator()
, a cursor is not bothered by concurrent modifications to the underlying list.Specifically, when elements are added to the list before or after the cursor, the cursor simply picks them up automatically. 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--i.e., it cannot be removed).Note that the returned
ListIterator
does not support theListIterator.nextIndex()
andListIterator.previousIndex()
methods (they throwUnsupportedOperationException
when invoked.Historical Note: In previous versions of this class, the object returned from this method was required to be explicitly closed. This is no longer necessary.
- See Also:
-
cursor
Returns aListIterator
for iterating through the elements of this list, initialized such thatListIterator.next()
will return the element at the specified index (if any) andListIterator.previous()
will return the element immediately preceding it (if any). Unlikeiterator()
, a cursor is not bothered by concurrent modifications to the underlying list.- Throws:
IndexOutOfBoundsException
- if the index is out of range (index < 0 || index > size()).- See Also:
-
equals
Compares the specified object with this list for equality. Returns true if and only if the specified object is also a list, both lists have the same size, and all corresponding pairs of elements in the two lists are equal. (Two elements e1 and e2 are equal if (e1==null ? e2==null : e1.equals(e2)).) In other words, two lists are defined to be equal if they contain the same elements in the same order. This definition ensures that the equals method works properly across different implementations of the List interface. -
get
Returns the element at the specified position in this list.- Specified by:
get
in interfaceList<E>
- Parameters:
index
- index of element to return.- Returns:
- the element at the specified position in this list.
- Throws:
IndexOutOfBoundsException
- if the index is out of range (index < 0 || index >= size()).
-
getFirst
Returns the element at the beginning of this list. -
getLast
Returns the element at the end of this list. -
hashCode
public int hashCode()Returns the hash code value for this list. The hash code of a list is defined to be the result of the following calculation:hashCode = 1; Iterator i = list.iterator(); while (i.hasNext()) { Object obj = i.next(); hashCode = 31*hashCode + (obj==null ? 0 : obj.hashCode()); }
This ensures that list1.equals(list2) implies that list1.hashCode()==list2.hashCode() for any two lists, list1 and list2, as required by the general contract of Object.hashCode. -
indexOf
Returns the index in this list of the first occurrence of the specified element, or -1 if this list does not contain this element. More formally, returns the lowest index i such that (o==null ? get(i)==null : o.equals(get(i))), or -1 if there is no such index. -
isEmpty
public boolean isEmpty()Returns true if this list contains no elements. -
iterator
Returns a fail-fast iterator. -
lastIndexOf
Returns the index in this list of the last occurrence of the specified element, or -1 if this list does not contain this element. More formally, returns the highest index i such that (o==null ? get(i)==null : o.equals(get(i))), or -1 if there is no such index.- Specified by:
lastIndexOf
in interfaceList<E>
- Parameters:
o
- element to search for.- Returns:
- the index in this list of the last occurrence of the specified element, or -1 if this list does not contain this element.
-
listIterator
Returns a fail-fast ListIterator.- Specified by:
listIterator
in interfaceList<E>
- See Also:
-
listIterator
Returns a fail-fast ListIterator.- Specified by:
listIterator
in interfaceList<E>
- See Also:
-
remove
Removes the first occurrence in this list of the specified element. If this list does not contain the element, it is unchanged. More formally, removes the element with the lowest index i such that (o==null ? get(i)==null : o.equals(get(i))) (if such an element exists). -
remove
Removes the element at the specified position in this list (optional operation). Shifts any subsequent elements to the left (subtracts one from their indices). Returns the element that was removed from the list.- Specified by:
remove
in interfaceList<E>
- Parameters:
index
- the index of the element to removed.- Returns:
- the element previously at the specified position.
- Throws:
IndexOutOfBoundsException
- if the index is out of range (index < 0 || index >= size()).
-
removeAll
Removes from this list all the elements that are contained in the specified collection. -
removeFirst
Removes the first element of this list, if any. -
removeLast
Removes the last element of this list, if any. -
retainAll
Retains only the elements in this list that are contained in the specified collection. In other words, removes from this list all the elements that are not contained in the specified collection. -
set
Replaces the element at the specified position in this list with the specified element.- Specified by:
set
in interfaceList<E>
- Parameters:
index
- index of element to replace.element
- element to be stored at the specified position.- Returns:
- the element previously at the specified position.
- Throws:
ClassCastException
- if the class of the specified element prevents it from being added to this list.IllegalArgumentException
- if some aspect of the specified element prevents it from being added to this list.IndexOutOfBoundsException
- if the index is out of range (index < 0 || index >= size()).
-
size
public int size()Returns the number of elements in this list. -
toArray
Returns an array containing all of the elements in this list in proper sequence. Obeys the general contract of theCollection.toArray()
method. -
toArray
public <T> T[] toArray(T[] a) Returns an array containing all of the elements in this list in proper sequence; the runtime type of the returned array is that of the specified array. Obeys the general contract of theCollection.toArray()
method.- Specified by:
toArray
in interfaceCollection<E>
- Specified by:
toArray
in interfaceList<E>
- Parameters:
a
- the array into which the elements of this list are to be stored, if it is big enough; otherwise, a new array of the same runtime type is allocated for this purpose.- Returns:
- an array containing the elements of this list.
- Throws:
ArrayStoreException
- if the runtime type of the specified array is not a supertype of the runtime type of every element in this list.
-
toString
Returns aString
representation of this list, suitable for debugging. -
subList
Returns a fail-fast sublist. -
insertListable
protected CursorableLinkedList.Listable<E> insertListable(CursorableLinkedList.Listable<E> before, CursorableLinkedList.Listable<E> after, E value) Inserts a new value into my list, after the specified before element, and before the specified after element- Returns:
- the newly created
invalid reference
org.apache.commons.collections.CursorableLinkedList.Listable
-
removeListable
Removes the giveninvalid reference
org.apache.commons.collections.CursorableLinkedList.Listable
-
getListableAt
Returns theinvalid reference
org.apache.commons.collections.CursorableLinkedList.Listable
- Throws:
IndexOutOfBoundsException
- if index is less than zero or greater than or equal to the size of this list.
-
registerCursor
Registers aCursorableLinkedList.Cursor
to be notified of changes to this list. -
unregisterCursor
Removes aCursorableLinkedList.Cursor
from the set of cursors to be notified of changes to this list. -
invalidateCursors
protected void invalidateCursors()Informs all of my registered cursors that they are now invalid. -
broadcastListableChanged
Informs all of my registered cursors that the specified element was changed.- See Also:
-
broadcastListableRemoved
Informs all of my registered cursors that the specified element was just removed from my list. -
broadcastListableInserted
Informs all of my registered cursors that the specified element was just added to my list. -
writeObject
- Throws:
IOException
-
readObject
- Throws:
IOException
ClassNotFoundException
-