org.ka2ddo.util
Class FastBlockingQueue<E>

java.lang.Object
  extended by java.util.AbstractCollection<E>
      extended by org.ka2ddo.util.FastBlockingQueue<E>
Type Parameters:
E - data type of queued records
All Implemented Interfaces:
java.lang.Iterable<E>, java.util.Collection<E>, java.util.concurrent.BlockingQueue<E>, java.util.Queue<E>

public class FastBlockingQueue<E>
extends java.util.AbstractCollection<E>
implements java.util.concurrent.BlockingQueue<E>

This attempts to make a simpler and faster FIFO queue than ArrayBlockingQueue with no guarantees regarding fairness, minimum execution time, and minimum transient memory allocations. Optimized for single producer and single consumer.


Constructor Summary
FastBlockingQueue(int capacity)
          Create a FastBlockingQueue with the specified maximum queue backlog.
 
Method Summary
 boolean add(E e)
          Add an element to the queue, throwing an exception if the queue is full and has no more room.
 void clear()
          Remove everything from the queue.
 boolean contains(java.lang.Object o)
          Check if this queue contains the specified object (or at least an instance that matches by equals()).
 int drainTo(java.util.Collection<? super E> c)
          Removes all available elements from this queue and adds them to the given collection.
 int drainTo(java.util.Collection<? super E> c, int maxElements)
          Removes at most the given number of available elements from this queue and adds them to the given collection.
 int drainTo(E[] a)
          Removes at most the given number of available elements from this queue and adds them to the given array.
 E element()
          Retrieves, but does not remove, the head of this queue.
 java.util.Iterator<E> iterator()
          Returns an iterator over the elements contained in this collection.
 boolean offer(E e)
          Inserts the specified element into this queue if it is possible to do so immediately without violating capacity restrictions.
 boolean offer(E e, long timeout, java.util.concurrent.TimeUnit unit)
          Inserts the specified element into this queue, waiting up to the specified wait time if necessary for space to become available.
 E peek()
          Retrieves, but does not remove, the head of this queue, or returns null if this queue is empty.
 E poll()
          Retrieves and removes the head of this queue, or returns null if this queue is empty.
 E poll(long timeout, java.util.concurrent.TimeUnit unit)
          Retrieves and removes the head of this queue, waiting up to the specified wait time if necessary for an element to become available.
 void put(E e)
          Inserts the specified element into this queue, waiting if necessary for space to become available.
 int remainingCapacity()
          Returns the number of additional elements that this queue can ideally (in the absence of memory or resource constraints) accept without blocking, or Integer.MAX_VALUE if there is no intrinsic limit.
 E remove()
          Retrieves and removes the head of this queue.
 int size()
          Returns the number of elements in this queue.
 E take()
          Retrieves and removes the head of this queue, waiting if necessary until an element becomes available.
 
Methods inherited from class java.util.AbstractCollection
addAll, containsAll, isEmpty, remove, removeAll, retainAll, toArray, toArray, toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.concurrent.BlockingQueue
remove
 
Methods inherited from interface java.util.Collection
addAll, containsAll, equals, hashCode, isEmpty, removeAll, retainAll, toArray, toArray
 

Constructor Detail

FastBlockingQueue

public FastBlockingQueue(int capacity)
Create a FastBlockingQueue with the specified maximum queue backlog.

Parameters:
capacity - int maximum capacity of queue
Method Detail

clear

public void clear()
Remove everything from the queue.

Specified by:
clear in interface java.util.Collection<E>
Overrides:
clear in class java.util.AbstractCollection<E>

drainTo

public int drainTo(java.util.Collection<? super E> c)
Removes all available elements from this queue and adds them to the given collection. A failure encountered while attempting to add elements to collection c may result in elements being in neither, either or both collections when the associated exception is thrown. Attempts to drain a queue to itself result in IllegalArgumentException. Further, the behavior of this operation is undefined if the specified collection is modified while the operation is in progress.

