package rpi.goldsd.container;

/**
 * The <tt>ListNode</tt> class represents a single node of the
 * <tt>LinkedList</tt> container class by implementing the <tt>Linkable</tt>
 * interface.  Note that the <tt>Comparable</tt> interface is also implemented
 * by simply invoking the underlying interface of the <tt>Comparable</tt>
 * object that is stored in this <tt>ListNode</tt>.
 *
 * @see LinkedList
 * @see Linkable
 * @see Comparable
 * @version 1.3, 4/16/98
 * @author David Goldschmidt
 */
public class ListNode implements Comparable, Linkable
{
  /**
   * The constructor contains a single required argument that represents
   * the <tt>Comparable</tt> object to be stored in this <tt>ListNode</tt>.
   * @param data the <tt>Comparable</tt> object to be stored in this
   *             <tt>ListNode</tt>.
   */
  public ListNode( Comparable data )
  {
    this.data = data;
    this.next = this.previous = null;
  }


  /** The default constructor is not to be invoked. */
  protected ListNode()  {}


  /**
   * Returns the <tt>Comparable</tt> object stored in this <tt>ListNode</tt>.
   * @return the <tt>Comparable</tt> object stored in this <tt>ListNode</tt>.
   */
  public Comparable getData()  { return data; }


  /**
   * Returns a reference to the next <tt>Linkable</tt> object that follows
   * this <tt>ListNode</tt>.
   * @return a reference to the next <tt>Linkable</tt> object that follows
   *         this <tt>ListNode</tt>.
   */
  public Linkable getNext()  { return next; }


  /**
   * Returns a reference to the previous <tt>Linkable</tt> object that
   * precedes this <tt>ListNode</tt>.
   * @return a reference to the previous <tt>Linkable</tt> object that
   *         precedes this <tt>ListNode</tt>.
   */
  public Linkable getPrevious()  { return previous; }


  /**
   * Compares this <tt>ListNode</tt> to either another <tt>ListNode</tt>
   * object or to another <tt>Comparable</tt> object (presumably of the
   * same type of the data associated with this <tt>ListNode</tt>).
   * @return <tt>true</tt> if this <tt>ListNode</tt> is determined to be
   *         equal to the <tt>Comparable</tt> argument <tt>C</tt>;
   *         <tt>false</tt> otherwise.
   */
  public boolean isEqualTo( Comparable C )
  {
    if ( C instanceof ListNode )
      return ( data.isEqualTo( ((ListNode)C).getData() ) );
    else
      return ( data.isEqualTo( C ) );
  }


  /**
   * Compares this <tt>ListNode</tt> to either another <tt>ListNode</tt>
   * object or to another <tt>Comparable</tt> object (presumably of the
   * same type of the data associated with this <tt>ListNode</tt>).
   * @return <tt>true</tt> if this <tt>ListNode</tt> is determined to be
   *         less than the <tt>Comparable</tt> argument <tt>C</tt>;
   *         <tt>false</tt> otherwise.
   */
  public boolean isLessThan( Comparable C )
  {
    if ( C instanceof ListNode )
      return ( data.isLessThan( ((ListNode)C).getData() ) );
    else
      return ( data.isLessThan( C ) );
  }


  /**
   * Sets the <tt>Comparable</tt> object stored in this <tt>ListNode</tt>.
   * @param data the <tt>Comparable</tt> object to be stored in this
   *             <tt>ListNode</tt>.
   */
  public void setData( Comparable data )  { this.data = data; }


  /**
   * Establishes a link to the next <tt>Linkable</tt> object in the list.
   * @param node the <tt>Linkable</tt> object to be linked as the next
   *             <tt>Linkable</tt> object after this <tt>ListNode</tt>.
   */
  public void setNext( Linkable node )  { next = (ListNode)node; }


  /**
   * Establishes a link to the previous <tt>Linkable</tt> object in the list.
   * @param node the <tt>Linkable</tt> object to be linked as the previous
   *             <tt>Linkable</tt> object to this <tt>ListNode</tt>.
   */
  public void setPrevious( Linkable node )
  {
    previous = (ListNode)node;
  }


  /**
   * The <tt>toString()</tt> is the default method used to display this
   * <tt>ListNode</tt> object in the form of a <tt>String</tt> object.
   * @return a <tt>String</tt> representing this <tt>ListNode</tt>.
   */
  public String toString()  { return ( "" + data ); }


  /**
   * The data associated with this <tt>ListNode</tt> must implement the
   * <tt>Comparable</tt> interface.
   */
  protected Comparable data;


  /**
   * Identifies the next <tt>ListNode</tt> object in the corresponding
   * <tt>LinkedList</tt> object.
   */
  protected ListNode next;


  /**
   * Identifies the previous <tt>ListNode</tt> object in the corresponding
   * <tt>LinkedList</tt> object.
   */
  protected ListNode previous;
}

