View Javadoc
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  
19  import java.io.IOException;
20  import java.io.ObjectInputStream;
21  import java.io.ObjectOutputStream;
22  import java.util.Collection;
23  import java.util.Iterator;
24  import java.util.Queue;
25  import java.util.function.Predicate;
26  
27  import org.apache.commons.collections4.Unmodifiable;
28  import org.apache.commons.collections4.iterators.UnmodifiableIterator;
29  
30  /**
31   * Decorates another {@link Queue} to ensure it can't be altered.
32   * <p>
33   * Attempts to modify it will result in an UnsupportedOperationException.
34   * </p>
35   *
36   * @param <E> the type of elements held in this queue
37   * @since 4.0
38   */
39  public final class UnmodifiableQueue<E>
40          extends AbstractQueueDecorator<E>
41          implements Unmodifiable {
42  
43      /** Serialization version */
44      private static final long serialVersionUID = 1832948656215393357L;
45  
46      /**
47       * Factory method to create an unmodifiable queue.
48       * <p>
49       * If the queue passed in is already unmodifiable, it is returned.
50       *
51       * @param <E> the type of the elements in the queue
52       * @param queue  the queue to decorate, must not be null
53       * @return an unmodifiable Queue
54       * @throws NullPointerException if queue is null
55       */
56      public static <E> Queue<E> unmodifiableQueue(final Queue<? extends E> queue) {
57          if (queue instanceof Unmodifiable) {
58              @SuppressWarnings("unchecked") // safe to upcast
59              final Queue<E> tmpQueue = (Queue<E>) queue;
60              return tmpQueue;
61          }
62          return new UnmodifiableQueue<>(queue);
63      }
64  
65      /**
66       * Constructor that wraps (not copies).
67       *
68       * @param queue  the queue to decorate, must not be null
69       * @throws NullPointerException if queue is null
70       */
71      @SuppressWarnings("unchecked") // safe to upcast
72      private UnmodifiableQueue(final Queue<? extends E> queue) {
73          super((Queue<E>) queue);
74      }
75  
76      @Override
77      public boolean add(final Object object) {
78          throw new UnsupportedOperationException();
79      }
80  
81      @Override
82      public boolean addAll(final Collection<? extends E> coll) {
83          throw new UnsupportedOperationException();
84      }
85  
86      @Override
87      public void clear() {
88          throw new UnsupportedOperationException();
89      }
90  
91      @Override
92      public Iterator<E> iterator() {
93          return UnmodifiableIterator.unmodifiableIterator(decorated().iterator());
94      }
95  
96      @Override
97      public boolean offer(final E obj) {
98          throw new UnsupportedOperationException();
99      }
100 
101     @Override
102     public E poll() {
103         throw new UnsupportedOperationException();
104     }
105 
106     /**
107      * Read the collection in using a custom routine.
108      *
109      * @param in  the input stream
110      * @throws IOException if an I/O error occurs while reading from the input stream
111      * @throws ClassNotFoundException if the class of a serialized object can not be found
112      */
113     @SuppressWarnings("unchecked")
114     private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException {
115         in.defaultReadObject();
116         setCollection((Collection<E>) in.readObject());
117     }
118 
119     @Override
120     public E remove() {
121         throw new UnsupportedOperationException();
122     }
123 
124     @Override
125     public boolean remove(final Object object) {
126         throw new UnsupportedOperationException();
127     }
128 
129     @Override
130     public boolean removeAll(final Collection<?> coll) {
131         throw new UnsupportedOperationException();
132     }
133 
134     /**
135      * @since 4.4
136      */
137     @Override
138     public boolean removeIf(final Predicate<? super E> filter) {
139         throw new UnsupportedOperationException();
140     }
141 
142     @Override
143     public boolean retainAll(final Collection<?> coll) {
144         throw new UnsupportedOperationException();
145     }
146 
147     /**
148      * Write the collection out using a custom routine.
149      *
150      * @param out  the output stream
151      * @throws IOException if an I/O error occurs while writing to the output stream
152      */
153     private void writeObject(final ObjectOutputStream out) throws IOException {
154         out.defaultWriteObject();
155         out.writeObject(decorated());
156     }
157 
158 }