0N/A/*
2362N/A * Copyright (c) 2000, 2005, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/Apackage java.beans;
0N/A
0N/Aimport java.util.HashMap;
0N/Aimport java.util.IdentityHashMap;
0N/Aimport java.util.Map;
0N/A
0N/Aimport static java.util.Locale.ENGLISH;
0N/A
0N/A/**
0N/A * A utility class which generates unique names for object instances.
0N/A * The name will be a concatenation of the unqualified class name
0N/A * and an instance number.
0N/A * <p>
0N/A * For example, if the first object instance javax.swing.JButton
0N/A * is passed into <code>instanceName</code> then the returned
0N/A * string identifier will be &quot;JButton0&quot;.
0N/A *
0N/A * @author Philip Milne
0N/A */
0N/Aclass NameGenerator {
0N/A
0N/A private Map valueToName;
0N/A private Map nameToCount;
0N/A
0N/A public NameGenerator() {
0N/A valueToName = new IdentityHashMap();
0N/A nameToCount = new HashMap();
0N/A }
0N/A
0N/A /**
0N/A * Clears the name cache. Should be called to near the end of
0N/A * the encoding cycle.
0N/A */
0N/A public void clear() {
0N/A valueToName.clear();
0N/A nameToCount.clear();
0N/A }
0N/A
0N/A /**
0N/A * Returns the root name of the class.
0N/A */
0N/A public static String unqualifiedClassName(Class type) {
0N/A if (type.isArray()) {
0N/A return unqualifiedClassName(type.getComponentType())+"Array";
0N/A }
0N/A String name = type.getName();
0N/A return name.substring(name.lastIndexOf('.')+1);
0N/A }
0N/A
0N/A /**
0N/A * Returns a String which capitalizes the first letter of the string.
0N/A */
0N/A public static String capitalize(String name) {
0N/A if (name == null || name.length() == 0) {
0N/A return name;
0N/A }
0N/A return name.substring(0, 1).toUpperCase(ENGLISH) + name.substring(1);
0N/A }
0N/A
0N/A /**
0N/A * Returns a unique string which identifies the object instance.
0N/A * Invocations are cached so that if an object has been previously
0N/A * passed into this method then the same identifier is returned.
*
* @param instance object used to generate string
* @return a unique string representing the object
*/
public String instanceName(Object instance) {
if (instance == null) {
return "null";
}
if (instance instanceof Class) {
return unqualifiedClassName((Class)instance);
}
else {
String result = (String)valueToName.get(instance);
if (result != null) {
return result;
}
Class type = instance.getClass();
String className = unqualifiedClassName(type);
Object size = nameToCount.get(className);
int instanceNumber = (size == null) ? 0 : ((Integer)size).intValue() + 1;
nameToCount.put(className, new Integer(instanceNumber));
result = className + instanceNumber;
valueToName.put(instance, result);
return result;
}
}
}