public class BoundedBuffer<E> extends SynchronizedBuffer<E> implements BoundedCollection<E>
Buffer to 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 BoundedFifoBuffer or CircularFifoBuffer.
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.
collection, lock| Modifier | Constructor and Description |
|---|---|
protected |
BoundedBuffer(Buffer<E> buffer,
int maximumSize,
long timeout)
Constructor that wraps (not copies) another buffer, making it bounded
waiting only up to a maximum amount of time.
|
| Modifier and Type | Method and Description |
|---|---|
boolean |
add(E o) |
boolean |
addAll(Collection<? extends E> c) |
static <E> BoundedBuffer<E> |
boundedBuffer(Buffer<E> buffer,
int maximumSize)
Factory method to create a bounded buffer.
|
static <E> BoundedBuffer<E> |
boundedBuffer(Buffer<E> buffer,
int maximumSize,
long timeout)
Factory method to create a bounded buffer that blocks for a maximum
amount of time.
|
boolean |
isFull()
Returns true if this collection is full and no new elements can be added.
|
Iterator<E> |
iterator()
Iterators must be manually synchronized.
|
int |
maxSize()
Gets the maximum size of the collection (the bound).
|
E |
remove()
Gets and removes the next object from the buffer.
|
decorated, get, synchronizedBufferclear, contains, containsAll, equals, hashCode, isEmpty, remove, removeAll, retainAll, size, synchronizedCollection, toArray, toArray, toStringprotected BoundedBuffer(Buffer<E> buffer, int maximumSize, long timeout)
buffer - the buffer to wrap, must not be nullmaximumSize - the maximum size, must be size one or greatertimeout - the maximum amount of time to waitIllegalArgumentException - if the buffer is nullIllegalArgumentException - if the maximum size is zero or lesspublic static <E> BoundedBuffer<E> boundedBuffer(Buffer<E> buffer, int maximumSize)
When the buffer is full, it will immediately throw a
BufferOverflowException on calling add(Object).
E - the type of the elements in the bufferbuffer - the buffer to decorate, must not be nullmaximumSize - the maximum size, must be size one or greaterIllegalArgumentException - if the buffer is nullIllegalArgumentException - if the maximum size is zero or lesspublic static <E> BoundedBuffer<E> boundedBuffer(Buffer<E> buffer, int maximumSize, long timeout)
E - the type of the elements in the bufferbuffer - 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 millisecondsIllegalArgumentException - if the buffer is nullIllegalArgumentException - if the maximum size is zero or lesspublic E remove()
Bufferpublic boolean add(E o)
add in interface Collection<E>add in class SynchronizedCollection<E>public boolean addAll(Collection<? extends E> c)
addAll in interface Collection<E>addAll in class SynchronizedCollection<E>public Iterator<E> iterator()
SynchronizedCollection
synchronized (coll) {
Iterator it = coll.iterator();
// do stuff with iterator
}iterator in interface Iterable<E>iterator in interface Collection<E>iterator in class SynchronizedCollection<E>public boolean isFull()
BoundedCollectionisFull in interface BoundedCollection<E>true if the collection is fullpublic int maxSize()
BoundedCollectionmaxSize in interface BoundedCollection<E>Copyright © 2001-2013 The Apache Software Foundation. All Rights Reserved.