1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.collections.set;
18
19 import java.io.IOException;
20 import java.io.ObjectInputStream;
21 import java.io.ObjectOutputStream;
22 import java.io.Serializable;
23 import java.util.Collection;
24 import java.util.Iterator;
25 import java.util.SortedSet;
26
27 import org.apache.commons.collections.Unmodifiable;
28 import org.apache.commons.collections.iterators.UnmodifiableIterator;
29
30
31
32
33
34
35
36
37
38
39
40 public final class UnmodifiableSortedSet<E>
41 extends AbstractSortedSetDecorator<E>
42 implements Unmodifiable, Serializable {
43
44
45 private static final long serialVersionUID = -725356885467962424L;
46
47
48
49
50
51
52
53
54
55 public static <E> SortedSet<E> unmodifiableSortedSet(final SortedSet<E> set) {
56 if (set instanceof Unmodifiable) {
57 return set;
58 }
59 return new UnmodifiableSortedSet<E>(set);
60 }
61
62
63
64
65
66
67
68
69 private void writeObject(final ObjectOutputStream out) throws IOException {
70 out.defaultWriteObject();
71 out.writeObject(collection);
72 }
73
74
75
76
77
78
79
80
81 @SuppressWarnings("unchecked")
82 private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException {
83 in.defaultReadObject();
84 collection = (Collection<E>) in.readObject();
85 }
86
87
88
89
90
91
92
93
94 private UnmodifiableSortedSet(final SortedSet<E> set) {
95 super(set);
96 }
97
98
99 @Override
100 public Iterator<E> iterator() {
101 return UnmodifiableIterator.unmodifiableIterator(decorated().iterator());
102 }
103
104 @Override
105 public boolean add(final E object) {
106 throw new UnsupportedOperationException();
107 }
108
109 @Override
110 public boolean addAll(final Collection<? extends E> coll) {
111 throw new UnsupportedOperationException();
112 }
113
114 @Override
115 public void clear() {
116 throw new UnsupportedOperationException();
117 }
118
119 @Override
120 public boolean remove(final Object object) {
121 throw new UnsupportedOperationException();
122 }
123
124 @Override
125 public boolean removeAll(final Collection<?> coll) {
126 throw new UnsupportedOperationException();
127 }
128
129 @Override
130 public boolean retainAll(final Collection<?> coll) {
131 throw new UnsupportedOperationException();
132 }
133
134
135 @Override
136 public SortedSet<E> subSet(final E fromElement, final E toElement) {
137 final SortedSet<E> sub = decorated().subSet(fromElement, toElement);
138 return new UnmodifiableSortedSet<E>(sub);
139 }
140
141 @Override
142 public SortedSet<E> headSet(final E toElement) {
143 final SortedSet<E> sub = decorated().headSet(toElement);
144 return new UnmodifiableSortedSet<E>(sub);
145 }
146
147 @Override
148 public SortedSet<E> tailSet(final E fromElement) {
149 final SortedSet<E> sub = decorated().tailSet(fromElement);
150 return new UnmodifiableSortedSet<E>(sub);
151 }
152
153 }