No repeat nicknames
For real usernames, remove already used nicknames from the list once chosen and update linesSize. To prevent IndexOutOfBoundsException from multi-threading make nextReal method synchronized.
This commit is contained in:
1 parent
5e109efc56
commit
3811004fda
1 file changed
+6
-3
@@ -78,10 +78,13 @@ public class NickGenerator {
|
|||||||
return prefix + generateRandom(nickLen);
|
return prefix + generateRandom(nickLen);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String nextReal() {
|
public synchronized String nextReal() {
|
||||||
String nick = prefix + lines.get(random.nextInt(linesSize));
|
|
||||||
return nick.length() <= 16 ? nick : nick.substring(0, 15);
|
|
||||||
|
|
||||||
|
int index = random.nextInt(linesSize);
|
||||||
|
String nick = prefix + lines.remove(index); // Remove chosen nicknames
|
||||||
|
linesSize = lines.size();
|
||||||
|
|
||||||
|
return nick.length() <= 16 ? nick : nick.substring(0, 15);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in new issue
Block a user