Class UnmodifiableQueue<E>

  • Type Parameters:
    E - the type of elements held in this queue
    All Implemented Interfaces:
    java.io.Serializable, java.lang.Iterable<E>, java.util.Collection<E>, java.util.Queue<E>
    Direct Known Subclasses:
    UnmodifiableDeque

    @Immutable
    public class UnmodifiableQueue<E>
    extends java.lang.Object
    implements java.util.Queue<E>, java.io.Serializable
    An unmodifiable view of a Queue. This implementation decorates a given queue and prevents any modification operations from succeeding. All mutation methods, such as add(E), remove(java.lang.Object), offer(E), and others, throw an UnsupportedOperationException.

    The behavior of this class is undefined if the underlying queue is modified directly while this view exists. It is intended to be used for creating immutable snapshots of queues where modification attempts are not allowed.

    Example Usage

    
     Queue<String> mutableQueue = new LinkedList<>();
     mutableQueue.add("Hello");
     Queue<String> unmodifiableQueue = new UnmodifiableQueue<>(mutableQueue);
     unmodifiableQueue.add("World"); // throws UnsupportedOperationException
      

    This class is serializable if the underlying queue is serializable.

    Since:
    1.0.0
    Author:
    Mercy
    See Also:
    Queue, CollectionUtils.unmodifiableIterator(Iterator), Serialized Form
    • Method Detail

      • size

        public int size()
        Specified by:
        size in interface java.util.Collection<E>
      • isEmpty

        public boolean isEmpty()
        Specified by:
        isEmpty in interface java.util.Collection<E>
      • contains

        public boolean contains​(java.lang.Object o)
        Specified by:
        contains in interface java.util.Collection<E>
      • iterator

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

        public java.lang.Object[] toArray()
        Specified by:
        toArray in interface java.util.Collection<E>
      • toArray

        public <T> T[] toArray​(T[] a)
        Specified by:
        toArray in interface java.util.Collection<E>
      • add

        public boolean add​(E e)
        Specified by:
        add in interface java.util.Collection<E>
        Specified by:
        add in interface java.util.Queue<E>
      • remove

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

        public boolean offer​(E e)
        Specified by:
        offer in interface java.util.Queue<E>
      • remove

        public E remove()
        Specified by:
        remove in interface java.util.Queue<E>
      • poll

        public E poll()
        Specified by:
        poll in interface java.util.Queue<E>
      • element

        public E element()
        Specified by:
        element in interface java.util.Queue<E>
      • peek

        public E peek()
        Specified by:
        peek in interface java.util.Queue<E>
      • containsAll

        public boolean containsAll​(java.util.Collection<?> c)
        Specified by:
        containsAll in interface java.util.Collection<E>
      • addAll

        public boolean addAll​(java.util.Collection<? extends E> c)
        Specified by:
        addAll in interface java.util.Collection<E>
      • removeAll

        public boolean removeAll​(java.util.Collection<?> c)
        Specified by:
        removeAll in interface java.util.Collection<E>
      • removeIf

        public boolean removeIf​(java.util.function.Predicate<? super E> filter)
        Specified by:
        removeIf in interface java.util.Collection<E>
      • retainAll

        public boolean retainAll​(java.util.Collection<?> c)
        Specified by:
        retainAll in interface java.util.Collection<E>
      • clear

        public void clear()
        Specified by:
        clear in interface java.util.Collection<E>
      • spliterator

        public java.util.Spliterator<E> spliterator()
        Specified by:
        spliterator in interface java.util.Collection<E>
        Specified by:
        spliterator in interface java.lang.Iterable<E>
      • stream

        public java.util.stream.Stream<E> stream()
        Specified by:
        stream in interface java.util.Collection<E>
      • parallelStream

        public java.util.stream.Stream<E> parallelStream()
        Specified by:
        parallelStream in interface java.util.Collection<E>
      • forEach

        public void forEach​(java.util.function.Consumer<? super E> action)
        Specified by:
        forEach in interface java.lang.Iterable<E>
      • equals

        public 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
      • toString

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