Class RollingCache<T>
- Type Parameters:
T- The type of data to store.
- All Implemented Interfaces:
Iterable<T>, Collection<T>
When the capacity is exceeded, elements are evicted to make space for the new element.
When adding an element to the head, the oldest element (i.e. the element at the tail) is evicted. When adding an element to the tail, the newest element (i.e. the element at the head) is evicted.
The head contains the element that has been in the cache the shortest and the tail contains the element that has been in the cache the longest.
Note that access to this class is not thread-safe and that as such, external synchronization is required if used in a multithreaded environment.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionfinal classAn iterator for the rolling cache.final classA spliterator for the rolling cache. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleanSeeaddFirst(Object).booleanaddAll(Collection<? extends T> c) voidAdds an element to this cache.voidAdds an element to this cache.voidclear()Removes all entries from this cache.voidget(int index) Gets the element at the given index if it exists.getFirst()Retrieves, but does not remove, the element at the tail of this cache.getLast()Retrieves, but does not remove, the element at the head of this cache.iterator()remove(int index) Removes an object at the given index.Removes the element at the tail of this cache.Removes the element at the head of this cache.intsize()Methods inherited from class AbstractCollection
contains, containsAll, isEmpty, remove, removeAll, retainAll, toArray, toArray, toStringMethods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface Collection
equals, hashCode, parallelStream, removeIf, stream, toArray
-
Constructor Details
-
RollingCache
public RollingCache(int limit)
-
-
Method Details
-
size
public int size()- Specified by:
sizein interfaceCollection<T>- Specified by:
sizein classAbstractCollection<T>
-
iterator
- Specified by:
iteratorin interfaceCollection<T>- Specified by:
iteratorin interfaceIterable<T>- Specified by:
iteratorin classAbstractCollection<T>
-
add
SeeaddFirst(Object).- Specified by:
addin interfaceCollection<T>- Overrides:
addin classAbstractCollection<T>
-
forEach
-
remove
Removes an object at the given index.The index is counted from the tail to the head (i.e. from the oldest to the newest value).
- Parameters:
index- The index of the object to remove.- Returns:
- The value that was removed.
- Throws:
IndexOutOfBoundsException- If the given index is negative or exceeds the current size of the cache.
-
addAll
- Specified by:
addAllin interfaceCollection<T>- Overrides:
addAllin classAbstractCollection<T>
-
spliterator
- Specified by:
spliteratorin interfaceCollection<T>- Specified by:
spliteratorin interfaceIterable<T>
-
addFirst
Adds an element to this cache. The element is inserted at the head of the cache.If the cache is currently at capacity, the oldest element (i.e. the element at the tail) will be evicted.
- Parameters:
t- The element to add.
-
addLast
Adds an element to this cache. The element is inserted at the tail of the cache.If the cache is currently at capacity, the newest element (i.e. the element at the head) will be evicted.
- Parameters:
t- The element to add.
-
getLast
Retrieves, but does not remove, the element at the head of this cache. This is the element that has been in the cache the shortest.- Returns:
- The last value. This is the value that has been in the cache the shortest.
- Throws:
NoSuchElementException- If this cache is empty.
-
getFirst
Retrieves, but does not remove, the element at the tail of this cache. This is the element that has been in the cache the longest.- Returns:
- The first value. This is the value that has been in the cache the longest.
- Throws:
NoSuchElementException- If this cache is empty.
-
removeLast
Removes the element at the head of this cache. This is the element that has been in this cache the shortest.- Returns:
- The removed element.
- Throws:
NoSuchElementException- If this cache is empty.
-
removeFirst
Removes the element at the tail of this cache. This is the element that has been in this cache the longest.- Returns:
- The removed element.
- Throws:
NoSuchElementException- If this cache is empty.
-
get
Gets the element at the given index if it exists.- Parameters:
index- The position of the index as counted from the oldest value.- Returns:
- The value at the given position.
- Throws:
IndexOutOfBoundsException- If the given index is negative or exceeds the current size of the cache.
-
clear
public void clear()Removes all entries from this cache.- Specified by:
clearin interfaceCollection<T>- Overrides:
clearin classAbstractCollection<T>
-