// The Science of Computing OrderedList class.

// History:
//   August 2003 -- Created by Greg Scragg.
//   October 2003 -- updates to several methods by GWS
//   Februray 2004 -- aligned with text, GWS
//   May 2004 -- Documentation cleaned up by Doug Baldwin.


package geneseo.cs.sc;


/**
 * Represents lists whose contents are ordered according to a "less
 * than" relation. Specifically, the smallest item is first in the list, then
 * the next smallest, and so forth. Some of this class's methods are destructive,
 * and therefore do not require assignments within client code when modifying ordered
 * lists.
 * <p>This class was created to support the text <cite>Algorithms & Data Structures:
 * The Science of Computing</cite> by Doug Baldwin and Greg Scragg. All references in
 * this documentation to &quot;the text&quot; refer to that book. In the text, lists 
 * and ordered lists are described in Chapter 11; this class is basically the one
 * developed in section 11.5.3.</p>
 */
 
public class OrderedList extends List {


    /**
     * Initialize an ordered list to be empty. For example,
     * <p><code>OrderedList oList = new OrderedList();</code></p>
     */

    public OrderedList() {
        super();
    }


    /**
     * Create a new list that is an instance of <code>OrderedList</code>,
     * as required of any subclass of <code>List</code>. Clients of
     * <code>OrderedList</code> generally do not send this message themselves,
     * but code inside inherited methods relies on it.
     * @return A new, empty, <code>OrderedList</code> object
     */
 
    public List makeNewList() {
        return new OrderedList();
    }


    /**
     * Inserts an object into its proper position in an ordered list. Specifically,
     * the new object is placed after all elements already in the list that are less
     * than or equal to it, but before any objects greater than it. For example
     * <p><code>oList.insert( "Jill" );</code></p>
     * @param newItem The object to be inserted into the List.
     */

    public void insert (Comparable newItem) {
        if (this.isEmpty())
            this.addItem(newItem);
        else {
            if(((Comparable) this.getFirst()).compareTo(newItem) > 0)
                    this.addItem(newItem);
            else ((OrderedList) this.getRest()).insert(newItem);
        } 
    }

}