gov.sandia.cognition.collection
Class FiniteCapacityBuffer<DataType>

java.lang.Object
  extended by java.util.AbstractCollection<E>
      extended by java.util.AbstractList<DataType>
          extended by gov.sandia.cognition.collection.FiniteCapacityBuffer<DataType>
Type Parameters:
DataType - Type of data contained in the buffer
All Implemented Interfaces:
CloneableSerializable, Serializable, Cloneable, Iterable<DataType>, Collection<DataType>, List<DataType>

@CodeReview(reviewer="Kevin R. Dixon",
            date="2008-02-08",
            changesNeeded=false,
            comments={"Cleaned up minor formatting issues.","Added class documentation about running times and usage of LinkedList."})
public class FiniteCapacityBuffer<DataType>
extends AbstractList<DataType>
implements CloneableSerializable

A finite capacity buffer backed by a fixed array. One can add and remove from the buffer, but the size will never be greater than the capacity. The running times are constant, except for removing in the middle of the list, which may require a relatively expensive copy/shift. This is essentially a circular array-back list.

Since:
2.0
Author:
Kevin R. Dixon
See Also:
Serialized Form

Nested Class Summary
protected  class FiniteCapacityBuffer.InternalIterator
          Iterator for FiniteCapacityBuffer
 
Field Summary
 
Fields inherited from class java.util.AbstractList
modCount
 
Constructor Summary
FiniteCapacityBuffer()
          Default constructor with capacity of one.
FiniteCapacityBuffer(FiniteCapacityBuffer<DataType> other)
          Copy constructor.
FiniteCapacityBuffer(int capacity)
          Creates a new instance of FiniteCapacityBuffer.
 
Method Summary
 boolean add(DataType e)
          Appends the element to the end of the buffer, removing excess elements if necessary
 boolean addFirst(DataType e)
          Prepend the element to the beginning of the buffer, removing excess elements if necessary
 boolean addLast(DataType e)
          Appends the element to the end of the buffer, removing excess elements if necessary
 void clear()
           
 FiniteCapacityBuffer<DataType> clone()
          Creates a new clone (shallow copy) of this object.
protected  int convert(int index)
          Converts the given index from zero-based world to circular world
 DataType get(int index)
           
 int getCapacity()
          Gets the capacity of the buffer, which is the maximum number of elements that can be stored in it.
 DataType getFirst()
          Returns the first element in the list.
 DataType getLast()
          Returns the last element in the list.
 boolean isFull()
          Returns true if the finite-capacity buffer is full.
 Iterator<DataType> iterator()
           
 DataType remove(int index)
           
 boolean remove(Object o)
           
 DataType set(int index, DataType element)
           
 void setCapacity(int capacity)
          Sets the capacity of the buffer, which is the maximum number of elements that can be stored in it.
 int size()
           
 
Methods inherited from class java.util.AbstractList
add, addAll, equals, hashCode, indexOf, lastIndexOf, listIterator, listIterator, removeRange, subList
 
Methods inherited from class java.util.AbstractCollection
addAll, contains, containsAll, isEmpty, removeAll, retainAll, toArray, toArray, toString
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.List
addAll, contains, containsAll, isEmpty, removeAll, retainAll, toArray, toArray
 

Constructor Detail

FiniteCapacityBuffer

public FiniteCapacityBuffer()
Default constructor with capacity of one.


FiniteCapacityBuffer

public FiniteCapacityBuffer(int capacity)
Creates a new instance of FiniteCapacityBuffer.

Parameters:
capacity - Maximum capacity of the buffer.

FiniteCapacityBuffer

public FiniteCapacityBuffer(FiniteCapacityBuffer<DataType> other)
Copy constructor.

Parameters:
other - FiniteCapacityBuffer to copy.
Method Detail

clone

public FiniteCapacityBuffer<DataType> clone()
Description copied from interface: CloneableSerializable
Creates a new clone (shallow copy) of this object.

Specified by:
clone in interface CloneableSerializable
Overrides:
clone in class Object
Returns:
A new clone (shallow copy) of this object.

size

public int size()
Specified by:
size in interface Collection<DataType>
Specified by:
size in interface List<DataType>
Specified by:
size in class AbstractCollection<DataType>

iterator

public Iterator<DataType> iterator()
Specified by:
iterator in interface Iterable<DataType>
Specified by:
iterator in interface Collection<DataType>
Specified by:
iterator in interface List<DataType>
Overrides:
iterator in class AbstractList<DataType>

add

public boolean add(DataType e)
Appends the element to the end of the buffer, removing excess elements if necessary

Specified by:
add in interface Collection<DataType>
Specified by:
add in interface List<DataType>
Overrides:
add in class AbstractList<DataType>
Parameters:
e - DataType to addLast to the buffer
Returns:
true if successful, false if unable to add

remove

public boolean remove(Object o)
Specified by:
remove in interface Collection<DataType>
Specified by:
remove in interface List<DataType>
Overrides:
remove in class AbstractCollection<DataType>

remove

public DataType remove(int index)
Specified by:
remove in interface List<DataType>
Overrides:
remove in class AbstractList<DataType>

convert

protected int convert(int index)
Converts the given index from zero-based world to circular world

Parameters:
index - Index to convert to circular world
Returns:
Circular world index

clear

public void clear()
Specified by:
clear in interface Collection<DataType>
Specified by:
clear in interface List<DataType>
Overrides:
clear in class AbstractList<DataType>

addFirst

public boolean addFirst(DataType e)
Prepend the element to the beginning of the buffer, removing excess elements if necessary

Parameters:
e - DataType to addFirst to the buffer
Returns:
true if successful, false if unable to add

addLast

public boolean addLast(DataType e)
Appends the element to the end of the buffer, removing excess elements if necessary

Parameters:
e - DataType to add to the buffer
Returns:
true if successful, false if unable to add

getFirst

public DataType getFirst()
Returns the first element in the list.

Returns:
The first element in the list.

getLast

public DataType getLast()
Returns the last element in the list.

Returns:
The last element in the list.

isFull

public boolean isFull()
Returns true if the finite-capacity buffer is full. This means that the number of elements in the buffer is equal to the capacity of the buffer. Adding an element to the full buffer

Returns:
True if the buffer is full.

getCapacity

public int getCapacity()
Gets the capacity of the buffer, which is the maximum number of elements that can be stored in it. Must be greater than zero.

Returns:
The maximum capacity of the buffer.

setCapacity

public void setCapacity(int capacity)
Sets the capacity of the buffer, which is the maximum number of elements that can be stored in it. Must be greater than zero.

Parameters:
capacity - The maximum capacity of the buffer. Must be greater than zero.

get

public DataType get(int index)
Specified by:
get in interface List<DataType>
Specified by:
get in class AbstractList<DataType>

set

public DataType set(int index,
                    DataType element)
Specified by:
set in interface List<DataType>
Overrides:
set in class AbstractList<DataType>