KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > beans > NameGenerator


1 /*
2  * @(#)NameGenerator.java 1.9 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7 package java.beans;
8
9 import java.util.HashMap JavaDoc;
10 import java.util.IdentityHashMap JavaDoc;
11 import java.util.Map JavaDoc;
12
13 /**
14  * A utility class which generates unique names for object instances.
15  * The name will be a concatenation of the unqualified class name
16  * and an instance number.
17  * <p>
18  * For example, if the first object instance javax.swing.JButton
19  * is passed into <code>instanceName</code> then the returned
20  * string identifier will be &quot;JButton0&quot;.
21  *
22  * @version 1.9 12/19/03
23  * @author Philip Milne
24  */

25 class NameGenerator {
26
27     private Map JavaDoc valueToName;
28     private Map JavaDoc nameToCount;
29     
30     public NameGenerator() {
31         valueToName = new IdentityHashMap JavaDoc();
32         nameToCount = new HashMap JavaDoc();
33     }
34     
35     /**
36      * Clears the name cache. Should be called to near the end of
37      * the encoding cycle.
38      */

39     public void clear() {
40     valueToName.clear();
41     nameToCount.clear();
42     }
43     
44     /**
45      * Returns the root name of the class.
46      */

47     public static String JavaDoc unqualifiedClassName(Class JavaDoc type) {
48         if (type.isArray()) {
49             return unqualifiedClassName(type.getComponentType())+"Array";
50         }
51         String JavaDoc name = type.getName();
52         return name.substring(name.lastIndexOf('.')+1);
53     }
54
55     /**
56      * Returns a String which capitalizes the first letter of the string.
57      */

58     public static String JavaDoc capitalize(String JavaDoc name) {
59     if (name == null || name.length() == 0) {
60         return name;
61         }
62     return name.substring(0, 1).toUpperCase() + name.substring(1);
63     }
64     
65     /**
66      * Returns a unique string which identifies the object instance.
67      * Invocations are cached so that if an object has been previously
68      * passed into this method then the same identifier is returned.
69      *
70      * @param instance object used to generate string
71      * @return a unique string representing the object
72      */

73     public String JavaDoc instanceName(Object JavaDoc instance) {
74         if (instance == null) {
75             return "null";
76         }
77         if (instance instanceof Class JavaDoc) {
78             return unqualifiedClassName((Class JavaDoc)instance);
79         }
80         else {
81             String JavaDoc result = (String JavaDoc)valueToName.get(instance);
82             if (result != null) {
83                 return result;
84             }
85             Class JavaDoc type = instance.getClass();
86             String JavaDoc className = unqualifiedClassName(type);
87
88             Object JavaDoc size = nameToCount.get(className);
89             int instanceNumber = (size == null) ? 0 : ((Integer JavaDoc)size).intValue() + 1;
90             nameToCount.put(className, new Integer JavaDoc(instanceNumber));
91
92             result = className + instanceNumber;
93             valueToName.put(instance, result);
94             return result;
95         }
96     }
97 }
98
Popular Tags