1 package org.djutils.event.util;
2
3 import java.io.Serializable;
4 import java.util.Collection;
5 import java.util.Map;
6 import java.util.Set;
7
8 import org.djutils.event.EventProducer;
9 import org.djutils.event.EventType;
10 import org.djutils.event.IdProvider;
11 import org.djutils.exceptions.Throw;
12 import org.djutils.metadata.MetaData;
13 import org.djutils.metadata.ObjectDescriptor;
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32 public class EventProducingMap<K, V> extends EventProducer implements Map<K, V>
33 {
34
35 private static final long serialVersionUID = 20191230L;
36
37
38 public static final EventType OBJECT_ADDED_EVENT =
39 new EventType("OBJECT_ADDED_EVENT", new MetaData("Size of the map after add", "Size of the map",
40 new ObjectDescriptor("Size of the map after add", "Size of the map", Integer.class)));
41
42
43 public static final EventType OBJECT_REMOVED_EVENT =
44 new EventType("OBJECT_REMOVED_EVENT", new MetaData("Size of the map after remove", "Size of the map",
45 new ObjectDescriptor("Size of the map after remove", "Size of the map", Integer.class)));
46
47
48 public static final EventType OBJECT_CHANGED_EVENT =
49 new EventType("OBJECT_CHANGED_EVENT", new MetaData("Size of the map after change", "Size of the map",
50 new ObjectDescriptor("Size of the map after change", "Size of the map", Integer.class)));
51
52
53 private final Map<K, V> parent;
54
55
56 private final IdProvider sourceIdProvider;
57
58
59
60
61
62
63 public EventProducingMap(final Map<K, V> parent, final Serializable sourceId)
64 {
65 this(parent, new IdProvider()
66 {
67
68 private static final long serialVersionUID = 20200119L;
69
70 @Override
71 public Serializable id()
72 {
73 return sourceId;
74 }
75 });
76 }
77
78
79
80
81
82
83
84 public EventProducingMap(final Map<K, V> parent, final IdProvider sourceIdProvider)
85 {
86 Throw.whenNull(parent, "parent cannot be null");
87 Throw.whenNull(sourceIdProvider, "sourceIdprovider cannot be null");
88 this.parent = parent;
89 this.sourceIdProvider = sourceIdProvider;
90 }
91
92
93 @Override
94 public Serializable getSourceId()
95 {
96 return this.sourceIdProvider.id();
97 }
98
99
100 @Override
101 public int size()
102 {
103 return this.parent.size();
104 }
105
106
107 @Override
108 public boolean isEmpty()
109 {
110 return this.parent.isEmpty();
111 }
112
113
114 @Override
115 public boolean containsKey(final Object key)
116 {
117 return this.parent.containsKey(key);
118 }
119
120
121 @Override
122 public boolean containsValue(final Object value)
123 {
124 return this.parent.containsValue(value);
125 }
126
127
128 @Override
129 public V get(final Object key)
130 {
131 return this.parent.get(key);
132 }
133
134
135 @Override
136 public V put(final K key, final V value)
137 {
138 int nr = this.parent.size();
139 V result = this.parent.put(key, value);
140 if (nr != this.parent.size())
141 {
142 this.fireEvent(OBJECT_ADDED_EVENT, this.parent.size());
143 }
144 else
145 {
146 this.fireEvent(OBJECT_CHANGED_EVENT, this.parent.size());
147 }
148 return result;
149 }
150
151
152 @Override
153 public V remove(final Object key)
154 {
155 int nr = this.parent.size();
156 V result = this.parent.remove(key);
157 if (nr != this.parent.size())
158 {
159 this.fireEvent(OBJECT_REMOVED_EVENT, this.parent.size());
160 }
161 return result;
162 }
163
164
165 @Override
166 public void putAll(final Map<? extends K, ? extends V> map)
167 {
168 int nr = this.parent.size();
169 this.parent.putAll(map);
170 if (nr != this.parent.size())
171 {
172 this.fireEvent(OBJECT_ADDED_EVENT, this.parent.size());
173 }
174 else
175 {
176 if (!map.isEmpty())
177 {
178 this.fireEvent(OBJECT_CHANGED_EVENT, this.parent.size());
179 }
180 }
181 }
182
183
184 @Override
185 public void clear()
186 {
187 int nr = this.parent.size();
188 this.parent.clear();
189 if (nr != this.parent.size())
190 {
191 this.fireEvent(OBJECT_REMOVED_EVENT, this.parent.size());
192 }
193 }
194
195
196 @Override
197 public Set<K> keySet()
198 {
199 return this.parent.keySet();
200 }
201
202
203 @Override
204 public Collection<V> values()
205 {
206 return this.parent.values();
207 }
208
209
210 @Override
211 public Set<Map.Entry<K, V>> entrySet()
212 {
213 return this.parent.entrySet();
214 }
215 }