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.SortedSet;
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
40
41
42 public final class UnmodifiableSortedSet<E>
43 extends AbstractSortedSetDecorator<E>
44 implements Unmodifiable {
45
46
47 private static final long serialVersionUID = -725356885467962424L;
48
49
50
51
52
53
54
55
56
57
58 public static <E> SortedSet<E> unmodifiableSortedSet(final SortedSet<E> set) {
59 if (set instanceof Unmodifiable) {
60 return set;
61 }
62 return new UnmodifiableSortedSet<>(set);
63 }
64
65
66
67
68
69
70
71 private UnmodifiableSortedSet(final SortedSet<E> set) {
72 super(set);
73 }
74
75 @Override
76 public boolean add(final E object) {
77 throw new UnsupportedOperationException();
78 }
79
80 @Override
81 public boolean addAll(final Collection<? extends E> coll) {
82 throw new UnsupportedOperationException();
83 }
84
85 @Override
86 public void clear() {
87 throw new UnsupportedOperationException();
88 }
89
90 @Override
91 public SortedSet<E> headSet(final E toElement) {
92 final SortedSet<E> head = decorated().headSet(toElement);
93 return unmodifiableSortedSet(head);
94 }
95
96 @Override
97 public Iterator<E> iterator() {
98 return UnmodifiableIterator.unmodifiableIterator(decorated().iterator());
99 }
100
101
102
103
104
105
106
107
108 @SuppressWarnings("unchecked")
109 private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException {
110 in.defaultReadObject();
111 setCollection((Collection<E>) in.readObject());
112 }
113
114 @Override
115 public boolean remove(final Object object) {
116 throw new UnsupportedOperationException();
117 }
118
119 @Override
120 public boolean removeAll(final Collection<?> coll) {
121 throw new UnsupportedOperationException();
122 }
123
124
125
126
127 @Override
128 public boolean removeIf(final Predicate<? super E> filter) {
129 throw new UnsupportedOperationException();
130 }
131
132 @Override
133 public boolean retainAll(final Collection<?> coll) {
134 throw new UnsupportedOperationException();
135 }
136
137 @Override
138 public SortedSet<E> subSet(final E fromElement, final E toElement) {
139 final SortedSet<E> sub = decorated().subSet(fromElement, toElement);
140 return unmodifiableSortedSet(sub);
141 }
142
143 @Override
144 public SortedSet<E> tailSet(final E fromElement) {
145 final SortedSet<E> tail = decorated().tailSet(fromElement);
146 return unmodifiableSortedSet(tail);
147 }
148
149
150
151
152
153
154
155 private void writeObject(final ObjectOutputStream out) throws IOException {
156 out.defaultWriteObject();
157 out.writeObject(decorated());
158 }
159
160 }