package list; /** * Represents a non-empty list. * Implemented as a composite. * * @author Dung X. Nguyen * @version 1.0 * @since 09/10/00 * @Custom Copyright 2000 -All rights reserved */ class NEList extends AList { private Object _first; /** @SBGen Variable (,rest,,64) */ private AList _rest; /** * Throws an IllegalArgumentException if any of the parameters is null. * * @param first != null, the first data element of this AList. * @param rest != null, the rest of this AList. */ NEList (Object first, AList rest) { if (first == null || rest == null) { throw new IllegalArgumentException ("NEList: first and rest must be non-null."); } _first = first; _rest = rest; } /** * @return the Object that is the fisrt element of this AList. */ public Object getFirst () { return _first; } /** * @return the AList that is the rest of this AList. */ public AList getRest () { return _rest; } /** * Returns 1 + the number of elements of the rest. */ public int length() { return 1 + _rest.length (); } /** * Asks the rest for help to compute the length, passing it * an accumulated length of 1. */ public int getLength() { return _rest.getLengthHelp(1); } /** * Adds 1 to the accumulated length and passes it down to * the rest for help to compute the length. */ protected int getLengthHelp(int acc) { return _rest.getLengthHelp(acc + 1); } /** * Returns a "(" followed by the String representation of _first, then * asks the rest to help compute the remaining String representation. */ public String toString () { return "(" + _first.toString () + _rest.toStringHelp(); } /** * Returns a space followed by the String representation of _first, then * asks the rest to help compute the remaining String representation. */ protected String toStringHelp() { return " " + _first.toString () + _rest.toStringHelp(); } /** * Creates and returns a list consisting of the first element and the clone * of the rest. */ public AList makeClone () { return new NEList (_first, _rest.makeClone ()); } /** * Checks for index n == 0. If true returns the first element. Otherwise * ask the rest to return the (n-1)th element. */ public Object nthElement(int n) { if (n == 0) return _first; else return _rest.nthElement(n-1); } /** * Passes the first element down to the rest and asks the rest for help * determining the last element. */ public Object lastElement() { return _rest.lastElementHelp(_first); } /** * Since this AList is not empty, the input parameter cannot * be the last element. * Passes the first if this AList down to _rest to ask for help * determining the last element. */ protected Object lastElementHelp(Object previous) { return _rest.helpGetLast(_first); } /** * Check for n == 0. If true returns the empty AList. Otherwise, * cons first with the first (n-1) elements of the rest, */ public AList firstElements (int num_elts) { if (num_elts < 0) { throw new IllegalArgumentException ("firstElements: Meaningless to ask for a negative number (" + num_elts + ") of elements."); } if (num_elts == 0) return EmptyList.Singleton; else return new NEList (_first, _rest.firstElements (num_elts-1)); } /** * Passes the list consisting of just the first to the rest * and ask the rest to do the job via reverseHelp(). */ public AList reverse() { return _rest.reverseHelp(new NEList (_first, EmptyList.Singleton)); } /** * Updates the reversed list so far, and passes it down recursively to the * rest to finish the job. */ protected AList reverseHelp(AList stack) { return _rest.reverseHelp(new NEList (_first, stack)); } /** * Returns the cons of first with the concatenation of the rest * with the input AList. */ public AList append (AList list2) { return new NEList (_first, _rest.append (list2)); } }