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 class
A list iterator over the linked list.protected static class
The sublist implementation for AbstractLinkedList.protected static class
A list iterator over the linked sub list.protected static class
A node within the linked list. -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected AbstractLinkedList.Node
AAbstractLinkedList.Node
which indicates the start and end of the list and does not hold a value.protected int
Modification count for iteratorsprotected int
The size of the list -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotected
Constructor that does nothing intended for deserialization.protected
AbstractLinkedList
(Collection coll) Constructs a list copying data from the specified collection. -
Method Summary
Modifier and TypeMethodDescriptionvoid
boolean
boolean
addAll
(int index, Collection coll) boolean
addAll
(Collection coll) boolean
boolean
protected void
addNode
(AbstractLinkedList.Node nodeToInsert, AbstractLinkedList.Node insertBeforeNode) Inserts a new node into the list.protected void
addNodeAfter
(AbstractLinkedList.Node node, Object value) Creates a new node with the specified object as itsvalue
and inserts it afternode
.protected void
addNodeBefore
(AbstractLinkedList.Node node, Object value) Creates a new node with the specified object as itsvalue
and inserts it beforenode
.void
clear()
boolean
boolean
containsAll
(Collection coll) protected AbstractLinkedList.Node
Creates a new node with previous, next and element all set to null.protected AbstractLinkedList.Node
createNode
(Object value) Creates a new node with the specified properties.protected Iterator
Creates an iterator for the sublist.protected ListIterator
createSubListListIterator
(AbstractLinkedList.LinkedSubList subList, int fromIndex) Creates a list iterator for the sublist.protected void
doReadObject
(ObjectInputStream inputStream) Deserializes the data held in this object to the stream specified.protected void
doWriteObject
(ObjectOutputStream outputStream) Serializes the data held in this object to the stream specified.boolean
get
(int index) getFirst()
getLast()
protected AbstractLinkedList.Node
getNode
(int index, boolean endMarkerAllowed) Gets the node at a particular index.int
hashCode()
int
protected void
init()
The equivalent of a default constructor, broken out so it can be called by any constructor and byreadObject
.boolean
isEmpty()
protected boolean
isEqualValue
(Object value1, Object value2) Compares two values for equals.iterator()
int
lastIndexOf
(Object value) listIterator
(int fromIndex) remove
(int index) boolean
boolean
removeAll
(Collection coll) protected void
Removes all nodes by resetting the circular list marker.protected void
Removes the specified node from the list.boolean
retainAll
(Collection coll) int
size()
subList
(int fromIndexInclusive, int toIndexExclusive) Gets a sublist of the main list.Object[]
toArray()
Object[]
toString()
protected void
updateNode
(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, wait
Methods inherited from interface java.util.Collection
parallelStream, removeIf, stream, toArray
Methods inherited from interface java.util.List
replaceAll, sort, spliterator
-
Field Details
-
header
AAbstractLinkedList.Node
which indicates the start and end of the list and does not hold a value. The value ofnext
is the first item in the list. The value of ofprevious
is 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:
size
in interfaceCollection
- Specified by:
size
in interfaceList
-
isEmpty
public boolean isEmpty()- Specified by:
isEmpty
in interfaceCollection
- Specified by:
isEmpty
in interfaceList
-
get
-
iterator
-
listIterator
- Specified by:
listIterator
in interfaceList
-
listIterator
- Specified by:
listIterator
in interfaceList
-
indexOf
-
lastIndexOf
- Specified by:
lastIndexOf
in interfaceList
-
contains
- Specified by:
contains
in interfaceCollection
- Specified by:
contains
in interfaceList
-
containsAll
- Specified by:
containsAll
in interfaceCollection
- Specified by:
containsAll
in interfaceList
-
toArray
- Specified by:
toArray
in interfaceCollection
- Specified by:
toArray
in interfaceList
-
toArray
- Specified by:
toArray
in interfaceCollection
- Specified by:
toArray
in interfaceList
-
subList
Gets a sublist of the main list. -
add
- Specified by:
add
in interfaceCollection
- Specified by:
add
in interfaceList
-
add
-
addAll
- Specified by:
addAll
in interfaceCollection
- Specified by:
addAll
in interfaceList
-
addAll
-
remove
-
remove
- Specified by:
remove
in interfaceCollection
- Specified by:
remove
in interfaceList
-
removeAll
- Specified by:
removeAll
in interfaceCollection
- Specified by:
removeAll
in interfaceList
-
retainAll
- Specified by:
retainAll
in interfaceCollection
- Specified by:
retainAll
in interfaceList
-
set
-
clear
public void clear()- Specified by:
clear
in interfaceCollection
- Specified by:
clear
in 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 itsvalue
and 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
- ifnode
is null
-
addNodeAfter
Creates a new node with the specified object as itsvalue
and 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
- ifnode
is 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
- ifnode
is 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:
IOException
ClassNotFoundException
-