1
2
3
4
5
6
7 package nl.tudelft.simulation.event.util;
8
9 import java.util.Collection;
10 import java.util.Map;
11 import java.util.Set;
12
13 import nl.tudelft.simulation.event.EventProducer;
14 import nl.tudelft.simulation.event.EventType;
15
16 /***
17 * The Event producing map provides a map to which one can subscribe interest in
18 * entry changes. This class does not keep track of changes which take place
19 * indirectly. One is for example not notified on
20 * <code>map.iterator.remove()</code>. A listener must subscribe to the
21 * iterator, key set, etc. individually.
22 * <p>
23 * (c) copyright 2002-2005 <a href="http://www.simulation.tudelft.nl">Delft
24 * University of Technology </a>, the Netherlands.
25 * <p>
26 * See for project information <a
27 * href="http://www.simulation.tudelft.nl/dsol/event">www.simulation.tudelft.nl/event
28 * </a> <br>
29 * License of use: <a href="http://www.gnu.org/copyleft/lesser.html">Lesser
30 * General Public License (LGPL) </a>, no warranty
31 *
32 * @author <a href="http://www.peter-jacobs.com">Peter Jacobs </a>
33 * @version $Revision: 1.5 $ $Date: 2005/07/04 12:23:01 $
34 * @since 1.5
35 * @param <K> the key
36 * @param <V> the key
37 */
38 public class EventProducingMap<K, V> extends EventProducer implements Map<K, V>
39 {
40 /*** OBJECT_ADDED_EVENT is fired on new entries */
41 public static final EventType OBJECT_ADDED_EVENT = new EventType(
42 "OBJECT_ADDED_EVENT");
43
44 /*** OBJECT_REMOVED_EVENT is fired on removel of entries */
45 public static final EventType OBJECT_REMOVED_EVENT = new EventType(
46 "OBJECT_REMOVED_EVENT");
47
48 /*** the parent map */
49 private Map<K, V> parent = null;
50
51 /***
52 * constructs a new EventProducingMap.
53 *
54 * @param parent the parent map.
55 */
56 public EventProducingMap(final Map<K, V> parent)
57 {
58 super();
59 this.parent = parent;
60 }
61
62 /***
63 * @see java.util.Map#size()
64 */
65 public int size()
66 {
67 return this.parent.size();
68 }
69
70 /***
71 * @see java.util.Map#isEmpty()
72 */
73 public boolean isEmpty()
74 {
75 return this.parent.isEmpty();
76 }
77
78 /***
79 * @see java.util.Map#containsKey(java.lang.Object)
80 */
81 public boolean containsKey(final Object arg0)
82 {
83 return this.parent.containsKey(arg0);
84 }
85
86 /***
87 * @see java.util.Map#containsValue(java.lang.Object)
88 */
89 public boolean containsValue(final Object arg0)
90 {
91 return this.parent.containsValue(arg0);
92 }
93
94 /***
95 * @see java.util.Map#get(java.lang.Object)
96 */
97 public V get(final Object arg0)
98 {
99 return this.parent.get(arg0);
100 }
101
102 /***
103 * @see java.util.Map#put(java.lang.Object, java.lang.Object)
104 */
105 public V put(final K arg0, final V arg1)
106 {
107 V result = this.parent.put(arg0, arg1);
108 this.fireEvent(OBJECT_ADDED_EVENT, null);
109 return result;
110 }
111
112 /***
113 * @see java.util.Map#remove(java.lang.Object)
114 */
115 public V remove(final Object arg0)
116 {
117 V result = this.parent.remove(arg0);
118 this.fireEvent(OBJECT_REMOVED_EVENT, null);
119 return result;
120 }
121
122 /***
123 * @see java.util.Map#putAll(java.util.Map)
124 */
125 public void putAll(final Map< ? extends K, ? extends V> arg0)
126 {
127 this.parent.putAll(arg0);
128 this.fireEvent(OBJECT_ADDED_EVENT, null);
129 }
130
131 /***
132 * @see java.util.Map#clear()
133 */
134 public void clear()
135 {
136 this.parent.clear();
137 this.fireEvent(OBJECT_REMOVED_EVENT, null);
138 }
139
140 /***
141 * @see java.util.Map#keySet()
142 */
143 public Set<K> keySet()
144 {
145 return this.parent.keySet();
146 }
147
148 /***
149 * @see java.util.Map#values()
150 */
151 public Collection<V> values()
152 {
153 return this.parent.values();
154 }
155
156 /***
157 * @see java.util.Map#entrySet()
158 */
159 public Set<Map.Entry<K, V>> entrySet()
160 {
161 return this.parent.entrySet();
162 }
163 }