1 package org.djutils.event.collection;
2
3 import java.util.Collection;
4 import java.util.Map;
5 import java.util.Set;
6
7 import org.djutils.event.EventType;
8 import org.djutils.event.LocalEventProducer;
9 import org.djutils.exceptions.Throw;
10 import org.djutils.metadata.MetaData;
11 import org.djutils.metadata.ObjectDescriptor;
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30 public class EventProducingMap<K, V> extends LocalEventProducer implements Map<K, V>
31 {
32
33 public static final EventType OBJECT_ADDED_EVENT =
34 new EventType("OBJECT_ADDED_EVENT", new MetaData("Size of the map after add", "Size of the map",
35 new ObjectDescriptor("Size of the map after add", "Size of the map", Integer.class)));
36
37
38 public static final EventType OBJECT_REMOVED_EVENT =
39 new EventType("OBJECT_REMOVED_EVENT", new MetaData("Size of the map after remove", "Size of the map",
40 new ObjectDescriptor("Size of the map after remove", "Size of the map", Integer.class)));
41
42
43 public static final EventType OBJECT_CHANGED_EVENT =
44 new EventType("OBJECT_CHANGED_EVENT", new MetaData("Size of the map after change", "Size of the map",
45 new ObjectDescriptor("Size of the map after change", "Size of the map", Integer.class)));
46
47
48 private final Map<K, V> wrappedMap;
49
50
51
52
53
54 public EventProducingMap(final Map<K, V> wrappedMap)
55 {
56 Throw.whenNull(wrappedMap, "wrappedMap cannot be null");
57 this.wrappedMap = wrappedMap;
58 }
59
60 @Override
61 public int size()
62 {
63 return this.wrappedMap.size();
64 }
65
66 @Override
67 public boolean isEmpty()
68 {
69 return this.wrappedMap.isEmpty();
70 }
71
72 @Override
73 public boolean containsKey(final Object key)
74 {
75 return this.wrappedMap.containsKey(key);
76 }
77
78 @Override
79 public boolean containsValue(final Object value)
80 {
81 return this.wrappedMap.containsValue(value);
82 }
83
84 @Override
85 public V get(final Object key)
86 {
87 return this.wrappedMap.get(key);
88 }
89
90 @Override
91 public V put(final K key, final V value)
92 {
93 int nr = this.wrappedMap.size();
94 V result = this.wrappedMap.put(key, value);
95 if (nr != this.wrappedMap.size())
96 {
97 fireEvent(OBJECT_ADDED_EVENT, this.wrappedMap.size());
98 }
99 else
100 {
101 fireEvent(OBJECT_CHANGED_EVENT, this.wrappedMap.size());
102 }
103 return result;
104 }
105
106 @Override
107 public V remove(final Object key)
108 {
109 int nr = this.wrappedMap.size();
110 V result = this.wrappedMap.remove(key);
111 if (nr != this.wrappedMap.size())
112 {
113 fireEvent(OBJECT_REMOVED_EVENT, this.wrappedMap.size());
114 }
115 return result;
116 }
117
118 @Override
119 public void putAll(final Map<? extends K, ? extends V> map)
120 {
121 int nr = this.wrappedMap.size();
122 this.wrappedMap.putAll(map);
123 if (nr != this.wrappedMap.size())
124 {
125 fireEvent(OBJECT_ADDED_EVENT, this.wrappedMap.size());
126 }
127 else
128 {
129 if (!map.isEmpty())
130 {
131 fireEvent(OBJECT_CHANGED_EVENT, this.wrappedMap.size());
132 }
133 }
134 }
135
136 @Override
137 public void clear()
138 {
139 int nr = this.wrappedMap.size();
140 this.wrappedMap.clear();
141 if (nr != this.wrappedMap.size())
142 {
143 fireEvent(OBJECT_REMOVED_EVENT, this.wrappedMap.size());
144 }
145 }
146
147 @Override
148 public Set<K> keySet()
149 {
150 return this.wrappedMap.keySet();
151 }
152
153 @Override
154 public Collection<V> values()
155 {
156 return this.wrappedMap.values();
157 }
158
159 @Override
160 public Set<Map.Entry<K, V>> entrySet()
161 {
162 return this.wrappedMap.entrySet();
163 }
164
165 }