org.apache.commons.collections.list
Class AbstractLinkedList<E>

java.lang.Object
  extended by org.apache.commons.collections.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:
Commons Collections 3.0
Version:
$Revision: 1.1.1.1 $ $Date: 2005/05/23 04:35:14 $
Author:
Rich Dougherty, Phil Steitz, Matt Hall, John Watkinson, Stephen Colebourne

Nested Class Summary
protected static class AbstractLinkedList.LinkedListIterator<E>
          A list iterator over the linked list.
protected static class AbstractLinkedList.LinkedSubList<E>
          The sublist implementation for AbstractLinkedList.
protected static class AbstractLinkedList.LinkedSubListIterator<E>
          A list iterator over the linked sub list.
protected static class AbstractLinkedList.Node<T>
          A node within the linked list.
 
Field Summary
protected  AbstractLinkedList.Node<E> header
          A AbstractLinkedList.Node which indicates the start and end of the list and does not hold a value.
protected  int modCount
          Modification count for iterators
protected  int size
          The size of the list
 
Constructor Summary
protected AbstractLinkedList()
          Constructor that does nothing intended for deserialization.
protected AbstractLinkedList(Collection<E> coll)
          Constructs a list copying data from the specified collection.
 
Method Summary
 boolean add(E value)
           
 void add(int index, E value)
           
 boolean addAll(Collection<? extends E> coll)
           
 boolean addAll(int index, Collection<? extends E> coll)
           
 boolean addFirst(E o)
           
 boolean addLast(E o)
           
protected  void addNode(AbstractLinkedList.Node<E> nodeToInsert, AbstractLinkedList.Node<E> insertBeforeNode)
          Inserts a new node into the list.
protected  void addNodeAfter(AbstractLinkedList.Node<E> node, E value)
          Creates a new node with the specified object as its value and inserts it after node.
