/*
 * $Id: VectorQueue.java,v 1.5 1999/12/29 21:23:38 yamauchi Exp $
 *     (M\pi)^2 : Queue implementation by Vector
 *	Copyright 1999 By YAMAUCHI Hitoshi 山内 斉 
 *	@author YAMAUCHI Hitoshi 山内 斉
 *
 * 	Queue : first in first out
 *
 * 1999年8月18日(水)
 */

package jp.ac.tohoku.is.archi.mpi2.util;

// import java.util.Vector;
import jp.ac.tohoku.is.archi.mpi2.jdk12src.Vector;

/**
 * Queue implementation (FIFO)
 */
public final class VectorQueue implements RandomAccessQueue {
    private Vector mQueueImple;

    public VectorQueue()
    {
	this.mQueueImple = new Vector();
    }

    /**
     * enqueue item to the queue
     */
    public void enq(Object item)
    {
	this.mQueueImple.addElement(item);
    }

    /**
     * dequeue item from the queue
     *	if there is no elements in queue, throw RuntimeException
     */
    public Object deq()
    {
	if(this.mQueueImple.isEmpty())
	{
	    throw new RuntimeException("Warning!:cannot deq from empty queue");
	}else
	{
	    Object retitem = this.mQueueImple.firstElement();
	    this.mQueueImple.removeElementAt(0);
	    return(retitem);
	}
    }

    /**
     * if queue is empty return true
     */
    public boolean isEmpty()
    {
	return(this.mQueueImple.isEmpty());
    }

    /**
     * if queue is not empty return true
     *	実際には何かある場合にどうこうするということが多いのでこれを付
     *	加した
     */
    public boolean hasElements()
    {
	return(!(this.mQueueImple.isEmpty()));	
    }

    /**
     * get length of this queue
     */
    public int length()
    {
	return(this.mQueueImple.size());
    }

    /**
     * peek insight
     */
    public Object peek(int i)
    {
	return(this.mQueueImple.elementAt(i));
    }

    /**
     * delete element
     */
    public Object del(int i)
    {
	if(this.mQueueImple.isEmpty())
	{
	    throw new RuntimeException("Warning!:cannot del from empty queue");
	}else
	{
	    Object retitem = this.mQueueImple.remove(i);
	    return(retitem);
	}
    }

    public String toString()
    {
	// 最初のいくつかを表示する
	final int printnum = 3;
	String s = "length = " + this.mQueueImple.size() + "\n";
	for(int i = 0; (i < this.mQueueImple.size()) && (i < printnum); i++)
	{
	    s += "[" + i + "] " + this.mQueueImple.elementAt(i) + "\n";
	}
	return(s);
    }
}
