Class EvictingQueue<T>

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

    public class EvictingQueue<T>
    extends java.lang.Object
    implements java.util.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 Summary

      Constructors 
      Constructor Description
      EvictingQueue​(int maximumSize)
      Construct a new EvictingQueue that holds maximumSize elements.
    • Method Summary

      Modifier and Type Method Description
      boolean add​(T t)
      Add the given element to the queue, possibly forcing an eviction from the head if remainingCapacity() is zero.
      boolean addAll​(java.util.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.
      void clear()  
      boolean contains​(java.lang.Object o)  
      boolean containsAll​(java.util.Collection<?> c)  
      T element()  
      boolean isEmpty()  
      java.util.Iterator<T> iterator()  
      boolean offer​(T t)  
      T peek()  
      T poll()  
      int remainingCapacity()  
      T remove()  
      boolean remove​(java.lang.Object o)  
      boolean removeAll​(java.util.Collection<?> c)  
      boolean retainAll​(java.util.Collection<?> c)  
      int size()  
      java.lang.Object[] toArray()  
      <T1> T1[] toArray​(T1[] a)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
      • Methods inherited from interface java.util.Collection

        equals, hashCode, parallelStream, removeIf, spliterator, stream, toArray
      • Methods inherited from interface java.lang.Iterable

        forEach
    • 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:
        java.lang.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 java.util.Collection<T>
        Specified by:
        add in interface java.util.Queue<T>
        Parameters:
        t - the element to add
        Returns:
        true if the element was added (always the case for EvictingQueue
      • offer

        public boolean offer​(T t)
        Specified by:
        offer in interface java.util.Queue<T>
        See Also:
        add(Object)
      • remove

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

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

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

        public T peek()
        Specified by:
        peek in interface java.util.Queue<T>
      • size

        public int size()
        Specified by:
        size in interface java.util.Collection<T>
      • isEmpty

        public boolean isEmpty()
        Specified by:
        isEmpty in interface java.util.Collection<T>
      • contains

        public boolean contains​(java.lang.Object o)
        Specified by:
        contains in interface java.util.Collection<T>
      • iterator

        public java.util.Iterator<T> iterator()
        Specified by:
        iterator in interface java.util.Collection<T>
        Specified by:
        iterator in interface java.lang.Iterable<T>
      • toArray

        public java.lang.Object[] toArray()
        Specified by:
        toArray in interface java.util.Collection<T>
      • toArray

        public <T1> T1[] toArray​(T1[] a)
        Specified by:
        toArray in interface java.util.Collection<T>
      • remove

        public boolean remove​(java.lang.Object o)
        Specified by:
        remove in interface java.util.Collection<T>
      • containsAll

        public boolean containsAll​(java.util.Collection<?> c)
        Specified by:
        containsAll in interface java.util.Collection<T>
      • addAll

        public boolean addAll​(java.util.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 java.util.Collection<T>
        Parameters:
        c - the collection of elements to add
        Returns:
        true if any elements were added to the queue
      • removeAll

        public boolean removeAll​(java.util.Collection<?> c)
        Specified by:
        removeAll in interface java.util.Collection<T>
      • retainAll

        public boolean retainAll​(java.util.Collection<?> c)
        Specified by:
        retainAll in interface java.util.Collection<T>
      • clear

        public void clear()
        Specified by:
        clear in interface java.util.Collection<T>