Class AbstractLinkedList<E>

  • All Implemented Interfaces:
    java.lang.Iterable<E>, java.util.Collection<E>, java.util.List<E>
    Direct Known Subclasses:
    CursorableLinkedList, NodeCachingLinkedList

    public abstract class AbstractLinkedList<E>
    extends java.lang.Object
    implements java.util.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
    Version:
    $Id: AbstractLinkedList.java 1494296 2013-06-18 20:54:29Z tn $
    • 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<? 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 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 java.util.Collection<E>
        Specified by:
        size in interface java.util.List<E>
      • isEmpty

        public boolean isEmpty()
        Specified by:
        isEmpty in interface java.util.Collection<E>
        Specified by:
        isEmpty in interface java.util.List<E>
      • get

        public E get​(int index)
        Specified by:
        get in interface java.util.List<E>
      • iterator

        public java.util.Iterator<E> iterator()
        Specified by:
        iterator in interface java.util.Collection<E>
        Specified by:
        iterator in interface java.lang.Iterable<E>
        Specified by:
        iterator in interface java.util.List<E>
      • listIterator

        public java.util.ListIterator<E> listIterator()
        Specified by:
        listIterator in interface java.util.List<E>
      • listIterator

        public java.util.ListIterator<E> listIterator​(int fromIndex)
        Specified by:
        listIterator in interface java.util.List<E>
      • indexOf

        public int indexOf​(java.lang.Object value)
        Specified by:
        indexOf in interface java.util.List<E>
      • lastIndexOf

        public int lastIndexOf​(java.lang.Object value)
        Specified by:
        lastIndexOf in interface java.util.List<E>
      • contains

        public boolean contains​(java.lang.Object value)
        Specified by:
        contains in interface java.util.Collection<E>
        Specified by:
        contains in interface java.util.List<E>
      • containsAll

        public boolean containsAll​(java.util.Collection<?> coll)
        Specified by:
        containsAll in interface java.util.Collection<E>
        Specified by:
        containsAll in interface java.util.List<E>
      • toArray

        public java.lang.Object[] toArray()
        Specified by:
        toArray in interface java.util.Collection<E>
        Specified by:
        toArray in interface java.util.List<E>
      • toArray

        public <T> T[] toArray​(T[] array)
        Specified by:
        toArray in interface java.util.Collection<E>
        Specified by:
        toArray in interface java.util.List<E>
      • subList

        public java.util.List<E> subList​(int fromIndexInclusive,
                                         int toIndexExclusive)
        Gets a sublist of the main list.
        Specified by:
        subList in interface java.util.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 java.util.Collection<E>
        Specified by:
        add in interface java.util.List<E>
      • add

        public void add​(int index,
                        E value)
        Specified by:
        add in interface java.util.List<E>
      • addAll

        public boolean addAll​(java.util.Collection<? extends E> coll)
        Specified by:
        addAll in interface java.util.Collection<E>
        Specified by:
        addAll in interface java.util.List<E>
      • addAll

        public boolean addAll​(int index,
                              java.util.Collection<? extends E> coll)
        Specified by:
        addAll in interface java.util.List<E>
      • remove

        public E remove​(int index)
        Specified by:
        remove in interface java.util.List<E>
      • remove

        public boolean remove​(java.lang.Object value)
        Specified by:
        remove in interface java.util.Collection<E>
        Specified by:
        remove in interface java.util.List<E>
      • removeAll

        public boolean removeAll​(java.util.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 for coll that provides a fast (e.g. O(1)) implementation of Collection.contains(Object).

        Specified by:
        removeAll in interface java.util.Collection<E>
        Specified by:
        removeAll in interface java.util.List<E>
      • retainAll

        public boolean retainAll​(java.util.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 for coll that provides a fast (e.g. O(1)) implementation of Collection.contains(Object).

        Specified by:
        retainAll in interface java.util.Collection<E>
        Specified by:
        retainAll in interface java.util.List<E>
      • set

        public E set​(int index,
                     E value)
        Specified by:
        set in interface java.util.List<E>
      • clear

        public void clear()
        Specified by:
        clear in interface java.util.Collection<E>
        Specified by:
        clear in interface java.util.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​(java.lang.Object obj)
        Specified by:
        equals in interface java.util.Collection<E>
        Specified by:
        equals in interface java.util.List<E>
        Overrides:
        equals in class java.lang.Object
      • hashCode

        public int hashCode()
        Specified by:
        hashCode in interface java.util.Collection<E>
        Specified by:
        hashCode in interface java.util.List<E>
        Overrides:
        hashCode in class java.lang.Object
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.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 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
        Returns:
        a new node containing the value
      • 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:
        java.lang.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:
        java.lang.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 java.lang.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
        Returns:
        the node at the given index
        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<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 java.util.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
        Returns:
        a new list iterator on the given sublist
      • doWriteObject

        protected void doWriteObject​(java.io.ObjectOutputStream outputStream)
                              throws java.io.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:
        java.io.IOException - if anything goes wrong
      • doReadObject

        protected void doReadObject​(java.io.ObjectInputStream inputStream)
                             throws java.io.IOException,
                                    java.lang.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:
        java.io.IOException - if any error occurs while reading from the stream
        java.lang.ClassNotFoundException - if a class read from the stream can not be loaded