package listFW.visitor; import listFW.*; /** * Reverses a list. The first of the varargs (input[0]) needs to be an IListFactory. */ public class Reverse implements IListAlgo,IListFactory> { /** * Returns the empty list. * @param fac fac[0] is a list factory */ @SuppressWarnings("unchecked") public IList emptyCase(IMTList host, IListFactory... fac) { return (IList)host; } /** * Returns the reversed list. * @param fac fac[0] is a list factory */ @SuppressWarnings("unchecked") public IList nonEmptyCase(INEList host, final IListFactory... fac) { // use a helper to do forward accumulation // initial value is the empty list return host.execute(new IListAlgo,IList>() { public IList emptyCase(IMTList host, IList... input) { // return the accumulator return input[0]; } public IList nonEmptyCase(INEList host, IList... input) { // create a new NEList out of the first and the accumulator, then recur return host.getRest().execute(this, fac[0].makeNEList(host.getFirst(), input[0])); } }, fac[0].makeEmptyList()); } }