package schemeFactory;
/**
* Represents an abstract Scheme-like list, an immutable linear recursive
* structure of data. Has abstract methods to provide its internal data and
* structure to the client.
* @author Dung X. Nguyen
* @version 3.1
* @since 02/24/02
* @Custom Copyright 2002 -All rights reserved
*/
public abstract class AList {
/**
* Returns the first element in this AList
, if any.
* The behavior for the empty list is undefined.
*/
public abstract Object getFirst();
/**
* Returns the tail ("rest") of this AList
, if any.
* The behavior for the empty list is undefined.
*/
public abstract AList getRest();
/**
* "Hook" method to execute any IListAlgo visitor.
* @param algo the algorithm operating on this AList.
* @param inp the input needed by algo to perform its task.
* @return the output Object of algo.
*/
public abstract Object execute(IListAlgo algo, Object inp);
/**
* Calls anonymous visitor to compute a String representation for AList.
*/
public String toString() {
return (String)execute(new IListAlgo() {
/**
* Returns "()"
* @param host an empty list
* @param inp not used
* @return String
*/
public Object emptyCase(AList host, Object inp) {
return "()";
}
/**
* Returns "(", followed by String reperesentation of elements in
* host separated by spaces, followed by ")".
* @param host a non-empty list.
* @param inp not used
* @return String
*/
public Object nonEmptyCase(AList host, Object inp) {
return "(" + host.getFirst() + host.getRest().execute(new IListAlgo() {
public Object emptyCase(AList h, Object i) {
return ")";
}
public Object nonEmptyCase(AList h, Object i) {
return " " + h.getFirst() + h.getRest().execute (this, null);
}
}, null);
}
}, null);
}
}