org.apache.commons.collections
public class: UnboundedFifoBuffer [javadoc |
source]
java.lang.Object
java.util.AbstractCollection
org.apache.commons.collections.UnboundedFifoBuffer
All Implemented Interfaces:
Buffer, Collection
Deprecated! Moved - to buffer subpackage. Due to be removed in v4.0.
UnboundedFifoBuffer is a very efficient buffer implementation.
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.
- since:
Commons - Collections 2.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
| Field Summary |
|---|
| protected Object[] | m_buffer | |
| protected int | m_head | |
| protected int | m_tail | |
| 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.UnboundedFifoBuffer Detail: |
public boolean add(Object obj) {
if (obj == null) {
throw new NullPointerException("Attempted to add null object to buffer");
}
if (size() + 1 >= m_buffer.length) {
Object[] tmp = new Object[((m_buffer.length - 1) * 2) + 1];
int j = 0;
for (int i = m_head; i != m_tail;) {
tmp[j] = m_buffer[i];
m_buffer[i] = null;
j++;
i++;
if (i == m_buffer.length) {
i = 0;
}
}
m_buffer = tmp;
m_head = 0;
m_tail = j;
}
m_buffer[m_tail] = obj;
m_tail++;
if (m_tail >= m_buffer.length) {
m_tail = 0;
}
return true;
} Deprecated!Adds the given element to this buffer. |
public Object get() {
if (isEmpty()) {
throw new BufferUnderflowException("The buffer is already empty");
}
return m_buffer[m_head];
} Deprecated!Returns the next object in the buffer. |
public boolean isEmpty() {
return (size() == 0);
} Deprecated!Returns true if this buffer is empty; false otherwise. |
public Iterator iterator() {
return new Iterator() {
private int index = m_head;
private int lastReturnedIndex = -1;
public boolean hasNext() {
return index != m_tail;
}
public Object next() {
if (!hasNext())
throw new NoSuchElementException();
lastReturnedIndex = index;
index = increment(index);
return m_buffer[lastReturnedIndex];
}
public void remove() {
if (lastReturnedIndex == -1)
throw new IllegalStateException();
// First element can be removed quickly
if (lastReturnedIndex == m_head) {
UnboundedFifoBuffer.this.remove();
lastReturnedIndex = -1;
return;
}
// Other elements require us to shift the subsequent elements
int i = increment(lastReturnedIndex);
while (i != m_tail) {
m_buffer[decrement(i)] = m_buffer[i];
i = increment(i);
}
lastReturnedIndex = -1;
m_tail = decrement(m_tail);
m_buffer[m_tail] = null;
index = decrement(index);
}
};
} Deprecated!Returns an iterator over this buffer's elements. |
public Object remove() {
if (isEmpty()) {
throw new BufferUnderflowException("The buffer is already empty");
}
Object element = m_buffer[m_head];
if (null != element) {
m_buffer[m_head] = null;
m_head++;
if (m_head >= m_buffer.length) {
m_head = 0;
}
}
return element;
} Deprecated!Removes the next object from the buffer |
public int size() {
int size = 0;
if (m_tail < m_head) {
size = m_buffer.length - m_head + m_tail;
} else {
size = m_tail - m_head;
}
return size;
} Deprecated!Returns the number of elements stored in the buffer. |