NEList.java
Created with JBuilder

/**
 * Represents a non-empty list.
 * Implemented as a composite.
 * @author Dung X. Nguyen
 * @version 1.1
 * @since 01/25/02
 * @Custom Copyright 2002 -All rights reserved
 */
public class NEList extends AList {
    /**
     * The first element of this NEList.
     */
    private Object _first;

    /**
     * The "tail" (or "rest") of this NEList.
     */
    private AList _rest;

    /**
     * Initializes this NEList to the given first and rest.
     * @param dat the first data element of this NEList.
     * @param tail != null, the rest of this AList.
     */
    public NEList(Object dat, AList tail) {
        _first = dat;
        _rest = tail;
    }

    /**
    * Returns the first element of this NEList.
    * @return an Object (may be null).
    */
    public Object getFirst() {
        return _first;
    }

    /**
    * Returns the rest of this NEList.
    * @return AList != null.
    */
    public AList getRest() {
        return _rest;
    }

    /**
     * Returns a String representation of this NEList. 
* @return "(" followed by the String representation of first data element, * followed by a space, followed by the String representation of the rest, * followed by ")". */ public String toString() { return "(" + _first + " " + _rest + ")"; } /** * Returns 1 + the number of elements in the rest of this NEList. * @return int > 0. */ public int getLength() { return 1 + _rest.getLength(); } /** * 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()); } /** * Asks the tail for help to compute the length, passing it an accumulated * length of 1, and returns the result. * @return int > 0. */ public int getLen() { return _rest.helpGetLen(1); } /** * Adds 1 to the accumulated length, passes it down to the tail for help * to compute the length, and returns the result. * @param acc the length of the list preceding this NEList. * @return int > acc. */ protected int helpGetLen(int acc) { return _rest.helpGetLen(acc + 1); } /** * Asks _rest for help to compute the min, passing it the int value of * _first as the accumulated min. * @return the minimum int contained in this NEList. */ public int getMin() { return _rest.helpGetMin(((Integer)_first).intValue()); } /** * Computes the smaller of the accumulated min parameter and the value of * _first, and passes the result down to the tail for help to compute the min. * @param accMin the smallest int containing in the list preceding this NEList. * @return the minimum int contained in the preceding list and this NEList. */ protected int helpGetMin(int accMin) { return _rest.helpGetMin(Math.min(((Integer)_first).intValue(), accMin)); } }
NEList.java
Created with JBuilder