View Javadoc

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