org.apache.commons.collections.buffer

Class BlockingBuffer

Implemented Interfaces:
Buffer, Collection, Serializable

public class BlockingBuffer
extends SynchronizedBuffer

Decorates another Buffer to make get() and remove() block when the Buffer is empty.

If either get or remove is called on an empty Buffer, the calling thread waits for notification that an add or addAll operation has completed.

When one or more entries are added to an empty Buffer, all threads blocked in get or remove are notified. There is no guarantee that concurrent blocked get or remove requests will be "unblocked" and receive data in the order that they arrive.

This class is Serializable from Commons Collections 3.1. This class contains an extra field in 3.2, however the serialization specification will handle this gracefully.

Version:
$Revision: 348808 $ $Date: 2005-11-24 21:35:09 +0000 (Thu, 24 Nov 2005) $
Authors:
Stephen Colebourne
Janek Bogucki
Phil Steitz
James Carman
Since:
Commons Collections 3.0

Field Summary

Fields inherited from class org.apache.commons.collections.collection.SynchronizedCollection

collection, lock

Constructor Summary

BlockingBuffer(Buffer buffer)
Constructor that wraps (not copies).
BlockingBuffer(Buffer buffer, long timeoutMillis)
Constructor that wraps (not copies).

Method Summary

boolean
add(Object o)
boolean
addAll(Collection c)
static Buffer
decorate(Buffer buffer)
Factory method to create a blocking buffer.
static Buffer
decorate(Buffer buffer, long timeoutMillis)
Factory method to create a blocking buffer with a timeout value.
Object
get()
Gets the next value from the buffer, waiting until an object is added if the buffer is empty.
Object
get(long timeout)
Gets the next value from the buffer, waiting until an object is added for up to the specified timeout value if the buffer is empty.
Object
remove()
Removes the next value from the buffer, waiting until an object is added if the buffer is empty.
Object
remove(long timeout)
Removes the next value from the buffer, waiting until an object is added for up to the specified timeout value if the buffer is empty.

Methods inherited from class org.apache.commons.collections.buffer.SynchronizedBuffer

decorate, get, getBuffer, remove

Methods inherited from class org.apache.commons.collections.collection.SynchronizedCollection

add, addAll, clear, contains, containsAll, decorate, equals, hashCode, isEmpty, iterator, remove, removeAll, retainAll, size, toArray, toArray, toString

Constructor Details

BlockingBuffer

protected BlockingBuffer(Buffer buffer)
Constructor that wraps (not copies).
Parameters:
buffer - the buffer to decorate, must not be null

BlockingBuffer

protected BlockingBuffer(Buffer buffer,
                         long timeoutMillis)
Constructor that wraps (not copies).
Parameters:
buffer - the buffer to decorate, must not be null
timeoutMillis - the timeout value in milliseconds, zero or less for no timeout
Since:
Commons Collections 3.2

Method Details

add

public boolean add(Object o)
Overrides:
add in interface SynchronizedCollection

addAll

public boolean addAll(Collection c)
Overrides:
addAll in interface SynchronizedCollection

decorate

public static Buffer decorate(Buffer buffer)
Factory method to create a blocking buffer.
Overrides:
decorate in interface SynchronizedBuffer
Parameters:
buffer - the buffer to decorate, must not be null
Returns:
a new blocking Buffer

decorate

public static Buffer decorate(Buffer buffer,
                              long timeoutMillis)
Factory method to create a blocking buffer with a timeout value.
Parameters:
buffer - the buffer to decorate, must not be null
timeoutMillis - the timeout value in milliseconds, zero or less for no timeout
Returns:
a new blocking buffer
Since:
Commons Collections 3.2

get

public Object get()
Gets the next value from the buffer, waiting until an object is added if the buffer is empty. This method uses the default timeout set in the constructor.
Specified by:
get in interface Buffer
Overrides:
get in interface SynchronizedBuffer

get

public Object get(long timeout)
Gets the next value from the buffer, waiting until an object is added for up to the specified timeout value if the buffer is empty.
Parameters:
timeout - the timeout value in milliseconds
Since:
Commons Collections 3.2

remove

public Object remove()
Removes the next value from the buffer, waiting until an object is added if the buffer is empty. This method uses the default timeout set in the constructor.
Specified by:
remove in interface Buffer
Overrides:
remove in interface SynchronizedBuffer

remove

public Object remove(long timeout)
Removes the next value from the buffer, waiting until an object is added for up to the specified timeout value if the buffer is empty.
Parameters:
timeout - the timeout value in milliseconds
Since:
Commons Collections 3.2

Copyright © 2001-2006 Apache Software Foundation. All Rights Reserved.