KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > media > util > registry > MapRegistry


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7
8 package org.jboss.media.util.registry;
9
10 import java.util.Collections JavaDoc;
11 import java.util.HashMap JavaDoc;
12 import java.util.Iterator JavaDoc;
13 import java.util.Map JavaDoc;
14
15 import org.jboss.logging.Logger;
16
17 /**
18  * A synchronized Map based registry with non-null keys or values.
19  *
20  * @version <tt>$Revision: 1.2 $</tt>
21  * @author <a HREF="mailto:ricardoarguello@users.sourceforge.net">Ricardo ArgŁello</a>
22  */

23 public class MapRegistry implements Registry
24 {
25    private static final Logger log = Logger.getLogger(MapRegistry.class);
26
27    private static final Map JavaDoc entries =
28       Collections.synchronizedMap(new HashMap JavaDoc());
29
30    public MapRegistry()
31    {
32    }
33
34    public MapRegistry(Map JavaDoc initialEntries) throws ObjectAlreadyBoundException
35    {
36       this();
37
38       Iterator JavaDoc keys = initialEntries.keySet().iterator();
39
40       while (keys.hasNext())
41       {
42          Object JavaDoc key = keys.next();
43          Object JavaDoc value = initialEntries.get(key);
44          bind(key, value);
45       }
46    }
47
48    public void bind(final Object JavaDoc key, final Object JavaDoc value)
49       throws ObjectAlreadyBoundException
50    {
51       if (key == null || value == null)
52       {
53          throw new NullPointerException JavaDoc();
54       }
55
56       if (entries.containsKey(key))
57       {
58          throw new ObjectAlreadyBoundException();
59       }
60
61       entries.put(key, value);
62
63       if (log.isTraceEnabled())
64          log.trace("bound " + key + "=" + value);
65    }
66
67    public void rebind(final Object JavaDoc key, final Object JavaDoc value)
68    {
69       if (key == null || value == null)
70       {
71          throw new NullPointerException JavaDoc();
72       }
73
74       entries.put(key, value);
75
76       if (log.isTraceEnabled())
77          log.trace("rebind " + key + "=" + value);
78    }
79
80    public Object JavaDoc unbind(final Object JavaDoc key) throws ObjectNotBoundException
81    {
82       if (key == null)
83       {
84          throw new NullPointerException JavaDoc();
85       }
86
87       if (!entries.containsKey(key))
88       {
89          throw new ObjectNotBoundException();
90       }
91
92       Object JavaDoc object = entries.remove(key);
93
94       if (log.isTraceEnabled())
95          log.trace("unbound " + key + "=" + object);
96
97       return object;
98    }
99
100    public Object JavaDoc lookup(final Object JavaDoc key) throws ObjectNotBoundException
101    {
102       if (!entries.containsKey(key))
103       {
104          throw new ObjectNotBoundException();
105       }
106
107       Object JavaDoc object = entries.get(key);
108
109       if (log.isTraceEnabled())
110          log.trace("lookup " + key + "=" + object);
111
112       return object;
113    }
114
115    public Iterator JavaDoc keyIterator()
116    {
117       synchronized (entries)
118       {
119          return entries.keySet().iterator();
120       }
121    }
122 }
123
Popular Tags