namegenerator.java

来自「java实现浏览器等本地桌面的功能」· Java 代码 · 共 67 行

JAVA
67
字号
/* * $Id: NameGenerator.java,v 1.5 2005/10/15 20:06:50 rbair Exp $ * * Copyright 2005 Sun Microsystems, Inc., 4150 Network Circle, * Santa Clara, California 95054, U.S.A. All rights reserved. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. *  * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU * Lesser General Public License for more details. *  * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */package org.jdesktop.dataset;import java.util.Map;import java.util.WeakHashMap;/** * Generates unique names. Give it a prefix, it adds a numeric postfix.  * Simple use:  * <pre> * NameGenerator ng = new NameGenerator("DATA_TABLE"); * String newName = ng.generateName(ng); * String newName2 = ng.generateName(ng); * String newName3 = ng.generateName(ng); * </pre> * * @author rbair */class NameGenerator {    /** The prefix for all names generated by this instance. */    private String prefix;        private int postfix = 1;        /**      * Creates a new instance of NameGenerator.     *     * @param prefix The prefix for all new names generated by this instance.     */    public NameGenerator(String prefix) {        this.prefix = prefix;    }        /**     * Generates a new unique name based on the prefix. The object reference is used     * to associate with the generated name. The object reference is kept in a     * weak reference, so that when this object retains the last reference to     * obj, the generated name can be removed automatically; generated names are thus      * unique by NG instance + object instance.     *     * @param obj The object to use as a tracker for unique names.     * @return a unique name beginning with the prefix given in the constructor.     */    public String generateName(Object obj) {        return prefix + postfix++;    }    }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?