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
79 @SuppressWarnings("unchecked")
80 private UnmodifiableOrderedMap(final OrderedMap<? extends K, ? extends V> map) {
81 super((OrderedMap<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 OrderedMapIterator<K, V> mapIterator() {
114 final OrderedMapIterator<K, V> it = decorated().mapIterator();
115 return UnmodifiableOrderedMapIterator.unmodifiableOrderedMapIterator(it);
116 }
117
118 @Override
119 public void clear() {
120 throw new UnsupportedOperationException();
121 }
122
123 @Override
124 public V put(final K key, final V value) {
125 throw new UnsupportedOperationException();
126 }
127
128 @Override
129 public void putAll(final Map<? extends K, ? extends V> mapToCopy) {
130 throw new UnsupportedOperationException();
131 }
132
133 @Override
134 public V remove(final Object key) {
135 throw new UnsupportedOperationException();
136 }
137
138 @Override
139 public Set<Map.Entry<K, V>> entrySet() {
140 final Set<Map.Entry<K, V>> set = super.entrySet();
141 return UnmodifiableEntrySet.unmodifiableEntrySet(set);
142 }
143
144 @Override
145 public Set<K> keySet() {
146 final Set<K> set = super.keySet();
147 return UnmodifiableSet.unmodifiableSet(set);
148 }
149
150 @Override
151 public Collection<V> values() {
152 final Collection<V> coll = super.values();
153 return UnmodifiableCollection.unmodifiableCollection(coll);
154 }
155
156 }