LastElement.java
Created with JBuilder

package listFW.visitor;

import listFW.*;

/**
 * Computes the last element in the host by passing the host's first down to
 * the host's rest and asks for help to find the last element, in the case the
 * host is not empty.  The case of an empty host is trivial: no such element!
 * @author D. X. Nguyen
 */
public class LastElement implements IListAlgo {
    public static final LastElement Singleton = new LastElement ();

    private LastElement () {
    }

    /**
    * Throws an IllegalArgumentException since the empty list has no element.
    * @param host  not used
    * @param input not used
    * @return does not return
    * @exception IllegalArgumentException
    */
    public Object emptyCase(IEmptyList host, Object input) {
        throw new IllegalArgumentException ("Empty list has no data!");
    }

    /**
    * Passes the host's fist to the host's rest and asks for help to figure out
    * what the last element is.
    * @param host a non-empty list
    * @param input not used
    * @return Object the last element in the host
    */
    public Object nonEmptyCase(INEList host, Object input) {
        return host.getRest ().execute(HelpGetLast.Singleton, host.getFirst ());
    }
}



LastElement.java
Created with JBuilder