Class SingletonDeque<E>

  • Type Parameters:
    E - The type of the singleton element 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 SingletonDeque<E>
    extends AbstractDeque<E>
    implements java.io.Serializable
    A Deque implementation that holds a single, immutable element.

    This class provides a lightweight, read-only deque structure containing exactly one element. All operations that attempt to modify the deque (e.g., add, remove, poll) will throw an UnsupportedOperationException because the deque is designed to be immutable.

    Example Usage

    
     SingletonDeque<String> deque = new SingletonDeque<>("Hello");
    
     System.out.println(deque.getFirst()); // Output: Hello
     System.out.println(deque.getLast());  // Output: Hello
     System.out.println(deque.size());     // Output: 1
    
     try {
         deque.addFirst("World");
     } catch (UnsupportedOperationException e) {
         System.out.println("Modification not allowed");
     }
    
     Iterator<String> it = deque.iterator();
     while (it.hasNext()) {
         System.out.println(it.next()); // Output: Hello
     }
     
    Since:
    1.0.0
    Author:
    Mercy
    See Also:
    AbstractDeque, Serialized Form
    • Constructor Summary

      Constructors 
      Constructor Description
      SingletonDeque​(E element)  
    • Constructor Detail

      • SingletonDeque

        public SingletonDeque​(E element)
    • 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>
      • removeFirstOccurrence

        public boolean removeFirstOccurrence​(java.lang.Object o)
        Specified by:
        removeFirstOccurrence in interface java.util.Deque<E>
        Overrides:
        removeFirstOccurrence in class AbstractDeque<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>
      • 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>
      • equals

        public final boolean equals​(java.lang.Object o)
        Specified by:
        equals in interface java.util.Collection<E>
        Overrides:
        equals in class java.lang.Object
      • hashCode

        public int hashCode()
        Specified by:
        hashCode in interface java.util.Collection<E>
        Overrides:
        hashCode in class java.lang.Object