import java.util.LinkedList;

public class Buffer
{
	int maxElements;
	LinkedList buffer;
	
	public Buffer(int size)
	{
		buffer = new LinkedList();
		maxElements = size;
	} //constructor
	
	public synchronized void put(Object o)
	{
		while(maxElements == buffer.size())
		{
			try
			{
				wait();
			} //try
			catch (InterruptedException ie)
			{
				System.out.println("An InterruptedException caught\n"+ie.getMessage());
				ie.printStackTrace();
			} //catch
		} //while
		
		buffer.add(o);
		notifyAll();
	} //put()
	
	public synchronized Object get()
	{
		while ( buffer.isEmpty() )
		{
			try
			{
				wait();
			} //try
			catch (InterruptedException ie)
			{
				System.out.println("An InterruptedException caught\n"+ie.getMessage());
				ie.printStackTrace();
			} //catch
		} //while
		
		notifyAll();
		return( buffer.removeFirst() );
	} //get()
	
	public synchronized int getSize()
	{
		return ( buffer.size() );
	} //getSize()
} //class Buffer		