1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
32
33
34
35
36
37
38
39 public final class UnmodifiableQueue<E>
40 extends AbstractQueueDecorator<E>
41 implements Unmodifiable {
42
43
44 private static final long serialVersionUID = 1832948656215393357L;
45
46
47
48
49
50
51
52
53
54
55
56 public static <E> Queue<E> unmodifiableQueue(final Queue<? extends E> queue) {
57 if (queue instanceof Unmodifiable) {
58 @SuppressWarnings("unchecked")
59 final Queue<E> tmpQueue = (Queue<E>) queue;
60 return tmpQueue;
61 }
62 return new UnmodifiableQueue<>(queue);
63 }
64
65
66
67
68
69
70
71 @SuppressWarnings("unchecked")
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
108
109
110
111
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
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
149
150
151
152
153 private void writeObject(final ObjectOutputStream out) throws IOException {
154 out.defaultWriteObject();
155 out.writeObject(decorated());
156 }
157
158 }