/**
 * SortedList.java
 *
 *
 * Created: Mon Mar  3 16:38:01 2003
 *
 * @author <a href="mailto:shapiro@cse.buffalo.edu">Stuart C. Shapiro</a>
 */

public class SortedList extends LinkdList {
    /**
     * Constructs an empty <code>SortedList</code>.
     */
    public SortedList (){
	super();
    }

    /**
     * Constructs a new <code>SortedList</code> consisting of a
     * <code>Comparable</code> object prepended to another list.
     *
     * @param obj the <code>Comparable</code> object to be the first
     * element of this list.
     * @param list a <code>SortedList</code> to be the
     * <code>rest</code> of this list.
     * @exception IllegalArgumentException if this list has a first
     * element and <code>obj</code> does not compare as less than or
     * equal to it.
     */
    public SortedList(Comparable obj, SortedList list) {
	if (list.isEmpty()
	    || obj.compareTo(list.first) <= 0) {
	    first = obj;
	    rest = list;
	    empty = false;
	} else {
	    throw new IllegalArgumentException("Attempt to construct an unsorted SortedList");
	}
    }

    /**
     * Unsupported
     *
     * @param o an <code>Object</code> value
     * @return a <code>boolean</code> value
     * @exception UnsupportedOperationException
     */
    public boolean add(Object o) {
	throw new UnsupportedOperationException();
    }

    /**
     * Returns a <code>SortedList</code> like this one, but with
     * <code>obj</code> inserted in its proper order.
     *
     * @param obj a <code>Comparable</code> object to be inserted into
     * this list.
     * @return a <code>SortedList</code> like this one, but with
     * <code>obj</code> inserted in its proper order.
     */
    public SortedList insert(Comparable obj) {
	if (isEmpty() || obj.compareTo(getFirst()) <= 0) {
	    return new SortedList(obj, this);
	} else {
	    rest = ((SortedList)rest).insert(obj);
	    return this;
	}
    }
    
}// SortedList
