org.apache.turbine.util.pool
Class BoundedBuffer

java.lang.Object
  extended by org.apache.turbine.util.pool.BoundedBuffer

public class BoundedBuffer
extends java.lang.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 534527 2007-05-02 16:10:59Z tv $
Author:
Ilkka Priha

Field Summary
protected  java.lang.Object[] buffer
           
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(java.lang.Object x)
          Puts an item in the buffer only if there is capacity available.
 java.lang.Object peek()
          Peeks, but does not remove the top item from the buffer.
 java.lang.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

buffer

protected final java.lang.Object[] buffer

takePtr

protected int takePtr

putPtr

protected int putPtr

usedSlots

protected int usedSlots

emptySlots

protected int emptySlots
Constructor Detail

BoundedBuffer

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

Parameters:
capacity - the capacity.
Throws:
java.lang.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 java.lang.Object peek()
Peeks, but does not remove the top item from the buffer.

Returns:
the object or null.

offer

public boolean offer(java.lang.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 java.lang.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 © 2000-2007 Apache Software Foundation. All Rights Reserved.