package eu.qualimaster.common.switching;

import java.util.Queue;

import backtype.storm.tuple.Tuple;
/**
 * A synchronized queue adopting the producer-consumer pattern.
 * @author Cui Qin
 *
 */
public class SynchronizedQueue {
    private Queue<Tuple> queue;
    private int size;
    
    /**
     * Creates a synchronized queue.
     * @param queue the queue to store data
     * @param size the size of a full queue 
     */
    public SynchronizedQueue(Queue<Tuple> queue, int size) {
        this.queue = queue;
        this.size = size;
    }
    /**
     * Consumes tuple data from the queue.
     * @return a tuple
     */
    public Tuple consume() {
      //wait if queue is empty
        while (queue.isEmpty()) {
            synchronized (queue) {
                try {
                    queue.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
      //Otherwise consume element and notify waiting producer
        synchronized (queue) {
            queue.notifyAll();
            return queue.poll();
        }
    }

    /**
     * Stores tuples into the queue.
     * @param data the tuple data to be stored
     */
    public void produce(Tuple data) {
        // wait if queue is full
        while (queue.size() == size) {
            synchronized (queue) {
                try {
                    queue.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
        // producing element and notify consumers
        synchronized (queue) {
            queue.offer(data);
            queue.notifyAll();
        }
    }
}

@@ -170,16 +170,45 @@
 ttern.%0D%0A
+ * @param %3CT%3E the data type%0D%0A
  * @auth
@@ -258,16 +258,19 @@
 zedQueue
+%3CT%3E
  %7B%0D%0A    
@@ -280,28 +280,24 @@
 vate Queue%3CT
-uple
 %3E queue;%0D%0A  
@@ -506,20 +506,16 @@
 (Queue%3CT
-uple
 %3E queue,
@@ -689,20 +689,16 @@
 public T
-uple
  consume
@@ -1313,20 +1313,16 @@
 roduce(T
-uple
  data) %7B

convert to generic type