import schemeFW.*; /** * Inserts an Integer into an ordered host list, assuming the host list contains * only Integer objects. * @author D. X. Nguyen */ public class InsertInOrder implements IListAlgo { public static final InsertInOrder Singleton = new InsertInOrder(); private InsertInOrder() { } /** * This is easy, don't you think? * @param inp an Integer to be inserted in order into host. */ public Object emptyCase(IEmptyList host, Object inp) { return new NEList(inp, host); } /** * Recur! * @param inp an Integer to be inserted in order into host. */ public Object nonEmptyCase(INEList host, Object inp) { int n = ((Integer)inp).intValue(); int f = ((Integer)host.getFirst()).intValue(); return n < f? new NEList(inp, host): new NEList(host.getFirst(), (IList)host.getRest().execute(this, inp)); } }