Package com.tangosol.util
Class SafeLinkedList
java.lang.Object
java.util.AbstractCollection
java.util.AbstractList
com.tangosol.util.SafeLinkedList
- All Implemented Interfaces:
 Serializable,Cloneable,Iterable,Collection,List,SequencedCollection
- Direct Known Subclasses:
 RecyclingLinkedList
Implementation of the Collection Framework interface "List" using a linked
 list algorithm.
 Read-only operations avoid synchronizing.  Some operations appear to be
 read-only but have side-effects, such as indexed operations against the
 List, because the List caches the last index used and the Node that it
 located (to facilitate processes that access the list sequentially using
 indexes etc.).  Other operations, even if read-only, require the data
 structures to remain unchanged for the duration of the call to avoid
 potential exceptions (such as a NullPointerException if a reference is
 cleared).
- Author:
 - cp 08/18/99
 - See Also:
 
- 
Nested Class Summary
Nested Classes - 
Field Summary
FieldsModifier and TypeFieldDescriptionprotected intThe size of the List.protected SafeLinkedList.NodeThe Node at the "cached position" in the List.protected SafeLinkedList.NodeThe first Node in the List (or null if empty).protected intThe "cached position" in the List.protected SafeLinkedList.NodeThe last Node in the List (or null if empty).Fields inherited from class java.util.AbstractList
modCount - 
Constructor Summary
ConstructorsConstructorDescriptionConstruct an empty List.SafeLinkedList(Collection collection) Construct a List containing the elements of the specified Collection. - 
Method Summary
Modifier and TypeMethodDescriptionvoidInserts the specified element at the specified position in this List.booleanAppends the specified element to the end of this List.booleanaddAll(int i, Collection collection) Inserts all of the elements in the specified collection at the specified location in this List.booleanaddAll(Collection collection) Appends all of the elements in the specified collection to the end of this List, in the order that they are returned by the specified collection's iterator.voidclear()Removes all of the elements from this List.clone()Create a clone of the SafeLinkedList.booleanReturns true if this List contains the specified element.booleancontainsAll(Collection collection) Returns true if this List contains all of the elements of the specified collection.get(int i) Returns the element at the specified position in this List.getFirst()Returns the first element in the list, or null if empty.protected SafeLinkedList.NodegetNode(int i) Find and return the specified Node.intReturns the index in this List of the first occurrence of the specified element, or -1 if this List does not contain this element.protected SafeLinkedList.NodeInstantiate a Node.intReturns the index in this List of the last occurrence of the specified element, or -1 if this List does not contain this element.static voidSelf-test for SafeLinkedList.protected voidmarkPosition(int i, SafeLinkedList.Node node) Remember that a certain Node occurs at a certain index.remove(int i) Removes the element at the specified position in this List.booleanRemoves the first occurrence in this List of the specified element.Removes and returns the first element in the list.protected voidreset()Completely reset the state of the List.Replaces the element at the specified position in this List with the specified element.intsize()Returns the number of elements in this List.Object[]toArray()Returns an array containing all of the elements in this List in the order that the elements occur in the List.Object[]Returns an array with a runtime type is that of the specified array and that contains all of the elements in this List.Methods inherited from class java.util.AbstractList
equals, hashCode, iterator, listIterator, listIterator, removeRange, subListMethods inherited from class java.util.AbstractCollection
isEmpty, removeAll, retainAll, toStringMethods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface java.util.Collection
parallelStream, removeIf, stream, toArrayMethods inherited from interface java.util.List
addFirst, addLast, equals, getLast, hashCode, isEmpty, iterator, listIterator, listIterator, removeAll, removeLast, replaceAll, retainAll, reversed, sort, spliterator, subList 
- 
Field Details
- 
m_head
The first Node in the List (or null if empty). - 
m_tail
The last Node in the List (or null if empty). - 
m_cNodes
protected volatile int m_cNodesThe size of the List. - 
m_iPos
protected transient int m_iPosThe "cached position" in the List. - 
m_current
The Node at the "cached position" in the List. 
 - 
 - 
Constructor Details
- 
SafeLinkedList
public SafeLinkedList()Construct an empty List. - 
SafeLinkedList
Construct a List containing the elements of the specified Collection.- Parameters:
 collection- the Collection to fill this List from
 
 - 
 - 
Method Details
- 
size
public int size()Returns the number of elements in this List. This method is not synchronized; it returns a snapshot of the size of the List. To ensure that the List has not changed its size by the time this method returns, the List must be synchronized on before calling this method:synchronized(list) { int c = list.size() ... }
- Specified by:
 sizein interfaceCollection- Specified by:
 sizein interfaceList- Specified by:
 sizein classAbstractCollection- Returns:
 - the number of elements in this List
 
 - 
add
Appends the specified element to the end of this List. Null values are supported.- Specified by:
 addin interfaceCollection- Specified by:
 addin interfaceList- Overrides:
 addin classAbstractList- Parameters:
 o- element to be appended to this List- Returns:
 - true because the List is modified
 
 - 
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).- Specified by:
 removein interfaceCollection- Specified by:
 removein interfaceList- Overrides:
 removein classAbstractCollection- Parameters:
 o- element to be removed from this List, if present- Returns:
 - true if this List contained the specified element
 
 - 
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)). To ensure that the List still definitely contains (or does not contain) the element after this method returns, the List must be synchronized on before calling this method: synchronized(list) { if list.contains(o) { ... } }- Specified by:
 containsin interfaceCollection- Specified by:
 containsin interfaceList- Overrides:
 containsin classAbstractCollection- Parameters:
 o- element whose presence in this List is to be tested- Returns:
 - true if this List contains the specified element
 
 - 
