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 @Override
73 public boolean add(final E object) {
74 throw new UnsupportedOperationException();
75 }
76
77 @Override
78 public boolean addAll(final Collection<? extends E> coll) {
79 throw new UnsupportedOperationException();
80 }
81
82 @Override
83 public void clear() {
84 throw new UnsupportedOperationException();
85 }
86
87 @Override
88 public Iterator<E> descendingIterator() {
89 return UnmodifiableIterator.unmodifiableIterator(decorated().descendingIterator());
90 }
91
92
93 @Override
94 public NavigableSet<E> descendingSet() {
95 return unmodifiableNavigableSet(decorated().descendingSet());
96 }
97
98 @Override
99 public SortedSet<E> headSet(final E toElement) {
100 final SortedSet<E> head = decorated().headSet(toElement);
101 return UnmodifiableSortedSet.unmodifiableSortedSet(head);
102 }
103
104 @Override
105 public NavigableSet<E> headSet(final E toElement, final boolean inclusive) {
106 final NavigableSet<E> head = decorated().headSet(toElement, inclusive);
107 return unmodifiableNavigableSet(head);
108 }
109
110 @Override
111 public Iterator<E> iterator() {
112 return UnmodifiableIterator.unmodifiableIterator(decorated().iterator());
113 }
114
115
116
117
118 @Override
119 public E pollFirst() {
120 throw new UnsupportedOperationException();
121 }
122
123
124
125
126 @Override
127 public E pollLast() {
128 throw new UnsupportedOperationException();
129 }
130
131
132
133
134
135
136
137
138 @SuppressWarnings("unchecked")
139 private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException {
140 in.defaultReadObject();
141 setCollection((Collection<E>) in.readObject());
142 }
143
144 @Override
145 public boolean remove(final Object object) {
146 throw new UnsupportedOperationException();
147 }
148
149 @Override
150 public boolean removeAll(final Collection<?> coll) {
151 throw new UnsupportedOperationException();
152 }
153
154
155
156
157 @Override
158 public boolean removeIf(final Predicate<? super E> filter) {
159 throw new UnsupportedOperationException();
160 }
161
162 @Override
163 public boolean retainAll(final Collection<?> coll) {
164 throw new UnsupportedOperationException();
165 }
166
167 @Override
168 public NavigableSet<E> subSet(final E fromElement, final boolean fromInclusive, final E toElement,
169 final boolean toInclusive) {
170 final NavigableSet<E> sub = decorated().subSet(fromElement, fromInclusive, toElement, toInclusive);
171 return unmodifiableNavigableSet(sub);
172 }
173
174
175 @Override
176 public SortedSet<E> subSet(final E fromElement, final E toElement) {
177 final SortedSet<E> sub = decorated().subSet(fromElement, toElement);
178 return UnmodifiableSortedSet.unmodifiableSortedSet(sub);
179 }
180
181 @Override
182 public SortedSet<E> tailSet(final E fromElement) {
183 final SortedSet<E> tail = decorated().tailSet(fromElement);
184 return UnmodifiableSortedSet.unmodifiableSortedSet(tail);
185 }
186
187 @Override
188 public NavigableSet<E> tailSet(final E fromElement, final boolean inclusive) {
189 final NavigableSet<E> tail = decorated().tailSet(fromElement, inclusive);
190 return unmodifiableNavigableSet(tail);
191 }
192
193
194
195
196
197
198
199 private void writeObject(final ObjectOutputStream out) throws IOException {
200 out.defaultWriteObject();
201 out.writeObject(decorated());
202 }
203
204 }