1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.collections4;
18
19 import java.util.Collection;
20 import java.util.Map;
21 import java.util.Objects;
22 import java.util.Set;
23
24 import org.apache.commons.collections4.collection.UnmodifiableCollection;
25 import org.apache.commons.collections4.iterators.UnmodifiableMapIterator;
26 import org.apache.commons.collections4.map.EntrySetToMapIteratorAdapter;
27 import org.apache.commons.collections4.map.UnmodifiableEntrySet;
28 import org.apache.commons.collections4.set.UnmodifiableSet;
29
30
31
32
33
34
35
36
37
38 public class SplitMapUtils {
39
40 private static final class WrappedGet<K, V> implements IterableMap<K, V>, Unmodifiable {
41 private final Get<K, V> get;
42
43 private WrappedGet(final Get<K, V> get) {
44 this.get = get;
45 }
46
47 @Override
48 public void clear() {
49 throw new UnsupportedOperationException();
50 }
51
52 @Override
53 public boolean containsKey(final Object key) {
54 return get.containsKey(key);
55 }
56
57 @Override
58 public boolean containsValue(final Object value) {
59 return get.containsValue(value);
60 }
61
62 @Override
63 public Set<Map.Entry<K, V>> entrySet() {
64 return UnmodifiableEntrySet.unmodifiableEntrySet(get.entrySet());
65 }
66
67 @Override
68 public boolean equals(final Object arg0) {
69 if (arg0 == this) {
70 return true;
71 }
72 return arg0 instanceof WrappedGet && ((WrappedGet<?, ?>) arg0).get.equals(get);
73 }
74
75 @Override
76 public V get(final Object key) {
77 return get.get(key);
78 }
79
80 @Override
81 public int hashCode() {
82 return "WrappedGet".hashCode() << 4 | get.hashCode();
83 }
84
85 @Override
86 public boolean isEmpty() {
87 return get.isEmpty();
88 }
89
90 @Override
91 public Set<K> keySet() {
92 return UnmodifiableSet.unmodifiableSet(get.keySet());
93 }
94
95 @Override
96 public MapIterator<K, V> mapIterator() {
97 final MapIterator<K, V> it;
98 if (get instanceof IterableGet) {
99 it = ((IterableGet<K, V>) get).mapIterator();
100 } else {
101 it = new EntrySetToMapIteratorAdapter<>(get.entrySet());
102 }
103 return UnmodifiableMapIterator.unmodifiableMapIterator(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> t) {
113 throw new UnsupportedOperationException();
114 }
115
116 @Override
117 public V remove(final Object key) {
118 return get.remove(key);
119 }
120
121 @Override
122 public int size() {
123 return get.size();
124 }
125
126 @Override
127 public Collection<V> values() {
128 return UnmodifiableCollection.unmodifiableCollection(get.values());
129 }
130 }
131
132 private static final class WrappedPut<K, V> implements Map<K, V>, Put<K, V> {
133 private final Put<K, V> put;
134
135 private WrappedPut(final Put<K, V> put) {
136 this.put = put;
137 }
138
139 @Override
140 public void clear() {
141 put.clear();
142 }
143
144 @Override
145 public boolean containsKey(final Object key) {
146 throw new UnsupportedOperationException();
147 }
148
149 @Override
150 public boolean containsValue(final Object value) {
151 throw new UnsupportedOperationException();
152 }
153
154 @Override
155 public Set<Map.Entry<K, V>> entrySet() {
156 throw new UnsupportedOperationException();
157 }
158
159 @Override
160 public boolean equals(final Object obj) {
161 if (obj == this) {
162 return true;
163 }
164 return obj instanceof WrappedPut && ((WrappedPut<?, ?>) obj).put.equals(put);
165 }
166
167 @Override
168 public V get(final Object key) {
169 throw new UnsupportedOperationException();
170 }
171
172 @Override
173 public int hashCode() {
174 return "WrappedPut".hashCode() << 4 | put.hashCode();
175 }
176
177 @Override
178 public boolean isEmpty() {
179 throw new UnsupportedOperationException();
180 }
181
182 @Override
183 public Set<K> keySet() {
184 throw new UnsupportedOperationException();
185 }
186
187 @Override
188 @SuppressWarnings("unchecked")
189 public V put(final K key, final V value) {
190 return (V) put.put(key, value);
191 }
192
193 @Override
194 public void putAll(final Map<? extends K, ? extends V> t) {
195 put.putAll(t);
196 }
197
198 @Override
199 public V remove(final Object key) {
200 throw new UnsupportedOperationException();
201 }
202
203 @Override
204 public int size() {
205 throw new UnsupportedOperationException();
206 }
207
208 @Override
209 public Collection<V> values() {
210 throw new UnsupportedOperationException();
211 }
212 }
213
214
215
216
217
218
219
220
221
222
223
224
225
226 @SuppressWarnings("unchecked")
227 public static <K, V> IterableMap<K, V> readableMap(final Get<K, V> get) {
228 Objects.requireNonNull(get, "get");
229 if (get instanceof Map) {
230 return get instanceof IterableMap ?
231 (IterableMap<K, V>) get :
232 MapUtils.iterableMap((Map<K, V>) get);
233 }
234 return new WrappedGet<>(get);
235 }
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250 @SuppressWarnings("unchecked")
251 public static <K, V> Map<K, V> writableMap(final Put<K, V> put) {
252 Objects.requireNonNull(put, "put");
253 if (put instanceof Map) {
254 return (Map<K, V>) put;
255 }
256 return new WrappedPut<>(put);
257 }
258
259
260
261
262 private SplitMapUtils() {
263
264 }
265
266 }