Class BoundedList<T>
- java.lang.Object
-
- org.openjdk.jmc.common.collection.BoundedList<T>
-
- Type Parameters:
T
- type of the stored elements
- All Implemented Interfaces:
java.lang.Iterable<T>
public class BoundedList<T> extends java.lang.Object implements java.lang.Iterable<T>
Ordered bounded list that implementsIterable
. It is technically not a list, since it does not implement theList
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.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class 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.
-
Field Summary
Fields Modifier and Type Field Description private BoundedList.INode<T>
first
private BoundedList.INode<T>
last
private int
maxSize
private int
size
-
Constructor Summary
Constructors Constructor Description BoundedList(int maxSize)
Create a new bounded list.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method 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!
-
-
-
Field Detail
-
maxSize
private int maxSize
-
size
private int size
-
first
private BoundedList.INode<T> first
-
last
private BoundedList.INode<T> last
-
-
Method Detail
-
add
public void add(T t)
Adds a value to this list. If the list is at max capacity then the oldest element will be dropped.- Parameters:
t
- the value to add
-
addNode
private void addNode(BoundedList.INode<T> t)
-
iterator
public java.util.Iterator<T> iterator()
Get an iterator from the first available to the last available element at the time the iterator was created. Keeping a reference to an iterator for longer than necessary may keep memory from properly being reclaimed.- Specified by:
iterator
in interfacejava.lang.Iterable<T>
- Returns:
- an iterator over the list elements
-
getFirst
public T getFirst()
Get the first element in the list.- Returns:
- the first element
-
getLast
public T getLast()
Get the last element in the list.- Returns:
- the last element
-
getSize
public int getSize()
Get the number of elements in this list.- Returns:
- the size of the list
-
getMaxSize
public int getMaxSize()
Get the maximum number of elements to retain in this list.- Returns:
- the maximum size of the list
-
setMaxSize
public void setMaxSize(int maxSize)
Set the maximum number of elements to retain in this list.- Parameters:
maxSize
- the maximum size of the list
-
toString
public java.lang.String toString()
Use only for debugging purposes!- Overrides:
toString
in classjava.lang.Object
-
-