1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.jboss.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 public int size() {
42 return delegate.size();
43 }
44
45 public boolean isEmpty() {
46 return delegate.isEmpty();
47 }
48
49 public boolean containsKey(Object key) {
50 return delegate.containsKey(key);
51 }
52
53 public boolean containsValue(Object value) {
54 throw new UnsupportedOperationException();
55 }
56
57 public V get(Object key) {
58 return unfold(delegate.get(key));
59 }
60
61 public V put(K key, V value) {
62 return unfold(delegate.put(key, fold(value)));
63 }
64
65 public V remove(Object key) {
66 return unfold(delegate.remove(key));
67 }
68
69 public void putAll(Map<? extends K, ? extends V> m) {
70 for (Entry<? extends K, ? extends V> entry : m.entrySet()) {
71 delegate.put(entry.getKey(), fold(entry.getValue()));
72 }
73 }
74
75 public void clear() {
76 delegate.clear();
77 }
78
79 public Set<K> keySet() {
80 return delegate.keySet();
81 }
82
83 public Collection<V> values() {
84 throw new UnsupportedOperationException();
85 }
86
87 public Set<Entry<K, V>> entrySet() {
88 throw new UnsupportedOperationException();
89 }
90 }