org.crosswire.common.util

## Class ChainLink<E>

- java.lang.Object
- - org.crosswire.common.util.ChainLink<E>

- Type Parameters:
    `E` - the class to be chained

      public final class ChainLink<E>
      extends Object

  ChainLink allows for a doubly linked list embedded within objects. This is meant for small lists.
  Author:
    DM Smith
  See Also:
    `The GNU Lesser General Public License for details.`

- - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `private boolean` | `head`
    `private E` | `item`
    `private ChainLink<E>` | `left`
    `private ChainLink<E>` | `right`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `ChainLink()` Create an empty ChainLink.
    `ChainLink(E item)` Create a ChainLink containing an item.

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `void` | `addAfter(ChainLink<E> node)` Add this node after the given node.
    `void` | `addBefore(ChainLink<E> node)` Add this node before the given node.
    `void` | `addFirst(ChainLink<E> anyNode)` Add this node as the head of the chain.
    `void` | `addLast(ChainLink<E> anyNode)` Add this node as the tail of the chain.
    `ChainLink<E>` | `findHead()` Find the head of the chain.
    `ChainLink<E>` | `findTail()` Find the tail of the chain.
    `E` | `getItem()` Get the item from this ChainLink.
    `ChainLink<E>` | `getLeft()` Get the node to the left of this node.
    `ChainLink<E>` | `getRight()` Get the node to the right of this node.
    `boolean` | `isHead()` One node is marked as the head of the chain.
    `ChainLink<E>` | `locate(E element, Comparator<E> comparator)` Locate the node or the closest node in the chain using the comparator.
    `void` | `remove()` Remove this ChainLink from the chain.
    `void` | `setItem(E item)` Set the item for this ChainLink.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Field Detail

    - #### item

          private E item

    - #### head

          private boolean head

    - #### left

          private ChainLink<E> left

    - #### right

          private ChainLink<E> right

  - ### Constructor Detail

    - #### ChainLink

          public ChainLink()

      Create an empty ChainLink. It is necessary to call `setItem()` for this to be useful.

    - #### ChainLink

          public ChainLink(E item)

      Create a ChainLink containing an item.
      Parameters:
        `item` - this ChainLink's item

  - ### Method Detail

    - #### setItem

          public void setItem(E item)

      Set the item for this ChainLink.
      Parameters:
        `item` - this ChainLink's item

    - #### getItem

          public E getItem()

      Get the item from this ChainLink.
      Returns:
        the item

    - #### isHead

          public boolean isHead()

      One node is marked as the head of the chain.
      Returns:
        whether this node is the head of the chain.

    - #### getLeft

          public ChainLink<E> getLeft()

      Get the node to the left of this node. The left node of the head is the tail.
      Returns:
        the node to the left

    - #### getRight

          public ChainLink<E> getRight()

      Get the node to the right of this node. The right node of the tail is the head.
      Returns:
        the node to the right

    - #### remove

          public void remove()

      Remove this ChainLink from the chain. If the head is removed, then the node that was to the right is now the head.

    - #### addBefore

          public void addBefore(ChainLink<E> node)

      Add this node before the given node. If the given node was the head, then this node is now the head.
      Parameters:
        `node` - A location in the chain for insertion.

    - #### addAfter

          public void addAfter(ChainLink<E> node)

      Add this node after the given node. If the given node was the head, it still is.
      Parameters:
        `node` - A location in the chain for insertion.

    - #### findHead

          public ChainLink<E> findHead()

      Find the head of the chain. Note, this is most efficient if this node is the head.
      Returns:
        the head of the chain.

    - #### findTail

          public ChainLink<E> findTail()

      Find the tail of the chain. Note, this is most efficient if this node is the head.
      Returns:
        the tail of the chain.

    - #### addFirst

          public void addFirst(ChainLink<E> anyNode)

      Add this node as the head of the chain. Note, this is most efficient if anyNode is the head of the chain.
      Parameters:
        `anyNode` - any node in the chain

    - #### addLast

          public void addLast(ChainLink<E> anyNode)

      Add this node as the tail of the chain. Note, this is most efficient if anyNode is the head of the chain.
      Parameters:
        `anyNode` - any node in the chain

    - #### locate

          public ChainLink<E> locate(E element,
                            Comparator<E> comparator)

      Locate the node or the closest node in the chain using the comparator. This assumes that the collection is ordered on some characteristic that comparator assumes.
      Parameters:
        `element` - The node to look for
        `comparator` - The comparator that understands the ordering of the list
      Returns:
        The node or the closest node.