KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jarg > NameCreater


1 /* ====================================================================
2  * Copyright (c) 2002, Hidetoshi Ohuchi <hchacha@users.sourceforge.net>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * Neither the name of the hchacha nor the names of its contributors
17  * may be used to endorse or promote products derived from this
18  * software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  * ====================================================================
33  */

34 package jarg;
35
36 import java.util.Set JavaDoc;
37 import java.util.HashSet JavaDoc;
38
39 /**
40  * The class for generating a short name.
41  *
42  * @version $Id: NameCreater.java,v 1.2 2002/04/30 12:27:09 hchacha Exp $
43  * @author Hidetoshi Ohuchi &lt;hchacha@users.sourceforge.net&gt;
44  */

45 class NameCreater {
46    private final int smallA = (int)'a';
47    private final int smallZ = (int)'z';
48    private final int smallRange = smallZ - smallA + 1;
49
50    private int current = 0;
51    private String JavaDoc pre = "";
52    private Set JavaDoc nmset = new HashSet JavaDoc();
53    private String JavaDoc pushBacked;
54
55    NameCreater() {
56    }
57
58    NameCreater(String JavaDoc pre) {
59       this.pre = pre;
60    }
61
62    void addName(String JavaDoc nm) {
63       if (nm != null) {
64          nmset.add(nm);
65       }
66    }
67
68    void pushBach(String JavaDoc nm) {
69       pushBacked = nm;
70    }
71
72    String JavaDoc createNext() {
73       String JavaDoc nm;
74
75       if (pushBacked != null) {
76          nm = pushBacked;
77          pushBacked = null;
78          return nm;
79       }
80
81       do {
82          nm = createNext0();
83       } while (nmset.contains(nm));
84       nmset.add(nm);
85
86       return nm;
87    }
88
89    private String JavaDoc createNext0() {
90       int c, s, x;
91       String JavaDoc r = "";
92
93       x = current;
94       do {
95          c = (x % smallRange);
96          r += (char)(c + smallA);
97          x = (x / smallRange) -1;
98       } while (x >= 0);
99       current ++;
100
101       return r;
102    }
103
104 // <local test>
105
// public static void main(String[] args) {
106
// NameCreater nc = new NameCreater();
107
// for (int i=0; i<10000; i++) {
108
// System.out.println(nc.createNext());
109
// }
110
// }
111
}
112
Popular Tags