import java.util.*;

/*
 * QueueObject - general class that implements a queue mechanism
 * using a Java Vector.  Also implements signaling in the
 * GetQueueItem and SetQueueItem methods
 *
 * Author:  Mike Criscolo
 * Date:    09/02/97
 */
public class QueueObject {

    protected Vector    queue;
    protected int       itemcount;
    protected String    queueName;

    public QueueObject(String name) {

        queue = new Vector();
        queueName = name;
        itemcount = 0;
    }

    // Get an item from the vector.  Wait if no items available
    public synchronized Object GetQueueItem() {

        Object   item = null;

        // If no items available, drop into wait() call
        if (itemcount == 0) {
            try {
                wait();
            } catch (InterruptedException e) {
                System.out.println(queueName + ": Hey! Somebody woke me up!");
            }
        }

        // Get the first item from the vector, remove it and decrement
        // the item count.
        item = (Object)queue.firstElement();
        queue.removeElement(item);
        itemcount--;

        // Send it back
        return item;
    }

    // Place an item onto the vector. Signal threads that an item
    // is available.
    public synchronized void AddQueueItem(Object o) {

        System.out.println(queueName + ": Adding item to queue...");
        itemcount++;
        queue.addElement(o);
        notify();
    }

    // Handy place to put a separate notify call - used during
    // shutdown.
    public synchronized void BumpQueue() {

        notify();
    }
}
