package listFW.visitor; import listFW.*; /** * Reverses the host list by accumulating the * reverse as one traverses the list. * This is best done via a helper visitor. * The accumulated reverse list can be * viewed as a "stack": just stack the first * as one traverses down the list; by the time * the end is reached, the accumulated stack * is the reverse list. * Remember that the end of a list is marked * by the empty list. * An IListFactory is passed to the constructor. * @author D. X. Nguyen * @custom Copyright 2002 - All rights reserved */ public class Reverse implements IListAlgo { private IListFactory _fact; public Reverse (IListFactory f) { _fact = f; } /** * Returns the host since the reverse of * the empty list is the empty list. * @param host an IEmptyList * @param nu not used * @return host */ public Object emptyCase(IEmptyList host, Object nu) { return host; } /** * Passes to the host's rest the list consisting * of the host's first as the accumulated * reverse of the list preceding the host's rest, * and asks for help to reverse the host. * @param host * @param nu nut used * @return INEList, the reverse of host. */ public Object nonEmptyCase(INEList host, Object nu) { IList acc = _fact.makeNEList(host.getFirst(), _fact.makeEmptyList()); return host.getRest().execute(new HelpReverse(_fact), acc); } }