Showing the changes to java/pkg/java/beans/NameGenerator.java (1 of 18352 files). Show all
Unified · Split
| @@ -0,0 +1,116 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 2000, 2005, Oracle and/or its affiliates. All rights reserved. | ||
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | ||
| 4 | * | ||
| 5 | * This code is free software; you can redistribute it and/or modify it | ||
| 6 | * under the terms of the GNU General Public License version 2 only, as | ||
| 7 | * published by the Free Software Foundation. Oracle designates this | ||
| 8 | * particular file as subject to the "Classpath" exception as provided | ||
| 9 | * by Oracle in the LICENSE file that accompanied this code. | ||
| 10 | * | ||
| 11 | * This code is distributed in the hope that it will be useful, but WITHOUT | ||
| 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
| 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||
| 14 | * version 2 for more details (a copy is included in the LICENSE file that | ||
| 15 | * accompanied this code). | ||
| 16 | * | ||
| 17 | * You should have received a copy of the GNU General Public License version | ||
| 18 | * 2 along with this work; if not, write to the Free Software Foundation, | ||
| 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | ||
| 20 | * | ||
| 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | ||
| 22 | * or visit www.oracle.com if you need additional information or have any | ||
| 23 | * questions. | ||
| 24 | */ | ||
| 25 | package java.beans; | ||
| 26 | |||
| 27 | import java.util.HashMap; | ||
| 28 | import java.util.IdentityHashMap; | ||
| 29 | import java.util.Map; | ||
| 30 | |||
| 31 | import static java.util.Locale.ENGLISH; | ||
| 32 | |||
| 33 | /** | ||
| 34 | * A utility class which generates unique names for object instances. | ||
| 35 | * The name will be a concatenation of the unqualified class name | ||
| 36 | * and an instance number. | ||
| 37 | * <p> | ||
| 38 | * For example, if the first object instance javax.swing.JButton | ||
| 39 | * is passed into <code>instanceName</code> then the returned | ||
| 40 | * string identifier will be "JButton0". | ||
| 41 | * | ||
| 42 | * @author Philip Milne | ||
| 43 | */ | ||
| 44 | class NameGenerator { | ||
| 45 | |||
| 46 | private Map valueToName; | ||
| 47 | private Map nameToCount; | ||
| 48 | |||
| 49 | public NameGenerator() { | ||
| 50 | valueToName = new IdentityHashMap(); | ||
| 51 | nameToCount = new HashMap(); | ||
| 52 | } | ||
| 53 | |||
| 54 | /** | ||
| 55 | * Clears the name cache. Should be called to near the end of | ||
| 56 | * the encoding cycle. | ||
| 57 | */ | ||
| 58 | public void clear() { | ||
| 59 | valueToName.clear(); | ||
| 60 | nameToCount.clear(); | ||
| 61 | } | ||
| 62 | |||
| 63 | /** | ||
| 64 | * Returns the root name of the class. | ||
| 65 | */ | ||
| 66 | public static String unqualifiedClassName(Class type) { | ||
| 67 | if (type.isArray()) { | ||
| 68 | return unqualifiedClassName(type.getComponentType())+"Array"; | ||
| 69 | } | ||
| 70 | String name = type.getName(); | ||
| 71 | return name.substring(name.lastIndexOf('.')+1); | ||
| 72 | } | ||
| 73 | |||
| 74 | /** | ||
| 75 | * Returns a String which capitalizes the first letter of the string. | ||
| 76 | */ | ||
| 77 | public static String capitalize(String name) { | ||
| 78 | if (name == null || name.length() == 0) { | ||
| 79 | return name; | ||
| 80 | } | ||
| 81 | return name.substring(0, 1).toUpperCase(ENGLISH) + name.substring(1); | ||
| 82 | } | ||
| 83 | |||
| 84 | /** | ||
| 85 | * Returns a unique string which identifies the object instance. | ||
| 86 | * Invocations are cached so that if an object has been previously | ||
| 87 | * passed into this method then the same identifier is returned. | ||
| 88 | * | ||
| 89 | * @param instance object used to generate string | ||
| 90 | * @return a unique string representing the object | ||
| 91 | */ | ||
| 92 | public String instanceName(Object instance) { | ||
| 93 | if (instance == null) { | ||
| 94 | return "null"; | ||
| 95 | } | ||
| 96 | if (instance instanceof Class) { | ||
| 97 | return unqualifiedClassName((Class)instance); | ||
| 98 | } | ||
| 99 | else { | ||
| 100 | String result = (String)valueToName.get(instance); | ||
| 101 | if (result != null) { | ||
| 102 | return result; | ||
| 103 | } | ||
| 104 | Class type = instance.getClass(); | ||
| 105 | String className = unqualifiedClassName(type); | ||
| 106 | |||
| 107 | Object size = nameToCount.get(className); | ||
| 108 | int instanceNumber = (size == null) ? 0 : ((Integer)size).intValue() + 1; | ||
| 109 | nameToCount.put(className, new Integer(instanceNumber)); | ||
| 110 | |||
| 111 | result = className + instanceNumber; | ||
| 112 | valueToName.put(instance, result); | ||
| 113 | return result; | ||
| 114 | } | ||
| 115 | } | ||
| 116 | } | ||