package lrs.visitor; import lrs.*; /** * Represents an abstract algorithm on an LRStruct. Acts as a visitor to a * LRStruct host. * @author Dung X. Nguyen Copyright 2005 - All rights reserved. * @since 03/29/05 */ public class SwapEnds implements IAlgo { public static final SwapEnds Singleton = new SwapEnds(); private SwapEnds(){} public Object emptyCase(LRStruct host, Object... nu){ return null; // there is nothing to swap. } public Object nonEmptyCase(final LRStruct host, Object... nu){ LRStruct taillist = host.getRest(); return taillist.execute(new IAlgo(){ public Object emptyCase(LRStruct child, Object... nu){ return null; // host is a single element list. } public Object nonEmptyCase(LRStruct child, Object... nu){ return child.getRest().execute(new IAlgo(){ public Object emptyCase(LRStruct c, Object... p){ Integer lastElement = (Integer)((LRStruct)p[0]).getFirst(); Integer firstElement = (Integer)host.getFirst(); host.setFirst(lastElement); ((LRStruct) p[0]).setFirst(firstElement); return null; } public Object nonEmptyCase(LRStruct c, Object... nu){ return c.getRest().execute(this, c); } }, child); } }); } }