Class RollingCache<T>
java.lang.Object
java.util.AbstractCollection<T>
nl.pim16aap2.animatedarchitecture.core.data.cache.RollingCache<T>
- Type Parameters:
T- The type of data to store.
- All Implemented Interfaces:
Iterable<T>,Collection<T>
Represents a LIFO (Last-In-First-Out) stack of objects with a limited capacity.
When the capacity is exceeded, the last element is evicted to make space for the new element.
The head contains the element that has been in the stack the shortest and the tail contains the element that has been in the stack 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.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleanSeeinsertFirst(Object).booleanaddAll(Collection<? extends T> c) voidclear()Removes all entries from this stack.voidget(int index) Gets the element at the given index if it exists.voidinsertFirst(T t) Adds an element to this stack.voidinsertLast(T t) Adds an element to this stack.RollingCache<T>.nl.pim16aap2.animatedarchitecture.core.data.cache.RollingCache.RollingIteratoriterator()peekLast()remove(int index) Removes an object at the given index.Removes the element at the tail of this stack.Removes the element at the head of this stack.intsize()RollingCache<T>.nl.pim16aap2.animatedarchitecture.core.data.cache.RollingCache.RollingSpliteratorMethods inherited from class java.util.AbstractCollection
contains, containsAll, isEmpty, remove, removeAll, retainAll, toArray, toArray, toStringMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface java.util.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
public RollingCache<T>.nl.pim16aap2.animatedarchitecture.core.data.cache.RollingCache.RollingIterator iterator()- Specified by:
iteratorin interfaceCollection<T>- Specified by:
iteratorin interfaceIterable<T>- Specified by:
iteratorin classAbstractCollection<T>
-
add
SeeinsertFirst(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 stack.
-
addAll
- Specified by:
addAllin interfaceCollection<T>- Overrides:
addAllin classAbstractCollection<T>
-
spliterator
public RollingCache<T>.nl.pim16aap2.animatedarchitecture.core.data.cache.RollingCache.RollingSpliterator spliterator()- Specified by:
spliteratorin interfaceCollection<T>- Specified by:
spliteratorin interfaceIterable<T>
-
insertFirst
Adds an element to this stack. The element is inserted at the head of the stack.If the stack is currently at capacity, the oldest element (i.e. the element at the tail) will be evicted.
- Parameters:
t- The element to add.
-
insertLast
Adds an element to this stack. The element is inserted at the tail of the stack.If the stack is currently at capacity, the newest element (i.e. the element at the head) will be evicted.
- Parameters:
t- The element to add.
-
peekLast
- Returns:
- The last value. This is the value that has been in the stack the shortest.
- Throws:
NoSuchElementException- If this stack is empty.
-
peekFirst
- Returns:
- The first value. This is the value that has been in the stack the longest.
- Throws:
NoSuchElementException- If this stack is empty.
-
removeLast
Removes the element at the head of this stack. This is the element that has been in this stack the shortest.- Returns:
- The removed element.
- Throws:
NoSuchElementException- If this stack is empty.
-
removeFirst
Removes the element at the tail of this stack. This is the element that has been in this stack the longest.- Returns:
- The removed element.
- Throws:
NoSuchElementException- If this stack 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 stack.
-
clear
public void clear()Removes all entries from this stack.- Specified by:
clearin interfaceCollection<T>- Overrides:
clearin classAbstractCollection<T>
-