ToString.java
Created with JBuilder

package listFW.visitor;

import listFW.*;

/**
 * Computes a Scheme-like String representation of a list.  Uses a helper
 * visitor to compute the String representaion of the rest to avoid having an
 * extra space before closing the parenthesis.
 * @author D. X. Nguyen
 */
public class ToString implements IListAlgo {
    public static final ToString Singleton = new ToString ();

    private ToString() {
    }

    /**
     * Return "()" as in Scheme.
     * @param host empty not used
     * @param nu  not used
     * @return String
     */
    public Object emptyCase(IEmptyList host, Object nu) {
        return "()";
    }

    /**
     * Concatenates "(" with the host's first and the String representation of
     * the host's rest computed by a helper visitor.
     * @param host a non-empty list.
     * @param nu  not used
     * @return String
     */
    public Object nonEmptyCase(INEList host, Object input) {
        return "(" + host.getFirst()
                + host.getRest().execute(HelpToString.Singleton, null);
    }
}



ToString.java
Created with JBuilder