/**
* 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. Since an AList has an internal structure that is
* isomorphic to itself, it's best to implement it using the composite pattern.
*/
public abstract class AList {
/**
* Computes and returns the number of elements in this AList.
* @return int >= 0.
*/
public abstract int getLength();
/**
* Computes and returns the minimum of this AList, assuming it contains
* Integer objects.
* @return behavior relegated to concrete subclasses.
*/
public abstract int getMinimum();
// Other methods elided...
}
| public
class EmptyList extends
AList { public final static EmptyList Singleton = new EmptyList(); private EmptyList() { } |
public class
NEList extends AList { private Object _first; private AList _rest; |
| /** * Computes the length of this EmptyList. * @return 0 */ public int getLength() { return 0; } |
/** * Returns 1 + the number of elements in the rest of this NEList. * @return int > 0. */ public int getLength() { return 1 + _rest.getLength(); } |
|
/** * The minimum of the Empty list can be thought of as +infinity. * @return Integet.MAX_VALUE representing +infinity. */ public int getMinimum() { return Integer.MAX_VALUE; } } |
/** * Returns the minimum of the first and the minimum of the rest. * @return an int. */ public int getMinimum() { return Math.min(((Integer)_first).intValue(), _rest.getMinimum()); } } |