/*
 * $Author: bhevans $
 * $Date: 1996/05/26 22:46:34 $
 * $Source: /home/phoenix/cscstd/ijkl/jlarsen/box/RCS/Queue.java,v $
 * $Revision: 1.1 $
 * $Log: Queue.java,v $
# Revision 1.1  1996/05/26  22:46:34  bhevans
# Initial revision
#
 */

package com.nanosoft.pcm.machine;
import com.nanosoft.pcm.machine.*;

/**
 * @see Log
 * @author $Author: bhevans $
 * @version $Revision: 1.1 $
 */
public final
class Queue implements Cloneable {
  private QueueElement head, tail;
  private Object obj;

  public Queue() {;}

  /**
	* @param obj The object to push onto the queue
	*/
  public void push(Object obj) {
	 if ( head == null ) { 
		head = new QueueElement();
		tail = head;
		head.data = obj;
	 }
	 else {
		QueueElement temp  = new QueueElement();
		temp.data = o;
		head.previous = temp;
		head = temp;
	 }
  }

  /**
	* @return  The oldest entry in the queue is returned and removed from 
	* the queue.
	*/
  public Object pop() throws MemoryError {
    if ( head == null ) {
		throw new MemoryError("queue.pop");
	 }
	 else if ( tail == head ) {
		obj = tail.data;
		tail = null;
		head = null;
	 }
	 else {
      obj =  tail.data;
	   tail = tail.previous;
	 }
    return obj;
  }

  /**
	* @return A boolean value indicating whether the queue is empty.
	*/
  public final boolean isEmpty() {
    return (head == null);
  }

  /*
	* @return  A full copy of itself, including the data.
	*/
  public Object clone() {
  // copies the queue
    try { 
		Queue q = (Queue)super.clone();
		return q;
    } 
    catch (CloneNotSupportedException e) { 
      // this shouldn't happen, since we are Cloneable
      throw new InternalError();
    }
  }

  /**
	* Removes the queue's elements
	*/
  public void clean() {
	 while ( ! this.isEmpty() ) this.pop();
  }
}

/**
 *  The queue element is used solely by the Queue class.
 *  A simple structure.
 *  @see Queue
 */
class QueueElement {   
  public Object data;
  public QueueElement previous;  // reference
}