Importing the XDebugProxy code in the HEAD. The repo was tagged with T_BEFORE_XDEBUGP...
[phpeclipse.git] / net.sourceforge.phpeclipse.xdebug.core / src / net / sourceforge / phpeclipse / xdebug / core / ListenerMap.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpeclipse.xdebug.core;
12
13 import java.util.HashMap;
14
15
16 /**
17  * Local version of org.eclipse.jface.util.ListenerList (modified)s
18  */
19 public class ListenerMap {
20         /**
21          * The current number of listeners.
22          * Maintains invariant: 0 <= fSize <= listeners.length.
23          */
24         private int fSize;
25
26         /**
27          * The map of listeners.  Initially <code>null</code> but initialized
28          * to an array of size capacity the first time a listener is added.
29          * Maintains invariant: listeners != null if and only if fSize != 0
30          */
31         private HashMap fListeners = null;
32 //      private HashMap<String,Object> fListeners = null;
33
34         /**
35          * Creates a listener map with the given initial capacity.
36          *
37          * @param capacity the number of listeners which this list can initially accept 
38          *    without growing its internal representation; must be at least 1
39          */
40         public ListenerMap(int capacity) {
41                 if (capacity < 1) {
42                         throw new IllegalArgumentException();
43                 }
44                 fListeners = new HashMap(capacity);
45                 fSize = 0;
46         }
47
48         /**
49          * Adds a listener to the Map.
50          * Overwrites an existing listener with the same IDE_Key.
51          *
52          * @param listener a listener
53          * @param ideKey IDE-key of the listener
54          */
55         public synchronized void add(Object listener,String ideKey) {
56                 if (listener == null) {
57                         throw new IllegalArgumentException();
58                 }
59                 // check for duplicates using identity
60                 if (fListeners.get(ideKey) != null)
61                         fSize++;
62                 fListeners.put(ideKey,listener);
63         }
64
65         /**
66          * Returns a map containing all the registered listeners.
67          * The resulting map is unaffected by subsequent adds or removes.
68          * If there are no listeners registered, the result is an empty map.
69          * Use this method when notifying listeners, so that any modifications
70          * to the listener list during the notification will have no effect on the
71          * notification itself.
72          */
73         public synchronized HashMap getListeners() {
74                 if (fSize == 0) {
75                         return null;
76                 }
77                 HashMap result = new HashMap(fListeners);
78                 return result;
79         }
80         
81         /**
82          * Returns the listener associated with the ideKey.
83          * If there is no listener registered, the result is null.
84          * Use this method when notifying listeners, so that any modifications
85          * to the listener list during the notification will have no effect on the
86          * notification itself.
87          */
88         public synchronized Object getListener(String ideKey) {
89                 return fListeners.get(ideKey);
90         }
91
92         /**
93          * Removes a listener from the list.
94          * Has no effect if an identical listener was not already registered.
95          *
96          * @param listener a listener
97          */
98         public synchronized void remove(Object listener,String ideKey) {
99                 if (listener == null) {
100                         throw new IllegalArgumentException();
101                 }
102                 
103                 if (fListeners.get(ideKey) == listener) {
104                         fListeners.remove(ideKey);
105                         fSize--;
106                 }
107         }
108
109         /**
110          * Removes all the listeners from the list.
111          */
112         public synchronized void removeAll() {
113                 fListeners.clear();
114                 fSize = 0;
115         }
116
117         /**
118          * Returns the number of registered listeners
119          *
120          * @return the number of registered listeners
121          */
122         public int size() {
123                 return fSize;
124         }
125 }