Class AbstractLinkedList<E>
- java.lang.Object
-
- org.apache.commons.collections4.list.AbstractLinkedList<E>
-
- All Implemented Interfaces:
Iterable<E>,Collection<E>,List<E>
- Direct Known Subclasses:
CursorableLinkedList,NodeCachingLinkedList
public abstract class AbstractLinkedList<E> extends Object implements List<E>
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:
- 3.0
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected static classAbstractLinkedList.LinkedListIterator<E>A list iterator over the linked list.protected static classAbstractLinkedList.LinkedSubList<E>The sublist implementation for AbstractLinkedList.protected static classAbstractLinkedList.LinkedSubListIterator<E>A list iterator over the linked sub list.protected static classAbstractLinkedList.Node<E>A node within the linked list.
-
Field Summary
Fields Modifier and Type Field Description (package private) AbstractLinkedList.Node<E>headerAAbstractLinkedList.Nodewhich indicates the start and end of the list and does not hold a value.(package private) intmodCountModification count for iterators(package private) intsizeThe size of the list
-
Constructor Summary
Constructors Modifier Constructor Description protectedAbstractLinkedList()Constructor that does nothing intended for deserialization.protectedAbstractLinkedList(Collection<? extends E> coll)Constructs a list copying data from the specified collection.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidadd(int index, E value)booleanadd(E value)booleanaddAll(int index, Collection<? extends E> coll)booleanaddAll(Collection<? extends E> coll)booleanaddFirst(E o)booleanaddLast(E o)protected voidaddNode(AbstractLinkedList.Node<E> nodeToInsert, AbstractLinkedList.Node<E> insertBeforeNode)Inserts a new node into the list.protected voidaddNodeAfter(AbstractLinkedList.Node<E> node, E value)Creates a new node with the specified object as itsvalueand inserts it afternode.protected voidaddNodeBefore(AbstractLinkedList.Node<E> node, E value)Creates a new node with the specified object as itsvalueand inserts it beforenode.voidclear()booleancontains(Object value)booleancontainsAll(Collection<?> coll)protected AbstractLinkedList.Node<E>createHeaderNode()Creates a new node with previous, next and element all set to null.protected AbstractLinkedList.Node<E>createNode(E value)Creates a new node with the specified properties.protected Iterator<E>createSubListIterator(AbstractLinkedList.LinkedSubList<E> subList)Creates an iterator for the sublist.protected ListIterator<E>createSubListListIterator(AbstractLinkedList.LinkedSubList<E> 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.booleanequals(Object obj)Eget(int index)EgetFirst()EgetLast()protected AbstractLinkedList.Node<E>getNode(int index, boolean endMarkerAllowed)Gets the node at a particular index.inthashCode()intindexOf(Object value)protected 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<E>iterator()intlastIndexOf(Object value)ListIterator<E>listIterator()ListIterator<E>listIterator(int fromIndex)Eremove(int index)booleanremove(Object value)booleanremoveAll(Collection<?> coll)protected voidremoveAllNodes()Removes all nodes by resetting the circular list marker.EremoveFirst()EremoveLast()protected voidremoveNode(AbstractLinkedList.Node<E> node)Removes the specified node from the list.booleanretainAll(Collection<?> coll)Eset(int index, E value)intsize()List<E>subList(int fromIndexInclusive, int toIndexExclusive)Gets a sublist of the main list.Object[]toArray()<T> T[]toArray(T[] array)StringtoString()protected voidupdateNode(AbstractLinkedList.Node<E> node, E 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 Detail
-
header
transient AbstractLinkedList.Node<E> 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
transient int size
The size of the list
-
modCount
transient int modCount
Modification count for iterators
-
-
Constructor Detail
-
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
protected AbstractLinkedList(Collection<? extends E> coll)
Constructs a list copying data from the specified collection.- Parameters:
coll- the collection to copy
-
-
Method Detail
-
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()
-
isEmpty
public boolean isEmpty()
-
listIterator
public ListIterator<E> listIterator()
- Specified by:
listIteratorin interfaceList<E>
-
listIterator
public ListIterator<E> listIterator(int fromIndex)
- Specified by:
listIteratorin interfaceList<E>
-
lastIndexOf
public int lastIndexOf(Object value)
- Specified by:
lastIndexOfin interfaceList<E>
-
contains
public boolean contains(Object value)
-
containsAll
public boolean containsAll(Collection<?> coll)
- Specified by:
containsAllin interfaceCollection<E>- Specified by:
containsAllin interfaceList<E>
-
toArray
public Object[] toArray()
-
toArray
public <T> T[] toArray(T[] array)
-
subList
public List<E> subList(int fromIndexInclusive, int toIndexExclusive)
Gets a sublist of the main list.
-
add
public boolean add(E value)
-
addAll
public boolean addAll(Collection<? extends E> coll)
-
addAll
public boolean addAll(int index, Collection<? extends E> coll)
-
remove
public boolean remove(Object value)
-
removeAll
public boolean removeAll(Collection<?> coll)
This implementation iterates over the elements of this list, checking each element in turn to see if it's contained in
coll. If it's contained, it's removed from this list. As a consequence, it is advised to use a collection type forcollthat provides a fast (e.g. O(1)) implementation ofCollection.contains(Object).
-
retainAll
public boolean retainAll(Collection<?> coll)
This implementation iterates over the elements of this list, checking each element in turn to see if it's contained in
coll. If it's not contained, it's removed from this list. As a consequence, it is advised to use a collection type forcollthat provides a fast (e.g. O(1)) implementation ofCollection.contains(Object).
-
clear
public void clear()
-
getFirst
public E getFirst()
-
getLast
public E getLast()
-
addFirst
public boolean addFirst(E o)
-
addLast
public boolean addLast(E o)
-
removeFirst
public E removeFirst()
-
removeLast
public E removeLast()
-
equals
public boolean equals(Object obj)
-
hashCode
public int hashCode()
-
isEqualValue
protected boolean isEqualValue(Object value1, Object value2)
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
protected void updateNode(AbstractLinkedList.Node<E> node, E value)
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
protected AbstractLinkedList.Node<E> 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
protected AbstractLinkedList.Node<E> createNode(E value)
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- Returns:
- a new node containing the value
-
addNodeBefore
protected void addNodeBefore(AbstractLinkedList.Node<E> node, E value)
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
protected void addNodeAfter(AbstractLinkedList.Node<E> node, E value)
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<E> nodeToInsert, AbstractLinkedList.Node<E> 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
protected void removeNode(AbstractLinkedList.Node<E> node)
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<E> 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- Returns:
- the node at the given index
- 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
protected Iterator<E> createSubListIterator(AbstractLinkedList.LinkedSubList<E> subList)
Creates an iterator for the sublist.- Parameters:
subList- the sublist to get an iterator for- Returns:
- a new iterator on the given sublist
-
createSubListListIterator
protected ListIterator<E> createSubListListIterator(AbstractLinkedList.LinkedSubList<E> 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- Returns:
- a new list iterator on the given sublist
-
doWriteObject
protected void doWriteObject(ObjectOutputStream outputStream) throws IOException
Serializes the data held in this object to the stream specified.The first serializable subclass must call this method from
writeObject.- Parameters:
outputStream- the stream to write the object to- Throws:
IOException- if anything goes wrong
-
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.- Parameters:
inputStream- the stream to read the object from- Throws:
IOException- if any error occurs while reading from the streamClassNotFoundException- if a class read from the stream can not be loaded
-
-