NameGenerator.java
上传用户:zhengdagz
上传日期:2014-03-06
资源大小:1956k
文件大小:2k
源码类别:

xml/soap/webservice

开发平台:

Java

  1. /*
  2.  * $Id: NameGenerator.java,v 1.5 2005/10/15 20:06:50 rbair Exp $
  3.  *
  4.  * Copyright 2005 Sun Microsystems, Inc., 4150 Network Circle,
  5.  * Santa Clara, California 95054, U.S.A. All rights reserved.
  6.  *
  7.  * This library is free software; you can redistribute it and/or
  8.  * modify it under the terms of the GNU Lesser General Public
  9.  * License as published by the Free Software Foundation; either
  10.  * version 2.1 of the License, or (at your option) any later version.
  11.  * 
  12.  * This library is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15.  * Lesser General Public License for more details.
  16.  * 
  17.  * You should have received a copy of the GNU Lesser General Public
  18.  * License along with this library; if not, write to the Free Software
  19.  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  20.  */
  21. package org.jdesktop.dataset;
  22. import java.util.Map;
  23. import java.util.WeakHashMap;
  24. /**
  25.  * Generates unique names. Give it a prefix, it adds a numeric postfix. 
  26.  * Simple use: 
  27.  * <pre>
  28.  * NameGenerator ng = new NameGenerator("DATA_TABLE");
  29.  * String newName = ng.generateName(ng);
  30.  * String newName2 = ng.generateName(ng);
  31.  * String newName3 = ng.generateName(ng);
  32.  * </pre>
  33.  *
  34.  * @author rbair
  35.  */
  36. class NameGenerator {
  37.     /** The prefix for all names generated by this instance. */
  38.     private String prefix;
  39.     
  40.     private int postfix = 1;
  41.     
  42.     /** 
  43.      * Creates a new instance of NameGenerator.
  44.      *
  45.      * @param prefix The prefix for all new names generated by this instance.
  46.      */
  47.     public NameGenerator(String prefix) {
  48.         this.prefix = prefix;
  49.     }
  50.     
  51.     /**
  52.      * Generates a new unique name based on the prefix. The object reference is used
  53.      * to associate with the generated name. The object reference is kept in a
  54.      * weak reference, so that when this object retains the last reference to
  55.      * obj, the generated name can be removed automatically; generated names are thus 
  56.      * unique by NG instance + object instance.
  57.      *
  58.      * @param obj The object to use as a tracker for unique names.
  59.      * @return a unique name beginning with the prefix given in the constructor.
  60.      */
  61.     public String generateName(Object obj) {
  62.         return prefix + postfix++;
  63.     }    
  64. }