1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.collections4.map;
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.Comparator;
25 import java.util.Map;
26 import java.util.Set;
27 import java.util.SortedMap;
28
29 import org.apache.commons.collections4.Unmodifiable;
30 import org.apache.commons.collections4.collection.UnmodifiableCollection;
31 import org.apache.commons.collections4.set.UnmodifiableSet;
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46 public final class UnmodifiableSortedMap<K, V>
47 extends AbstractSortedMapDecorator<K, V>
48 implements Unmodifiable, Serializable {
49
50
51 private static final long serialVersionUID = 5805344239827376360L;
52
53
54
55
56
57
58
59
60
61
62
63 public static <K, V> SortedMap<K, V> unmodifiableSortedMap(final SortedMap<K, ? extends V> map) {
64 if (map instanceof Unmodifiable) {
65 @SuppressWarnings("unchecked")
66 final SortedMap<K, V> tmpMap = (SortedMap<K, V>) map;
67 return tmpMap;
68 }
69 return new UnmodifiableSortedMap<>(map);
70 }
71
72
73
74
75
76
77
78
79 @SuppressWarnings("unchecked")
80 private UnmodifiableSortedMap(final SortedMap<K, ? extends V> map) {
81 super((SortedMap<K, V>) map);
82 }
83
84
85
86
87
88
89
90
91
92 private void writeObject(final ObjectOutputStream out) throws IOException {
93 out.defaultWriteObject();
94 out.writeObject(map);
95 }
96
97
98
99
100
101
102
103
104
105 @SuppressWarnings("unchecked")
106 private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException {
107 in.defaultReadObject();
108 map = (Map<K, V>) in.readObject();
109 }
110
111
112 @Override
113 public void clear() {
114 throw new UnsupportedOperationException();
115 }
116
117 @Override
118 public V put(final K key, final V value) {
119 throw new UnsupportedOperationException();
120 }
121
122 @Override
123 public void putAll(final Map<? extends K, ? extends V> mapToCopy) {
124 throw new UnsupportedOperationException();
125 }
126
127 @Override
128 public V remove(final Object key) {
129 throw new UnsupportedOperationException();
130 }
131
132 @Override
133 public Set<Map.Entry<K, V>> entrySet() {
134 return UnmodifiableEntrySet.unmodifiableEntrySet(super.entrySet());
135 }
136
137 @Override
138 public Set<K> keySet() {
139 return UnmodifiableSet.unmodifiableSet(super.keySet());
140 }
141
142 @Override
143 public Collection<V> values() {
144 return UnmodifiableCollection.unmodifiableCollection(super.values());
145 }
146
147
148 @Override
149 public K firstKey() {
150 return decorated().firstKey();
151 }
152
153 @Override
154 public K lastKey() {
155 return decorated().lastKey();
156 }
157
158 @Override
159 public Comparator<? super K> comparator() {
160 return decorated().comparator();
161 }
162
163 @Override
164 public SortedMap<K, V> subMap(final K fromKey, final K toKey) {
165 return new UnmodifiableSortedMap<>(decorated().subMap(fromKey, toKey));
166 }
167
168 @Override
169 public SortedMap<K, V> headMap(final K toKey) {
170 return new UnmodifiableSortedMap<>(decorated().headMap(toKey));
171 }
172
173 @Override
174 public SortedMap<K, V> tailMap(final K fromKey) {
175 return new UnmodifiableSortedMap<>(decorated().tailMap(fromKey));
176 }
177
178 }