package lrs.visitor; import lrs.*; /** * Reverses the host. * @since 10/05/01 * @author DXN */ public class Reverse implements IAlgo { public static final Reverse Singleton = new Reverse(); private Reverse() { } /** * Does nothing: the empty host is the reverse of itself. * @return null */ public Object forEmpty(LRStruct host, Object input) { return null; } /** * Asks for help to recursively move down the host, removes one element at * a time and inserting it to the front of the host. In the end, the host * is reversed. * @return null */ public Object forNonEmpty(LRStruct host, Object input) { return host.getRest().execute(ReverseHelp.Singleton, host); } public static void main (String[] args) { LRStruct p = new LRStruct (); testVisitor (p, Reverse.Singleton, null); p.insertFront(new Integer (-1)); testVisitor (p, Reverse.Singleton, null); p.insertFront (new Integer (3)); testVisitor (p, Reverse.Singleton, null); p.insertFront(new Integer (5)); testVisitor (p, Reverse.Singleton, null); p.insertFront(new Integer (99)); testVisitor (p, Reverse.Singleton, null); } private static void testVisitor(LRStruct p, IAlgo algo, Object input) { // NOTE the use of Object.getClass ()!!! // Also watch what happens when input == null!!! try { System.out.println (p + " execute " + algo.getClass () + ", " + input + "..."); System.out.println (" ---> " + p.execute (algo, input) + " " + p); } catch (Exception e) { System.out.println (e); } } }