Skip to content
Permalink
Browse files
simpler algorithm for hex id generation (avoid substring)
  • Loading branch information
rmannibucau committed Jun 4, 2018
1 parent 981f111 commit f0d617209352e0ff53fc051965f32dd1627208af
Showing 1 changed file with 4 additions and 3 deletions.
@@ -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();
}
};
}

0 comments on commit f0d6172

Please sign in to comment.