Class ConcurrentEvictingQueue<E>

  • All Implemented Interfaces:
    java.lang.Iterable<E>, java.util.Collection<E>, java.util.Queue<E>

    public class ConcurrentEvictingQueue<E>
    extends java.util.AbstractQueue<E>
    The purpose of this queue is to store the N most recently inserted elements. If the ConcurrentEvictingQueue is already full ConcurrentEvictingQueue.size() == capacity, the oldest element (the head) will be evicted, and then the new element added at the tail.

    In order to achieve thread-safety it utilizes capability-based locking features of StampedLock. All spins optimistic/pessimistic reads and writes are encapsulated in following methods:

    All other logic just relies on this utility methods.

    Also please take into account that size and modificationsCount are volatile fields, so we can read them and compare against them without any additional synchronizations.

    This class IS thread-safe, and does NOT accept null elements.

    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void clear()
      Atomically removes all of the elements from this queue.
      java.util.Iterator<E> iterator()
      Returns an iterator over the elements in this queue in proper sequence.
      boolean offer​(E e)
      Inserts the specified element at the tail of this queue if it is possible to do so immediately or if capacity limit is exited the oldest element (the head) will be evicted, and then the new element added at the tail.
      E peek()
      E poll()
      int size()
      Returns the number of elements in this queue.
      java.lang.Object[] toArray()
      Returns an array containing all of the elements in this queue, in proper sequence.
      <T> T[] toArray​(T[] destination)
      Returns an array containing all of the elements in this queue, in proper sequence; the runtime type of the returned array is that of the specified array.
      • Methods inherited from class java.util.AbstractQueue

        add, addAll, element, remove
      • Methods inherited from class java.util.AbstractCollection

        contains, containsAll, isEmpty, remove, removeAll, retainAll, toString
      • Methods inherited from class java.lang.Object

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

        contains, containsAll, equals, hashCode, isEmpty, parallelStream, remove, removeAll, removeIf, retainAll, spliterator, stream, toArray
      • Methods inherited from interface java.lang.Iterable

        forEach
    • Constructor Detail

      • ConcurrentEvictingQueue

        public ConcurrentEvictingQueue​(int capacity)
    • Method Detail

      • iterator

        public java.util.Iterator<E> iterator()
        Returns an iterator over the elements in this queue in proper sequence. The elements will be returned in order from first (head) to last (tail).

        This iterator implementation NOT allow removes and co-modifications.

        Specified by:
        iterator in interface java.util.Collection<E>
        Specified by:
        iterator in interface java.lang.Iterable<E>
        Specified by:
        iterator in class java.util.AbstractCollection<E>
        Returns:
        an iterator over the elements in this queue in proper sequence
      • size

        public int size()
        Returns the number of elements in this queue.
        Specified by:
        size in interface java.util.Collection<E>
        Specified by:
        size in class java.util.AbstractCollection<E>
        Returns:
        the number of elements in this queue
      • offer

        public boolean offer​(E e)
        Inserts the specified element at the tail of this queue if it is possible to do so immediately or if capacity limit is exited the oldest element (the head) will be evicted, and then the new element added at the tail. This method is generally preferable to method AbstractQueue.add(E), which can fail to insert an element only by throwing an exception.
        Throws:
        java.lang.NullPointerException - if the specified element is null
      • poll

        public E poll()
      • peek

        public E peek()
      • clear

        public void clear()
        Atomically removes all of the elements from this queue. The queue will be empty after this call returns.
        Specified by:
        clear in interface java.util.Collection<E>
        Overrides:
        clear in class java.util.AbstractQueue<E>
      • toArray

        public java.lang.Object[] toArray()
        Returns an array containing all of the elements in this queue, in proper sequence.

        The returned array will be "safe" in that no references to it are maintained by this queue. (In other words, this method must allocate a new array). The caller is free to modify the returned array.

        This method acts as bridge between array-based and collection-based APIs.

        Specified by:
        toArray in interface java.util.Collection<E>
        Overrides:
        toArray in class java.util.AbstractCollection<E>
        Returns:
        an array containing all of the elements in this queue
      • toArray

        public <T> T[] toArray​(T[] destination)
        Returns an array containing all of the elements in this queue, in proper sequence; the runtime type of the returned array is that of the specified array. If the queue fits in the specified array, it is returned therein. Otherwise, a new array is allocated with the runtime type of the specified array and the size of this queue.

        Like the toArray() method, this method acts as bridge between array-based and collection-based APIs. Further, this method allows precise control over the runtime type of the output array, and may, under certain circumstances, be used to save allocation costs.

        Note that toArray(new Object[0]) is identical in function to toArray().

        Specified by:
        toArray in interface java.util.Collection<E>
        Overrides:
        toArray in class java.util.AbstractCollection<E>
        Parameters:
        destination - the array into which the elements of the queue are to be stored, if it is big enough; otherwise, a new array of the same runtime type is allocated for this purpose
        Returns:
        an array containing all of the elements in this queue
        Throws:
        java.lang.ArrayStoreException - if the runtime type of the specified array is not a supertype of the runtime type of every element in this queue
        java.lang.NullPointerException - if the specified array is null