1 21 package proguard.obfuscate; 22 23 import java.util.*; 24 25 26 32 public class SimpleNameFactory implements NameFactory 33 { 34 private static final int CHARACTER_COUNT = 26; 35 36 private static final List cachedMixedCaseNames = new ArrayList(); 37 private static final List cachedLowerCaseNames = new ArrayList(); 38 39 private boolean generateMixedCaseNames; 40 private int index = 0; 41 42 43 46 public SimpleNameFactory() 47 { 48 this(true); 49 } 50 51 52 57 public SimpleNameFactory(boolean generateMixedCaseNames) 58 { 59 this.generateMixedCaseNames = generateMixedCaseNames; 60 } 61 62 63 65 public void reset() 66 { 67 index = 0; 68 } 69 70 71 public String nextName() 72 { 73 return name(index++); 74 } 75 76 77 80 private String name(int index) 81 { 82 List cachedNames = generateMixedCaseNames ? 84 cachedMixedCaseNames : 85 cachedLowerCaseNames; 86 87 if (index < cachedNames.size()) 89 { 90 return (String )cachedNames.get(index); 91 } 92 93 String name = newName(index); 95 cachedNames.add(index, name); 96 97 return name; 98 } 99 100 101 104 private String newName(int index) 105 { 106 int totalCharacterCount = generateMixedCaseNames ? 109 2 * CHARACTER_COUNT : 110 CHARACTER_COUNT; 111 112 int baseIndex = index / totalCharacterCount; 113 int offset = index % totalCharacterCount; 114 115 char newChar = charAt(offset); 116 117 String newName = baseIndex == 0 ? 118 new String (new char[] { newChar }) : 119 (name(baseIndex-1) + newChar); 120 121 return newName; 122 } 123 124 125 129 private char charAt(int index) 130 { 131 return (char)((index < CHARACTER_COUNT ? 'a' - 0 : 132 'A' - CHARACTER_COUNT) + index); 133 } 134 135 136 public static void main(String [] args) 137 { 138 System.out.println("Some mixed-case names:"); 139 printNameSamples(new SimpleNameFactory(true), 60); 140 System.out.println("Some lower-case names:"); 141 printNameSamples(new SimpleNameFactory(false), 60); 142 System.out.println("Some more mixed-case names:"); 143 printNameSamples(new SimpleNameFactory(true), 80); 144 System.out.println("Some more lower-case names:"); 145 printNameSamples(new SimpleNameFactory(false), 80); 146 } 147 148 149 private static void printNameSamples(SimpleNameFactory factory, int count) 150 { 151 for (int counter = 0; counter < count; counter++) 152 { 153 System.out.println(" ["+factory.nextName()+"]"); 154 } 155 } 156 } 157 | Popular Tags |