package clist.visitor; import clist.*; /** * Remove the n-th element in the host list. n == 0 corresponds to the first * element in the host list. * n is passed to the visitor as the input parameter */ public class RemNthClockwise implements ICLVisitor { public final static RemNthClockwise Singleton = new RemNthClockwise (); private RemNthClockwise() { } /** * Throws an exception. * @param host * @param stop * @param inp the Integer index n. * @return */ public Object forEmpty (CLList host, CLList stop, Object inp) { throw new java.util.NoSuchElementException ("No such element"); } /** * @param host * @param stop * @param inp the Integer index n. * @return the data Obkect removed (if any). * @throw IllegalArgumentException if the index n > number of elements in host - 1. */ public Object forNonEmpty (CLList host, CLList stop, Object inp) { int ix = ((Integer)inp).intValue (); CLList tail = host.getClockwiseTail(); if (0 == ix) { if (tail.equals (stop)) { return stop.remCounterwise(); } else { return host.remClockwise(); } } else if (tail.equals (stop)) { throw new IllegalArgumentException ("No such element"); } else { return tail.execClockwise (this, stop, new Integer (--ix)); // important to decrement ix first! } } }