// file: ListEnum.java // author: Robert Keller // purpose: List enumeration class of Poly package package Poly; import java.lang.*; import java.util.Enumeration; /** * ListEnum is an enumeration class for the class List. It * implements the interface java.util.Enumeration, i.e. the methods: * hasMoreElements() and nextElement(). Note that because ListEnum * implements Enumeration, the return type for nextElement() is Object * and not Poly. **/ public class ListEnum implements java.util.Enumeration { List L; // current list /** * ListEnum constructs a ListEnum from a List. **/ public ListEnum(List L) // constructor { this.L = L; } /** * hasMoreElements() indicates whether there are more elements left in * the enumeration. **/ public boolean hasMoreElements() { return L.nonEmpty(); } /** * nextElement returns the next element in the enumeration. **/ public Object nextElement() { try { if( L.isEmpty() ) throw new java.util.NoSuchElementException("No next element in EmptyList"); NonEmptyList L1 = L.getNonEmpty(); Object result = L1.first(); L = L1.rest(); return result; } catch( EmptyListException e ) { System.err.println("*** Internal error, empty list should not occur"); throw new java.util.NoSuchElementException("No next element in EmptyList"); } } }