1
2
3
4
5
6
7
8
9
10 package nl.tudelft.simulation.event.util;
11
12 import java.util.Collection;
13 import java.util.Iterator;
14
15 import nl.tudelft.simulation.event.EventProducer;
16 import nl.tudelft.simulation.event.EventType;
17
18 /***
19 * The Event producing collection provides a set to which one can subscribe
20 * interest in entry changes. This class does not keep track of changes which
21 * take place indirectly. One is for example not notified on
22 * <code>map.iterator.remove()</code>. A listener must subscribe to the
23 * iterator individually.
24 * <p>
25 * (c) copyright 2003-2004 <a href="http://www.simulation.tudelft.nl">Delft
26 * University of Technology </a>, the Netherlands. <br>
27 * See for project information <a
28 * href="http://www.simulation.tudelft.nl">www.simulation.tudelft.nl </a> <br>
29 * License of use: <a href="http://www.gnu.org/copyleft/gpl.html">General Public
30 * License (GPL) </a>, no warranty <br>
31 *
32 * @author <a href="http://www.simulation.tudelft.nl/people/jacobs.html">Peter
33 * Jacobs </a>
34 * @version 1.1, 2004-03-18
35 * @since 1.2
36 */
37 public class EventProducingCollection extends EventProducer implements
38 Collection
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 collection */
49 private Collection parent = null;
50
51 /***
52 * constructs a new EventProducingList.
53 *
54 * @param parent the parent collection.
55 */
56 public EventProducingCollection(final Collection parent)
57 {
58 super();
59 this.parent = parent;
60 }
61
62 /***
63 * @see java.util.Collection#size()
64 */
65 public int size()
66 {
67 return this.parent.size();
68 }
69
70 /***
71 * @see java.util.Collection#isEmpty()
72 */
73 public boolean isEmpty()
74 {
75 return this.parent.isEmpty();
76 }
77
78 /***
79 * @see java.util.Collection#clear()
80 */
81 public void clear()
82 {
83 this.parent.clear();
84 this.fireEvent(OBJECT_REMOVED_EVENT, null);
85 }
86
87 /***
88 * @see java.util.Collection#add(java.lang.Object)
89 */
90 public boolean add(final Object o)
91 {
92 boolean result = this.parent.add(o);
93 this.fireEvent(OBJECT_ADDED_EVENT, null);
94 return result;
95 }
96
97 /***
98 * @see java.util.Collection#addAll(java.util.Collection)
99 */
100 public boolean addAll(final Collection c)
101 {
102 boolean result = this.parent.addAll(c);
103 this.fireEvent(OBJECT_ADDED_EVENT, null);
104 return result;
105 }
106
107 /***
108 * @see java.util.Collection#contains(java.lang.Object)
109 */
110 public boolean contains(final Object o)
111 {
112 return this.parent.contains(o);
113 }
114
115 /***
116 * @see java.util.Collection#containsAll(java.util.Collection)
117 */
118 public boolean containsAll(final Collection c)
119 {
120 return this.parent.containsAll(c);
121 }
122
123 /***
124 * @see java.util.Collection#iterator()
125 */
126 public Iterator iterator()
127 {
128 return new EventIterator(this.parent.iterator());
129 }
130
131 /***
132 * @see java.util.Collection#remove(java.lang.Object)
133 */
134 public boolean remove(final Object o)
135 {
136 boolean result = this.parent.remove(o);
137 this.fireEvent(OBJECT_REMOVED_EVENT, null);
138 return result;
139 }
140
141 /***
142 * @see java.util.Collection#removeAll(java.util.Collection)
143 */
144 public boolean removeAll(final Collection c)
145 {
146 boolean result = this.parent.removeAll(c);
147 this.fireEvent(OBJECT_REMOVED_EVENT, null);
148 return result;
149 }
150
151 /***
152 * @see java.util.Collection#retainAll(java.util.Collection)
153 */
154 public boolean retainAll(final Collection c)
155 {
156 boolean result = this.parent.retainAll(c);
157 this.fireEvent(OBJECT_REMOVED_EVENT, null);
158 return result;
159 }
160
161 /***
162 * @see java.util.Collection#toArray()
163 */
164 public Object[] toArray()
165 {
166 return this.parent.toArray();
167 }
168
169 /***
170 * @see java.util.Collection#toArray(java.lang.Object[])
171 */
172 public Object[] toArray(final Object[] a)
173 {
174 return this.parent.toArray(a);
175 }
176 }