Specified by:
drainTo in interface java.util.concurrent.BlockingQueue<E>
Parameters:
c - the collection to transfer elements into
Returns:
the number of elements transferred
Throws:
java.lang.UnsupportedOperationException - if addition of elements is not supported by the specified collection
java.lang.ClassCastException - if the class of an element of this queue prevents it from being added to the specified collection
java.lang.NullPointerException - if the specified collection is null
java.lang.IllegalArgumentException - if the specified collection is this queue, or some property of an element of this queue prevents it from being added to the specified collection

iterator

public java.util.Iterator<E> iterator()
Returns an iterator over the elements contained in this collection.

Specified by:
iterator in interface java.lang.Iterable<E>
Specified by:
iterator in interface java.util.Collection<E>
Specified by:
iterator in class java.util.AbstractCollection<E>
Returns:
an iterator over the elements contained in this collection

size

public int size()
Returns the number of elements in this queue.

Specified by:
size in interface java.util.Collection<E>
Specified by:
size in class java.util.AbstractCollection<E>
Returns:
the number of elements in this collection

put

public void put(E e)
         throws java.lang.InterruptedException
Inserts the specified element into this queue, waiting if necessary for space to become available.

Specified by:
put in interface java.util.concurrent.BlockingQueue<E>
Parameters:
e - the element to add
Throws:
java.lang.InterruptedException - if interrupted while waiting
java.lang.ClassCastException - if the class of the specified element prevents it from being added to this queue
java.lang.NullPointerException - if the specified element is null
java.lang.IllegalArgumentException - if some property of the specified element prevents it from being added to this queue

offer

public boolean offer(E e,
                     long timeout,
                     java.util.concurrent.TimeUnit unit)
              throws java.lang.InterruptedException
Inserts the specified element into this queue, waiting up to the specified wait time if necessary for space to become available.

Specified by:
offer in interface java.util.concurrent.BlockingQueue<E>
Parameters:
e - the element to add
timeout - how long to wait before giving up, in units of unit
unit - a TimeUnit determining how to interpret the timeout parameter
Returns:
true if successful, or false if the specified waiting time elapses before space is available
Throws:
java.lang.InterruptedException - if interrupted while waiting
java.lang.ClassCastException - if the class of the specified element prevents it from being added to this queue
java.lang.NullPointerException - if the specified element is null
java.lang.IllegalArgumentException - if some property of the specified element prevents it from being added to this queue

take

public E take()
       throws java.lang.InterruptedException
Retrieves and removes the head of this queue, waiting if necessary until an element becomes available.

Specified by:
take in interface java.util.concurrent.BlockingQueue<E>
Returns:
the head of this queue
Throws:
java.lang.InterruptedException - if interrupted while waiting

poll

public E poll(long timeout,
              java.util.concurrent.TimeUnit unit)
       throws java.lang.InterruptedException
Retrieves and removes the head of this queue, waiting up to the specified wait time if necessary for an element to become available.

Specified by:
poll in interface java.util.concurrent.BlockingQueue<E>
Parameters:
timeout - how long to wait before giving up, in units of unit
unit - a TimeUnit determining how to interpret the timeout parameter
Returns:
the head of this queue, or null if the specified waiting time elapses before an element is available
Throws:
java.lang.InterruptedException - if interrupted while waiting

remainingCapacity

public int remainingCapacity()
Returns the number of additional elements that this queue can ideally (in the absence of memory or resource constraints) accept without blocking, or Integer.MAX_VALUE if there is no intrinsic limit.

Note that you cannot always tell if an attempt to insert an element will succeed by inspecting remainingCapacity because it may be the case that another thread is about to insert or remove an element.

Specified by:
remainingCapacity in interface java.util.concurrent.BlockingQueue<E>
Returns:
the remaining capacity

drainTo

public int drainTo(java.util.Collection<? super E> c,
                   int maxElements)
Removes at most the given number of available elements from this queue and adds them to the given collection. A failure encountered while attempting to add elements to collection c may result in elements being in neither, either or both collections when the associated exception is thrown. Attempts to drain a queue to itself result in IllegalArgumentException. Further, the behavior of this operation is undefined if the specified collection is modified while the operation is in progress.

