Lucio Lelii 2016-07-14 13:14:40 +00:00
parent fb77e1f630
commit 751f4f4621
1 changed files with 11 additions and 7 deletions

View File

@ -347,28 +347,32 @@ public class SQLDatabaseWrangler implements DatabaseWrangler {
public static class RandomString { public static class RandomString {
private static final char[] symbols; private static final char[] symbols;
private static final char[] startingSymbols;
static { static {
StringBuilder tmp = new StringBuilder(); StringBuilder tmp = new StringBuilder();
for (char ch = '0'; ch <= '9'; ++ch)
tmp.append(ch);
for (char ch = 'a'; ch <= 'z'; ++ch) for (char ch = 'a'; ch <= 'z'; ++ch)
tmp.append(ch); tmp.append(ch);
startingSymbols = tmp.toString().toCharArray();
for (char ch = '0'; ch <= '9'; ++ch)
tmp.append(ch);
symbols = tmp.toString().toCharArray(); symbols = tmp.toString().toCharArray();
} }
private final int length;
private final Random random = new Random(); private final Random random = new Random();
private final char[] buf;
public RandomString(int length) { public RandomString(int length) {
if (length < 1) if (length < 1)
throw new IllegalArgumentException("length < 1: " + length); throw new IllegalArgumentException("length < 1: " + length);
buf = new char[length]; this.length = length;
} }
public String nextString() { public synchronized String nextString() {
for (int idx = 0; idx < buf.length; ++idx) char[] buf = new char[this.length];
buf[0] = startingSymbols[random.nextInt(startingSymbols.length)];
for (int idx = 1; idx < buf.length; ++idx)
buf[idx] = symbols[random.nextInt(symbols.length)]; buf[idx] = symbols[random.nextInt(symbols.length)];
return new String(buf); return new String(buf);
} }