org.apache.commons.collections.buffer
public class: UnboundedFifoBuffer [javadoc |
source]
java.lang.Object
java.util.AbstractCollection
org.apache.commons.collections.buffer.UnboundedFifoBuffer
All Implemented Interfaces:
Serializable, Buffer, Collection
UnboundedFifoBuffer is a very efficient implementation of
Buffer that can grow to any size.
According to performance testing, it exhibits a constant access time, but it
also outperforms ArrayList when used for the same purpose.
The removal order of an UnboundedFifoBuffer is based on the insertion
order; elements are removed in the same order in which they were added.
The iteration order is the same as the removal order.
The #remove() and #get() operations perform in constant time.
The #add(Object) operation performs in amortized constant time. All
other operations perform in linear time or worse.
Note that this implementation is not synchronized. The following can be
used to provide synchronized access to your UnboundedFifoBuffer:
Buffer fifo = BufferUtils.synchronizedBuffer(new UnboundedFifoBuffer());
This buffer prevents null objects from being added.
This class is Serializable from Commons Collections 3.1.
- since:
Commons - Collections 3.0 (previously in main package v2.1)
- version:
$ - Revision: 646777 $ $Date: 2008-04-10 13:33:15 +0100 (Thu, 10 Apr 2008) $
- author:
Avalon -
- author:
Federico - Barbieri
- author:
Berin - Loritsch
- author:
Paul - Jack
- author:
Stephen - Colebourne
- author:
Andreas - Schlosser
- author:
Thomas - Knych
- author:
Jordan - Krey
| Field Summary |
|---|
| protected transient Object[] | buffer | The array of objects in the buffer. |
| protected transient int | head | The current head index. |
| protected transient int | tail | The current tail index. |
| Methods from java.util.AbstractCollection: |
|---|
|
add, addAll, clear, contains, containsAll, isEmpty, iterator, remove, removeAll, retainAll, size, toArray, toArray, toString |
| Method from org.apache.commons.collections.buffer.UnboundedFifoBuffer Detail: |
public boolean add(Object obj) {
if (obj == null) {
throw new NullPointerException("Attempted to add null object to buffer");
}
if (size() + 1 >= buffer.length) {
// copy contents to a new buffer array
Object[] tmp = new Object[((buffer.length - 1) * 2) + 1];
int j = 0;
// move head to element zero in the new array
for (int i = head; i != tail;) {
tmp[j] = buffer[i];
buffer[i] = null;
j++;
i = increment(i);
}
buffer = tmp;
head = 0;
tail = j;
}
buffer[tail] = obj;
tail = increment(tail);
return true;
}
Adds the given element to this buffer. |
public Object get() {
if (isEmpty()) {
throw new BufferUnderflowException("The buffer is already empty");
}
return buffer[head];
}
Returns the next object in the buffer. |
public boolean isEmpty() {
return (size() == 0);
}
Returns true if this buffer is empty; false otherwise. |
public Iterator iterator() {
return new Iterator() {
private int index = head;
private int lastReturnedIndex = -1;
public boolean hasNext() {
return index != tail;
}
public Object next() {
if (!hasNext()) {
throw new NoSuchElementException();
}
lastReturnedIndex = index;
index = increment(index);
return buffer[lastReturnedIndex];
}
public void remove() {
if (lastReturnedIndex == -1) {
throw new IllegalStateException();
}
// First element can be removed quickly
if (lastReturnedIndex == head) {
UnboundedFifoBuffer.this.remove();
lastReturnedIndex = -1;
return;
}
// Other elements require us to shift the subsequent elements
int i = increment(lastReturnedIndex);
while (i != tail) {
buffer[decrement(i)] = buffer[i];
i = increment(i);
}
lastReturnedIndex = -1;
tail = decrement(tail);
buffer[tail] = null;
index = decrement(index);
}
};
}
Returns an iterator over this buffer's elements. |
public Object remove() {
if (isEmpty()) {
throw new BufferUnderflowException("The buffer is already empty");
}
Object element = buffer[head];
if (element != null) {
buffer[head] = null;
head = increment(head);
}
return element;
}
Removes the next object from the buffer |
public int size() {
int size = 0;
if (tail < head) {
size = buffer.length - head + tail;
} else {
size = tail - head;
}
return size;
}
Returns the number of elements stored in the buffer. |