重构代码

dev
ZhouXY108 2024-10-21 01:25:08 +08:00
parent 442374e53b
commit 269f9d686e
2 changed files with 72 additions and 20 deletions

View File

@ -20,6 +20,8 @@ import java.security.SecureRandom;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;
import javax.annotation.Nonnull;
public final class RandomTools {
public static final SecureRandom DEFAULT_SECURE_RANDOM = new SecureRandom();
@ -30,7 +32,7 @@ public final class RandomTools {
/**
* 使
*
*
* @param random
* {@link java.util.concurrent.ThreadLocalRandom}{@link java.security.SecureRandom}
*
@ -38,7 +40,68 @@ public final class RandomTools {
* @param length
* @return
*/
public static String randomStr(Random random, char[] sourceCharacters, int length) {
public static String randomStr(@Nonnull Random random, @Nonnull char[] sourceCharacters, int length) {
AssertTools.checkArgumentNotNull(random, "Random cannot be null.");
AssertTools.checkArgumentNotNull(sourceCharacters, "Source characters cannot be null.");
AssertTools.checkArgument(length >= 0, "The length should be greater than or equal to zero.");
return randomStrInternal(random, sourceCharacters, length);
}
public static String randomStr(@Nonnull char[] sourceCharacters, int length) {
AssertTools.checkArgumentNotNull(sourceCharacters, "Source characters cannot be null.");
AssertTools.checkArgument(length >= 0, "The length should be greater than or equal to zero.");
return randomStrInternal(ThreadLocalRandom.current(), sourceCharacters, length);
}
public static String secureRandomStr(@Nonnull char[] sourceCharacters, int length) {
AssertTools.checkArgumentNotNull(sourceCharacters, "Source characters cannot be null.");
AssertTools.checkArgument(length >= 0, "The length should be greater than or equal to zero.");
return randomStrInternal(DEFAULT_SECURE_RANDOM, sourceCharacters, length);
}
/**
* 使
*
* @param random
* {@link java.util.concurrent.ThreadLocalRandom}{@link java.security.SecureRandom}
*
* @param sourceCharacters
* @param length
* @return
*/
public static String randomStr(@Nonnull Random random, @Nonnull String sourceCharacters, int length) {
AssertTools.checkArgumentNotNull(random, "Random cannot be null.");
AssertTools.checkArgumentNotNull(sourceCharacters, "Source characters cannot be null.");
AssertTools.checkArgument(length >= 0, "The length should be greater than or equal to zero.");
return randomStrInternal(random, sourceCharacters, length);
}
public static String randomStr(@Nonnull String sourceCharacters, int length) {
AssertTools.checkArgumentNotNull(sourceCharacters, "Source characters cannot be null.");
AssertTools.checkArgument(length >= 0, "The length should be greater than or equal to zero.");
return randomStrInternal(ThreadLocalRandom.current(), sourceCharacters, length);
}
public static String secureRandomStr(@Nonnull String sourceCharacters, int length) {
AssertTools.checkArgumentNotNull(sourceCharacters, "Source characters cannot be null.");
AssertTools.checkArgument(length >= 0, "The length should be greater than or equal to zero.");
return randomStrInternal(DEFAULT_SECURE_RANDOM, sourceCharacters, length);
}
/**
* 使
*
* @param random
* {@link java.util.concurrent.ThreadLocalRandom}{@link java.security.SecureRandom}
*
* @param sourceCharacters
* @param length
* @return
*/
private static String randomStrInternal(@Nonnull Random random, @Nonnull char[] sourceCharacters, int length) {
if (length == 0) {
return StringTools.EMPTY_STRING;
}
final char[] result = new char[length];
for (int i = 0; i < length; i++) {
result[i] = sourceCharacters[random.nextInt(sourceCharacters.length)];
@ -46,17 +109,9 @@ public final class RandomTools {
return String.valueOf(result);
}
public static String randomStr(char[] sourceCharacters, int length) {
return randomStr(ThreadLocalRandom.current(), sourceCharacters, length);
}
public static String secureRandomStr(char[] sourceCharacters, int length) {
return randomStr(DEFAULT_SECURE_RANDOM, sourceCharacters, length);
}
/**
* 使
*
*
* @param random
* {@link java.util.concurrent.ThreadLocalRandom}{@link java.security.SecureRandom}
*
@ -64,7 +119,10 @@ public final class RandomTools {
* @param length
* @return
*/
public static String randomStr(Random random, String sourceCharacters, int length) {
private static String randomStrInternal(@Nonnull Random random, @Nonnull String sourceCharacters, int length) {
if (length == 0) {
return StringTools.EMPTY_STRING;
}
final char[] result = new char[length];
for (int i = 0; i < length; i++) {
result[i] = sourceCharacters.charAt(random.nextInt(sourceCharacters.length()));
@ -72,14 +130,6 @@ public final class RandomTools {
return String.valueOf(result);
}
public static String randomStr(String sourceCharacters, int length) {
return randomStr(ThreadLocalRandom.current(), sourceCharacters, length);
}
public static String secureRandomStr(String sourceCharacters, int length) {
return randomStr(DEFAULT_SECURE_RANDOM, sourceCharacters, length);
}
private RandomTools() {
throw new IllegalStateException("Utility class");
}

View File

@ -21,6 +21,8 @@ import com.google.common.annotations.Beta;
@Beta
public class StringTools {
public static final String EMPTY_STRING = "";
public static boolean isNotBlank(final String cs) {
if (cs == null || cs.isEmpty()) {
return false;