Class AbstractLinkedList

  • All Implemented Interfaces:
    Iterable, Collection, List
    Direct Known Subclasses:
    CursorableLinkedList, NodeCachingLinkedList

    public abstract class AbstractLinkedList
    extends Object
    implements List
    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
    • Field Detail

      • header

        protected transient AbstractLinkedList.Node 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 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
        Specified by:
        size in interface List
      • get

        public Object get​(int index)
        Specified by:
        get in interface List
      • indexOf

        public int indexOf​(Object value)
        Specified by:
        indexOf in interface List
      • subList

        public List subList​(int fromIndexInclusive,
                            int toIndexExclusive)
        Gets a sublist of the main list.
        Specified by:
        subList in interface List
        Parameters:
        fromIndexInclusive - the index to start from
        toIndexExclusive - the index to end at
        Returns:
        the new sublist
      • add

        public void add​(int index,
                        Object value)
        Specified by:
        add in interface List
      • addAll

        public boolean addAll​(int index,
                              Collection coll)
        Specified by:
        addAll in interface List
      • remove

        public Object remove​(int index)
        Specified by:
        remove in interface List
      • getFirst

        public Object getFirst()
      • getLast

        public Object getLast()
      • addFirst

        public boolean addFirst​(Object o)
      • addLast

        public boolean addLast​(Object o)
      • removeFirst

        public Object removeFirst()
      • removeLast

        public Object removeLast()
      • 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 null
        value2 - the second value to compare, may be null
        Returns:
        true if equal
      • updateNode

        protected void updateNode​(AbstractLinkedList.Node node,
                                  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 update
        value - 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​(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
      • 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 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 createSubListIterator​(AbstractLinkedList.LinkedSubList subList)
        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 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