View Javadoc

1   /*
2    * @(#) EventProducingCollection.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  
12  import nl.tudelft.simulation.event.EventProducer;
13  import nl.tudelft.simulation.event.EventType;
14  
15  /***
16   * The Event producing collection provides a set to which one can subscribe
17   * interest in entry changes. This class does not keep track of changes which
18   * take place indirectly. One is for example not notified on
19   * <code>map.iterator.remove()</code>. A listener must subscribe to the
20   * iterator individually.
21   * <p>
22   * (c) copyright 2002-2005 <a href="http://www.simulation.tudelft.nl">Delft
23   * University of Technology </a>, the Netherlands.
24   * <p>
25   * See for project information <a
26   * href="http://www.simulation.tudelft.nl/dsol/event">www.simulation.tudelft.nl/event
27   * </a> <br>
28   * License of use: <a href="http://www.gnu.org/copyleft/lesser.html">Lesser
29   * General Public License (LGPL) </a>, no warranty
30   * 
31   * @author <a href="http://www.peter-jacobs.com">Peter Jacobs </a>
32   * @version $Revision: 1.5 $ $Date: 2005/07/04 12:23:01 $
33   * @since 1.5
34   * @param <T> The type t od the eventproducing list.
35   */
36  public class EventProducingCollection<T> extends EventProducer implements
37          Collection<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 collection */
48      private Collection<T> parent = null;
49  
50      /***
51       * constructs a new EventProducingList.
52       * 
53       * @param parent the parent collection.
54       */
55      public EventProducingCollection(final Collection<T> parent)
56      {
57          super();
58          this.parent = parent;
59      }
60  
61      /***
62       * @see java.util.Collection#size()
63       */
64      public int size()
65      {
66          return this.parent.size();
67      }
68  
69      /***
70       * @see java.util.Collection#isEmpty()
71       */
72      public boolean isEmpty()
73      {
74          return this.parent.isEmpty();
75      }
76  
77      /***
78       * @see java.util.Collection#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.Collection#toArray(java.lang.Object[])
170      */
171     public <T> T[] toArray(final T[] a)
172     {
173         return this.parent.toArray(a);
174     }
175 }