Class AbstractLinkedList
- java.lang.Object
-
- org.apache.commons.collections.list.AbstractLinkedList
-
- All Implemented Interfaces:
java.lang.Iterable,java.util.Collection,java.util.List
- Direct Known Subclasses:
CursorableLinkedList,NodeCachingLinkedList
public abstract class AbstractLinkedList extends java.lang.Object implements java.util.ListAn 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) $
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected static classAbstractLinkedList.LinkedListIteratorA list iterator over the linked list.protected static classAbstractLinkedList.LinkedSubListThe sublist implementation for AbstractLinkedList.protected static classAbstractLinkedList.LinkedSubListIteratorA list iterator over the linked sub list.protected static classAbstractLinkedList.NodeA node within the linked list.
-
Field Summary
Fields Modifier and Type Field Description protected AbstractLinkedList.NodeheaderAAbstractLinkedList.Nodewhich indicates the start and end of the list and does not hold a value.protected intmodCountModification count for iteratorsprotected intsizeThe size of the list
-
Constructor Summary
Constructors Modifier Constructor Description protectedAbstractLinkedList()Constructor that does nothing intended for deserialization.protectedAbstractLinkedList(java.util.Collection 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, java.lang.Object value)booleanadd(java.lang.Object value)booleanaddAll(int index, java.util.Collection coll)booleanaddAll(java.util.Collection coll)booleanaddFirst(java.lang.Object o)booleanaddLast(java.lang.Object o)protected voidaddNode(AbstractLinkedList.Node nodeToInsert, AbstractLinkedList.Node insertBeforeNode)Inserts a new node into the list.protected voidaddNodeAfter(AbstractLinkedList.Node node, java.lang.Object value)Creates a new node with the specified object as itsvalueand inserts it afternode.protected voidaddNodeBefore(AbstractLinkedList.Node node, java.lang.Object value)Creates a new node with the specified object as itsvalueand inserts it beforenode.voidclear()booleancontains(java.lang.Object value)booleancontainsAll(java.util.Collection coll)protected AbstractLinkedList.NodecreateHeaderNode()Creates a new node with previous, next and element all set to null.protected AbstractLinkedList.NodecreateNode(java.lang.Object value)Creates a new node with the specified properties.protected java.util.IteratorcreateSubListIterator(AbstractLinkedList.LinkedSubList subList)Creates an iterator for the sublist.protected java.util.ListIteratorcreateSubListListIterator(AbstractLinkedList.LinkedSubList subList, int fromIndex)Creates a list iterator for the sublist.protected voiddoReadObject(java.io.ObjectInputStream inputStream)Deserializes the data held in this object to the stream specified.protected voiddoWriteObject(java.io.ObjectOutputStream outputStream)Serializes the data held in this object to the stream specified.booleanequals(java.lang.Object obj)java.lang.Objectget(int index)java.lang.ObjectgetFirst()java.lang.ObjectgetLast()protected AbstractLinkedList.NodegetNode(int index, boolean endMarkerAllowed)Gets the node at a particular index.inthashCode()intindexOf(java.lang.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(java.lang.Object value1, java.lang.Object value2)Compares two values for equals.java.util.Iteratoriterator()intlastIndexOf(java.lang.Object value)java.util.ListIteratorlistIterator()java.util.ListIteratorlistIterator(int fromIndex)java.lang.Objectremove(int index)booleanremove(java.lang.Object value)booleanremoveAll(java.util.Collection coll)protected voidremoveAllNodes()Removes all nodes by resetting the circular list marker.java.lang.ObjectremoveFirst()java.lang.ObjectremoveLast()protected voidremoveNode(AbstractLinkedList.Node node)Removes the specified node from the list.booleanretainAll(java.util.Collection coll)java.lang.Objectset(int index, java.lang.Object value)intsize()java.util.ListsubList(int fromIndexInclusive, int toIndexExclusive)Gets a sublist of the main list.java.lang.Object[]toArray()java.lang.Object[]toArray(java.lang.Object[] array)java.lang.StringtoString()protected voidupdateNode(AbstractLinkedList.Node node, java.lang.Object value)Updates the node with a new value.
-
-
-
Field Detail
-
header
protected transient AbstractLinkedList.Node 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 size
The size of the list
-
modCount
protected 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(java.util.Collection 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()
- Specified by:
sizein interfacejava.util.Collection- Specified by:
sizein interfacejava.util.List
-
isEmpty
public boolean isEmpty()
- Specified by:
isEmptyin interfacejava.util.Collection- Specified by:
isEmptyin interfacejava.util.List
-
get
public java.lang.Object get(int index)
- Specified by:
getin interfacejava.util.List
-
iterator
public java.util.Iterator iterator()
- Specified by:
iteratorin interfacejava.util.Collection- Specified by:
iteratorin interfacejava.lang.Iterable- Specified by:
iteratorin interfacejava.util.List
-
listIterator
public java.util.ListIterator listIterator()
- Specified by:
listIteratorin interfacejava.util.List
-
listIterator
public java.util.ListIterator listIterator(int fromIndex)
- Specified by:
listIteratorin interfacejava.util.List
-
indexOf
public int indexOf(java.lang.Object value)
- Specified by:
indexOfin interfacejava.util.List
-
lastIndexOf
public int lastIndexOf(java.lang.Object value)
- Specified by:
lastIndexOfin interfacejava.util.List
-
contains
public boolean contains(java.lang.Object value)
- Specified by:
containsin interfacejava.util.Collection- Specified by:
containsin interfacejava.util.List
-
containsAll
public boolean containsAll(java.util.Collection coll)
- Specified by:
containsAllin interfacejava.util.Collection- Specified by:
containsAllin interfacejava.util.List
-
toArray
public java.lang.Object[] toArray()
- Specified by:
toArrayin interfacejava.util.Collection- Specified by:
toArrayin interfacejava.util.List
-
toArray
public java.lang.Object[] toArray(java.lang.Object[] array)
- Specified by:
toArrayin interfacejava.util.Collection- Specified by:
toArrayin interfacejava.util.List
-
subList
public java.util.List subList(int fromIndexInclusive, int toIndexExclusive)Gets a sublist of the main list.- Specified by:
subListin interfacejava.util.List- Parameters:
fromIndexInclusive- the index to start fromtoIndexExclusive- the index to end at- Returns:
- the new sublist
-
add
public boolean add(java.lang.Object value)
- Specified by:
addin interfacejava.util.Collection- Specified by:
addin interfacejava.util.List
-
add
public void add(int index, java.lang.Object value)- Specified by:
addin interfacejava.util.List
-
addAll
public boolean addAll(java.util.Collection coll)
- Specified by:
addAllin interfacejava.util.Collection- Specified by:
addAllin interfacejava.util.List
-
addAll
public boolean addAll(int index, java.util.Collection coll)- Specified by:
addAllin interfacejava.util.List
-
remove
public java.lang.Object remove(int index)
- Specified by:
removein interfacejava.util.List
-
remove
public boolean remove(java.lang.Object value)
- Specified by:
removein interfacejava.util.Collection- Specified by:
removein interfacejava.util.List
-
removeAll
public boolean removeAll(java.util.Collection coll)
- Specified by:
removeAllin interfacejava.util.Collection- Specified by:
removeAllin interfacejava.util.List
-
retainAll
public boolean retainAll(java.util.Collection coll)
- Specified by:
retainAllin interfacejava.util.Collection- Specified by:
retainAllin interfacejava.util.List
-
set
public java.lang.Object set(int index, java.lang.Object value)- Specified by:
setin interfacejava.util.List
-
clear
public void clear()
- Specified by:
clearin interfacejava.util.Collection- Specified by:
clearin interfacejava.util.List
-
getFirst
public java.lang.Object getFirst()
-
getLast
public java.lang.Object getLast()
-
addFirst
public boolean addFirst(java.lang.Object o)
-
addLast
public boolean addLast(java.lang.Object o)
-
removeFirst
public java.lang.Object removeFirst()
-
removeLast
public java.lang.Object removeLast()
-
equals
public boolean equals(java.lang.Object obj)
- Specified by:
equalsin interfacejava.util.Collection- Specified by:
equalsin interfacejava.util.List- Overrides:
equalsin classjava.lang.Object
-
hashCode
public int hashCode()
- Specified by:
hashCodein interfacejava.util.Collection- Specified by:
hashCodein interfacejava.util.List- Overrides:
hashCodein classjava.lang.Object
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
isEqualValue
protected boolean isEqualValue(java.lang.Object value1, java.lang.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 node, java.lang.Object 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 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 createNode(java.lang.Object 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
-
addNodeBefore
protected void addNodeBefore(AbstractLinkedList.Node node, java.lang.Object 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:
java.lang.NullPointerException- ifnodeis null
-
addNodeAfter
protected void addNodeAfter(AbstractLinkedList.Node node, java.lang.Object 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:
java.lang.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:
java.lang.NullPointerException- if either node is null
-
removeNode
protected void removeNode(AbstractLinkedList.Node node)
Removes the specified node from the list.- Parameters:
node- the node to remove- Throws:
java.lang.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 java.lang.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:
java.lang.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 java.util.Iterator createSubListIterator(AbstractLinkedList.LinkedSubList subList)
Creates an iterator for the sublist.- Parameters:
subList- the sublist to get an iterator for
-
createSubListListIterator
protected java.util.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
protected void doWriteObject(java.io.ObjectOutputStream outputStream) throws java.io.IOExceptionSerializes the data held in this object to the stream specified.The first serializable subclass must call this method from
writeObject.- Throws:
java.io.IOException
-
doReadObject
protected void doReadObject(java.io.ObjectInputStream inputStream) throws java.io.IOException, java.lang.ClassNotFoundExceptionDeserializes the data held in this object to the stream specified.The first serializable subclass must call this method from
readObject.- Throws:
java.io.IOExceptionjava.lang.ClassNotFoundException
-
-