KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > preferences > ListenerRegistry


1 /*******************************************************************************
2  * Copyright (c) 2004, 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 org.eclipse.core.internal.preferences;
12
13 import org.eclipse.core.runtime.ListenerList;
14
15 /**
16  * A class which holds onto a listener list object for a given path.
17  * Typically the path is the absolute path of a preference node.
18  *
19  * @since 3.1
20  */

21 public class ListenerRegistry {
22
23     /**
24      * Specialized map-like data structure for storing change listeners.
25      */

26     private static class ListenerMap {
27         private static final int GROW_SIZE = 10;
28         String JavaDoc[] keys;
29         ListenerList[] values;
30
31         /**
32          * Create a map of exactly the specified size.
33          */

34         ListenerMap(int size) {
35             super();
36             this.keys = new String JavaDoc[size];
37             this.values = new ListenerList[size];
38         }
39
40         /**
41          * Return the listener list associated with the given key,
42          * or <code>null</code> if it doesn't exist.
43          */

44         ListenerList get(String JavaDoc key) {
45             if (key == null)
46                 throw new NullPointerException JavaDoc();
47             for (int i = 0; i < keys.length; i++)
48                 if (key.equals(keys[i]))
49                     return values[i];
50             return null;
51         }
52
53         /**
54          * Associate the given listener list with the specified key. Overwrite
55          * an existing association, if applicable.
56          */

57         void put(String JavaDoc key, ListenerList value) {
58             if (key == null)
59                 throw new NullPointerException JavaDoc();
60             if (value == null) {
61                 remove(key);
62                 return;
63             }
64             // replace if exists, keeping track of an empty position
65
int emptyIndex = -1;
66             for (int i = 0; i < keys.length; i++) {
67                 String JavaDoc existing = keys[i];
68                 if (existing == null) {
69                     emptyIndex = i;
70                     continue;
71                 }
72                 if (existing.equals(key)) {
73                     values[i] = value;
74                     return;
75                 }
76             }
77             if (emptyIndex == -1)
78                 emptyIndex = grow();
79             keys[emptyIndex] = key;
80             values[emptyIndex] = value;
81         }
82
83         /*
84          * Make the backing arrays larger
85          */

86         private int grow() {
87             int size = keys.length;
88             String JavaDoc[] tempKeys = new String JavaDoc[size + GROW_SIZE];
89             System.arraycopy(keys, 0, tempKeys, 0, size);
90             keys = tempKeys;
91             ListenerList[] tempValues = new ListenerList[size + GROW_SIZE];
92             System.arraycopy(values, 0, tempValues, 0, size);
93             values = tempValues;
94             return size;
95         }
96
97         /**
98          * Remove the association specified by the given key.
99          * Do nothing if none exists.
100          *
101          * Note: Should consider shrinking the array. Hold off for now
102          * as we don't expect #remove to be a common code path.
103          */

104         void remove(String JavaDoc key) {
105             if (key == null)
106                 throw new NullPointerException JavaDoc();
107             for (int i = 0; i < keys.length; i++)
108                 if (key.equals(keys[i])) {
109                     keys[i] = null;
110                     values[i] = null;
111                     return;
112                 }
113         }
114     }
115
116     static final Object JavaDoc[] EMPTY_LIST = new Object JavaDoc[0];
117     ListenerMap registry = new ListenerMap(25);
118
119     /**
120      * Return the listeners for this path or an empty list if none.
121      */

122     public synchronized Object JavaDoc[] getListeners(String JavaDoc path) {
123         ListenerList list = registry.get(path);
124         return list == null ? EMPTY_LIST : list.getListeners();
125     }
126
127     /**
128      * Add the given listener to the listeners registered for this path.
129      * If the listener already exists, then do nothing.
130      */

131     public synchronized void add(String JavaDoc path, Object JavaDoc listener) {
132         ListenerList list = registry.get(path);
133         if (list == null)
134             list = new ListenerList(ListenerList.IDENTITY);
135         list.add(listener);
136         registry.put(path, list);
137     }
138
139     /**
140      * Remove the given listener from this path's collection of
141      * listeners. If it is not associated with this path, then do nothing.
142      */

143     public synchronized void remove(String JavaDoc path, Object JavaDoc listener) {
144         ListenerList list = registry.get(path);
145         if (list == null)
146             return;
147         list.remove(listener);
148         if (list.isEmpty())
149             registry.remove(path);
150     }
151
152     /**
153      * Remove all of the listeners registered under the given path.
154      */

155     public synchronized void clear(String JavaDoc path) {
156         registry.remove(path);
157     }
158
159 }
160
Popular Tags