Class EmptyDeque<E>

  • Type Parameters:
    E - The type of elements held in this deque
    All Implemented Interfaces:
    java.io.Serializable, java.lang.Iterable<E>, java.util.Collection<E>, java.util.Deque<E>, java.util.Queue<E>

    @Immutable
    public class EmptyDeque<E>
    extends AbstractDeque<E>
    implements java.io.Serializable
    An empty and immutable Deque implementation that throws UnsupportedOperationException for methods that attempt to modify the deque, and returns appropriate default values for read-only operations.

    This class is a singleton implementation, and it's serializable. It's useful as a placeholder or default value where an empty deque is needed without creating a new instance every time.

    Example Usage

    
     Deque<String> deque = EmptyDeque.INSTANCE;
    
     // Reading from the deque
     System.out.println(deque.isEmpty()); // true
     System.out.println(deque.size());    // 0
    
     // Iterating over elements (no output)
     for (String element : deque) {
         System.out.println(element);
     }
    
     // Attempting to modify will throw an exception
     try {
         deque.add("test");
     } catch (UnsupportedOperationException e) {
         System.out.println("Modification not allowed: " + e.getMessage());
     }
     
    Since:
    1.0.0
    Author:
    Mercy
    See Also:
    AbstractDeque, Serialized Form
    • Constructor Summary

      Constructors 
      Constructor Description
      EmptyDeque()  
    • Field Detail

      • INSTANCE

        public static final EmptyDeque<?> INSTANCE
    • Constructor Detail

      • EmptyDeque

        public EmptyDeque()
    • Method Detail

      • iterator

        public java.util.Iterator<E> iterator()
        Specified by:
        iterator in interface java.util.Collection<E>
        Specified by:
        iterator in interface java.util.Deque<E>
        Specified by:
        iterator in interface java.lang.Iterable<E>
        Specified by:
        iterator in class java.util.AbstractCollection<E>
      • descendingIterator

        public java.util.Iterator<E> descendingIterator()
        Specified by:
        descendingIterator in interface java.util.Deque<E>
      • offerFirst

        public boolean offerFirst​(E e)
        Specified by:
        offerFirst in interface java.util.Deque<E>
      • offerLast

        public boolean offerLast​(E e)
        Specified by:
        offerLast in interface java.util.Deque<E>
      • pollFirst

        public E pollFirst()
        Specified by:
        pollFirst in interface java.util.Deque<E>
      • pollLast

        public E pollLast()
        Specified by:
        pollLast in interface java.util.Deque<E>
      • getFirst

        public E getFirst()
        Specified by:
        getFirst in interface java.util.Deque<E>
      • getLast

        public E getLast()
        Specified by:
        getLast in interface java.util.Deque<E>
      • peekFirst

        public E peekFirst()
        Specified by:
        peekFirst in interface java.util.Deque<E>
      • peekLast

        public E peekLast()
        Specified by:
        peekLast in interface java.util.Deque<E>
      • removeLastOccurrence

        public boolean removeLastOccurrence​(java.lang.Object o)
        Specified by:
        removeLastOccurrence in interface java.util.Deque<E>
      • remove

        public boolean remove​(java.lang.Object o)
        Specified by:
        remove in interface java.util.Collection<E>
        Specified by:
        remove in interface java.util.Deque<E>
        Overrides:
        remove in class java.util.AbstractCollection<E>
      • size

        public int size()
        Specified by:
        size in interface java.util.Collection<E>
        Specified by:
        size in interface java.util.Deque<E>
        Specified by:
        size in class java.util.AbstractCollection<E>