/** * SortedList.java * * * Created: Mon Mar 3 16:38:01 2003 * * @author Stuart C. Shapiro */ public class SortedList extends LinkdList { /** * Constructs an empty SortedList. */ public SortedList (){ super(); } /** * Constructs a new SortedList consisting of a * Comparable object prepended to another list. * * @param obj the Comparable object to be the first * element of this list. * @param list a SortedList to be the * rest of this list. * @exception IllegalArgumentException if this list has a first * element and obj 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 Object value * @return a boolean value * @exception UnsupportedOperationException */ public boolean add(Object o) { throw new UnsupportedOperationException(); } /** * Returns a SortedList like this one, but with * obj inserted in its proper order. * * @param obj a Comparable object to be inserted into * this list. * @return a SortedList like this one, but with * obj 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