// ShapeBoundedBuffer.java
public class ShapeBoundedBuffer
{
private ShapeOutline[] buffer;
private int beginIndex = 0;
private int subArrayLength = 0;
public ShapeBoundedBuffer(int capacity)
{
if ( capacity < 0 )
throw new NegativeSizeException(
"capacity=" + capacity
+ " should be non-negative.");
buffer = new ShapeOutline[capacity];
} // constructor
public int getCapacity() { return buffer.length; }
public int getLengthFilled() { return subArrayLength; }
public void add(ShapeOutline shape)
{
if ( subArrayLength < buffer.length )
{
buffer[subArrayLength] = shape;
subArrayLength++;
}
else
{
buffer[beginIndex] = shape;
beginIndex = (beginIndex + 1) % buffer.length;
}
} // method add
public ShapeOutline getShapeAt(int index)
{
if ( index < 0 || index >= subArrayLength )
throw new IndexOutOfBoundsException(
"index=" + index
+ " should be in range [0, "
+ subArrayLength + "]");
return buffer[(beginIndex + index) % buffer.length];
} // method getShapeAt
} // class ShapeBoundedBuffer