org.apache.excalibur.event.impl
Class DefaultQueue

java.lang.Object
  extended by org.apache.excalibur.event.impl.AbstractQueue
      extended by org.apache.excalibur.event.impl.DefaultQueue
All Implemented Interfaces:
org.apache.excalibur.event.Queue, org.apache.excalibur.event.Sink, org.apache.excalibur.event.Source

public final class DefaultQueue
extends AbstractQueue

The default queue implementation is a variable size queue. This queue is thread safe, however the overhead in synchronization costs a few extra milliseconds.

Author:
Avalon Development Team

Nested Class Summary
private static class DefaultQueue.DefaultPreparedEnqueue
           
 
Field Summary
private  org.apache.commons.collections.Buffer m_elements
           
private  int m_maxSize
           
private  EDU.oswego.cs.dl.util.concurrent.ReentrantLock m_mutex
           
protected  int m_reserve
           
 
Fields inherited from class org.apache.excalibur.event.impl.AbstractQueue
EMPTY_ARRAY, m_interceptor, m_predicate, m_timeout
 
Fields inherited from interface org.apache.excalibur.event.Queue
ROLE
 
Constructor Summary
DefaultQueue()
          Create an unbounded DefaultQueue.
DefaultQueue(org.apache.excalibur.event.EnqueuePredicate predicate)
           
DefaultQueue(int size)
          Construct a new DefaultQueue with the specified number of elements.
 
Method Summary
 java.lang.Object dequeue()
           
 java.lang.Object[] dequeue(int numElements)
           
 java.lang.Object[] dequeueAll()
           
 void enqueue(java.lang.Object element)
           
 void enqueue(java.lang.Object[] elements)
           
 int maxSize()
          Return the maximum number of elements that will fit in the Queue.
 org.apache.excalibur.event.PreparedEnqueue prepareEnqueue(java.lang.Object[] elements)
           
private static java.lang.Object[] retrieveElements(org.apache.commons.collections.Buffer buf, int count)
          Removes the given number of elements from the given buf and returns them in an array.
 int size()
          Return the number of elements currently in the Queue.
 boolean tryEnqueue(java.lang.Object element)
           
 
Methods inherited from class org.apache.excalibur.event.impl.AbstractQueue
block, canAccept, getDequeueInterceptor, getEnqueuePredicate, isFull, setDequeueInterceptor, setEnqueuePredicate, setTimeout
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

m_elements

private final org.apache.commons.collections.Buffer m_elements

m_mutex

private final EDU.oswego.cs.dl.util.concurrent.ReentrantLock m_mutex

m_reserve

protected int m_reserve

m_maxSize

private final int m_maxSize
Constructor Detail

DefaultQueue

public DefaultQueue(int size)
Construct a new DefaultQueue with the specified number of elements. if the number of elements is greater than zero, then the Queue is bounded by that number. Otherwise, the Queue is not bounded at all.

Parameters:
size - The maximum number of elements in the Queue. Any number less than 1 means there is no limit.

DefaultQueue

public DefaultQueue(org.apache.excalibur.event.EnqueuePredicate predicate)

DefaultQueue

public DefaultQueue()
Create an unbounded DefaultQueue.

Method Detail

size

public int size()
Return the number of elements currently in the Queue.

Returns:
int representing the number of elements (including the reserved ones).

maxSize

public int maxSize()
Return the maximum number of elements that will fit in the Queue. A number below 1 indecates an unbounded Queue, which means there is no limit.

Specified by:
maxSize in interface org.apache.excalibur.event.Sink
Overrides:
maxSize in class AbstractQueue
Returns:
int representing the maximum number of elements

prepareEnqueue

public org.apache.excalibur.event.PreparedEnqueue prepareEnqueue(java.lang.Object[] elements)
                                                          throws org.apache.excalibur.event.SinkException
Throws:
org.apache.excalibur.event.SinkException

tryEnqueue

public boolean tryEnqueue(java.lang.Object element)

enqueue

public void enqueue(java.lang.Object[] elements)
             throws org.apache.excalibur.event.SinkException
Throws:
org.apache.excalibur.event.SinkException

enqueue

public void enqueue(java.lang.Object element)
             throws org.apache.excalibur.event.SinkException
Throws:
org.apache.excalibur.event.SinkException

dequeue

public java.lang.Object[] dequeue(int numElements)

dequeueAll

public java.lang.Object[] dequeueAll()

retrieveElements

private static java.lang.Object[] retrieveElements(org.apache.commons.collections.Buffer buf,
                                                   int count)
Removes the given number of elements from the given buf and returns them in an array. Trusts the caller to pass in a buffer full of Objects and with at least count elements available.

Parameters:
buf - to remove elements from, the caller is responsible for synchronizing access
count - number of elements to remove/return
Returns:
requested number of elements

dequeue

public java.lang.Object dequeue()