1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty.handler.codec.serialization;
17
18 import java.lang.ref.Reference;
19 import java.util.Collection;
20 import java.util.Map;
21 import java.util.Set;
22
23 abstract class ReferenceMap<K, V> implements Map<K, V> {
24
25 private final Map<K, Reference<V>> delegate;
26
27 protected ReferenceMap(Map<K, Reference<V>> delegate) {
28 this.delegate = delegate;
29 }
30
31 abstract Reference<V> fold(V value);
32
33 private V unfold(Reference<V> ref) {
34 if (ref == null) {
35 return null;
36 }
37
38 return ref.get();
39 }
40
41 @Override
42 public int size() {
43 return delegate.size();
44 }
45
46 @Override
47 public boolean isEmpty() {
48 return delegate.isEmpty();
49 }
50
51 @Override
52 public boolean containsKey(Object key) {
53 return delegate.containsKey(key);
54 }
55
56 @Override
57 public boolean containsValue(Object value) {
58 throw new UnsupportedOperationException();
59 }
60
61 @Override
62 public V get(Object key) {
63 return unfold(delegate.get(key));
64 }
65
66 @Override
67 public V put(K key, V value) {
68 return unfold(delegate.put(key, fold(value)));
69 }
70
71 @Override
72 public V remove(Object key) {
73 return unfold(delegate.remove(key));
74 }
75
76 @Override
77 public void putAll(Map<? extends K, ? extends V> m) {
78 for (Entry<? extends K, ? extends V> entry : m.entrySet()) {
79 delegate.put(entry.getKey(), fold(entry.getValue()));
80 }
81 }
82
83 @Override
84 public void clear() {
85 delegate.clear();
86 }
87
88 @Override
89 public Set<K> keySet() {
90 return delegate.keySet();
91 }
92
93 @Override
94 public Collection<V> values() {
95 throw new UnsupportedOperationException();
96 }
97
98 @Override
99 public Set<Entry<K, V>> entrySet() {
100 throw new UnsupportedOperationException();
101 }
102 }