Class BoundedBuffer
- java.lang.Object
-
- org.apache.commons.collections.collection.SynchronizedCollection
-
- org.apache.commons.collections.buffer.SynchronizedBuffer
-
- org.apache.commons.collections.buffer.BoundedBuffer
-
- All Implemented Interfaces:
java.io.Serializable,java.lang.Iterable,java.util.Collection,BoundedCollection,Buffer
public class BoundedBuffer extends SynchronizedBuffer implements BoundedCollection
Decorates anotherBufferto ensure a fixed maximum size.Note: This class should only be used if you need to add bounded behaviour to another buffer. If you just want a bounded buffer then you should use
BoundedFifoBufferorCircularFifoBuffer.The decoration methods allow you to specify a timeout value. This alters the behaviour of the add methods when the buffer is full. Normally, when the buffer is full, the add method will throw an exception. With a timeout, the add methods will wait for up to the timeout period to try and add the elements.
- Since:
- Commons Collections 3.2
- Version:
- $Revision: 646777 $ $Date: 2008-04-10 14:33:15 +0200 (Thu, 10 Apr 2008) $
- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private classBoundedBuffer.NotifyingIteratorBoundedBuffer iterator.
-
Field Summary
Fields Modifier and Type Field Description private intmaximumSizeThe maximum size.private static longserialVersionUIDThe serialization version.private longtimeoutThe timeout milliseconds.-
Fields inherited from class org.apache.commons.collections.collection.SynchronizedCollection
collection, lock
-
-
Constructor Summary
Constructors Modifier Constructor Description protectedBoundedBuffer(Buffer buffer, int maximumSize, long timeout)Constructor that wraps (not copies) another buffer, making it bounded waiting only up to a maximum amount of time.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanadd(java.lang.Object o)booleanaddAll(java.util.Collection c)static BoundedBufferdecorate(Buffer buffer, int maximumSize)Factory method to create a bounded buffer.static BoundedBufferdecorate(Buffer buffer, int maximumSize, long timeout)Factory method to create a bounded buffer that blocks for a maximum amount of time.booleanisFull()Returns true if this collection is full and no new elements can be added.java.util.Iteratoriterator()Iterators must be manually synchronized.intmaxSize()Gets the maximum size of the collection (the bound).java.lang.Objectremove()Gets and removes the next object from the buffer.private voidtimeoutWait(int nAdditions)-
Methods inherited from class org.apache.commons.collections.buffer.SynchronizedBuffer
decorate, get, getBuffer
-
Methods inherited from class org.apache.commons.collections.collection.SynchronizedCollection
clear, contains, containsAll, decorate, equals, hashCode, isEmpty, remove, removeAll, retainAll, size, toArray, toArray, toString
-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
-
-
-
Field Detail
-
serialVersionUID
private static final long serialVersionUID
The serialization version.- See Also:
- Constant Field Values
-
maximumSize
private final int maximumSize
The maximum size.
-
timeout
private final long timeout
The timeout milliseconds.
-
-
Constructor Detail
-
BoundedBuffer
protected BoundedBuffer(Buffer buffer, int maximumSize, long timeout)
Constructor that wraps (not copies) another buffer, making it bounded waiting only up to a maximum amount of time.- Parameters:
buffer- the buffer to wrap, must not be nullmaximumSize- the maximum size, must be size one or greatertimeout- the maximum amount of time to wait- Throws:
java.lang.IllegalArgumentException- if the buffer is nulljava.lang.IllegalArgumentException- if the maximum size is zero or less
-
-
Method Detail
-
decorate
public static BoundedBuffer decorate(Buffer buffer, int maximumSize)
Factory method to create a bounded buffer.When the buffer is full, it will immediately throw a
BufferOverflowExceptionon callingadd().- Parameters:
buffer- the buffer to decorate, must not be nullmaximumSize- the maximum size, must be size one or greater- Returns:
- a new bounded buffer
- Throws:
java.lang.IllegalArgumentException- if the buffer is nulljava.lang.IllegalArgumentException- if the maximum size is zero or less
-
decorate
public static BoundedBuffer decorate(Buffer buffer, int maximumSize, long timeout)
Factory method to create a bounded buffer that blocks for a maximum amount of time.- Parameters:
buffer- the buffer to decorate, must not be nullmaximumSize- the maximum size, must be size one or greatertimeout- the maximum amount of time to wait in milliseconds- Returns:
- a new bounded buffer
- Throws:
java.lang.IllegalArgumentException- if the buffer is nulljava.lang.IllegalArgumentException- if the maximum size is zero or less
-
remove
public java.lang.Object remove()
Description copied from interface:BufferGets and removes the next object from the buffer.- Specified by:
removein interfaceBuffer- Overrides:
removein classSynchronizedBuffer- Returns:
- the next object in the buffer, which is also removed
-
add
public boolean add(java.lang.Object o)
- Specified by:
addin interfacejava.util.Collection- Overrides:
addin classSynchronizedCollection
-
addAll
public boolean addAll(java.util.Collection c)
- Specified by:
addAllin interfacejava.util.Collection- Overrides:
addAllin classSynchronizedCollection
-
iterator
public java.util.Iterator iterator()
Description copied from class:SynchronizedCollectionIterators must be manually synchronized.synchronized (coll) { Iterator it = coll.iterator(); // do stuff with iterator }- Specified by:
iteratorin interfacejava.util.Collection- Specified by:
iteratorin interfacejava.lang.Iterable- Overrides:
iteratorin classSynchronizedCollection- Returns:
- an iterator that must be manually synchronized on the collection
-
timeoutWait
private void timeoutWait(int nAdditions)
-
isFull
public boolean isFull()
Description copied from interface:BoundedCollectionReturns true if this collection is full and no new elements can be added.- Specified by:
isFullin interfaceBoundedCollection- Returns:
trueif the collection is full
-
maxSize
public int maxSize()
Description copied from interface:BoundedCollectionGets the maximum size of the collection (the bound).- Specified by:
maxSizein interfaceBoundedCollection- Returns:
- the maximum number of elements the collection can hold
-
-