UnmodifiableQueue.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.collections4.queue;

  18. import java.io.IOException;
  19. import java.io.ObjectInputStream;
  20. import java.io.ObjectOutputStream;
  21. import java.util.Collection;
  22. import java.util.Iterator;
  23. import java.util.Queue;
  24. import java.util.function.Predicate;

  25. import org.apache.commons.collections4.Unmodifiable;
  26. import org.apache.commons.collections4.iterators.UnmodifiableIterator;

  27. /**
  28.  * Decorates another {@link Queue} to ensure it can't be altered.
  29.  * <p>
  30.  * Attempts to modify it will result in an UnsupportedOperationException.
  31.  * </p>
  32.  *
  33.  * @param <E> the type of elements held in this queue
  34.  * @since 4.0
  35.  */
  36. public final class UnmodifiableQueue<E>
  37.         extends AbstractQueueDecorator<E>
  38.         implements Unmodifiable {

  39.     /** Serialization version */
  40.     private static final long serialVersionUID = 1832948656215393357L;

  41.     /**
  42.      * Factory method to create an unmodifiable queue.
  43.      * <p>
  44.      * If the queue passed in is already unmodifiable, it is returned.
  45.      *
  46.      * @param <E> the type of the elements in the queue
  47.      * @param queue  the queue to decorate, must not be null
  48.      * @return an unmodifiable Queue
  49.      * @throws NullPointerException if queue is null
  50.      */
  51.     public static <E> Queue<E> unmodifiableQueue(final Queue<? extends E> queue) {
  52.         if (queue instanceof Unmodifiable) {
  53.             @SuppressWarnings("unchecked") // safe to upcast
  54.             final Queue<E> tmpQueue = (Queue<E>) queue;
  55.             return tmpQueue;
  56.         }
  57.         return new UnmodifiableQueue<>(queue);
  58.     }

  59.     /**
  60.      * Constructor that wraps (not copies).
  61.      *
  62.      * @param queue  the queue to decorate, must not be null
  63.      * @throws NullPointerException if queue is null
  64.      */
  65.     @SuppressWarnings("unchecked") // safe to upcast
  66.     private UnmodifiableQueue(final Queue<? extends E> queue) {
  67.         super((Queue<E>) queue);
  68.     }

  69.     @Override
  70.     public boolean add(final Object object) {
  71.         throw new UnsupportedOperationException();
  72.     }

  73.     @Override
  74.     public boolean addAll(final Collection<? extends E> coll) {
  75.         throw new UnsupportedOperationException();
  76.     }

  77.     @Override
  78.     public void clear() {
  79.         throw new UnsupportedOperationException();
  80.     }

  81.     @Override
  82.     public Iterator<E> iterator() {
  83.         return UnmodifiableIterator.unmodifiableIterator(decorated().iterator());
  84.     }

  85.     @Override
  86.     public boolean offer(final E obj) {
  87.         throw new UnsupportedOperationException();
  88.     }

  89.     @Override
  90.     public E poll() {
  91.         throw new UnsupportedOperationException();
  92.     }

  93.     /**
  94.      * Deserializes the collection in using a custom routine.
  95.      *
  96.      * @param in  the input stream
  97.      * @throws IOException if an I/O error occurs while reading from the input stream
  98.      * @throws ClassNotFoundException if the class of a serialized object cannot be found
  99.      */
  100.     @SuppressWarnings("unchecked")
  101.     private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException {
  102.         in.defaultReadObject();
  103.         setCollection((Collection<E>) in.readObject());
  104.     }

  105.     @Override
  106.     public E remove() {
  107.         throw new UnsupportedOperationException();
  108.     }

  109.     @Override
  110.     public boolean remove(final Object object) {
  111.         throw new UnsupportedOperationException();
  112.     }

  113.     @Override
  114.     public boolean removeAll(final Collection<?> coll) {
  115.         throw new UnsupportedOperationException();
  116.     }

  117.     /**
  118.      * @since 4.4
  119.      */
  120.     @Override
  121.     public boolean removeIf(final Predicate<? super E> filter) {
  122.         throw new UnsupportedOperationException();
  123.     }

  124.     @Override
  125.     public boolean retainAll(final Collection<?> coll) {
  126.         throw new UnsupportedOperationException();
  127.     }

  128.     /**
  129.      * Serializes this object to an ObjectOutputStream.
  130.      *
  131.      * @param out the target ObjectOutputStream.
  132.      * @throws IOException thrown when an I/O errors occur writing to the target stream.
  133.      */
  134.     private void writeObject(final ObjectOutputStream out) throws IOException {
  135.         out.defaultWriteObject();
  136.         out.writeObject(decorated());
  137.     }

  138. }