/* Copyright (C) 1998 H. Conrad Cunningham.  All rights reserved.  */

package SoftwareInterfaces;

/**
 * A class that provides an enumeration iterator for the doubly linked list
 * implementation of the ranked sequence abstract data type 
 *
 * @author H. Conrad Cunningham
 * @version 1 June 1998
 */

public class DoubleLinkRankedSeqEnum implements java.util.Enumeration
{   
    /**
     * Constructs an enumeration iterator for instances of class 
     * <code>DoubleLinkRankedSeq</code>.
     *
     * @param frontSeq a reference to the first node of the list containing 
     *        the sequence elements
     * <dt><b>Postcondition:</strong> 
     *     enumerator is initialized to the first element (if any) of the 
     *     sequence
     */
    public DoubleLinkRankedSeqEnum (DoubleLink frontSeq)
    {   next = frontSeq; }


    /**
     * Determines whether more elements from the sequence remain to be 
     * enumerated by this enumerator.
     *
     * @return true if more elements exist; false otherwise
     */
    public boolean hasMoreElements()
    {   return (next != null); }


    /**
     * Retrieves the next unprocessed element of this enumeration of 
     * the sequence. 
     *
     * <dt><b>Preconditon:</b> 
     * <dd><code>hasMoreElements()</code>
     *
     * @return the next "unprocessed" element of this enumeration.
     * <dt><b>Postcondition:</b> 
     * <dd>enumerator is advanced so that the succeeding element (if any)
     *     will be returned on any future call.
     */
    public Object nextElement()
    {   try // AssertionViolation not in Enumeration interface definition
        {   Assert.pre(hasMoreElements()); }
        catch(AssertionViolation m)
	{   System.out.println("Precondition Violation exception " +
			       "in DoubleLinkRankedSeqEnum.nextElement.");
	    System.exit(1);
	}
        Object ret = next.getElem();
        next = next.getNext();
	return ret;
    }
    
    /* Invariant:  next refers to a node in a valid DoubleLinkRankedSeq
                   instance
     */

    private DoubleLink next;  // next unprocessed element of the enumeration
}