containsAll
Returns true if this List contains all of the elements of the specified collection. This method is not synchronized. To ensure that the List still definitely contains (or does not contain) the elements after this method returns, the List must be synchronized on before calling this method: synchronized(list) { if list.containsAll(o) { ... } }- Specified by:
 containsAllin interfaceCollection- Specified by:
 containsAllin interfaceList- Overrides:
 containsAllin classAbstractCollection- Parameters:
 collection- collection to be checked for containment in this List- Returns:
 - true if this List contains all of the elements of the specified collection
 
 - 
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.- Specified by:
 indexOfin interfaceList- Overrides:
 indexOfin classAbstractList- Parameters:
 o- element to search for.- Returns:
 - the index in this List of the first occurrence of the specified element, or -1 if this List does not contain this element.
 
 - 
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:
 lastIndexOfin interfaceList- Overrides:
 lastIndexOfin classAbstractList- 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.
 
 - 
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:
 addin interfaceList- Overrides:
 addin classAbstractList- Parameters:
 i- the index at which to insert the specified elemento- element to be inserted- Throws:
 IndexOutOfBoundsException- if the index is out of range (index < 0 || index > size()).
 - 
remove
Removes the element at the specified position in this List. Shifts any subsequent elements to the left (subtracts one from their indices). Returns the element that was removed from the list.- Specified by:
 removein interfaceList- Overrides:
 removein classAbstractList- Parameters:
 i- 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()).
 - 
removeFirst
Removes and returns the first element in the list.- Specified by:
 removeFirstin interfaceList- Specified by:
 removeFirstin interfaceSequencedCollection- Returns:
 - the removed element, or null if the list was empty
 
 - 
get
Returns the element at the specified position in this List.- Specified by:
 getin interfaceList- Specified by:
 getin classAbstractList- Parameters:
 i- the index of the 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 first element in the list, or null if empty.- Specified by:
 getFirstin interfaceList- Specified by:
 getFirstin interfaceSequencedCollection- Returns:
 - the first element in the list, or null if empty
 
 - 
set
Replaces the element at the specified position in this List with the specified element.- Specified by:
 setin interfaceList- Overrides:
 setin classAbstractList- Parameters:
 i- the index of the element to replaceo- the value to be stored at the specified position- Returns:
 - the value previously at the specified position
 - Throws:
 IndexOutOfBoundsException- if the index is out of range (index < 0 || index >= size())
 - 
addAll
Inserts all of the elements in the specified collection at the specified location in this List. The elements are inserted in the order that they are returned by the specified collection's iterator.- Specified by:
 addAllin interfaceList- Overrides:
 addAllin classAbstractList- Parameters:
 i- the index at which insertion will occurcollection- a collection whose elements are to be inserted into this List- Returns:
 - true if this list changed as a result of the call
 - Throws:
 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 specified collection's iterator.- Specified by:
 addAllin interfaceCollection- Specified by:
 addAllin interfaceList- Overrides:
 addAllin classAbstractCollection- Parameters:
 collection- a collection whose elements are to be added to this List- Returns:
 - true if this list changed as a result of the call
 
 - 
clear
public void clear()Removes all of the elements from this List. This List will be empty after this call returns.- Specified by:
 clearin interfaceCollection- Specified by:
 clearin interfaceList- Overrides:
 clearin classAbstractList
 - 
toArray
Returns an array containing all of the elements in this List in the order that the elements occur in the List. The returned array will be "safe" in that no references to it are maintained by the List. The caller is thus free to modify the returned array.- Specified by:
 toArrayin interfaceCollection- Specified by:
 toArrayin interfaceList- Overrides:
 toArrayin classAbstractCollection- Returns:
 - an array containing all of the elements in this List
 
 - 
toArray
Returns an array with a runtime type is that of the specified array and that contains all of the elements in this List. If the elements all fit in the specified array, it is returned therein. Otherwise, a new array is allocated with the runtime type of the specified array and the size of this List.If the List fits in the specified array with room to spare (i.e., the array has more elements than the List), the element in the array immediately following the end of the last element is set to null. This is useful in determining the length of the List only if the caller knows that the List does not contain any null elements.)
- Specified by:
 toArrayin interfaceCollection- Specified by:
 toArrayin interfaceList- Overrides:
 toArrayin classAbstractCollection- Parameters:
 a- the array into which the elements of the 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 the 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
 - 
clone
Create a clone of the SafeLinkedList. - 
markPosition
Remember that a certain Node occurs at a certain index.- Parameters:
 i- the index; the marker is updated only if the index is greater than zero and less than size() - 1node- the Node
 - 
reset
protected void reset()Completely reset the state of the List. - 
instantiateNode
Instantiate a Node. (Factory pattern.) This method is over-ridden by inheriting implementations if the inheriting implementation extends the inner Node class.- Parameters:
 o- the value for the Node
 - 
getNode
Find and return the specified Node.- Parameters:
 i- the Node index- Returns:
 - the Node
 - Throws:
 IndexOutOfBoundsException- if the index is out of range (index < 0 || index > size()).
 - 
main
Self-test for SafeLinkedList.- Throws:
 Exception
 
 -