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
81 @SuppressWarnings("unchecked")
82 private UnmodifiableMap(final Map<? extends K, ? extends V> map) {
83 super((Map<K, V>) map);
84 }
85
86
87
88
89
90
91
92
93
94 private void writeObject(final ObjectOutputStream out) throws IOException {
95 out.defaultWriteObject();
96 out.writeObject(map);
97 }
98
99
100
101
102
103
104
105
106
107 @SuppressWarnings("unchecked")
108 private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException {
109 in.defaultReadObject();
110 map = (Map<K, V>) in.readObject();
111 }
112
113
114 @Override
115 public void clear() {
116 throw new UnsupportedOperationException();
117 }
118
119 @Override
120 public V put(final K key, final V value) {
121 throw new UnsupportedOperationException();
122 }
123
124 @Override
125 public void putAll(final Map<? extends K, ? extends V> mapToCopy) {
126 throw new UnsupportedOperationException();
127 }
128
129 @Override
130 public V remove(final Object key) {
131 throw new UnsupportedOperationException();
132 }
133
134 @Override
135 public MapIterator<K, V> mapIterator() {
136 if (map instanceof IterableMap) {
137 final MapIterator<K, V> it = ((IterableMap<K, V>) map).mapIterator();
138 return UnmodifiableMapIterator.unmodifiableMapIterator(it);
139 }
140 final MapIterator<K, V> it = new EntrySetMapIterator<>(map);
141 return UnmodifiableMapIterator.unmodifiableMapIterator(it);
142 }
143
144 @Override
145 public Set<Map.Entry<K, V>> entrySet() {
146 final Set<Map.Entry<K, V>> set = super.entrySet();
147 return UnmodifiableEntrySet.unmodifiableEntrySet(set);
148 }
149
150 @Override
151 public Set<K> keySet() {
152 final Set<K> set = super.keySet();
153 return UnmodifiableSet.unmodifiableSet(set);
154 }
155
156 @Override
157 public Collection<V> values() {
158 final Collection<V> coll = super.values();
159 return UnmodifiableCollection.unmodifiableCollection(coll);
160 }
161
162 }