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>

public final class RollingCache<T> extends AbstractCollection<T> implements Iterable<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 Details

    • RollingCache

      public RollingCache(int limit)
  • Method Details

    • size

      public int size()
      Specified by:
      size in interface Collection<T>
      Specified by:
      size in class AbstractCollection<T>
    • iterator

      public RollingCache<T>.nl.pim16aap2.animatedarchitecture.core.data.cache.RollingCache.RollingIterator iterator()
      Specified by:
      iterator in interface Collection<T>
      Specified by:
      iterator in interface Iterable<T>
      Specified by:
      iterator in class AbstractCollection<T>
    • add

      public boolean add(T t)
      Specified by:
      add in interface Collection<T>
      Overrides:
      add in class AbstractCollection<T>
    • forEach

      public void forEach(Consumer<? super T> action)
      Specified by:
      forEach in interface Iterable<T>
    • remove

      public T remove(int index)
      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

      public boolean addAll(Collection<? extends T> c)
      Specified by:
      addAll in interface Collection<T>
      Overrides:
      addAll in class AbstractCollection<T>
    • spliterator

      public RollingCache<T>.nl.pim16aap2.animatedarchitecture.core.data.cache.RollingCache.RollingSpliterator spliterator()
      Specified by:
      spliterator in interface Collection<T>
      Specified by:
      spliterator in interface Iterable<T>
    • insertFirst

      public void insertFirst(T t)
      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

      public void insertLast(T t)
      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

      public T 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

      public T 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

      public T 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

      public T 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

      public T get(int index)
      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:
      clear in interface Collection<T>
      Overrides:
      clear in class AbstractCollection<T>