View Javadoc

1   /*
2    * @(#) EventProducingSet.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.Set;
15  
16  import nl.tudelft.simulation.event.EventProducer;
17  import nl.tudelft.simulation.event.EventType;
18  
19  /***
20   * The Event producing set provides a set to which one can subscribe interest in
21   * entry changes. This class does not keep track of changes which take place
22   * indirectly. One is for example not notified on
23   * <code>map.iterator.remove()</code>. A listener must subscribe to the
24   * iterator individually.
25   * <p>
26   * (c) copyright 2003-2004 <a href="http://www.simulation.tudelft.nl">Delft
27   * University of Technology </a>, the Netherlands. <br>
28   * See for project information <a
29   * href="http://www.simulation.tudelft.nl">www.simulation.tudelft.nl </a> <br>
30   * License of use: <a href="http://www.gnu.org/copyleft/gpl.html">General Public
31   * License (GPL) </a>, no warranty <br>
32   * 
33   * @author <a href="http://www.simulation.tudelft.nl/people/jacobs.html">Peter
34   *         Jacobs </a>
35   * @version 1.1, 2004-03-18
36   * @since 1.2
37   */
38  public class EventProducingSet extends EventProducer implements Set
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 set */
49  	private Set parent = null;
50  
51  	/***
52  	 * constructs a new EventProducingList.
53  	 * 
54  	 * @param parent the parent set.
55  	 */
56  	public EventProducingSet(final Set parent)
57  	{
58  		super();
59  		this.parent = parent;
60  	}
61  
62  	/***
63  	 * @see java.util.Set#size()
64  	 */
65  	public int size()
66  	{
67  		return this.parent.size();
68  	}
69  
70  	/***
71  	 * @see java.util.Set#isEmpty()
72  	 */
73  	public boolean isEmpty()
74  	{
75  		return this.parent.isEmpty();
76  	}
77  
78  	/***
79  	 * @see java.util.Set#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 }