package lrs.visitor; import lrs.*; /** * Reverses the host. * @author DXN */ public class Reverse implements IAlgo { public static final Reverse Singleton = new Reverse (); private Reverse () { } /** */ public Object forEmpty(LRStruct host, Object input) { 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); } } }