/**
 * A buffer contains up to SIZE elements.
 *
 * @version     1.1    January 28, 2000
 * @author      Franck van Breugel
 */
public class Buffer {
    private static final int SIZE = 10;
    private Object[] buffer = new Object[SIZE];
    private int inCount = 0;  // number of elements put in this buffer
    private int outCount = 0; // number of elements got out of this buffer

    /** 
     * Removes the first element from this buffer and returns it.
     * @return Element removed from this buffer.
     */
    public synchronized Object get() {
        while (inCount - outCount <= 0) {
            try {
                wait();
            } catch (InterruptedException e) {}
        }
        Object temp = buffer[outCount % SIZE];
        outCount++;
        notifyAll();
        return temp;
    }

    /** 
     * Puts the specified element in at the end of this buffer.
     * @param element Element to be added to this buffer.
     */
    public synchronized void put(Object element) {
        while (inCount - outCount >= SIZE) {
            try {
                wait();
            } catch (InterruptedException e) {}
        }
        buffer[inCount % SIZE] = element;
        inCount++;
        notifyAll();
    }
}