Specified by:
drainTo in interface java.util.concurrent.BlockingQueue<E>
Parameters:
c - the collection to transfer elements into
maxElements - the maximum number of elements to transfer
Returns:
the number of elements transferred
Throws:
java.lang.UnsupportedOperationException - if addition of elements is not supported by the specified collection
java.lang.ClassCastException - if the class of an element of this queue prevents it from being added to the specified collection
java.lang.NullPointerException - if the specified collection is null
java.lang.IllegalArgumentException - if the specified collection is this queue, or some property of an element of this queue prevents it from being added to the specified collection

drainTo

public int drainTo(E[] a)
Removes at most the given number of available elements from this queue and adds them to the given array. A failure encountered while attempting to add elements to array a may result in elements being in neither, either or both queue and array when the associated exception is thrown. Further, the behavior of this operation is undefined if the specified collection is modified while the operation is in progress.

Parameters:
a - the array to transfer elements into
Returns:
the number of elements transferred
Throws:
java.lang.UnsupportedOperationException - if addition of elements is not supported by the specified collection
java.lang.ClassCastException - if the class of an element of this queue prevents it from being added to the specified collection
java.lang.NullPointerException - if the specified collection is null
java.lang.IllegalArgumentException - if the specified collection is this queue, or some property of an element of this queue prevents it from being added to the specified collection

offer

public boolean offer(E e)
Inserts the specified element into this queue if it is possible to do so immediately without violating capacity restrictions. When using a capacity-restricted queue, this method is generally preferable to add(E), which can fail to insert an element only by throwing an exception.

Specified by:
offer in interface java.util.concurrent.BlockingQueue<E>
Specified by:
offer in interface java.util.Queue<E>
Parameters:
e - the element to add
Returns:
true if the element was added to this queue, else false
Throws:
java.lang.ClassCastException - if the class of the specified element prevents it from being added to this queue
java.lang.NullPointerException - if the specified element is null and this queue does not permit null elements
java.lang.IllegalArgumentException - if some property of this element prevents it from being added to this queue

add

public boolean add(E e)
Add an element to the queue, throwing an exception if the queue is full and has no more room.

Specified by:
add in interface java.util.Collection<E>
Specified by:
add in interface java.util.concurrent.BlockingQueue<E>
Specified by:
add in interface java.util.Queue<E>
Overrides:
add in class java.util.AbstractCollection<E>
Throws:
java.lang.IllegalStateException - if queue is already full

poll

public E poll()
Retrieves and removes the head of this queue, or returns null if this queue is empty.

Specified by:
poll in interface java.util.Queue<E>
Returns:
the head of this queue, or null if this queue is empty

peek

public E peek()
Retrieves, but does not remove, the head of this queue, or returns null if this queue is empty.

Specified by:
peek in interface java.util.Queue<E>
Returns:
the head of this queue, or null if this queue is empty

element

public E element()
Retrieves, but does not remove, the head of this queue. This method differs from peek only in that it throws an exception if this queue is empty.

This implementation returns the result of peek unless the queue is empty.

Specified by:
element in interface java.util.Queue<E>
Returns:
the head of this queue
Throws:
java.util.NoSuchElementException - if this queue is empty

remove

public E remove()
Retrieves and removes the head of this queue. This method differs from poll only in that it throws an exception if this queue is empty.

This implementation returns the result of poll unless the queue is empty.

Specified by:
remove in interface java.util.Queue<E>
Returns:
the head of this queue
Throws:
java.util.NoSuchElementException - if this queue is empty

contains

public boolean contains(java.lang.Object o)
Check if this queue contains the specified object (or at least an instance that matches by equals()).

Specified by:
contains in interface java.util.Collection<E>
Specified by:
contains in interface java.util.concurrent.BlockingQueue<E>
Overrides:
contains in class java.util.AbstractCollection<E>