Interface QueueHandler<E>

Type Parameters:
E - the type of the elements in the queue.

public interface QueueHandler<E>
Instances of this class handle an optionally bounded queue of elements.

It may, upon request, spawn one or more dequeuing threads that will process elements according to a processing function supplied by the consumer.

It also maintains basic statistics about the queue size, peak size, number of queued and rejected elements.

Author:
Laurent Cohen
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Interface
    Description
    static class 
    Builder class for QueueHandler instances.
    static interface 
    An operation applied to each element taken from the queue by the dequeuer thread(s).
    static interface 
    Callback invoked when a new peak queue size is reached.
  • Method Summary

    Modifier and Type
    Method
    Description
    static <E> QueueHandler.Builder<E>
    Create a builder to construct and configure a QueueHandler.
    void
    Close this queue handler.
    int
    Get the peak queue size.
    long
    Get the count elements that were sucessfully queued.
    long
    Get the count elements that were rejected.
    boolean
    Determine whether the queue is empty.
    boolean
    offer(E element)
    Add the specified element to the tail of the queue.
    boolean
    offerToHead(E element)
    Add the specified element to the head of the queue.
    Get the head element from the queue.
    void
    put(E element)
    Add the specified element to the tail of the queue, waiting if necessary for available space in the queue.
    void
    putToHead(E element)
    Add the specified element to the head of the queue, waiting if necessary for available space in the queue.
    int
    Get the size of the queue.
    Get the head element from the queue, waiting if necessary for an element to becomes available.
  • Method Details

    • offer

      boolean offer(E element)
      Add the specified element to the tail of the queue.
      Parameters:
      element - the element to add.
      Returns:
      true if the element was successfully added, false otherwise.
    • offerToHead

      boolean offerToHead(E element)
      Add the specified element to the head of the queue.
      Parameters:
      element - the element to add.
      Returns:
      true if the element was successfully added, false otherwise.
    • put

      void put(E element) throws Exception
      Add the specified element to the tail of the queue, waiting if necessary for available space in the queue.
      Parameters:
      element - the element to add.
      Throws:
      Exception - if any error occurs.
    • putToHead

      void putToHead(E element) throws Exception
      Add the specified element to the head of the queue, waiting if necessary for available space in the queue.
      Parameters:
      element - the element to add.
      Throws:
      Exception - if any error occurs.
    • poll

      E poll()
      Get the head element from the queue.
      Returns:
      the head element, or null if the queue is empty.
    • take

      E take() throws Exception
      Get the head element from the queue, waiting if necessary for an element to becomes available.
      Returns:
      the head element.
      Throws:
      Exception - if any error occurs.
    • isEmpty

      boolean isEmpty()
      Determine whether the queue is empty.
      Returns:
      true if the queue is empty, false otherwise.
    • size

      int size()
      Get the size of the queue.
      Returns:
      the queue size as an int.
    • getPeakSize

      int getPeakSize()
      Get the peak queue size.
      Returns:
      the peak queue size as an int.
    • getQueuedElements

      long getQueuedElements()
      Get the count elements that were sucessfully queued.
      Returns:
      the count as a long value.
    • getRejectedElements

      long getRejectedElements()
      Get the count elements that were rejected.
      Returns:
      the count as a long value.
    • close

      void close()
      Close this queue handler.
    • builder

      static <E> QueueHandler.Builder<E> builder()
      Create a builder to construct and configure a QueueHandler.
      Type Parameters:
      E - the type of elements in the queue.
      Returns:
      A build object ot construct and configure a QueueHandler.