T
- type of the stored elementspublic class BoundedList<T>
extends java.lang.Object
implements java.lang.Iterable<T>
Iterable
. It is technically not a list,
since it does not implement the List
interface, but is rather a bounded
Iterable.
The list has a fixed max size. If more elements are added to it, then the oldest elements will be dropped from it.
Modifier and Type | Class and Description |
---|---|
private class |
BoundedList.BoundedIterator
The actual iterator.
|
static interface |
BoundedList.INode<T>
The list elements are stored in nodes that takes care of the actual linking.
|
private static class |
BoundedList.Node<T>
Private class used to wrap values as nodes.
|
Modifier and Type | Field and Description |
---|---|
private BoundedList.INode<T> |
first |
private BoundedList.INode<T> |
last |
private int |
maxSize |
private int |
size |
Constructor and Description |
---|
BoundedList(int maxSize)
Create a new bounded list.
|
Modifier and Type | Method and Description |
---|---|
void |
add(T t)
Adds a value to this list.
|
private void |
addNode(BoundedList.INode<T> t) |
T |
getFirst()
Get the first element in the list.
|
T |
getLast()
Get the last element in the list.
|
int |
getMaxSize()
Get the maximum number of elements to retain in this list.
|
int |
getSize()
Get the number of elements in this list.
|
java.util.Iterator<T> |
iterator()
Get an iterator from the first available to the last available element at the time the
iterator was created.
|
void |
setMaxSize(int maxSize)
Set the maximum number of elements to retain in this list.
|
java.lang.String |
toString()
Use only for debugging purposes!
|
private int maxSize
private int size
private BoundedList.INode<T> first
private BoundedList.INode<T> last
public BoundedList(int maxSize)
maxSize
- maximum number of elements to keeppublic void add(T t)
t
- the value to addprivate void addNode(BoundedList.INode<T> t)
public java.util.Iterator<T> iterator()
iterator
in interface java.lang.Iterable<T>
public T getFirst()
public T getLast()
public int getSize()
public int getMaxSize()
public void setMaxSize(int maxSize)
maxSize
- the maximum size of the listpublic java.lang.String toString()
toString
in class java.lang.Object