Class YACBRingQueue<T>

java.lang.Object
net.okapia.osid.torrefacto.queue.YACBRingQueue<T>
Type Parameters:
T - the element type
All Implemented Interfaces:
AutoCloseable, YACBQueue<T>

public class YACBRingQueue<T> extends Object implements YACBQueue<T>
A concurrent bounded blocking queue backed by a ring buffer.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructs a new YACBRingQueue with the default capacity.
    YACBRingQueue(int capacity)
    Constructs a new YACBRingQueue with the given capacity.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Closes the Queue.
    void
    A producer calls this method when there are no more items to add.
    boolean
    Tests if production has ended.
    boolean
    Tests if this Queue has been closed.
    Retrieves the first item at the head of the Queue without removing it.
    Retrieves the first item at the head of the Queue and removes it from the Queue.
    T[]
    pollMany(int n)
    A dandy bulk poll method.
    void
    put(T item)
    Add an item to the end of the Queue.
    void
    putMany(T[] items)
    Another dandy method.
    int
    Gets the current number of items in the Queue.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface YACBQueue

    putMany
    Modifier and Type
    Method
    Description
    default void
    Another dandy method.
  • Constructor Details

    • YACBRingQueue

      public YACBRingQueue()
      Constructs a new YACBRingQueue with the default capacity.
    • YACBRingQueue

      public YACBRingQueue(int capacity)
      Constructs a new YACBRingQueue with the given capacity.
      Parameters:
      capacity - the queue capacity
  • Method Details

    • size

      public int size()
      Gets the current number of items in the Queue.
      Specified by:
      size in interface YACBQueue<T>
      Returns:
      the number of items
    • peek

      public T peek() throws InterruptedException
      Retrieves the first item at the head of the Queue without removing it. If no items are available, this method will block until one item is available or the producer has completed its run.
      Specified by:
      peek in interface YACBQueue<T>
      Returns:
      the first item
      Throws:
      InterruptedException - if thread has been interrupted
    • poll

      public T poll() throws InterruptedException
      Retrieves the first item at the head of the Queue and removes it from the Queue. This method will block if no item is yet available.
      Specified by:
      poll in interface YACBQueue<T>
      Returns:
      the first item
      Throws:
      InterruptedException - if thread has been interrupted
    • pollMany

      public T[] pollMany(int n) throws InterruptedException
      A dandy bulk poll method. Retrieves and removes the given amount of items. If n is larger than what is available, then fewer items may be returned. This method will block until at least one item is available.
      Specified by:
      pollMany in interface YACBQueue<T>
      Parameters:
      n - the number of items to retrieve
      Returns:
      an array of the items
      Throws:
      InterruptedException - if thread has been interrupted
    • put

      public void put(T item) throws InterruptedException
      Add an item to the end of the Queue.
      Specified by:
      put in interface YACBQueue<T>
      Parameters:
      item - an item to add
      Throws:
      InterruptedException - if thread has been interrupted
    • putMany

      public void putMany(T[] items) throws InterruptedException
      Another dandy method. Adds the contents of an array to the queue. All items are enqueued under a single lock acquisition, waiting on notFull if the queue fills mid-batch.
      Specified by:
      putMany in interface YACBQueue<T>
      Parameters:
      items - an array of items to add
      Throws:
      InterruptedException - if thread has been interrupted
    • endProduction

      public void endProduction()
      A producer calls this method when there are no more items to add. It wakes up waiting consumers.
      Specified by:
      endProduction in interface YACBQueue<T>
    • close

      public void close()
      Closes the Queue.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface YACBQueue<T>
    • hasProductionEnded

      public boolean hasProductionEnded()
      Tests if production has ended.
      Specified by:
      hasProductionEnded in interface YACBQueue<T>
      Returns:
      true if production has ended, false otherwise
    • isClosed

      public boolean isClosed()
      Tests if this Queue has been closed.
      Specified by:
      isClosed in interface YACBQueue<T>
      Returns:
      true if closed, false otherwise