// Filename: BoundedBuffer.java
// Author: Professor Needham
// Course: SI411
// A modification of the Chapter 4 example in Silberschatz's OS text.


import java.util.*;

public class BoundedBuffer {
   public BoundedBuffer() {
      // buffer is initially empty
      count = 0;
      in = 0;
      out = 0;
      buffer = new Object[BUFFER_SIZE];
   }

   // producer calls this method
   public void enter(Object item) {
      while (count == BUFFER_SIZE)
         ; // do nothing

      // add an item to the buffer
      ++count;
      buffer[in] = item;
      in = (in + 1) % BUFFER_SIZE;

	if (count == BUFFER_SIZE)
                System.out.println("Producer Entered " + item + " Buffer FULL");
        else
                System.out.println("Producer Entered " + item + " Buffer Size = " +  count);
   }

   // consumer calls this method
   public Object remove() {
      Object item;

      while (count == 0)
         ; // do nothing

      // remove an item from the buffer
      --count;
      item = buffer[out];
      out = (out + 1) % BUFFER_SIZE;

	if (count == 0)
                System.out.println("Consumer Consumed " + item + " Buffer EMPTY");
        else
                System.out.println("Consumer Consumed " + item + " Buffer Size = " + count);

      return item;
   }


   public static final int    NAP_TIME = 5;
   private static final int   BUFFER_SIZE = 3;

   private volatile int count;
   private int in;   // points to the next free position in the buffer
   private int out;  // points to the position in the buffer from which
                     // the next item should be removed
   private Object[] buffer;
}

