1 package nl.tudelft.goal.ut2004.visualizer.util;
2
3 import java.util.Collection;
4 import java.util.Collections;
5 import java.util.HashMap;
6 import java.util.Map;
7 import java.util.Set;
8
9 import cz.cuni.amis.utils.listener.Listeners;
10
11 public class ObservableMap<K, V> implements Map<K, V> {
12
13 protected Listeners<MapEventListener> eventListeners = new Listeners<MapEventListener>();
14
15 protected final Map<K, V> map = new HashMap<K, V>();
16
17 @Override
18 public int size() {
19 return map.size();
20 }
21
22 @Override
23 public boolean isEmpty() {
24 return map.isEmpty();
25 }
26
27 @Override
28 public boolean containsKey(Object key) {
29 return map.containsKey(key);
30 }
31
32 @Override
33 public boolean containsValue(Object value) {
34 return map.containsKey(value);
35 }
36
37 @Override
38 public V get(Object key) {
39 return map.get(key);
40 }
41
42 @Override
43 public V put(K key, V value) {
44 Map<K, V> put = Collections.singletonMap(key, value);
45 notifyPrePut(put);
46 V ret = map.put(key, value);
47 notifyPostPut(put);
48 return ret;
49 }
50
51
52 @Override
53 public V remove(Object key) {
54
55 V value = map.get(key);
56 Map<Object, V> rem = Collections.singletonMap(key, value);
57 notifyPreRemove(rem);
58 value = map.remove(key);
59 notifyPostRemove(rem);
60 return value;
61 }
62
63
64
65 @Override
66 public void putAll(Map<? extends K, ? extends V> m) {
67 notifyPrePut(m);
68 map.putAll(m);
69 notifyPostPut(m);
70
71 }
72
73 @Override
74 public void clear() {
75 Map<Object, V> copy = new HashMap<Object, V>(map);
76 notifyPreRemove(copy);
77 map.clear();
78 notifyPostRemove(copy);
79 }
80
81 @Override
82 public Set<K> keySet() {
83 return map.keySet();
84 }
85
86 @Override
87 public Collection<V> values() {
88 return map.values();
89 }
90
91 @Override
92 public Set<Entry<K, V>> entrySet() {
93 return map.entrySet();
94 }
95
96 protected synchronized void notifyPostPut(Map<? extends K, ? extends V> m) {
97
98
99 }
100
101 protected synchronized void notifyPrePut(Map<? extends K, ? extends V> m) {
102
103
104 }
105 protected synchronized void notifyPostRemove(Map<Object, V> rem) {
106
107
108 }
109
110 protected synchronized void notifyPreRemove(Map<Object, V> rem) {
111
112
113 }
114
115 }