OLD | NEW |
1 // Protocol Buffers - Google's data interchange format | 1 // Protocol Buffers - Google's data interchange format |
2 // Copyright 2008 Google Inc. All rights reserved. | 2 // Copyright 2008 Google Inc. All rights reserved. |
3 // https://developers.google.com/protocol-buffers/ | 3 // https://developers.google.com/protocol-buffers/ |
4 // | 4 // |
5 // Redistribution and use in source and binary forms, with or without | 5 // Redistribution and use in source and binary forms, with or without |
6 // modification, are permitted provided that the following conditions are | 6 // modification, are permitted provided that the following conditions are |
7 // met: | 7 // met: |
8 // | 8 // |
9 // * Redistributions of source code must retain the above copyright | 9 // * Redistributions of source code must retain the above copyright |
10 // notice, this list of conditions and the following disclaimer. | 10 // notice, this list of conditions and the following disclaimer. |
(...skipping 15 matching lines...) Expand all Loading... |
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
30 | 30 |
31 package com.google.protobuf; | 31 package com.google.protobuf; |
32 | 32 |
33 import com.google.protobuf.Internal.EnumLite; | 33 import com.google.protobuf.Internal.EnumLite; |
34 | 34 |
35 import java.util.Arrays; | 35 import java.util.Arrays; |
36 import java.util.Collection; | |
37 import java.util.Collections; | 36 import java.util.Collections; |
38 import java.util.Iterator; | |
39 import java.util.LinkedHashMap; | 37 import java.util.LinkedHashMap; |
40 import java.util.Map; | 38 import java.util.Map; |
41 import java.util.Set; | 39 import java.util.Set; |
42 | 40 |
43 /** | 41 /** |
44 * Internal representation of map fields in generated lite-runtime messages. | 42 * Internal representation of map fields in generated lite-runtime messages. |
45 * | 43 * |
46 * This class is a protobuf implementation detail. Users shouldn't use this | 44 * This class is a protobuf implementation detail. Users shouldn't use this |
47 * class directly. | 45 * class directly. |
48 */ | 46 */ |
49 public final class MapFieldLite<K, V> implements MutabilityOracle { | 47 public final class MapFieldLite<K, V> extends LinkedHashMap<K, V> { |
50 private MutatabilityAwareMap<K, V> mapData; | 48 |
51 private boolean isMutable; | 49 private boolean isMutable; |
52 | 50 |
53 private MapFieldLite(Map<K, V> mapData) { | 51 private MapFieldLite() { |
54 this.mapData = new MutatabilityAwareMap<K, V>(this, mapData); | |
55 this.isMutable = true; | 52 this.isMutable = true; |
56 } | 53 } |
57 | 54 |
| 55 private MapFieldLite(Map<K, V> mapData) { |
| 56 super(mapData); |
| 57 this.isMutable = true; |
| 58 } |
| 59 |
58 @SuppressWarnings({"rawtypes", "unchecked"}) | 60 @SuppressWarnings({"rawtypes", "unchecked"}) |
59 private static final MapFieldLite EMPTY_MAP_FIELD = | 61 private static final MapFieldLite EMPTY_MAP_FIELD = new MapFieldLite(); |
60 new MapFieldLite(Collections.emptyMap()); | |
61 static { | 62 static { |
62 EMPTY_MAP_FIELD.makeImmutable(); | 63 EMPTY_MAP_FIELD.makeImmutable(); |
63 } | 64 } |
64 | 65 |
65 /** Returns an singleton immutable empty MapFieldLite instance. */ | 66 /** Returns an singleton immutable empty MapFieldLite instance. */ |
66 @SuppressWarnings({"unchecked", "cast"}) | 67 @SuppressWarnings({"unchecked", "cast"}) |
67 public static <K, V> MapFieldLite<K, V> emptyMapField() { | 68 public static <K, V> MapFieldLite<K, V> emptyMapField() { |
68 return (MapFieldLite<K, V>) EMPTY_MAP_FIELD; | 69 return (MapFieldLite<K, V>) EMPTY_MAP_FIELD; |
69 } | 70 } |
70 | 71 |
71 /** Creates a new MapFieldLite instance. */ | 72 public void mergeFrom(MapFieldLite<K, V> other) { |
72 public static <K, V> MapFieldLite<K, V> newMapField() { | 73 ensureMutable(); |
73 return new MapFieldLite<K, V>(new LinkedHashMap<K, V>()); | 74 if (!other.isEmpty()) { |
| 75 putAll(other); |
| 76 } |
74 } | 77 } |
75 | 78 |
76 /** Gets the content of this MapField as a read-only Map. */ | 79 @SuppressWarnings({"unchecked", "cast"}) |
77 public Map<K, V> getMap() { | 80 @Override public Set<Map.Entry<K, V>> entrySet() { |
78 return Collections.unmodifiableMap(mapData); | 81 return isEmpty() ? Collections.<Map.Entry<K, V>>emptySet() : super.entrySet(
); |
79 } | 82 } |
80 | 83 |
81 /** Gets a mutable Map view of this MapField. */ | 84 @Override public void clear() { |
82 public Map<K, V> getMutableMap() { | 85 ensureMutable(); |
83 return mapData; | 86 clear(); |
84 } | 87 } |
85 | 88 |
86 public void mergeFrom(MapFieldLite<K, V> other) { | 89 @Override public V put(K key, V value) { |
87 mapData.putAll(copy(other.mapData)); | 90 ensureMutable(); |
| 91 return super.put(key, value); |
88 } | 92 } |
89 | 93 |
90 public void clear() { | 94 public V put(Map.Entry<K, V> entry) { |
91 mapData.clear(); | 95 return put(entry.getKey(), entry.getValue()); |
92 } | 96 } |
93 | 97 |
| 98 @Override public void putAll(Map<? extends K, ? extends V> m) { |
| 99 ensureMutable(); |
| 100 super.putAll(m); |
| 101 } |
| 102 |
| 103 @Override public V remove(Object key) { |
| 104 ensureMutable(); |
| 105 return super.remove(key); |
| 106 } |
| 107 |
94 private static boolean equals(Object a, Object b) { | 108 private static boolean equals(Object a, Object b) { |
95 if (a instanceof byte[] && b instanceof byte[]) { | 109 if (a instanceof byte[] && b instanceof byte[]) { |
96 return Arrays.equals((byte[]) a, (byte[]) b); | 110 return Arrays.equals((byte[]) a, (byte[]) b); |
97 } | 111 } |
98 return a.equals(b); | 112 return a.equals(b); |
99 } | 113 } |
100 | 114 |
101 /** | 115 /** |
102 * Checks whether two {@link Map}s are equal. We don't use the default equals | 116 * Checks whether two {@link Map}s are equal. We don't use the default equals |
103 * method of {@link Map} because it compares by identity not by content for | 117 * method of {@link Map} because it compares by identity not by content for |
104 * byte arrays. | 118 * byte arrays. |
105 */ | 119 */ |
106 static <K, V> boolean equals(Map<K, V> a, Map<K, V> b) { | 120 static <K, V> boolean equals(Map<K, V> a, Map<K, V> b) { |
107 if (a == b) { | 121 if (a == b) { |
108 return true; | 122 return true; |
109 } | 123 } |
110 if (a.size() != b.size()) { | 124 if (a.size() != b.size()) { |
111 return false; | 125 return false; |
112 } | 126 } |
113 for (Map.Entry<K, V> entry : a.entrySet()) { | 127 for (Map.Entry<K, V> entry : a.entrySet()) { |
114 if (!b.containsKey(entry.getKey())) { | 128 if (!b.containsKey(entry.getKey())) { |
115 return false; | 129 return false; |
116 } | 130 } |
117 if (!equals(entry.getValue(), b.get(entry.getKey()))) { | 131 if (!equals(entry.getValue(), b.get(entry.getKey()))) { |
118 return false; | 132 return false; |
119 } | 133 } |
120 } | 134 } |
121 return true; | 135 return true; |
122 } | 136 } |
123 | 137 |
124 /** | 138 /** |
125 * Checks whether two map fields are equal. | 139 * Checks whether two map fields are equal. |
126 */ | 140 */ |
127 @SuppressWarnings("unchecked") | 141 @SuppressWarnings("unchecked") |
128 @Override | 142 @Override |
129 public boolean equals(Object object) { | 143 public boolean equals(Object object) { |
130 if (!(object instanceof MapFieldLite)) { | 144 return (object instanceof Map) && equals(this, (Map<K, V>) object); |
131 return false; | |
132 } | |
133 MapFieldLite<K, V> other = (MapFieldLite<K, V>) object; | |
134 return equals(mapData, other.mapData); | |
135 } | 145 } |
136 | 146 |
137 private static int calculateHashCodeForObject(Object a) { | 147 private static int calculateHashCodeForObject(Object a) { |
138 if (a instanceof byte[]) { | 148 if (a instanceof byte[]) { |
139 return Internal.hashCode((byte[]) a); | 149 return Internal.hashCode((byte[]) a); |
140 } | 150 } |
141 // Enums should be stored as integers internally. | 151 // Enums should be stored as integers internally. |
142 if (a instanceof EnumLite) { | 152 if (a instanceof EnumLite) { |
143 throw new UnsupportedOperationException(); | 153 throw new UnsupportedOperationException(); |
144 } | 154 } |
145 return a.hashCode(); | 155 return a.hashCode(); |
146 } | 156 } |
147 | 157 |
148 /** | 158 /** |
149 * Calculates the hash code for a {@link Map}. We don't use the default hash | 159 * Calculates the hash code for a {@link Map}. We don't use the default hash |
150 * code method of {@link Map} because for byte arrays and protobuf enums it | 160 * code method of {@link Map} because for byte arrays and protobuf enums it |
151 * use {@link Object#hashCode()}. | 161 * use {@link Object#hashCode()}. |
152 */ | 162 */ |
153 static <K, V> int calculateHashCodeForMap(Map<K, V> a) { | 163 static <K, V> int calculateHashCodeForMap(Map<K, V> a) { |
154 int result = 0; | 164 int result = 0; |
155 for (Map.Entry<K, V> entry : a.entrySet()) { | 165 for (Map.Entry<K, V> entry : a.entrySet()) { |
156 result += calculateHashCodeForObject(entry.getKey()) | 166 result += calculateHashCodeForObject(entry.getKey()) |
157 ^ calculateHashCodeForObject(entry.getValue()); | 167 ^ calculateHashCodeForObject(entry.getValue()); |
158 } | 168 } |
159 return result; | 169 return result; |
160 } | 170 } |
161 | 171 |
162 @Override | 172 @Override |
163 public int hashCode() { | 173 public int hashCode() { |
164 return calculateHashCodeForMap(mapData); | 174 return calculateHashCodeForMap(this); |
165 } | 175 } |
166 | 176 |
167 private static Object copy(Object object) { | 177 private static Object copy(Object object) { |
168 if (object instanceof byte[]) { | 178 if (object instanceof byte[]) { |
169 byte[] data = (byte[]) object; | 179 byte[] data = (byte[]) object; |
170 return Arrays.copyOf(data, data.length); | 180 return Arrays.copyOf(data, data.length); |
171 } | 181 } |
172 return object; | 182 return object; |
173 } | 183 } |
174 | 184 |
175 /** | 185 /** |
176 * Makes a deep copy of a {@link Map}. Immutable objects in the map will be | 186 * Makes a deep copy of a {@link Map}. Immutable objects in the map will be |
177 * shared (e.g., integers, strings, immutable messages) and mutable ones will | 187 * shared (e.g., integers, strings, immutable messages) and mutable ones will |
178 * have a copy (e.g., byte arrays, mutable messages). | 188 * have a copy (e.g., byte arrays, mutable messages). |
179 */ | 189 */ |
180 @SuppressWarnings("unchecked") | 190 @SuppressWarnings("unchecked") |
181 static <K, V> Map<K, V> copy(Map<K, V> map) { | 191 static <K, V> Map<K, V> copy(Map<K, V> map) { |
182 Map<K, V> result = new LinkedHashMap<K, V>(); | 192 Map<K, V> result = new LinkedHashMap<K, V>(); |
183 for (Map.Entry<K, V> entry : map.entrySet()) { | 193 for (Map.Entry<K, V> entry : map.entrySet()) { |
184 result.put(entry.getKey(), (V) copy(entry.getValue())); | 194 result.put(entry.getKey(), (V) copy(entry.getValue())); |
185 } | 195 } |
186 return result; | 196 return result; |
187 } | 197 } |
188 | 198 |
189 /** Returns a deep copy of this map field. */ | 199 /** Returns a deep copy of this map field. */ |
190 public MapFieldLite<K, V> copy() { | 200 public MapFieldLite<K, V> mutableCopy() { |
191 return new MapFieldLite<K, V>(copy(mapData)); | 201 return isEmpty() ? new MapFieldLite<K, V>() : new MapFieldLite<K, V>(this); |
192 } | 202 } |
193 | 203 |
194 /** | 204 /** |
195 * Makes this field immutable. All subsequent modifications will throw an | 205 * Makes this field immutable. All subsequent modifications will throw an |
196 * {@link UnsupportedOperationException}. | 206 * {@link UnsupportedOperationException}. |
197 */ | 207 */ |
198 public void makeImmutable() { | 208 public void makeImmutable() { |
199 isMutable = false; | 209 isMutable = false; |
200 } | 210 } |
201 | 211 |
202 /** | 212 /** |
203 * Returns whether this field can be modified. | 213 * Returns whether this field can be modified. |
204 */ | 214 */ |
205 public boolean isMutable() { | 215 public boolean isMutable() { |
206 return isMutable; | 216 return isMutable; |
207 } | 217 } |
208 | 218 |
209 @Override | 219 private void ensureMutable() { |
210 public void ensureMutable() { | |
211 if (!isMutable()) { | 220 if (!isMutable()) { |
212 throw new UnsupportedOperationException(); | 221 throw new UnsupportedOperationException(); |
213 } | 222 } |
214 } | 223 } |
215 | |
216 /** | |
217 * An internal map that checks for mutability before delegating. | |
218 */ | |
219 static class MutatabilityAwareMap<K, V> implements Map<K, V> { | |
220 private final MutabilityOracle mutabilityOracle; | |
221 private final Map<K, V> delegate; | |
222 | |
223 MutatabilityAwareMap(MutabilityOracle mutabilityOracle, Map<K, V> delegate)
{ | |
224 this.mutabilityOracle = mutabilityOracle; | |
225 this.delegate = delegate; | |
226 } | |
227 | |
228 @Override | |
229 public int size() { | |
230 return delegate.size(); | |
231 } | |
232 | |
233 @Override | |
234 public boolean isEmpty() { | |
235 return delegate.isEmpty(); | |
236 } | |
237 | |
238 @Override | |
239 public boolean containsKey(Object key) { | |
240 return delegate.containsKey(key); | |
241 } | |
242 | |
243 @Override | |
244 public boolean containsValue(Object value) { | |
245 return delegate.containsValue(value); | |
246 } | |
247 | |
248 @Override | |
249 public V get(Object key) { | |
250 return delegate.get(key); | |
251 } | |
252 | |
253 @Override | |
254 public V put(K key, V value) { | |
255 mutabilityOracle.ensureMutable(); | |
256 return delegate.put(key, value); | |
257 } | |
258 | |
259 @Override | |
260 public V remove(Object key) { | |
261 mutabilityOracle.ensureMutable(); | |
262 return delegate.remove(key); | |
263 } | |
264 | |
265 @Override | |
266 public void putAll(Map<? extends K, ? extends V> m) { | |
267 mutabilityOracle.ensureMutable(); | |
268 delegate.putAll(m); | |
269 } | |
270 | |
271 @Override | |
272 public void clear() { | |
273 mutabilityOracle.ensureMutable(); | |
274 delegate.clear(); | |
275 } | |
276 | |
277 @Override | |
278 public Set<K> keySet() { | |
279 return new MutatabilityAwareSet<K>(mutabilityOracle, delegate.keySet()); | |
280 } | |
281 | |
282 @Override | |
283 public Collection<V> values() { | |
284 return new MutatabilityAwareCollection<V>(mutabilityOracle, delegate.value
s()); | |
285 } | |
286 | |
287 @Override | |
288 public Set<java.util.Map.Entry<K, V>> entrySet() { | |
289 return new MutatabilityAwareSet<Entry<K, V>>(mutabilityOracle, delegate.en
trySet()); | |
290 } | |
291 | |
292 @Override | |
293 public boolean equals(Object o) { | |
294 return delegate.equals(o); | |
295 } | |
296 | |
297 @Override | |
298 public int hashCode() { | |
299 return delegate.hashCode(); | |
300 } | |
301 | |
302 @Override | |
303 public String toString() { | |
304 return delegate.toString(); | |
305 } | |
306 } | |
307 | |
308 /** | |
309 * An internal collection that checks for mutability before delegating. | |
310 */ | |
311 private static class MutatabilityAwareCollection<E> implements Collection<E> { | |
312 private final MutabilityOracle mutabilityOracle; | |
313 private final Collection<E> delegate; | |
314 | |
315 MutatabilityAwareCollection(MutabilityOracle mutabilityOracle, Collection<E>
delegate) { | |
316 this.mutabilityOracle = mutabilityOracle; | |
317 this.delegate = delegate; | |
318 } | |
319 | |
320 @Override | |
321 public int size() { | |
322 return delegate.size(); | |
323 } | |
324 | |
325 @Override | |
326 public boolean isEmpty() { | |
327 return delegate.isEmpty(); | |
328 } | |
329 | |
330 @Override | |
331 public boolean contains(Object o) { | |
332 return delegate.contains(o); | |
333 } | |
334 | |
335 @Override | |
336 public Iterator<E> iterator() { | |
337 return new MutatabilityAwareIterator<E>(mutabilityOracle, delegate.iterato
r()); | |
338 } | |
339 | |
340 @Override | |
341 public Object[] toArray() { | |
342 return delegate.toArray(); | |
343 } | |
344 | |
345 @Override | |
346 public <T> T[] toArray(T[] a) { | |
347 return delegate.toArray(a); | |
348 } | |
349 | |
350 @Override | |
351 public boolean add(E e) { | |
352 // Unsupported operation in the delegate. | |
353 throw new UnsupportedOperationException(); | |
354 } | |
355 | |
356 @Override | |
357 public boolean remove(Object o) { | |
358 mutabilityOracle.ensureMutable(); | |
359 return delegate.remove(o); | |
360 } | |
361 | |
362 @Override | |
363 public boolean containsAll(Collection<?> c) { | |
364 return delegate.containsAll(c); | |
365 } | |
366 | |
367 @Override | |
368 public boolean addAll(Collection<? extends E> c) { | |
369 // Unsupported operation in the delegate. | |
370 throw new UnsupportedOperationException(); | |
371 } | |
372 | |
373 @Override | |
374 public boolean removeAll(Collection<?> c) { | |
375 mutabilityOracle.ensureMutable(); | |
376 return delegate.removeAll(c); | |
377 } | |
378 | |
379 @Override | |
380 public boolean retainAll(Collection<?> c) { | |
381 mutabilityOracle.ensureMutable(); | |
382 return delegate.retainAll(c); | |
383 } | |
384 | |
385 @Override | |
386 public void clear() { | |
387 mutabilityOracle.ensureMutable(); | |
388 delegate.clear(); | |
389 } | |
390 | |
391 @Override | |
392 public boolean equals(Object o) { | |
393 return delegate.equals(o); | |
394 } | |
395 | |
396 @Override | |
397 public int hashCode() { | |
398 return delegate.hashCode(); | |
399 } | |
400 | |
401 @Override | |
402 public String toString() { | |
403 return delegate.toString(); | |
404 } | |
405 } | |
406 | |
407 /** | |
408 * An internal set that checks for mutability before delegating. | |
409 */ | |
410 private static class MutatabilityAwareSet<E> implements Set<E> { | |
411 private final MutabilityOracle mutabilityOracle; | |
412 private final Set<E> delegate; | |
413 | |
414 MutatabilityAwareSet(MutabilityOracle mutabilityOracle, Set<E> delegate) { | |
415 this.mutabilityOracle = mutabilityOracle; | |
416 this.delegate = delegate; | |
417 } | |
418 | |
419 @Override | |
420 public int size() { | |
421 return delegate.size(); | |
422 } | |
423 | |
424 @Override | |
425 public boolean isEmpty() { | |
426 return delegate.isEmpty(); | |
427 } | |
428 | |
429 @Override | |
430 public boolean contains(Object o) { | |
431 return delegate.contains(o); | |
432 } | |
433 | |
434 @Override | |
435 public Iterator<E> iterator() { | |
436 return new MutatabilityAwareIterator<E>(mutabilityOracle, delegate.iterato
r()); | |
437 } | |
438 | |
439 @Override | |
440 public Object[] toArray() { | |
441 return delegate.toArray(); | |
442 } | |
443 | |
444 @Override | |
445 public <T> T[] toArray(T[] a) { | |
446 return delegate.toArray(a); | |
447 } | |
448 | |
449 @Override | |
450 public boolean add(E e) { | |
451 mutabilityOracle.ensureMutable(); | |
452 return delegate.add(e); | |
453 } | |
454 | |
455 @Override | |
456 public boolean remove(Object o) { | |
457 mutabilityOracle.ensureMutable(); | |
458 return delegate.remove(o); | |
459 } | |
460 | |
461 @Override | |
462 public boolean containsAll(Collection<?> c) { | |
463 return delegate.containsAll(c); | |
464 } | |
465 | |
466 @Override | |
467 public boolean addAll(Collection<? extends E> c) { | |
468 mutabilityOracle.ensureMutable(); | |
469 return delegate.addAll(c); | |
470 } | |
471 | |
472 @Override | |
473 public boolean retainAll(Collection<?> c) { | |
474 mutabilityOracle.ensureMutable(); | |
475 return delegate.retainAll(c); | |
476 } | |
477 | |
478 @Override | |
479 public boolean removeAll(Collection<?> c) { | |
480 mutabilityOracle.ensureMutable(); | |
481 return delegate.removeAll(c); | |
482 } | |
483 | |
484 @Override | |
485 public void clear() { | |
486 mutabilityOracle.ensureMutable(); | |
487 delegate.clear(); | |
488 } | |
489 | |
490 @Override | |
491 public boolean equals(Object o) { | |
492 return delegate.equals(o); | |
493 } | |
494 | |
495 @Override | |
496 public int hashCode() { | |
497 return delegate.hashCode(); | |
498 } | |
499 | |
500 @Override | |
501 public String toString() { | |
502 return delegate.toString(); | |
503 } | |
504 } | |
505 | |
506 /** | |
507 * An internal iterator that checks for mutability before delegating. | |
508 */ | |
509 private static class MutatabilityAwareIterator<E> implements Iterator<E> { | |
510 private final MutabilityOracle mutabilityOracle; | |
511 private final Iterator<E> delegate; | |
512 | |
513 MutatabilityAwareIterator(MutabilityOracle mutabilityOracle, Iterator<E> del
egate) { | |
514 this.mutabilityOracle = mutabilityOracle; | |
515 this.delegate = delegate; | |
516 } | |
517 | |
518 @Override | |
519 public boolean hasNext() { | |
520 return delegate.hasNext(); | |
521 } | |
522 | |
523 @Override | |
524 public E next() { | |
525 return delegate.next(); | |
526 } | |
527 | |
528 @Override | |
529 public void remove() { | |
530 mutabilityOracle.ensureMutable(); | |
531 delegate.remove(); | |
532 } | |
533 | |
534 @Override | |
535 public boolean equals(Object obj) { | |
536 return delegate.equals(obj); | |
537 } | |
538 | |
539 @Override | |
540 public int hashCode() { | |
541 return delegate.hashCode(); | |
542 } | |
543 | |
544 @Override | |
545 public String toString() { | |
546 return delegate.toString(); | |
547 } | |
548 } | |
549 } | 224 } |
OLD | NEW |