View Javadoc

1   /*
2    * @(#) EventProducingList.java Nov 19, 2003
3    * 
4    * Copyright (c) 2003 Delft University of Technology Jaffalaan 5, 2628 BX Delft,
5    * the Netherlands All rights reserved.
6    * 
7    * This software is proprietary information of Delft University of Technology
8    * The code is published under the General Public License
9    */
10  package nl.tudelft.simulation.event.util;
11  
12  import java.util.Collection;
13  import java.util.Iterator;
14  import java.util.List;
15  import java.util.ListIterator;
16  
17  import nl.tudelft.simulation.event.EventProducer;
18  import nl.tudelft.simulation.event.EventType;
19  
20  /***
21   * The Event producing list provides a list to which one can subscribe interest
22   * in entry changes. This class does not keep track of changes which take place
23   * indirectly. One is for example not notified on
24   * <code>map.iterator.remove()</code>. A listener must subscribe to the
25   * iterator individually.
26   * <p>
27   * (c) copyright 2003-2004 <a href="http://www.simulation.tudelft.nl">Delft
28   * University of Technology </a>, the Netherlands. <br>
29   * See for project information <a
30   * href="http://www.simulation.tudelft.nl">www.simulation.tudelft.nl </a> <br>
31   * License of use: <a href="http://www.gnu.org/copyleft/gpl.html">General Public
32   * License (GPL) </a>, no warranty <br>
33   * 
34   * @author <a href="http://www.simulation.tudelft.nl/people/jacobs.html">Peter
35   *         Jacobs </a>
36   * @version 1.1, 2004-03-18
37   * @since 1.2
38   */
39  public class EventProducingList extends EventProducer implements List
40  {
41  	/*** OBJECT_ADDED_EVENT is fired on new entries */
42  	public static final EventType OBJECT_ADDED_EVENT = new EventType(
43  			"OBJECT_ADDED_EVENT");
44  
45  	/*** OBJECT_REMOVED_EVENT is fired on removel of entries */
46  	public static final EventType OBJECT_REMOVED_EVENT = new EventType(
47  			"OBJECT_REMOVED_EVENT");
48  
49  	/*** the parent list */
50  	private List parent = null;
51  
52  	/***
53  	 * constructs a new EventProducingList.
54  	 * 
55  	 * @param parent the parent list.
56  	 */
57  	public EventProducingList(final List parent)
58  	{
59  		super();
60  		this.parent = parent;
61  	}
62  
63  	/***
64  	 * @see java.util.List#size()
65  	 */
66  	public int size()
67  	{
68  		return this.parent.size();
69  	}
70  
71  	/***
72  	 * @see java.util.List#isEmpty()
73  	 */
74  	public boolean isEmpty()
75  	{
76  		return this.parent.isEmpty();
77  	}
78  
79  	/***
80  	 * @see java.util.List#clear()
81  	 */
82  	public void clear()
83  	{
84  		this.parent.clear();
85  		this.fireEvent(OBJECT_REMOVED_EVENT, null);
86  	}
87  
88  	/***
89  	 * @see java.util.List#add(int, java.lang.Object)
90  	 */
91  	public void add(final int index, final Object element)
92  	{
93  		this.parent.add(index, element);
94  		this.fireEvent(OBJECT_ADDED_EVENT, null);
95  	}
96  
97  	/***
98  	 * @see java.util.Collection#add(java.lang.Object)
99  	 */
100 	public boolean add(final Object o)
101 	{
102 		boolean result = this.parent.add(o);
103 		this.fireEvent(OBJECT_ADDED_EVENT, null);
104 		return result;
105 	}
106 
107 	/***
108 	 * @see java.util.Collection#addAll(java.util.Collection)
109 	 */
110 	public boolean addAll(final Collection c)
111 	{
112 		boolean result = this.parent.addAll(c);
113 		this.fireEvent(OBJECT_ADDED_EVENT, null);
114 		return result;
115 	}
116 
117 	/***
118 	 * @see java.util.List#addAll(int, java.util.Collection)
119 	 */
120 	public boolean addAll(final int index, final Collection c)
121 	{
122 		boolean result = this.parent.addAll(index, c);
123 		this.fireEvent(OBJECT_ADDED_EVENT, null);
124 		return result;
125 	}
126 
127 	/***
128 	 * @see java.util.Collection#contains(java.lang.Object)
129 	 */
130 	public boolean contains(final Object o)
131 	{
132 		return this.parent.contains(o);
133 	}
134 
135 	/***
136 	 * @see java.util.Collection#containsAll(java.util.Collection)
137 	 */
138 	public boolean containsAll(final Collection c)
139 	{
140 		return this.parent.containsAll(c);
141 	}
142 
143 	/***
144 	 * @see java.util.List#get(int)
145 	 */
146 	public Object get(final int index)
147 	{
148 		return this.parent.get(index);
149 	}
150 
151 	/***
152 	 * @see java.util.List#indexOf(java.lang.Object)
153 	 */
154 	public int indexOf(final Object o)
155 	{
156 		return this.parent.indexOf(o);
157 	}
158 
159 	/***
160 	 * @see java.util.Collection#iterator()
161 	 */
162 	public Iterator iterator()
163 	{
164 		return new EventIterator(this.parent.iterator());
165 	}
166 
167 	/***
168 	 * @see java.util.List#lastIndexOf(java.lang.Object)
169 	 */
170 	public int lastIndexOf(final Object o)
171 	{
172 		return this.parent.lastIndexOf(o);
173 	}
174 
175 	/***
176 	 * @see java.util.List#listIterator()
177 	 */
178 	public ListIterator listIterator()
179 	{
180 		return this.parent.listIterator();
181 	}
182 
183 	/***
184 	 * @see java.util.List#listIterator(int)
185 	 */
186 	public ListIterator listIterator(final int index)
187 	{
188 		return this.parent.listIterator(index);
189 	}
190 
191 	/***
192 	 * @see java.util.List#remove(int)
193 	 */
194 	public Object remove(final int index)
195 	{
196 		Object result = this.parent.remove(index);
197 		this.fireEvent(OBJECT_REMOVED_EVENT, null);
198 		return result;
199 	}
200 
201 	/***
202 	 * @see java.util.Collection#remove(java.lang.Object)
203 	 */
204 	public boolean remove(final Object o)
205 	{
206 		boolean result = this.parent.remove(o);
207 		this.fireEvent(OBJECT_REMOVED_EVENT, null);
208 		return result;
209 	}
210 
211 	/***
212 	 * @see java.util.Collection#removeAll(java.util.Collection)
213 	 */
214 	public boolean removeAll(final Collection c)
215 	{
216 		boolean result = this.parent.removeAll(c);
217 		this.fireEvent(OBJECT_REMOVED_EVENT, null);
218 		return result;
219 	}
220 
221 	/***
222 	 * @see java.util.Collection#retainAll(java.util.Collection)
223 	 */
224 	public boolean retainAll(final Collection c)
225 	{
226 		boolean result = this.parent.retainAll(c);
227 		this.fireEvent(OBJECT_REMOVED_EVENT, null);
228 		return result;
229 	}
230 
231 	/***
232 	 * @see java.util.List#set(int, java.lang.Object)
233 	 */
234 	public Object set(final int index, final Object element)
235 	{
236 		Object result = this.parent.set(index, element);
237 		this.fireEvent(OBJECT_REMOVED_EVENT, null);
238 		return result;
239 	}
240 
241 	/***
242 	 * @see java.util.List#subList(int, int)
243 	 */
244 	public List subList(final int fromIndex, final int toIndex)
245 	{
246 		return this.parent.subList(fromIndex, toIndex);
247 	}
248 
249 	/***
250 	 * @see java.util.Collection#toArray()
251 	 */
252 	public Object[] toArray()
253 	{
254 		return this.parent.toArray();
255 	}
256 
257 	/***
258 	 * @see java.util.Collection#toArray(java.lang.Object[])
259 	 */
260 	public Object[] toArray(final Object[] a)
261 	{
262 		return this.parent.toArray(a);
263 	}
264 }