1 34 package jarg; 35 36 import java.util.Set ; 37 import java.util.HashSet ; 38 39 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 pre = ""; 52 private Set nmset = new HashSet (); 53 private String pushBacked; 54 55 NameCreater() { 56 } 57 58 NameCreater(String pre) { 59 this.pre = pre; 60 } 61 62 void addName(String nm) { 63 if (nm != null) { 64 nmset.add(nm); 65 } 66 } 67 68 void pushBach(String nm) { 69 pushBacked = nm; 70 } 71 72 String createNext() { 73 String 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 createNext0() { 90 int c, s, x; 91 String 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 } 112 | Popular Tags |