Skip to content

Commit

Permalink
simpler algorithm for hex id generation (avoid substring)
Browse files Browse the repository at this point in the history
  • Loading branch information
rmannibucau committed Jun 4, 2018
1 parent 981f111 commit f0d6172
Showing 1 changed file with 4 additions and 3 deletions.
Expand Up @@ -57,15 +57,16 @@ public Object get() {
default:
delegate = new Supplier<Object>() {
private final Random random = new Random(System.nanoTime());
private final char[] hexDigits = "0123456789abcdef".toCharArray();
private final String constantPart = config.read("id.generator.hex.prefix", "");

@Override
public Object get() {
final StringBuilder sb = new StringBuilder(16).append(constantPart);
while (sb.length() < 16) {
sb.append(Integer.toHexString(random.nextInt()));
for (int i = 0; i < 16 - constantPart.length(); i++) {
sb.append(hexDigits[random.nextInt(16)]);
}
return sb.length() > 16 ? sb.substring(0, 16) : sb.toString();
return sb.toString();
}
};
}
Expand Down

0 comments on commit f0d6172

Please sign in to comment.