protected  void addNodeBefore(AbstractLinkedList.Node<E> node, E value)
          Creates a new node with the specified object as its value and inserts it before node.
 void clear()
           
 boolean contains(Object value)
           
 boolean containsAll(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  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 equals(Object obj)
           
 E get(int index)
           
 E getFirst()
           
 E getLast()
           
protected  AbstractLinkedList.Node<E> getNode(int index, boolean endMarkerAllowed)
          Gets the node at a particular index.
 int hashCode()
           
 int indexOf(Object value)
           
protected  void init()
          The equivalent of a default constructor, broken out so it can be called by any constructor and by readObject.
 boolean isEmpty()
           
protected  boolean isEqualValue(E value1, E value2)
          Compares two values for equals.
 Iterator<E> iterator()
           
 int lastIndexOf(Object value)
           
 ListIterator<E> listIterator()
           
 ListIterator<E> listIterator(int fromIndex)
           
 E remove(int index)
           
 boolean remove(Object value)
           
 boolean removeAll(Collection<?> coll)
           
protected  void removeAllNodes()
          Removes all nodes by resetting the circular list marker.
 E removeFirst()
           
 E removeLast()
           
protected  void removeNode(AbstractLinkedList.Node<E> node)
          Removes the specified node from the list.
 boolean retainAll(Collection<?> coll)
           
 E set(int index, E value)
           
 int size()
           
 List<E> subList(int fromIndexInclusive, int toIndexExclusive)
          Gets a sublist of the main list.
 Object[] toArray()
           
<T> T[]
toArray(T[] array)
           
 String toString()
           
protected  void updateNode(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
 

Field Detail

header

protected transient AbstractLinkedList.Node<E> header
A AbstractLinkedList.Node which indicates the start and end of the list and does not hold a value. The value of next is the first item in the list. The value of of previous is 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(Collection<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 by readObject. 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 interface Collection<E>
Specified by:
size in interface List<E>

isEmpty

public boolean isEmpty()
Specified by:
isEmpty in interface Collection<E>
Specified by:
isEmpty in interface List<E>

get

public E get(int index)
Specified by:
get in interface List<E>

iterator

public Iterator<E> iterator()
Specified by:
iterator in interface Iterable<E>
Specified by:
iterator in interface Collection<E>
Specified by:
iterator in interface List<E>

listIterator

public ListIterator<E> listIterator()
Specified by:
listIterator in interface List<E>

listIterator

public ListIterator<E> listIterator(int fromIndex)
Specified by:
listIterator in interface List<E>

indexOf

public int indexOf(Object value)
Specified by:
indexOf in interface List<E>

lastIndexOf

public int lastIndexOf(Object value)
Specified by:
lastIndexOf in interface List<E>

contains

public boolean contains(Object value)
Specified by:
contains in interface Collection<E>
Specified by:
contains in interface List<E>

containsAll

public boolean containsAll(Collection<?> coll)
Specified by:
containsAll in interface Collection<E>
Specified by:
containsAll in interface List<E>

toArray

public Object[] toArray()
Specified by:
toArray in interface Collection<E>
Specified by:
toArray in interface List<E>

toArray

public <T> T[] toArray(T[] array)
Specified by:
toArray in interface Collection<E>
Specified by:
toArray in interface List<E>

subList

public List<E> subList(int fromIndexInclusive,
                       int toIndexExclusive)
Gets a sublist of the main list.

Specified by:
subList in interface List<E>
Parameters:
fromIndexInclusive - the index to start from
toIndexExclusive - the index to end at
Returns:
the new sublist

add

public boolean add(E value)
Specified by:
add in interface Collection<E>
Specified by:
add in interface List<E>

add

public void add(int index,
                E value)
Specified by:
add in interface List<E>

addAll

public boolean addAll(Collection<? extends E> coll)
Specified by:
addAll in interface Collection<E>
Specified by:
addAll in interface List<E>

addAll

public boolean addAll(int index,
                      Collection<? extends E> coll)
Specified by:
addAll in interface List<E>

remove

public E remove(int index)
Specified by:
remove in interface List<E>

remove

public boolean remove(Object value)
Specified by:
remove in interface Collection<E>
Specified by:
remove in interface List<E>

removeAll

public boolean removeAll(Collection<?> coll)
Specified by:
removeAll in interface Collection<E>
Specified by:
removeAll in interface List<E>

retainAll

public boolean retainAll(Collection<?> coll)
Specified by:
retainAll in interface Collection<E>
Specified by:
retainAll in interface List<E>

set

public E set(int index,
             E value)
Specified by:
set in interface List<E>

clear

public void clear()
Specified by:
clear in interface Collection<E>
Specified by:
clear in interface List<E>

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)
Specified by:
equals in interface Collection<E>
Specified by:
equals in interface List<E>
Overrides:
equals in class Object

hashCode

public int hashCode()
Specified by:
hashCode in interface Collection<E>
Specified by:
hashCode in interface List<E>
Overrides:
hashCode in class Object

toString

public String toString()
Overrides:
toString in class Object

isEqualValue

protected boolean isEqualValue(E value1,
                               E 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 null
value2 - 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 update
value - 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

addNodeBefore

protected void addNodeBefore(AbstractLinkedList.Node<E> node,
                             E value)
Creates a new node with the specified object as its value and inserts it before node.

This implementation uses createNode(Object) and addNode(AbstractLinkedList.Node,AbstractLinkedList.Node).

Parameters:
node - node to insert before
value - value of the newly added node
Throws:
NullPointerException - if node is null

addNodeAfter

protected void addNodeAfter(AbstractLinkedList.Node<E> node,
                            E value)
Creates a new node with the specified object as its value and inserts it after node.

This implementation uses createNode(Object) and addNode(AbstractLinkedList.Node,AbstractLinkedList.Node).

Parameters:
node - node to insert after
value - value of the newly added node
Throws:
NullPointerException - if node is 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 insert
insertBeforeNode - 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 - if node is 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 0
endMarkerAllowed - 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

protected Iterator<E> createSubListIterator(AbstractLinkedList.LinkedSubList<E> subList)
Creates an iterator for the sublist.

Parameters:
subList - the sublist to get an iterator for

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 for
fromIndex - the index to start from, relative to the 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.

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


Copyright © 2005-2005 Apache Software Foundation, Matt Hall, John Watkinson. All Rights Reserved.