5.7. Using a Buffer

Problem

You need a data structure that can act as a temporary staging area, i.e., a buffer.

Solution

Use Buffer from Jakarta 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.”

Discussion

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.

A FIFO buffer

Figure 5-1. A FIFO buffer

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!" );
}

Tip

Later recipes will demonstrate the use of Buffer objects in processing pipelines where it is important to provide a limit on how much memory a Buffer is able to consume.

See Also

A Buffer in Jakarta 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 Jakarta 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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
52.14.172.93