1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.collections4.set;
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.NavigableSet;
25 import java.util.SortedSet;
26 import java.util.function.Predicate;
27
28 import org.apache.commons.collections4.Unmodifiable;
29 import org.apache.commons.collections4.iterators.UnmodifiableIterator;
30
31
32
33
34
35
36
37
38
39
40 public final class UnmodifiableNavigableSet<E>
41 extends AbstractNavigableSetDecorator<E>
42 implements Unmodifiable {
43
44
45 private static final long serialVersionUID = 20150528L;
46
47
48
49
50
51
52
53
54
55 public static <E> NavigableSet<E> unmodifiableNavigableSet(final NavigableSet<E> set) {
56 if (set instanceof Unmodifiable) {
57 return set;
58 }
59 return new UnmodifiableNavigableSet<>(set);
60 }
61
62
63
64
65
66
67
68 private UnmodifiableNavigableSet(final NavigableSet<E> set) {
69 super(set);
70 }
71
72
73
74
75
76
77
78 @Override
79 public boolean add(final E object) {
80 throw new UnsupportedOperationException();
81 }
82
83
84
85
86
87
88
89 @Override
90 public boolean addAll(final Collection<? extends E> coll) {
91 throw new UnsupportedOperationException();
92 }
93
94
95
96
97
98
99 @Override
100 public void clear() {
101 throw new UnsupportedOperationException();
102 }
103
104 @Override
105 public Iterator<E> descendingIterator() {
106 return UnmodifiableIterator.unmodifiableIterator(decorated().descendingIterator());
107 }
108
109
110 @Override
111 public NavigableSet<E> descendingSet() {
112 return unmodifiableNavigableSet(decorated().descendingSet());
113 }
114
115 @Override
116 public SortedSet<E> headSet(final E toElement) {
117 return UnmodifiableSortedSet.unmodifiableSortedSet(decorated().headSet(toElement));
118 }
119
120 @Override
121 public NavigableSet<E> headSet(final E toElement, final boolean inclusive) {
122 return unmodifiableNavigableSet(decorated().headSet(toElement, inclusive));
123 }
124
125 @Override
126 public Iterator<E> iterator() {
127 return UnmodifiableIterator.unmodifiableIterator(decorated().iterator());
128 }
129
130
131
132
133
134
135
136 @Override
137 public E pollFirst() {
138 throw new UnsupportedOperationException();
139 }
140
141
142
143
144
145
146
147 @Override
148 public E pollLast() {
149 throw new UnsupportedOperationException();
150 }
151
152
153
154
155
156
157
158
159 @SuppressWarnings("unchecked")
160 private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException {
161 in.defaultReadObject();
162 setCollection((Collection<E>) in.readObject());
163 }
164
165
166
167
168
169
170
171 @Override
172 public boolean remove(final Object object) {
173 throw new UnsupportedOperationException();
174 }
175
176
177
178
179
180
181
182 @Override
183 public boolean removeAll(final Collection<?> coll) {
184 throw new UnsupportedOperationException();
185 }
186
187
188
189
190
191
192
193
194 @Override
195 public boolean removeIf(final Predicate<? super E> filter) {
196 throw new UnsupportedOperationException();
197 }
198
199
200
201
202
203
204
205 @Override
206 public boolean retainAll(final Collection<?> coll) {
207 throw new UnsupportedOperationException();
208 }
209
210 @Override
211 public NavigableSet<E> subSet(final E fromElement, final boolean fromInclusive, final E toElement,
212 final boolean toInclusive) {
213 return unmodifiableNavigableSet(decorated().subSet(fromElement, fromInclusive, toElement, toInclusive));
214 }
215
216
217 @Override
218 public SortedSet<E> subSet(final E fromElement, final E toElement) {
219 return UnmodifiableSortedSet.unmodifiableSortedSet(decorated().subSet(fromElement, toElement));
220 }
221
222 @Override
223 public SortedSet<E> tailSet(final E fromElement) {
224 return UnmodifiableSortedSet.unmodifiableSortedSet(decorated().tailSet(fromElement));
225 }
226
227 @Override
228 public NavigableSet<E> tailSet(final E fromElement, final boolean inclusive) {
229 return unmodifiableNavigableSet(decorated().tailSet(fromElement, inclusive));
230 }
231
232
233
234
235
236
237
238 private void writeObject(final ObjectOutputStream out) throws IOException {
239 out.defaultWriteObject();
240 out.writeObject(decorated());
241 }
242
243 }