Class AbstractLinkedList
java.lang.Object
org.apache.commons.collections.list.AbstractLinkedList
- All Implemented Interfaces:
Iterable,Collection,List
- Direct Known Subclasses:
CursorableLinkedList,NodeCachingLinkedList
An abstract implementation of a linked list which provides numerous points for
subclasses to override.
Overridable methods are provided to change the storage node and to change how nodes are added to and removed. Hopefully, all you need for unusual subclasses is here.
- Since:
- Commons Collections 3.0
- Version:
- $Revision: 646777 $ $Date: 2008-04-10 14:33:15 +0200 (Thu, 10 Apr 2008) $
- Author:
- Rich Dougherty, Phil Steitz, Stephen Colebourne
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected static classA list iterator over the linked list.protected static classThe sublist implementation for AbstractLinkedList.protected static classA list iterator over the linked sub list.protected static classA node within the linked list. -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected AbstractLinkedList.NodeAAbstractLinkedList.Nodewhich indicates the start and end of the list and does not hold a value.protected intModification count for iteratorsprotected intThe size of the list -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedConstructor that does nothing intended for deserialization.protectedAbstractLinkedList(Collection coll) Constructs a list copying data from the specified collection. -
Method Summary
Modifier and TypeMethodDescriptionvoidbooleanbooleanaddAll(int index, Collection coll) booleanaddAll(Collection coll) booleanbooleanprotected voidaddNode(AbstractLinkedList.Node nodeToInsert, AbstractLinkedList.Node insertBeforeNode) Inserts a new node into the list.protected voidaddNodeAfter(AbstractLinkedList.Node node, Object value) Creates a new node with the specified object as itsvalueand inserts it afternode.protected voidaddNodeBefore(AbstractLinkedList.Node node, Object value) Creates a new node with the specified object as itsvalueand inserts it beforenode.voidclear()booleanbooleancontainsAll(Collection coll) protected AbstractLinkedList.NodeCreates a new node with previous, next and element all set to null.protected AbstractLinkedList.NodecreateNode(Object value) Creates a new node with the specified properties.protected IteratorCreates an iterator for the sublist.protected ListIteratorcreateSubListListIterator(AbstractLinkedList.LinkedSubList subList, int fromIndex) Creates a list iterator for the sublist.protected voiddoReadObject(ObjectInputStream inputStream) Deserializes the data held in this object to the stream specified.protected voiddoWriteObject(ObjectOutputStream outputStream) Serializes the data held in this object to the stream specified.booleanget(int index) getFirst()getLast()protected AbstractLinkedList.NodegetNode(int index, boolean endMarkerAllowed) Gets the node at a particular index.inthashCode()intprotected voidinit()The equivalent of a default constructor, broken out so it can be called by any constructor and byreadObject.booleanisEmpty()protected booleanisEqualValue(Object value1, Object value2) Compares two values for equals.iterator()intlastIndexOf(Object value) listIterator(int fromIndex) remove(int index) booleanbooleanremoveAll(Collection coll) protected voidRemoves all nodes by resetting the circular list marker.protected voidRemoves the specified node from the list.booleanretainAll(Collection coll) intsize()subList(int fromIndexInclusive, int toIndexExclusive) Gets a sublist of the main list.Object[]toArray()Object[]toString()protected voidupdateNode(AbstractLinkedList.Node node, Object value) Updates the node with a new value.Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface java.util.Collection
parallelStream, removeIf, stream, toArrayMethods inherited from interface java.util.List
replaceAll, sort, spliterator
-
Field Details
-
header
AAbstractLinkedList.Nodewhich indicates the start and end of the list and does not hold a value. The value ofnextis the first item in the list. The value of ofpreviousis the last item in the list. -
size
protected transient int sizeThe size of the list -
modCount
protected transient int modCountModification count for iterators
-
-
Constructor Details
-
AbstractLinkedList
protected AbstractLinkedList()Constructor that does nothing intended for deserialization.If this constructor is used by a serializable subclass then the init() method must be called.
-
AbstractLinkedList
Constructs a list copying data from the specified collection.- Parameters:
coll- the collection to copy
-
-
Method Details
-
init
protected void init()The equivalent of a default constructor, broken out so it can be called by any constructor and byreadObject. Subclasses which override this method should make sure they call super, so the list is initialised properly. -
size
public int size()- Specified by:
sizein interfaceCollection- Specified by:
sizein interfaceList
-
isEmpty
public boolean isEmpty()- Specified by:
isEmptyin interfaceCollection- Specified by:
isEmptyin interfaceList
-
get
-
iterator
-
listIterator
- Specified by:
listIteratorin interfaceList
-
listIterator
- Specified by:
listIteratorin interfaceList
-
indexOf
-
lastIndexOf
- Specified by:
lastIndexOfin interfaceList
-
contains
- Specified by:
containsin interfaceCollection- Specified by:
containsin interfaceList
-
containsAll
- Specified by:
containsAllin interfaceCollection- Specified by:
containsAllin interfaceList
-
toArray
- Specified by:
toArrayin interfaceCollection- Specified by:
toArrayin interfaceList
-
toArray
- Specified by:
toArrayin interfaceCollection- Specified by:
toArrayin interfaceList
-
subList
Gets a sublist of the main list. -
add
- Specified by:
addin interfaceCollection- Specified by:
addin interfaceList
-
add
-
addAll
- Specified by:
addAllin interfaceCollection- Specified by:
addAllin interfaceList
-
addAll
-
remove
-
remove
- Specified by:
removein interfaceCollection- Specified by:
removein interfaceList
-
removeAll
- Specified by:
removeAllin interfaceCollection- Specified by:
removeAllin interfaceList
-
retainAll
- Specified by:
retainAllin interfaceCollection- Specified by:
retainAllin interfaceList
-
set
-
clear
public void clear()- Specified by:
clearin interfaceCollection- Specified by:
clearin interfaceList
-
getFirst
-
getLast
-
addFirst
-
addLast
-
removeFirst
-
removeLast
-
equals
-
hashCode
public int hashCode() -
toString
-
isEqualValue
Compares two values for equals. This implementation uses the equals method. Subclasses can override this to match differently.- Parameters:
value1- the first value to compare, may be nullvalue2- the second value to compare, may be null- Returns:
- true if equal
-
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- node to updatevalue- new value of the node
-
createHeaderNode
Creates a new node with previous, next and element all set to null. This implementation creates a new empty Node. Subclasses can override this to create a different class.- Returns:
- newly created node
-
createNode
Creates a new node with the specified properties. This implementation creates a new Node with data. Subclasses can override this to create a different class.- Parameters:
value- value of the new node
-
addNodeBefore
Creates a new node with the specified object as itsvalueand inserts it beforenode.This implementation uses
createNode(Object)andaddNode(AbstractLinkedList.Node,AbstractLinkedList.Node).- Parameters:
node- node to insert beforevalue- value of the newly added node- Throws:
NullPointerException- ifnodeis null
-
addNodeAfter
Creates a new node with the specified object as itsvalueand inserts it afternode.This implementation uses
createNode(Object)andaddNode(AbstractLinkedList.Node,AbstractLinkedList.Node).- Parameters:
node- node to insert aftervalue- value of the newly added node- Throws:
NullPointerException- ifnodeis null
-
addNode
protected void addNode(AbstractLinkedList.Node nodeToInsert, AbstractLinkedList.Node insertBeforeNode) Inserts a new node into the list.- Parameters:
nodeToInsert- new node to insertinsertBeforeNode- node to insert before- Throws:
NullPointerException- if either node is null
-
removeNode
Removes the specified node from the list.- Parameters:
node- the node to remove- Throws:
NullPointerException- ifnodeis null
-
removeAllNodes
protected void removeAllNodes()Removes all nodes by resetting the circular list marker. -
getNode
protected AbstractLinkedList.Node getNode(int index, boolean endMarkerAllowed) throws IndexOutOfBoundsException Gets the node at a particular index.- Parameters:
index- the index, starting from 0endMarkerAllowed- whether or not the end marker can be returned if startIndex is set to the list's size- Throws:
IndexOutOfBoundsException- if the index is less than 0; equal to the size of the list and endMakerAllowed is false; or greater than the size of the list
-
createSubListIterator
Creates an iterator for the sublist.- Parameters:
subList- the sublist to get an iterator for
-
createSubListListIterator
protected ListIterator createSubListListIterator(AbstractLinkedList.LinkedSubList subList, int fromIndex) Creates a list iterator for the sublist.- Parameters:
subList- the sublist to get an iterator forfromIndex- the index to start from, relative to the sublist
-
doWriteObject
Serializes the data held in this object to the stream specified.The first serializable subclass must call this method from
writeObject.- Throws:
IOException
-
doReadObject
protected void doReadObject(ObjectInputStream inputStream) throws IOException, ClassNotFoundException Deserializes the data held in this object to the stream specified.The first serializable subclass must call this method from
readObject.- Throws:
IOExceptionClassNotFoundException
-