Use Buffer
from Commons
Collections. A buffer is an object that is
defined by the algorithm used for element removal. Buffer
objects can be priority queues, staging
areas, message queues, or buffers for I/O. One of the simplest Buffer
implementations is the UnboundedFifoBuffer
, a first-in, first-out
data structure with no size limit. The following example demonstrates
the use of an UnboundedFifoBuffer
:
import org.apache.commons.collections.Buffer; import org.apache.commons.collections.buffer.UnboundedFifoBuffer; // Create an Unbounded FIFO Buffer buffer = new UnboundedFifoBuffer( ); // Add elements to the Buffer buffer.add("A"); buffer.add("B"); buffer.add("D"); // Remove element from the buffer String value = (String) buffer.remove( ); buffer.add("E"); value = (String) buffer.remove( );
This example creates an UnboundedFifoBuffer
, adding three elements:
"A," "B," and "D." When remove()
is
invoked, the buffer selects the item that was placed into the buffer
first—the first-in is the first-out. The first call to remove( )
returns "A," and the second call
returns "B."
A FIFO buffer can be viewed as a kind of stack; instead of
pop( )
returning the last item to be
placed on the stack, remove( )
returns the bottom of the stack—the first item added to the Buffer
. A Buffer
throws a BufferUnderflowException
if you try to
remove( )
or get( )
an element from an empty Buffer
. This data structure is useful if you
need a temporary holding area between stages in a pipeline—a queue to
hold objects as they wait to be processed by the next stage. An
unbounded FIFO buffer has no size limit, and, as such, it could
grow to a limitless size. In fact, if an application continued to fill
an unbounded buffer, it would exhaust available memory. Figure 5-1 describes a FIFO buffer as
a linear sequence of elements: items flow through this sequence from
left to right.
In addition to UnboundedFifoBuffer
, there is a bounded counterpart—BoundedFifoBuffer
. BoundedFifoBuffer
has a maximum size that cannot be exceeded; adding an
object to a BoundedFifoBuffer
already
at maximum size will cause a BufferOverflowException
. Example 5-4
demonstrates the use of a bounded buffer with a maximum size limit of
two.
Example 5-4. Using a BoundedFifoBuffer
import org.apache.commons.collections.Buffer; import org.apache.commons.collections.BufferOverflowException; import org.apache.commons.collections.buffer.BoundedFifoBuffer; ... // Create a Bounded FIFO with a max size of two Buffer buffer = new BoundedFifoBuffer(2); buffer.add( "One" ); buffer.add( "Two" ); // Adding a third element to this buffer will cause an exception try { buffer.add( "Three" ); } catch( BufferOverflowException bue ) { System.out.println( "Buffer is Full!" ); } // Remove an object... Buffer now contains one element. Object removed = buffer.remove( ); // Add another object buffer.add( "Three" ); Object remove1 = buffer.remove( ); Object remove2 = buffer.remove( ); // This next remove( ) should cause a BufferUnderflowException try { Object remove3 = buffer.remove( ); } catch( BufferUnderflowException bue ) { System.out.println( "Buffer is Empty!" ); }
A Buffer
in Commons Collections
is analogous to a Queue
in Java 5.0.
The Queue
interface is accompanied by
a number of interfaces that provide almost identical behavior to the
Buffer
interface as defined in
Commons Collections 3.0. Queue
contains some new methods, offer( )
and peek( )
, which perform the same
functions as add( )
and remove( )
, with one difference: these new
methods return false
if there is any
problem adding an object to a Queue
.
More information about queues and other changes to the Collections
framework in Java 5.0 are available at: http://java.sun.com/j2se/1.5.0/docs/guide/collections/changes5.html.