Class EvictingQueue<T>

  • Type Parameters:
    T - The type of elements in the queue.
    All Implemented Interfaces:
    Iterable<T>, Collection<T>, Queue<T>

    public class EvictingQueue<T>
    extends Object
    implements Queue<T>
    An EvictingQueue is a non-blocking queue which is limited to a maximum size; when new elements are added to a full queue, elements are evicted from the head of the queue to accommodate the new elements.
    • Constructor Detail

      • EvictingQueue

        public EvictingQueue​(int maximumSize)
        Construct a new EvictingQueue that holds maximumSize elements.
        Parameters:
        maximumSize - The maximum number of elements that the queue can hold
        Throws:
        IllegalArgumentException - if maximumSize is less than zero
    • Method Detail

      • remainingCapacity

        public int remainingCapacity()
        Returns:
        the number of additional elements that the queue can accommodate before evictions occur
      • add

        public boolean add​(T t)
        Add the given element to the queue, possibly forcing an eviction from the head if remainingCapacity() is zero.
        Specified by:
        add in interface Collection<T>
        Specified by:
        add in interface Queue<T>
        Parameters:
        t - the element to add
        Returns:
        true if the element was added (always the case for EvictingQueue
      • remove

        public T remove()
        Specified by:
        remove in interface Queue<T>
      • poll

        public T poll()
        Specified by:
        poll in interface Queue<T>
      • element

        public T element()
        Specified by:
        element in interface Queue<T>
      • peek

        public T peek()
        Specified by:
        peek in interface Queue<T>
      • toArray

        public <T1> T1[] toArray​(T1[] a)
        Specified by:
        toArray in interface Collection<T>
      • addAll

        public boolean addAll​(Collection<? extends T> c)
        Add the given elements to the queue, possibly forcing evictions from the head if remainingCapacity() is zero or becomes zero during the execution of this method.
        Specified by:
        addAll in interface Collection<T>
        Parameters:
        c - the collection of elements to add
        Returns:
        true if any elements were added to the queue