[COLLECTIONS-399] Added get(index) method to BoundedFifoBuffer.

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/collections/trunk@1351852 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Thomas Neidhart 2012-06-19 21:05:48 +00:00
parent bd2f3bcced
commit cb9f11c0c3
3 changed files with 76 additions and 3 deletions

View File

@ -131,7 +131,7 @@ public class BoundedFifoBuffer<E> extends AbstractCollection<E>
* Write the buffer out using a custom routine.
*
* @param out the output stream
* @throws IOException
* @throws IOException if an I/O error occurs while writing to the output stream
*/
private void writeObject(ObjectOutputStream out) throws IOException {
out.defaultWriteObject();
@ -145,8 +145,8 @@ public class BoundedFifoBuffer<E> extends AbstractCollection<E>
* Read the buffer in using a custom routine.
*
* @param in the input stream
* @throws IOException
* @throws ClassNotFoundException
* @throws IOException if an I/O error occurs while writing to the output stream
* @throws ClassNotFoundException if the class of a serialized object can not be found
*/
@SuppressWarnings("unchecked")
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
@ -269,6 +269,22 @@ public class BoundedFifoBuffer<E> extends AbstractCollection<E>
return elements[start];
}
/**
* Returns the element at the specified position in this buffer.
*
* @param index the position of the element in the buffer
* @return the element at position {@code index}
* @throws NoSuchElementException if the requested position is outside the range [0, size)
*/
public E get(int index) {
if (index < 0 || index >= size()) {
throw new NoSuchElementException();
}
final int idx = (start + index) % maxElements;
return elements[idx];
}
/**
* Removes the least recently inserted element from this buffer.
*

View File

@ -19,6 +19,7 @@ package org.apache.commons.collections.buffer;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
import junit.framework.Test;
@ -171,6 +172,44 @@ public class TestBoundedFifoBuffer<E> extends AbstractTestCollection<E> {
}
fail();
}
/**
* Tests that the get(index) method correctly throws an exception.
*/
public void testGetException() {
resetFull();
try {
getCollection().get(-1);
fail();
} catch (NoSuchElementException ex) {
// expected
}
try {
getCollection().get(getCollection().size());
fail();
} catch (NoSuchElementException ex) {
// expected
}
}
public void testGetIndex() {
resetFull();
BoundedFifoBuffer<E> buffer = getCollection();
List<E> confirmed = getConfirmed();
for (int i = 0; i < confirmed.size(); i++) {
assertEquals(confirmed.get(i), buffer.get(i));
}
// remove the first two elements and check again
buffer.remove();
buffer.remove();
for (int i = 0; i < buffer.size(); i++) {
assertEquals(confirmed.get(i + 2), buffer.get(i));
}
}
@Override
public String getCompatibilityVersion() {

View File

@ -415,6 +415,24 @@ public class TestCircularFifoBuffer<E> extends AbstractTestCollection<E> {
assertEquals(true, b3.contains("c"));
}
public void testGetIndex() {
resetFull();
CircularFifoBuffer<E> buffer = getCollection();
List<E> confirmed = getConfirmed();
for (int i = 0; i < confirmed.size(); i++) {
assertEquals(confirmed.get(i), buffer.get(i));
}
// remove the first two elements and check again
buffer.remove();
buffer.remove();
for (int i = 0; i < buffer.size(); i++) {
assertEquals(confirmed.get(i + 2), buffer.get(i));
}
}
@Override
public String getCompatibilityVersion() {
return "3.1";