Refactor into constants

This commit is contained in:
Gary Gregory 2024-09-11 09:36:17 -04:00
parent 61580f2c01
commit ec4f545b69
1 changed files with 15 additions and 15 deletions

View File

@ -93,6 +93,11 @@ public class RandomStringUtils {
'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '0', '1',
'2', '3', '4', '5', '6', '7', '8', '9' };
private static final int ASCII_0 = '0';
private static final int ASCII_9 = '9';
private static final int ASCII_A = 'A';
private static final int ASCII_z = 'z';
/**
* Gets the singleton instance based on {@link ThreadLocalRandom#current()}; <b>which is not cryptographically
* secure</b>; use {@link #secure()} to use an algorithms/providers specified in the
@ -279,24 +284,19 @@ public class RandomStringUtils {
// Optimizations and tests when chars == null and using ASCII characters (end <= 0x7f)
if (chars == null && end <= 0x7f) {
final int zeroDigitAscii = 48; // '0'
final int lastDigitAscii = 57; // '9'
final int firstLetterAscii = 65; // 'A'
final int lastLetterAscii = 122; // 'z'
// Optimize generation of full alphanumerical characters
// Normally, we would need to pick a 7-bit integer, since gap = 'z' - '0' + 1 = 75 > 64
// In turn, this would make us reject the sampling with probability 1 - 62 / 2^7 > 1 / 2
// Instead we can pick directly from the right set of 62 characters, which requires
// picking a 6-bit integer and only rejecting with probability 2 / 64 = 1 / 32
if (letters && numbers && start <= zeroDigitAscii && end >= lastLetterAscii + 1) {
if (letters && numbers && start <= ASCII_0 && end >= ASCII_z + 1) {
return random(count, 0, 0, false, false, ALPHANUMERICAL_CHARS, random);
}
if (numbers && end <= zeroDigitAscii || letters && end <= firstLetterAscii) {
if (numbers && end <= ASCII_0 || letters && end <= ASCII_A) {
throw new IllegalArgumentException(
"Parameter end (" + end + ") must be greater then (" + zeroDigitAscii + ") for generating digits "
+ "or greater then (" + firstLetterAscii + ") for generating letters.");
"Parameter end (" + end + ") must be greater then (" + ASCII_0 + ") for generating digits "
+ "or greater then (" + ASCII_A + ") for generating letters.");
}
// Optimize start and end when filtering by letters and/or numbers:
@ -308,16 +308,16 @@ public class RandomStringUtils {
// Note that because of the above test, we will always have start < end
// even after this optimization.
if (letters && numbers) {
start = Math.max(zeroDigitAscii, start);
end = Math.min(lastLetterAscii + 1, end);
start = Math.max(ASCII_0, start);
end = Math.min(ASCII_z + 1, end);
} else if (numbers) {
// just numbers, no letters
start = Math.max(zeroDigitAscii, start);
end = Math.min(lastDigitAscii + 1, end);
start = Math.max(ASCII_0, start);
end = Math.min(ASCII_9 + 1, end);
} else if (letters) {
// just letters, no numbers
start = Math.max(firstLetterAscii, start);
end = Math.min(lastLetterAscii + 1, end);
start = Math.max(ASCII_A, start);
end = Math.min(ASCII_z + 1, end);
}
}