KickJava   Java API By Example, From Geeks To Geeks.

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


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/DottedNameFactory.java,v 1.3 2005/12/25 03:42:02 tcfujii Exp $
26  * $Revision: 1.3 $
27  * $Date: 2005/12/25 03:42:02 $
28  */

29 package com.sun.enterprise.admin.dottedname;
30
31 import java.util.HashMap JavaDoc;
32
33 import com.sun.enterprise.admin.util.ClassUtil;
34
35 /*
36     This class caches dotted name strings against their DottedName counterparts.
37     
38     This helps avoid the fairly expensive parse of the string, as well as minimizing
39     creation of new objects.
40  */

41 public final class DottedNameFactory
42 {
43     static final Factory sImpl = newInstance();
44     
45     public interface Factory
46     {
47         //public DottedName lookup( final String dottedNameString );
48
public DottedName get( final String JavaDoc dottedNameString );
49         public void clear();
50         public void add( final DottedName dn );
51     }
52     
53     
54         private
55     DottedNameFactory()
56     {
57         // disallow instantiation outside newInstance()
58
}
59     
60         public static Factory
61     newInstance()
62     {
63         return( new Impl() );
64     }
65     
66         public static Factory
67     getInstance()
68     {
69         return( sImpl );
70     }
71         
72     
73     /* implements the cache */
74     private static final class Impl implements Factory
75     {
76         final HashMap JavaDoc mMapping;
77         
78             public
79         Impl()
80         {
81             // disallow outside instantiation
82
mMapping = new HashMap JavaDoc( 300 );
83         }
84         
85             public synchronized void
86         add( final DottedName dottedName )
87         {
88             mMapping.put( dottedName.toString(), dottedName );
89         }
90         
91             public synchronized void
92         clear()
93         {
94             mMapping.clear();
95         }
96         
97         
98             public DottedName
99         get( final String JavaDoc dottedNameString )
100         {
101             DottedName dn = lookup( dottedNameString );
102             
103             if ( dn == null )
104             {
105                 dn = new DottedName( dottedNameString );
106                 add( dn );
107             }
108             
109             return( dn );
110         }
111         
112         
113             synchronized DottedName
114         lookup( final String JavaDoc dottedNameString )
115         {
116             final DottedName dn = (DottedName)mMapping.get( dottedNameString );
117             
118             assert( dn == null || dn.toString().equals( dottedNameString ) );
119             
120             return( dn );
121         }
122
123     }
124 }
125
126
127
128
Popular Tags