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 @SuppressWarnings("unchecked")
79 private UnmodifiableSortedMap(final SortedMap<K, ? extends V> map) {
80 super((SortedMap<K, V>) map);
81 }
82
83 @Override
84 public void clear() {
85 throw new UnsupportedOperationException();
86 }
87
88 @Override
89 public Comparator<? super K> comparator() {
90 return decorated().comparator();
91 }
92
93 @Override
94 public Set<Map.Entry<K, V>> entrySet() {
95 return UnmodifiableEntrySet.unmodifiableEntrySet(super.entrySet());
96 }
97
98 @Override
99 public K firstKey() {
100 return decorated().firstKey();
101 }
102
103 @Override
104 public SortedMap<K, V> headMap(final K toKey) {
105 return new UnmodifiableSortedMap<>(decorated().headMap(toKey));
106 }
107
108 @Override
109 public Set<K> keySet() {
110 return UnmodifiableSet.unmodifiableSet(super.keySet());
111 }
112
113 @Override
114 public K lastKey() {
115 return decorated().lastKey();
116 }
117
118 @Override
119 public V put(final K key, final V value) {
120 throw new UnsupportedOperationException();
121 }
122
123 @Override
124 public void putAll(final Map<? extends K, ? extends V> mapToCopy) {
125 throw new UnsupportedOperationException();
126 }
127
128
129
130
131
132
133
134
135
136 @SuppressWarnings("unchecked")
137 private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException {
138 in.defaultReadObject();
139 map = (Map<K, V>) in.readObject();
140 }
141
142 @Override
143 public V remove(final Object key) {
144 throw new UnsupportedOperationException();
145 }
146
147 @Override
148 public SortedMap<K, V> subMap(final K fromKey, final K toKey) {
149 return new UnmodifiableSortedMap<>(decorated().subMap(fromKey, toKey));
150 }
151
152 @Override
153 public SortedMap<K, V> tailMap(final K fromKey) {
154 return new UnmodifiableSortedMap<>(decorated().tailMap(fromKey));
155 }
156
157 @Override
158 public Collection<V> values() {
159 return UnmodifiableCollection.unmodifiableCollection(super.values());
160 }
161
162
163
164
165
166
167
168
169 private void writeObject(final ObjectOutputStream out) throws IOException {
170 out.defaultWriteObject();
171 out.writeObject(map);
172 }
173
174 }