org.apache.fulcrum.pool
Class BoundedBuffer

java.lang.Object
  extended by org.apache.fulcrum.pool.BoundedBuffer

public class BoundedBuffer
extends Object

Efficient array-based bounded buffer class. Adapted from CPJ, chapter 8, which describes design. Originally written by Doug Lea and released into the public domain.

[ Introduction to this package. ]

Version:
$Id: BoundedBuffer.java 535465 2007-05-05 06:58:06Z tv $
Author:
Ilkka Priha

Field Summary
protected  Object[] array_
           
static int DEFAULT_CAPACITY
          The default capacity.
protected  int emptySlots_
           
protected  int putPtr_
           
protected  int takePtr_
           
protected  int usedSlots_
           
 
Constructor Summary
BoundedBuffer()
          Creates a buffer with the default capacity
BoundedBuffer(int capacity)
          Creates a buffer with the given capacity.
 
Method Summary
 int capacity()
          Returns the capacity of the buffer.
 boolean offer(Object x)
          Puts an item in the buffer only if there is capacity available.
 Object peek()
          Peeks, but does not remove the top item from the buffer.
 Object poll()
          Polls and removes the top item from the buffer if one is available.
 int size()
          Returns the number of elements in the buffer.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_CAPACITY

public static final int DEFAULT_CAPACITY
The default capacity.

See Also:
Constant Field Values

array_

protected final Object[] array_

takePtr_

protected int takePtr_

putPtr_

protected int putPtr_

usedSlots_

protected int usedSlots_

emptySlots_

protected int emptySlots_
Constructor Detail

BoundedBuffer

public BoundedBuffer(int capacity)
              throws IllegalArgumentException
Creates a buffer with the given capacity.

Parameters:
capacity - the capacity.
Throws:
IllegalArgumentException - if capacity less or equal to zero.

BoundedBuffer

public BoundedBuffer()
Creates a buffer with the default capacity

Method Detail

size

public int size()
Returns the number of elements in the buffer. This is only a snapshot value, that may change immediately after returning.

Returns:
the size.

capacity

public int capacity()
Returns the capacity of the buffer.

Returns:
the capacity.

peek

public Object peek()
Peeks, but does not remove the top item from the buffer.

Returns:
the object or null.

offer

public boolean offer(Object x)
Puts an item in the buffer only if there is capacity available.

Parameters:
item - the item to be inserted.
Returns:
true if accepted, else false.

poll

public Object poll()
Polls and removes the top item from the buffer if one is available.

Returns:
the oldest item from the buffer, or null if the buffer is empty.


Copyright © 2005-2009 The Apache Software Foundation. All Rights Reserved.