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 bounded deque with a fixed maximum capacity.

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.

  • 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>.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 cache.
    • 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>.RollingSpliterator spliterator()
      Specified by:
      spliterator in interface Collection<T>
      Specified by:
      spliterator in interface Iterable<T>
    • addFirst

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

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

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

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

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

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

      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 cache.
    • clear

      public void clear()
      Removes all entries from this cache.
      Specified by:
      clear in interface Collection<T>
      Overrides:
      clear in class AbstractCollection<T>