View Javadoc

1   /*
2    * @(#) EventListenerMap.java Jan 13, 2005 Copyright (c) 2004 Delft University
3    * of Technology Jaffalaan 5, 2628 BX Delft, the Netherlands All rights
4    * reserved. This software is proprietary information of Delft University of
5    * Technology The code is published under the General Public License
6    */
7   package nl.tudelft.simulation.event;
8   
9   import java.io.IOException;
10  import java.io.ObjectOutputStream;
11  import java.io.Serializable;
12  import java.util.ArrayList;
13  import java.util.Arrays;
14  import java.util.Collection;
15  import java.util.HashMap;
16  import java.util.Iterator;
17  import java.util.Map;
18  import java.util.Set;
19  
20  import nl.tudelft.simulation.event.ref.Reference;
21  import nl.tudelft.simulation.event.remote.RemoteEventListenerInterface;
22  
23  /***
24   * The specifies
25   * <p>
26   * (c) copyright 2004 <a href="http://www.simulation.tudelft.nl/dsol/">Delft
27   * University of Technology </a>, the Netherlands. <br>
28   * See for project information <a href="http://www.simulation.tudelft.nl/dsol/">
29   * www.simulation.tudelft.nl/dsol </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.peter-jacobs.com/index.htm"> Peter Jacobs </a>
34   * @version $Revision: 1.3 $ $Date: 2005/07/04 12:23:01 $
35   * @since 1.5
36   * @param <K> the key type in this eventlist
37   * @param <V> the value type of this map
38   */
39  public class EventListenerMap<K, V> implements Map<K, V>, Serializable
40  {
41      /*** the hasMap we map on */
42      private Map<K, V> map = new HashMap<K, V>();
43  
44      /***
45       * constructs a new EventListenerMap
46       */
47      public EventListenerMap()
48      {
49          super();
50      }
51  
52      /***
53       * @see java.util.Map#size()
54       */
55      public int size()
56      {
57          return this.map.size();
58      }
59  
60      /***
61       * @see java.util.Map#clear()
62       */
63      public void clear()
64      {
65          this.map.clear();
66      }
67  
68      /***
69       * @see java.util.Map#isEmpty()
70       */
71      public boolean isEmpty()
72      {
73          return this.map.isEmpty();
74      }
75  
76      /***
77       * @see java.util.Map#containsKey(java.lang.Object)
78       */
79      public boolean containsKey(Object key)
80      {
81          return this.map.containsKey(key);
82      }
83  
84      /***
85       * @see java.util.Map#containsValue(java.lang.Object)
86       */
87      public boolean containsValue(Object value)
88      {
89          return this.map.containsValue(value);
90      }
91  
92      /***
93       * @see java.util.Map#values()
94       */
95      public Collection<V> values()
96      {
97          return this.map.values();
98      }
99  
100     /***
101      * @see java.util.Map#putAll(java.util.Map)
102      */
103     public void putAll(Map< ? extends K, ? extends V> t)
104     {
105         this.map.putAll(t);
106     }
107 
108     /***
109      * @see java.util.Map#entrySet()
110      */
111     public Set<Map.Entry<K, V>> entrySet()
112     {
113         return this.map.entrySet();
114     }
115 
116     /***
117      * @see java.util.Map#keySet()
118      */
119     public Set<K> keySet()
120     {
121         return this.map.keySet();
122     }
123 
124     /***
125      * @see java.util.Map#get(java.lang.Object)
126      */
127     public V get(Object key)
128     {
129         return this.map.get(key);
130     }
131 
132     /***
133      * @see java.util.Map#remove(java.lang.Object)
134      */
135     public V remove(Object key)
136     {
137         return this.map.remove(key);
138     }
139 
140     /***
141      * @see java.util.Map#put(java.lang.Object, java.lang.Object)
142      */
143     public V put(K key, V value)
144     {
145         return this.map.put(key, value);
146     }
147 
148     /***
149      * writes a serializable method to stream
150      * 
151      * @param out the outputstream
152      * @throws IOException on IOException
153      */
154     @SuppressWarnings("unchecked")
155     private synchronized void writeObject(final ObjectOutputStream out)
156             throws IOException
157     {
158         Map outMap = new HashMap();
159         for (Iterator<K> i = this.keySet().iterator(); i.hasNext();)
160         {
161             Object key = i.next();
162             ArrayList entriesList = new ArrayList(Arrays
163                     .asList((Reference[]) this.get(key)));
164             for (Iterator ii = entriesList.iterator(); ii.hasNext();)
165             {
166                 Reference reference = (Reference) ii.next();
167                 if (reference.get() instanceof RemoteEventListenerInterface)
168                 {
169                     ii.remove();
170                 }
171             }
172             if (!entriesList.isEmpty())
173             {
174                 outMap.put(key, entriesList.toArray(new Reference[entriesList
175                         .size()]));
176             }
177         }
178         out.writeObject(outMap);
179     }
180 
181     /***
182      * reads a serializable method from stream
183      * 
184      * @param in the inputstream
185      * @throws IOException on IOException
186      * @throws ClassNotFoundException on ClassNotFoundException
187      */
188     @SuppressWarnings("unchecked")
189     private synchronized void readObject(final java.io.ObjectInputStream in)
190             throws IOException, ClassNotFoundException
191     {
192         this.map = (HashMap) in.readObject();
193     }
194 }