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