1
2
3
4
5
6
7 package nl.tudelft.simulation.event.util;
8
9 import java.util.Collection;
10 import java.util.Iterator;
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 set provides a set 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 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 <T> the type of the set
36 */
37 public class EventProducingSet<T> extends EventProducer implements Set<T>
38 {
39 /*** OBJECT_ADDED_EVENT is fired on new entries */
40 public static final EventType OBJECT_ADDED_EVENT = new EventType(
41 "OBJECT_ADDED_EVENT");
42
43 /*** OBJECT_REMOVED_EVENT is fired on removel of entries */
44 public static final EventType OBJECT_REMOVED_EVENT = new EventType(
45 "OBJECT_REMOVED_EVENT");
46
47 /*** the parent set */
48 private Set<T> parent = null;
49
50 /***
51 * constructs a new EventProducingList.
52 *
53 * @param parent the parent set.
54 */
55 public EventProducingSet(final Set<T> parent)
56 {
57 super();
58 this.parent = parent;
59 }
60
61 /***
62 * @see java.util.Set#size()
63 */
64 public int size()
65 {
66 return this.parent.size();
67 }
68
69 /***
70 * @see java.util.Set#isEmpty()
71 */
72 public boolean isEmpty()
73 {
74 return this.parent.isEmpty();
75 }
76
77 /***
78 * @see java.util.Set#clear()
79 */
80 public void clear()
81 {
82 this.parent.clear();
83 this.fireEvent(OBJECT_REMOVED_EVENT, null);
84 }
85
86 /***
87 * @see java.util.Collection#add(java.lang.Object)
88 */
89 public boolean add(final T o)
90 {
91 boolean result = this.parent.add(o);
92 this.fireEvent(OBJECT_ADDED_EVENT, null);
93 return result;
94 }
95
96 /***
97 * @see java.util.Collection#addAll(java.util.Collection)
98 */
99 public boolean addAll(final Collection< ? extends T> c)
100 {
101 boolean result = this.parent.addAll(c);
102 this.fireEvent(OBJECT_ADDED_EVENT, null);
103 return result;
104 }
105
106 /***
107 * @see java.util.Collection#contains(java.lang.Object)
108 */
109 public boolean contains(final Object o)
110 {
111 return this.parent.contains(o);
112 }
113
114 /***
115 * @see java.util.Collection#containsAll(java.util.Collection)
116 */
117 public boolean containsAll(final Collection c)
118 {
119 return this.parent.containsAll(c);
120 }
121
122 /***
123 * @see java.util.Collection#iterator()
124 */
125 public Iterator<T> iterator()
126 {
127 return new EventIterator<T>(this.parent.iterator());
128 }
129
130 /***
131 * @see java.util.Collection#remove(java.lang.Object)
132 */
133 public boolean remove(final Object o)
134 {
135 boolean result = this.parent.remove(o);
136 this.fireEvent(OBJECT_REMOVED_EVENT, null);
137 return result;
138 }
139
140 /***
141 * @see java.util.Collection#removeAll(java.util.Collection)
142 */
143 public boolean removeAll(final Collection c)
144 {
145 boolean result = this.parent.removeAll(c);
146 this.fireEvent(OBJECT_REMOVED_EVENT, null);
147 return result;
148 }
149
150 /***
151 * @see java.util.Collection#retainAll(java.util.Collection)
152 */
153 public boolean retainAll(final Collection c)
154 {
155 boolean result = this.parent.retainAll(c);
156 this.fireEvent(OBJECT_REMOVED_EVENT, null);
157 return result;
158 }
159
160 /***
161 * @see java.util.Collection#toArray()
162 */
163 public Object[] toArray()
164 {
165 return this.parent.toArray();
166 }
167
168 /***
169 * @see java.util.Set#toArray(T[])
170 */
171 public <T> T[] toArray(final T[] a)
172 {
173 return this.parent.toArray(a);
174 }
175 }