Path: blob/master/src/java.desktop/share/classes/java/beans/NameGenerator.java
41152 views
/*1* Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/24package java.beans;2526import java.util.HashMap;27import java.util.IdentityHashMap;28import java.util.Map;2930import static java.util.Locale.ENGLISH;3132/**33* A utility class which generates unique names for object instances.34* The name will be a concatenation of the unqualified class name35* and an instance number.36* <p>37* For example, if the first object instance javax.swing.JButton38* is passed into {@code instanceName} then the returned39* string identifier will be "JButton0".40*41* @author Philip Milne42*/43class NameGenerator {4445private Map<Object, String> valueToName;46private Map<String, Integer> nameToCount;4748public NameGenerator() {49valueToName = new IdentityHashMap<>();50nameToCount = new HashMap<>();51}5253/**54* Clears the name cache. Should be called to near the end of55* the encoding cycle.56*/57public void clear() {58valueToName.clear();59nameToCount.clear();60}6162/**63* Returns the root name of the class.64*/65@SuppressWarnings("rawtypes")66public static String unqualifiedClassName(Class type) {67if (type.isArray()) {68return unqualifiedClassName(type.getComponentType())+"Array";69}70String name = type.getName();71return name.substring(name.lastIndexOf('.')+1);72}7374/**75* Returns a String which capitalizes the first letter of the string.76*/77public static String capitalize(String name) {78if (name == null || name.length() == 0) {79return name;80}81return name.substring(0, 1).toUpperCase(ENGLISH) + name.substring(1);82}8384/**85* Returns a unique string which identifies the object instance.86* Invocations are cached so that if an object has been previously87* passed into this method then the same identifier is returned.88*89* @param instance object used to generate string90* @return a unique string representing the object91*/92public String instanceName(Object instance) {93if (instance == null) {94return "null";95}96if (instance instanceof Class) {97return unqualifiedClassName((Class)instance);98}99else {100String result = valueToName.get(instance);101if (result != null) {102return result;103}104Class<?> type = instance.getClass();105String className = unqualifiedClassName(type);106107Integer size = nameToCount.get(className);108int instanceNumber = (size == null) ? 0 : (size).intValue() + 1;109nameToCount.put(className, instanceNumber);110111result = className + instanceNumber;112valueToName.put(instance, result);113return result;114}115}116}117118119