Class SimpleDeque<T>

  • Type Parameters:
    T - Type of item in the SimpleDeque.

    @NotThreadSafe
    public class SimpleDeque<T>
    extends java.lang.Object
    Simplified Deque implementation that provides an efficient method to remove multiple items at once from the head. Note that this class does not implement Deque and as such not all methods in that interface are present here. The only reason it exists is because there is no way in ArrayDeque to efficiently remove multiple items at once (see pollFirst(int).
    • Constructor Detail

      • SimpleDeque

        public SimpleDeque()
        Creates a new instance of the SimpleDeque class.
      • SimpleDeque

        public SimpleDeque​(int initialCapacity)
        Creates a new instance of the SimpleDeque class.
        Parameters:
        initialCapacity - The initial capacity.
    • Method Detail

      • addLast

        public void addLast​(@NonNull
                            T item)
        Adds a new item at the tail of the SimpleDeque. See Deque.addLast(E).
        Parameters:
        item - The item to add. Must be non-null (since we use null internally as a sentinel)
      • pollFirst

        public T pollFirst()
        Removes one item from the head of the SimpleDeque. See Deque.pollFirst().
        Returns:
        The item at the head of the SimpleDeque, or null of empty.
      • pollFirst

        public java.util.Queue<T> pollFirst​(int maxCount)
        Removes one or more items from the head of the SimpleDeque. This method is equivalent to:
        
             SimpleDeque source;
             Queue target;
             int count = maxCount;
             while(count > 0 && !source.isEmpty()) {
                 target.add(source.removeFirst());
                 count--;
             }
         
        This method is optimized for a bulk copy and is preferred to using the code exemplified above.
        Parameters:
        maxCount - The maximum number of items to remove. If this number is larger than size(), then size() items will be removed.
        Returns:
        A Queue containing the removed items, in the same order as they were in the SimpleDeque.
      • peekFirst

        public T peekFirst()
        Returns (but does not remove) the item at the head of the SimpleDeque. This method does not alter the internal state of the SimpleDeque. See Deque.peekFirst().
        Returns:
        The item at the head of the SimpleDeque, or null if empty.
      • size

        public int size()
        Gets a value indicating the number of items in the SimpleDeque.
        Returns:
        The size of the SimpleDeque.
      • isEmpty

        public boolean isEmpty()
        Gets a value indicating whether the SimpleDeque is empty or not. Consider using this instead of {@link #size()} == 0 since this method does not need to compute anything.
        Returns:
        True if empty, false otherwise.
      • clear

        public void clear()
        Clears the entire SimpleDeque.
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object