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