package listFW; /** * Represents an abstract algorithm to be executed by a host IList. * Plays the role of a "visitor" in the design pattern language. *

* In non OO programming paradigms, an algorithm on lists is expressed as a * function or procedure that takes in a list as parameter. In particular, * our Comp 210 Scheme class prescribes a 'template' for writing functions that * operates on lists as follows.

*
 * (define f (lst)
 *   (cond
 *     [(empty? lst) ...]
 *     [(cons? lst) ...(first lst) ...(f (rest lst)...]))
 * 
* In our OO formulation, an alogrithm on lists is an interface that has two * methods, each takes in a list as a parameter: * When we want to perform an alogrithm (IListAlgo) on a list (IList), we asks * the IList to 'execute' the algorithm with a given input. An IEmptyList knows * instrinsically that is empty and calls the emptyCase method of the IListAlgo. * Analogously, a non-empty list (INEList) will correctly call the nonEmptyCase * method of the IListAlgo. *

* One need not (and should not) check for the type of a list before applying * an algorithm to it. THERE IS NO cond! *

* @author Dung X. Nguyen * @author Stephen B. Wong * @since Copyright 2002 - DXN, SBW All rights reserved * @dependency listFW.INEList operates on (via nonEmptyCase) * @dependency listFW.IEmptyList operates on (via emptyCase) */ public interface IListAlgo { /** * Operates on IEmptyList, the empty list. Since * IEmptyList has only one method, execute(), * there is not much the host list can do here. So the code for the empty * case usually involves: * This corresponds to the [(empty? lst) ...] part of the * (Comp 210) Scheme function template. * @param host the IEmptyList that is executing this algorithm. * @param inp input parameter(s) that can be used for any purpose. * @return result from calling this method. The type of the result is * problem-specific and may be null. */ public abstract Object emptyCase(IEmptyList host, Object... inp); /** * Operates on INEList, a non-empty list. The host list now has * a first and a rest. So the code here usually involves what * host.getFirst() and host.getRest() can do to * accomplish the task at hand.

* To summarize, the code for the non-empty case usually involves:

* This corresponds to the * [(cons? lst) ...(first lst) ...(f (rest lst))...] * part of the (Comp 210) Scheme function template.

* @param host the INEList that is executing this algorithm * @param inp input parameter(s) that can be used for any purpose. * @return result from calling this method. The type of the result is * problem-specific and may be null. */ public abstract Object nonEmptyCase(INEList host, Object... inp); }