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    *      https://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      /**
77       * Always throws {@link UnsupportedOperationException}.
78       *
79       * @param object Ignored.
80       * @throws UnsupportedOperationException Always thrown.
81       */
82      @Override
83      public boolean add(final Object object) {
84          throw new UnsupportedOperationException();
85      }
86  
87      /**
88       * Always throws {@link UnsupportedOperationException}.
89       *
90       * @param coll Ignored.
91       * @throws UnsupportedOperationException Always thrown.
92       */
93      @Override
94      public boolean addAll(final Collection<? extends E> coll) {
95          throw new UnsupportedOperationException();
96      }
97  
98      /**
99       * Always throws {@link UnsupportedOperationException}.
100      *
101      * @throws UnsupportedOperationException Always thrown.
102      */
103     @Override
104     public void clear() {
105         throw new UnsupportedOperationException();
106     }
107 
108     @Override
109     public Iterator<E> iterator() {
110         return UnmodifiableIterator.unmodifiableIterator(decorated().iterator());
111     }
112 
113     /**
114      * Always throws {@link UnsupportedOperationException}.
115      *
116      * @param obj Ignored.
117      * @throws UnsupportedOperationException Always thrown.
118      */
119     @Override
120     public boolean offer(final E obj) {
121         throw new UnsupportedOperationException();
122     }
123 
124     /**
125      * Always throws {@link UnsupportedOperationException}.
126      *
127      * @throws UnsupportedOperationException Always thrown.
128      */
129     @Override
130     public E poll() {
131         throw new UnsupportedOperationException();
132     }
133 
134     /**
135      * Deserializes the collection in using a custom routine.
136      *
137      * @param in  The input stream
138      * @throws IOException Thrown if an I/O error occurs while reading from the input stream
139      * @throws ClassNotFoundException if the class of a serialized object cannot be found
140      */
141     @SuppressWarnings("unchecked")
142     private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException {
143         in.defaultReadObject();
144         setCollection((Collection<E>) in.readObject());
145     }
146 
147     /**
148      * Always throws {@link UnsupportedOperationException}.
149      *
150      * @throws UnsupportedOperationException Always thrown.
151      */
152     @Override
153     public E remove() {
154         throw new UnsupportedOperationException();
155     }
156 
157     /**
158      * Always throws {@link UnsupportedOperationException}.
159      *
160      * @param object Ignored.
161      * @throws UnsupportedOperationException Always thrown.
162      */
163     @Override
164     public boolean remove(final Object object) {
165         throw new UnsupportedOperationException();
166     }
167 
168     /**
169      * Always throws {@link UnsupportedOperationException}.
170      *
171      * @param coll Ignored.
172      * @throws UnsupportedOperationException Always thrown.
173      */
174     @Override
175     public boolean removeAll(final Collection<?> coll) {
176         throw new UnsupportedOperationException();
177     }
178 
179     /**
180      * Always throws {@link UnsupportedOperationException}.
181      *
182      * @param filter Ignored.
183      * @throws UnsupportedOperationException Always thrown.
184      * @since 4.4
185      */
186     @Override
187     public boolean removeIf(final Predicate<? super E> filter) {
188         throw new UnsupportedOperationException();
189     }
190 
191     /**
192      * Always throws {@link UnsupportedOperationException}.
193      *
194      * @param coll Ignored.
195      * @throws UnsupportedOperationException Always thrown.
196      */
197     @Override
198     public boolean retainAll(final Collection<?> coll) {
199         throw new UnsupportedOperationException();
200     }
201 
202     /**
203      * Serializes this object to an ObjectOutputStream.
204      *
205      * @param out The target ObjectOutputStream.
206      * @throws IOException thrown when an I/O errors occur writing to the target stream.
207      */
208     private void writeObject(final ObjectOutputStream out) throws IOException {
209         out.defaultWriteObject();
210         out.writeObject(decorated());
211     }
212 
213 }