KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > dottedname > DottedNameRegistry1To1Impl


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23  
24 /*
25  * $Header: /cvs/glassfish/admin/mbeans/src/java/com/sun/enterprise/admin/dottedname/DottedNameRegistry1To1Impl.java,v 1.4 2005/12/25 03:42:03 tcfujii Exp $
26  * $Revision: 1.4 $
27  * $Date: 2005/12/25 03:42:03 $
28  */

29 package com.sun.enterprise.admin.dottedname;
30  
31  
32 import java.util.Set JavaDoc;
33 import java.util.HashSet JavaDoc;
34 import java.util.Iterator JavaDoc;
35 import java.util.HashMap JavaDoc;
36
37 import javax.management.ObjectName JavaDoc;
38
39 /*
40     While a dotted name can refer to only 1 ObjectName, an ObjectName could
41     be aliased to multiple dotted names.
42     
43     This implementation supports a 1 to 1 mapping each way; it cannot be used
44     to map multiple dotted names to the same ObjectName.
45  */

46 public final class DottedNameRegistry1To1Impl implements DottedNameRegistry
47 {
48     final HashMap JavaDoc mDottedNameStringsToObjectNames;
49     
50     final HashMap JavaDoc mObjectNamesToDottedNameStrings;
51     
52     private final static int INITIAL_CAPACITY = 300;
53     
54         public
55     DottedNameRegistry1To1Impl( )
56     {
57         /*
58             Keep mappings both ways to allow efficient removal
59             based on either.
60         */

61         mDottedNameStringsToObjectNames = new HashMap JavaDoc( INITIAL_CAPACITY );
62         mObjectNamesToDottedNameStrings = new HashMap JavaDoc( INITIAL_CAPACITY );
63     }
64     
65         public synchronized ObjectName JavaDoc
66     dottedNameToObjectName( String JavaDoc dottedName )
67     {
68         return( (ObjectName JavaDoc)mDottedNameStringsToObjectNames.get( dottedName ) );
69     }
70     
71         public synchronized String JavaDoc
72     objectNameToDottedName( ObjectName JavaDoc objectName )
73     {
74         return( (String JavaDoc)mObjectNamesToDottedNameStrings.get( objectName ) );
75     }
76     
77         private Set JavaDoc
78     copySet( Set JavaDoc input )
79     {
80         final HashSet JavaDoc newSet = new HashSet JavaDoc();
81         
82         newSet.addAll( input );
83         
84         return( newSet );
85     }
86     
87         public synchronized Set JavaDoc
88     allDottedNameStrings( )
89     {
90         return( copySet( mDottedNameStringsToObjectNames.keySet() ) );
91     }
92     
93         public synchronized Set JavaDoc
94     allObjectNames( )
95     {
96         return( copySet( mObjectNamesToDottedNameStrings.keySet() ) );
97     }
98
99         public synchronized void
100     add( String JavaDoc dottedName, ObjectName JavaDoc objectName )
101     {
102         new DottedName( dottedName );
103         /*
104             Don't allow more than one dotted name mapping for an ObjectName.
105             
106             The check here must be via the ObjectName; checking the dottedName
107             will do no good as this could be a new dottedName for the same
108             ObjectName.
109          */

110         if ( objectNameToDottedName( objectName ) != null )
111         {
112             remove( objectName );
113         }
114
115         mDottedNameStringsToObjectNames.put( dottedName, objectName );
116         mObjectNamesToDottedNameStrings.put( objectName, dottedName );
117     }
118     
119         synchronized void
120     remove( String JavaDoc dottedName, ObjectName JavaDoc objectName )
121     {
122         if ( dottedName != null && objectName != null )
123         {
124             mDottedNameStringsToObjectNames.remove( dottedName );
125             mObjectNamesToDottedNameStrings.remove( objectName );
126         }
127     }
128     
129         public void
130     remove( String JavaDoc dottedName )
131     {
132         remove( dottedName, dottedNameToObjectName( dottedName ) );
133     }
134     
135         public void
136     remove( ObjectName JavaDoc objectName )
137     {
138         remove( objectNameToDottedName( objectName ), objectName );
139     }
140 }
141
142
143
144
Popular Tags