Class ForwardingDeque<E>

  • All Implemented Interfaces:
    Iterable<E>, Collection<E>, Deque<E>, Queue<E>
    Direct Known Subclasses:
    ForwardingBlockingDeque, ForwardingBlockingDeque

    @GwtIncompatible
    public abstract class ForwardingDeque<E>
    extends ForwardingQueue<E>
    implements Deque<E>
    A deque which forwards all its method calls to another deque. Subclasses should override one or more methods to modify the behavior of the backing deque as desired per the decorator pattern.

    Warning: The methods of ForwardingDeque forward indiscriminately to the methods of the delegate. For example, overriding ForwardingCollection.add(E) alone will not change the behavior of ForwardingQueue.offer(E) which can lead to unexpected behavior. In this case, you should override offer as well.

    default method warning: This class does not forward calls to default methods. Instead, it inherits their default implementations. When those implementations invoke methods, they invoke methods on the ForwardingDeque.

    Since:
    12.0
    Author:
    Kurt Alfred Kluever
    • Constructor Detail

      • ForwardingDeque

        protected ForwardingDeque()
        Constructor for use by subclasses.
    • Method Detail

      • delegate

        protected abstract Deque<Edelegate()
        Description copied from class: ForwardingObject
        Returns the backing delegate instance that methods are forwarded to. Abstract subclasses generally override this method with an abstract method that has a more specific return type, such as ForwardingSet.delegate(). Concrete subclasses override this method to supply the instance being decorated.
        Specified by:
        delegate in class ForwardingQueue<E>
      • addFirst

        public void addFirst​(E e)
        Description copied from interface: java.util.Deque
        Inserts the specified element at the front of this deque if it is possible to do so immediately without violating capacity restrictions, throwing an IllegalStateException if no space is currently available. When using a capacity-restricted deque, it is generally preferable to use method Deque.offerFirst(E).
        Specified by:
        addFirst in interface Deque<E>
        Parameters:
        e - the element to add
      • addLast

        public void addLast​(E e)
        Description copied from interface: java.util.Deque
        Inserts the specified element at the end of this deque if it is possible to do so immediately without violating capacity restrictions, throwing an IllegalStateException if no space is currently available. When using a capacity-restricted deque, it is generally preferable to use method Deque.offerLast(E).

        This method is equivalent to Deque.add(E).

        Specified by:
        addLast in interface Deque<E>
        Parameters:
        e - the element to add
      • descendingIterator

        public Iterator<EdescendingIterator()
        Description copied from interface: java.util.Deque
        Returns an iterator over the elements in this deque in reverse sequential order. The elements will be returned in order from last (tail) to first (head).
        Specified by:
        descendingIterator in interface Deque<E>
        Returns:
        an iterator over the elements in this deque in reverse sequence
      • getFirst

        public E getFirst()
        Description copied from interface: java.util.Deque
        Retrieves, but does not remove, the first element of this deque. This method differs from peekFirst only in that it throws an exception if this deque is empty.
        Specified by:
        getFirst in interface Deque<E>
        Returns:
        the head of this deque
      • getLast

        public E getLast()
        Description copied from interface: java.util.Deque
        Retrieves, but does not remove, the last element of this deque. This method differs from peekLast only in that it throws an exception if this deque is empty.
        Specified by:
        getLast in interface Deque<E>
        Returns:
        the tail of this deque
      • offerFirst

        @CanIgnoreReturnValue
        public boolean offerFirst​(E e)
        Description copied from interface: java.util.Deque
        Inserts the specified element at the front of this deque unless it would violate capacity restrictions. When using a capacity-restricted deque, this method is generally preferable to the Deque.addFirst(E) method, which can fail to insert an element only by throwing an exception.
        Specified by:
        offerFirst in interface Deque<E>
        Parameters:
        e - the element to add
        Returns:
        true if the element was added to this deque, else false
      • offerLast

        @CanIgnoreReturnValue
        public boolean offerLast​(E e)
        Description copied from interface: java.util.Deque
        Inserts the specified element at the end of this deque unless it would violate capacity restrictions. When using a capacity-restricted deque, this method is generally preferable to the Deque.addLast(E) method, which can fail to insert an element only by throwing an exception.
        Specified by:
        offerLast in interface Deque<E>
        Parameters:
        e - the element to add
        Returns:
        true if the element was added to this deque, else false
      • peekFirst

        public E peekFirst()
        Description copied from interface: java.util.Deque
        Retrieves, but does not remove, the first element of this deque, or returns null if this deque is empty.
        Specified by:
        peekFirst in interface Deque<E>
        Returns:
        the head of this deque, or null if this deque is empty
      • peekLast

        public E peekLast()
        Description copied from interface: java.util.Deque
        Retrieves, but does not remove, the last element of this deque, or returns null if this deque is empty.
        Specified by:
        peekLast in interface Deque<E>
        Returns:
        the tail of this deque, or null if this deque is empty
      • pollFirst

        @CanIgnoreReturnValue
        public E pollFirst()
        Description copied from interface: java.util.Deque
        Retrieves and removes the first element of this deque, or returns null if this deque is empty.
        Specified by:
        pollFirst in interface Deque<E>
        Returns:
        the head of this deque, or null if this deque is empty
      • pollLast

        @CanIgnoreReturnValue
        public E pollLast()
        Description copied from interface: java.util.Deque
        Retrieves and removes the last element of this deque, or returns null if this deque is empty.
        Specified by:
        pollLast in interface Deque<E>
        Returns:
        the tail of this deque, or null if this deque is empty
      • pop

        @CanIgnoreReturnValue
        public E pop()
        Description copied from interface: java.util.Deque
        Pops an element from the stack represented by this deque. In other words, removes and returns the first element of this deque.

        This method is equivalent to Deque.removeFirst().

        Specified by:
        pop in interface Deque<E>
        Returns:
        the element at the front of this deque (which is the top of the stack represented by this deque)
      • push

        public void push​(E e)
        Description copied from interface: java.util.Deque
        Pushes an element onto the stack represented by this deque (in other words, at the head of this deque) if it is possible to do so immediately without violating capacity restrictions, throwing an IllegalStateException if no space is currently available.

        This method is equivalent to Deque.addFirst(E).

        Specified by:
        push in interface Deque<E>
        Parameters:
        e - the element to push
      • removeFirstOccurrence

        @CanIgnoreReturnValue
        public boolean removeFirstOccurrence​(Object o)
        Description copied from interface: java.util.Deque
        Removes the first occurrence of the specified element from this deque. If the deque does not contain the element, it is unchanged. More formally, removes the first element e such that Objects.equals(o, e) (if such an element exists). Returns true if this deque contained the specified element (or equivalently, if this deque changed as a result of the call).
        Specified by:
        removeFirstOccurrence in interface Deque<E>
        Parameters:
        o - element to be removed from this deque, if present
        Returns:
        true if an element was removed as a result of this call
      • removeLastOccurrence

        @CanIgnoreReturnValue
        public boolean removeLastOccurrence​(Object o)
        Description copied from interface: java.util.Deque
        Removes the last occurrence of the specified element from this deque. If the deque does not contain the element, it is unchanged. More formally, removes the last element e such that Objects.equals(o, e) (if such an element exists). Returns true if this deque contained the specified element (or equivalently, if this deque changed as a result of the call).
        Specified by:
        removeLastOccurrence in interface Deque<E>
        Parameters:
        o - element to be removed from this deque, if present
        Returns:
        true if an element was removed as a result of this call