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.Map;
25 import java.util.Set;
26
27 import org.apache.commons.collections4.IterableMap;
28 import org.apache.commons.collections4.MapIterator;
29 import org.apache.commons.collections4.Unmodifiable;
30 import org.apache.commons.collections4.collection.UnmodifiableCollection;
31 import org.apache.commons.collections4.iterators.EntrySetMapIterator;
32 import org.apache.commons.collections4.iterators.UnmodifiableMapIterator;
33 import org.apache.commons.collections4.set.UnmodifiableSet;
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48 public final class UnmodifiableMap<K, V>
49 extends AbstractMapDecorator<K, V>
50 implements Unmodifiable, Serializable {
51
52
53 private static final long serialVersionUID = 2737023427269031941L;
54
55
56
57
58
59
60
61
62
63
64
65 public static <K, V> Map<K, V> unmodifiableMap(final Map<? extends K, ? extends V> map) {
66 if (map instanceof Unmodifiable) {
67 @SuppressWarnings("unchecked")
68 final Map<K, V> tmpMap = (Map<K, V>) map;
69 return tmpMap;
70 }
71 return new UnmodifiableMap<>(map);
72 }
73
74
75
76
77
78
79
80 @SuppressWarnings("unchecked")
81 private UnmodifiableMap(final Map<? extends K, ? extends V> map) {
82 super((Map<K, V>) map);
83 }
84
85 @Override
86 public void clear() {
87 throw new UnsupportedOperationException();
88 }
89
90 @Override
91 public Set<Map.Entry<K, V>> entrySet() {
92 final Set<Map.Entry<K, V>> set = super.entrySet();
93 return UnmodifiableEntrySet.unmodifiableEntrySet(set);
94 }
95
96 @Override
97 public Set<K> keySet() {
98 final Set<K> set = super.keySet();
99 return UnmodifiableSet.unmodifiableSet(set);
100 }
101
102 @Override
103 public MapIterator<K, V> mapIterator() {
104 if (map instanceof IterableMap) {
105 final MapIterator<K, V> it = ((IterableMap<K, V>) map).mapIterator();
106 return UnmodifiableMapIterator.unmodifiableMapIterator(it);
107 }
108 final MapIterator<K, V> it = new EntrySetMapIterator<>(map);
109 return UnmodifiableMapIterator.unmodifiableMapIterator(it);
110 }
111
112 @Override
113 public V put(final K key, final V value) {
114 throw new UnsupportedOperationException();
115 }
116
117 @Override
118 public void putAll(final Map<? extends K, ? extends V> mapToCopy) {
119 throw new UnsupportedOperationException();
120 }
121
122
123
124
125
126
127
128
129
130 @SuppressWarnings("unchecked")
131 private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException {
132 in.defaultReadObject();
133 map = (Map<K, V>) in.readObject();
134 }
135
136 @Override
137 public V remove(final Object key) {
138 throw new UnsupportedOperationException();
139 }
140
141 @Override
142 public Collection<V> values() {
143 final Collection<V> coll = super.values();
144 return UnmodifiableCollection.unmodifiableCollection(coll);
145 }
146
147
148
149
150
151
152
153
154 private void writeObject(final ObjectOutputStream out) throws IOException {
155 out.defaultWriteObject();
156 out.writeObject(map);
157 }
158
159 }