Class BoundedDeque<E>

  • Type Parameters:
    E - element type
    All Implemented Interfaces:
    Iterable<E>, Collection<E>

    public class BoundedDeque<E>
    extends AbstractCollection<E>
    A generic deque-derived container which transparently acts either as a stack or a queue, and optionally a capacity restriction with a configurable policy which element is evicted (or reject) if the maximum capacity is reached.

    Note: Like ArrayDeque, this deque implementation is not thread-safe. Concurrent access by multiple threads requires explicit synchronization.

    • Constructor Detail

      • BoundedDeque

        public BoundedDeque​(BoundedDeque.AccessPolicy accessPolicy)
        Constructor. Creates an unbounded deque with the given access policy.
        Parameters:
        accessPolicy - whether this deque acts as a stack or a queue
      • BoundedDeque

        public BoundedDeque​(int capacity,
                            BoundedDeque.AccessPolicy accessPolicy,
                            BoundedDeque.EvictPolicy evictPolicy)
        Constructor. Creates a possibly capacity-restricted deque with the given access policy.
        Parameters:
        capacity - the maximum capacity of this deque. A value less than or equal to 0 means unbounded
        accessPolicy - whether this deque acts as a stack or a queue
        evictPolicy - which elements to remove if the maximum capacity is reached. If the capacity is unbounded, this parameter has no effect
    • Method Detail

      • insert

        public @Nullable E insert​(E element)
        Inserts an element into the deque, and returns the one that had to be evicted in case of a capacity violation.
        Parameters:
        element - the element to insert
        Returns:
        the evicted element, null if the maximum capacity has not been reached
      • retrieve

        public E retrieve()
        Retrieves and remove the top-most element, i.e., the element that is either the top of the stack or the head of the queue, depending on the configured BoundedDeque.AccessPolicy.
        Returns:
        the top-most element of the container
      • peek

        public E peek()
        Retrieves, but does not remove the top-most element, i.e., the element that is either the top of the stack or the head of the queue, depending on the configured BoundedDeque.AccessPolicy.
        Returns:
        the top-most element of the container
      • isBounded

        public boolean isBounded()
        Retrieves whether capacity restriction is in effect.
        Returns:
        true if the capacity is restricted, false otherwise