perf: 优化 AssertTools 代码

This commit is contained in:
2025-10-13 17:36:47 +08:00
parent 8106126e79
commit 27e5650482
5 changed files with 102 additions and 58 deletions

View File

@@ -17,6 +17,7 @@
package xyz.zhouxy.plusone.commons.model; package xyz.zhouxy.plusone.commons.model;
import static xyz.zhouxy.plusone.commons.util.AssertTools.checkArgument; import static xyz.zhouxy.plusone.commons.util.AssertTools.checkArgument;
import static xyz.zhouxy.plusone.commons.util.AssertTools.checkArgumentNotNull;
import java.util.Objects; import java.util.Objects;
import java.util.function.Supplier; import java.util.function.Supplier;
@@ -75,8 +76,8 @@ public abstract class ValidatableStringRecord<T extends ValidatableStringRecord<
* @param errorMessage 正则不匹配时的错误信息 * @param errorMessage 正则不匹配时的错误信息
*/ */
protected ValidatableStringRecord(String value, Pattern pattern, String errorMessage) { protected ValidatableStringRecord(String value, Pattern pattern, String errorMessage) {
checkArgument(Objects.nonNull(value), "The value cannot be null."); checkArgumentNotNull(value, "The value cannot be null.");
checkArgument(Objects.nonNull(pattern), "The pattern cannot be null."); checkArgumentNotNull(pattern, "The pattern cannot be null.");
this.matcher = pattern.matcher(value); this.matcher = pattern.matcher(value);
checkArgument(this.matcher.matches(), errorMessage); checkArgument(this.matcher.matches(), errorMessage);
this.value = value; this.value = value;

View File

@@ -17,6 +17,7 @@
package xyz.zhouxy.plusone.commons.util; package xyz.zhouxy.plusone.commons.util;
import static xyz.zhouxy.plusone.commons.util.AssertTools.checkArgument; import static xyz.zhouxy.plusone.commons.util.AssertTools.checkArgument;
import static xyz.zhouxy.plusone.commons.util.AssertTools.checkArgumentNotNull;
import static xyz.zhouxy.plusone.commons.util.AssertTools.checkNotNull; import static xyz.zhouxy.plusone.commons.util.AssertTools.checkNotNull;
import java.math.BigDecimal; import java.math.BigDecimal;
@@ -491,7 +492,7 @@ public class ArrayTools {
* @return 重复后的数组 * @return 重复后的数组
*/ */
public static char[] repeat(char[] arr, int times, int maxLength) { public static char[] repeat(char[] arr, int times, int maxLength) {
checkArgument(Objects.nonNull(arr)); checkArgumentNotNull(arr);
checkArgument(times >= 0, checkArgument(times >= 0,
"The number of times must be greater than or equal to zero"); "The number of times must be greater than or equal to zero");
checkArgument(maxLength >= 0, checkArgument(maxLength >= 0,
@@ -526,7 +527,7 @@ public class ArrayTools {
* @return 重复后的数组 * @return 重复后的数组
*/ */
public static byte[] repeat(byte[] arr, int times, int maxLength) { public static byte[] repeat(byte[] arr, int times, int maxLength) {
checkArgument(Objects.nonNull(arr)); checkArgumentNotNull(arr);
checkArgument(times >= 0, checkArgument(times >= 0,
"The number of times must be greater than or equal to zero"); "The number of times must be greater than or equal to zero");
checkArgument(maxLength >= 0, checkArgument(maxLength >= 0,
@@ -561,7 +562,7 @@ public class ArrayTools {
* @return 重复后的数组 * @return 重复后的数组
*/ */
public static short[] repeat(short[] arr, int times, int maxLength) { public static short[] repeat(short[] arr, int times, int maxLength) {
checkArgument(Objects.nonNull(arr)); checkArgumentNotNull(arr);
checkArgument(times >= 0, checkArgument(times >= 0,
"The number of times must be greater than or equal to zero"); "The number of times must be greater than or equal to zero");
checkArgument(maxLength >= 0, checkArgument(maxLength >= 0,
@@ -596,7 +597,7 @@ public class ArrayTools {
* @return 重复后的数组 * @return 重复后的数组
*/ */
public static int[] repeat(int[] arr, int times, int maxLength) { public static int[] repeat(int[] arr, int times, int maxLength) {
checkArgument(Objects.nonNull(arr)); checkArgumentNotNull(arr);
checkArgument(times >= 0, checkArgument(times >= 0,
"The number of times must be greater than or equal to zero"); "The number of times must be greater than or equal to zero");
checkArgument(maxLength >= 0, checkArgument(maxLength >= 0,
@@ -631,7 +632,7 @@ public class ArrayTools {
* @return 重复后的数组 * @return 重复后的数组
*/ */
public static long[] repeat(long[] arr, int times, int maxLength) { public static long[] repeat(long[] arr, int times, int maxLength) {
checkArgument(Objects.nonNull(arr)); checkArgumentNotNull(arr);
checkArgument(times >= 0, checkArgument(times >= 0,
"The number of times must be greater than or equal to zero"); "The number of times must be greater than or equal to zero");
checkArgument(maxLength >= 0, checkArgument(maxLength >= 0,
@@ -666,7 +667,7 @@ public class ArrayTools {
* @return 重复后的数组 * @return 重复后的数组
*/ */
public static float[] repeat(float[] arr, int times, int maxLength) { public static float[] repeat(float[] arr, int times, int maxLength) {
checkArgument(Objects.nonNull(arr)); checkArgumentNotNull(arr);
checkArgument(times >= 0, checkArgument(times >= 0,
"The number of times must be greater than or equal to zero"); "The number of times must be greater than or equal to zero");
checkArgument(maxLength >= 0, checkArgument(maxLength >= 0,
@@ -701,7 +702,7 @@ public class ArrayTools {
* @return 重复后的数组 * @return 重复后的数组
*/ */
public static double[] repeat(double[] arr, int times, int maxLength) { public static double[] repeat(double[] arr, int times, int maxLength) {
checkArgument(Objects.nonNull(arr)); checkArgumentNotNull(arr);
checkArgument(times >= 0, checkArgument(times >= 0,
"The number of times must be greater than or equal to zero"); "The number of times must be greater than or equal to zero");
checkArgument(maxLength >= 0, checkArgument(maxLength >= 0,
@@ -750,7 +751,7 @@ public class ArrayTools {
* @param values 填充内容 * @param values 填充内容
*/ */
public static void fill(char[] a, int fromIndex, int toIndex, @Nullable char[] values) { public static void fill(char[] a, int fromIndex, int toIndex, @Nullable char[] values) {
checkArgument(Objects.nonNull(a)); checkArgumentNotNull(a);
if (values == null || values.length == 0) { if (values == null || values.length == 0) {
return; return;
} }
@@ -793,7 +794,7 @@ public class ArrayTools {
* @param values 填充内容 * @param values 填充内容
*/ */
public static void fill(byte[] a, int fromIndex, int toIndex, @Nullable byte[] values) { public static void fill(byte[] a, int fromIndex, int toIndex, @Nullable byte[] values) {
checkArgument(Objects.nonNull(a)); checkArgumentNotNull(a);
if (values == null || values.length == 0) { if (values == null || values.length == 0) {
return; return;
} }
@@ -836,7 +837,7 @@ public class ArrayTools {
* @param values 填充内容 * @param values 填充内容
*/ */
public static void fill(short[] a, int fromIndex, int toIndex, @Nullable short[] values) { public static void fill(short[] a, int fromIndex, int toIndex, @Nullable short[] values) {
checkArgument(Objects.nonNull(a)); checkArgumentNotNull(a);
if (values == null || values.length == 0) { if (values == null || values.length == 0) {
return; return;
} }
@@ -879,7 +880,7 @@ public class ArrayTools {
* @param values 填充内容 * @param values 填充内容
*/ */
public static void fill(int[] a, int fromIndex, int toIndex, @Nullable int[] values) { public static void fill(int[] a, int fromIndex, int toIndex, @Nullable int[] values) {
checkArgument(Objects.nonNull(a)); checkArgumentNotNull(a);
if (values == null || values.length == 0) { if (values == null || values.length == 0) {
return; return;
} }
@@ -922,7 +923,7 @@ public class ArrayTools {
* @param values 填充内容 * @param values 填充内容
*/ */
public static void fill(long[] a, int fromIndex, int toIndex, @Nullable long[] values) { public static void fill(long[] a, int fromIndex, int toIndex, @Nullable long[] values) {
checkArgument(Objects.nonNull(a)); checkArgumentNotNull(a);
if (values == null || values.length == 0) { if (values == null || values.length == 0) {
return; return;
} }
@@ -965,7 +966,7 @@ public class ArrayTools {
* @param values 填充内容 * @param values 填充内容
*/ */
public static void fill(float[] a, int fromIndex, int toIndex, @Nullable float[] values) { public static void fill(float[] a, int fromIndex, int toIndex, @Nullable float[] values) {
checkArgument(Objects.nonNull(a)); checkArgumentNotNull(a);
if (values == null || values.length == 0) { if (values == null || values.length == 0) {
return; return;
} }
@@ -1008,7 +1009,7 @@ public class ArrayTools {
* @param values 填充内容 * @param values 填充内容
*/ */
public static void fill(double[] a, int fromIndex, int toIndex, @Nullable double[] values) { public static void fill(double[] a, int fromIndex, int toIndex, @Nullable double[] values) {
checkArgument(Objects.nonNull(a)); checkArgumentNotNull(a);
if (values == null || values.length == 0) { if (values == null || values.length == 0) {
return; return;
} }
@@ -1063,7 +1064,7 @@ public class ArrayTools {
* @param values 填充内容 * @param values 填充内容
*/ */
private static <T> void fillInternal(T[] a, int fromIndex, int toIndex, @Nullable T[] values) { private static <T> void fillInternal(T[] a, int fromIndex, int toIndex, @Nullable T[] values) {
checkArgument(Objects.nonNull(a)); checkArgumentNotNull(a);
if (values == null || values.length == 0) { if (values == null || values.length == 0) {
return; return;
} }

View File

@@ -54,7 +54,9 @@ public class AssertTools {
* @throws IllegalArgumentException 当条件不满足时抛出 * @throws IllegalArgumentException 当条件不满足时抛出
*/ */
public static void checkArgument(boolean condition) { public static void checkArgument(boolean condition) {
checkCondition(condition, IllegalArgumentException::new); if (!condition) {
throw new IllegalArgumentException();
}
} }
/** /**
@@ -65,7 +67,9 @@ public class AssertTools {
* @throws IllegalArgumentException 当条件不满足时抛出 * @throws IllegalArgumentException 当条件不满足时抛出
*/ */
public static void checkArgument(boolean condition, @Nullable String errorMessage) { public static void checkArgument(boolean condition, @Nullable String errorMessage) {
checkCondition(condition, () -> new IllegalArgumentException(errorMessage)); if (!condition) {
throw new IllegalArgumentException(errorMessage);
}
} }
/** /**
@@ -76,7 +80,9 @@ public class AssertTools {
* @throws IllegalArgumentException 当条件不满足时抛出 * @throws IllegalArgumentException 当条件不满足时抛出
*/ */
public static void checkArgument(boolean condition, Supplier<String> errorMessageSupplier) { public static void checkArgument(boolean condition, Supplier<String> errorMessageSupplier) {
checkCondition(condition, () -> new IllegalArgumentException(errorMessageSupplier.get())); if (!condition) {
throw new IllegalArgumentException(errorMessageSupplier.get());
}
} }
/** /**
@@ -89,8 +95,9 @@ public class AssertTools {
*/ */
public static void checkArgument(boolean condition, public static void checkArgument(boolean condition,
String errorMessageTemplate, Object... errorMessageArgs) { String errorMessageTemplate, Object... errorMessageArgs) {
checkCondition(condition, if (!condition) {
() -> new IllegalArgumentException(String.format(errorMessageTemplate, errorMessageArgs))); throw new IllegalArgumentException(String.format(errorMessageTemplate, errorMessageArgs));
}
} }
// ================================ // ================================
@@ -109,7 +116,9 @@ public class AssertTools {
* @throws IllegalArgumentException 当 {@code obj} 为 {@code null} 时抛出 * @throws IllegalArgumentException 当 {@code obj} 为 {@code null} 时抛出
*/ */
public static <T> T checkArgumentNotNull(@Nullable T obj) { public static <T> T checkArgumentNotNull(@Nullable T obj) {
checkCondition(obj != null, IllegalArgumentException::new); if (obj == null) {
throw new IllegalArgumentException();
}
return obj; return obj;
} }
@@ -122,7 +131,9 @@ public class AssertTools {
* @throws IllegalArgumentException 当 {@code obj} 为 {@code null} 时抛出 * @throws IllegalArgumentException 当 {@code obj} 为 {@code null} 时抛出
*/ */
public static <T> T checkArgumentNotNull(@Nullable T obj, String errorMessage) { public static <T> T checkArgumentNotNull(@Nullable T obj, String errorMessage) {
checkCondition(obj != null, () -> new IllegalArgumentException(errorMessage)); if (obj == null) {
throw new IllegalArgumentException(errorMessage);
}
return obj; return obj;
} }
@@ -135,7 +146,9 @@ public class AssertTools {
* @throws IllegalArgumentException 当 {@code obj} 为 {@code null} 时抛出 * @throws IllegalArgumentException 当 {@code obj} 为 {@code null} 时抛出
*/ */
public static <T> T checkArgumentNotNull(@Nullable T obj, Supplier<String> errorMessageSupplier) { public static <T> T checkArgumentNotNull(@Nullable T obj, Supplier<String> errorMessageSupplier) {
checkCondition(obj != null, () -> new IllegalArgumentException(errorMessageSupplier.get())); if (obj == null) {
throw new IllegalArgumentException(errorMessageSupplier.get());
}
return obj; return obj;
} }
@@ -150,8 +163,9 @@ public class AssertTools {
*/ */
public static <T> T checkArgumentNotNull(@Nullable T obj, public static <T> T checkArgumentNotNull(@Nullable T obj,
String errorMessageTemplate, Object... errorMessageArgs) { String errorMessageTemplate, Object... errorMessageArgs) {
checkCondition(obj != null, if (obj == null) {
() -> new IllegalArgumentException(String.format(errorMessageTemplate, errorMessageArgs))); throw new IllegalArgumentException(String.format(errorMessageTemplate, errorMessageArgs));
}
return obj; return obj;
} }
@@ -170,7 +184,9 @@ public class AssertTools {
* @throws IllegalStateException 当条件不满足时抛出 * @throws IllegalStateException 当条件不满足时抛出
*/ */
public static void checkState(boolean condition) { public static void checkState(boolean condition) {
checkCondition(condition, IllegalStateException::new); if (!condition) {
throw new IllegalStateException();
}
} }
/** /**
@@ -181,7 +197,9 @@ public class AssertTools {
* @throws IllegalStateException 当条件不满足时抛出 * @throws IllegalStateException 当条件不满足时抛出
*/ */
public static void checkState(boolean condition, @Nullable String errorMessage) { public static void checkState(boolean condition, @Nullable String errorMessage) {
checkCondition(condition, () -> new IllegalStateException(errorMessage)); if (!condition) {
throw new IllegalStateException(errorMessage);
}
} }
/** /**
@@ -192,7 +210,9 @@ public class AssertTools {
* @throws IllegalStateException 当条件不满足时抛出 * @throws IllegalStateException 当条件不满足时抛出
*/ */
public static void checkState(boolean condition, Supplier<String> errorMessageSupplier) { public static void checkState(boolean condition, Supplier<String> errorMessageSupplier) {
checkCondition(condition, () -> new IllegalStateException(errorMessageSupplier.get())); if (!condition) {
throw new IllegalStateException(errorMessageSupplier.get());
}
} }
/** /**
@@ -205,8 +225,9 @@ public class AssertTools {
*/ */
public static void checkState(boolean condition, public static void checkState(boolean condition,
String errorMessageTemplate, Object... errorMessageArgs) { String errorMessageTemplate, Object... errorMessageArgs) {
checkCondition(condition, if (!condition) {
() -> new IllegalStateException(String.format(errorMessageTemplate, errorMessageArgs))); throw new IllegalStateException(String.format(errorMessageTemplate, errorMessageArgs));
}
} }
// ================================ // ================================
@@ -225,7 +246,9 @@ public class AssertTools {
* @throws NullPointerException 当 {@code obj} 为 {@code null} 时抛出 * @throws NullPointerException 当 {@code obj} 为 {@code null} 时抛出
*/ */
public static <T> void checkNotNull(@Nullable T obj) { public static <T> void checkNotNull(@Nullable T obj) {
checkCondition(obj != null, NullPointerException::new); if (obj == null) {
throw new NullPointerException();
}
} }
/** /**
@@ -237,7 +260,9 @@ public class AssertTools {
* @throws NullPointerException 当 {@code obj} 为 {@code null} 时抛出 * @throws NullPointerException 当 {@code obj} 为 {@code null} 时抛出
*/ */
public static <T> void checkNotNull(@Nullable T obj, String errorMessage) { public static <T> void checkNotNull(@Nullable T obj, String errorMessage) {
checkCondition(obj != null, () -> new NullPointerException(errorMessage)); if (obj == null) {
throw new NullPointerException(errorMessage);
}
} }
/** /**
@@ -249,7 +274,9 @@ public class AssertTools {
* @throws NullPointerException 当 {@code obj} 为 {@code null} 时抛出 * @throws NullPointerException 当 {@code obj} 为 {@code null} 时抛出
*/ */
public static <T> void checkNotNull(@Nullable T obj, Supplier<String> errorMessageSupplier) { public static <T> void checkNotNull(@Nullable T obj, Supplier<String> errorMessageSupplier) {
checkCondition(obj != null, () -> new NullPointerException(errorMessageSupplier.get())); if (obj == null) {
throw new NullPointerException(errorMessageSupplier.get());
}
} }
/** /**
@@ -263,8 +290,9 @@ public class AssertTools {
*/ */
public static <T> void checkNotNull(@Nullable T obj, public static <T> void checkNotNull(@Nullable T obj,
String errorMessageTemplate, Object... errorMessageArgs) { String errorMessageTemplate, Object... errorMessageArgs) {
checkCondition(obj != null, if (obj == null) {
() -> new NullPointerException(String.format(errorMessageTemplate, errorMessageArgs))); throw new NullPointerException(String.format(errorMessageTemplate, errorMessageArgs));
}
} }
// ================================ // ================================
@@ -285,7 +313,9 @@ public class AssertTools {
*/ */
public static <T> T checkExists(@Nullable T obj) public static <T> T checkExists(@Nullable T obj)
throws DataNotExistsException { throws DataNotExistsException {
checkCondition(obj != null, DataNotExistsException::new); if (obj == null) {
throw new DataNotExistsException();
}
return obj; return obj;
} }
@@ -300,7 +330,9 @@ public class AssertTools {
*/ */
public static <T> T checkExists(@Nullable T obj, String errorMessage) public static <T> T checkExists(@Nullable T obj, String errorMessage)
throws DataNotExistsException { throws DataNotExistsException {
checkCondition(obj != null, () -> new DataNotExistsException(errorMessage)); if (obj == null) {
throw new DataNotExistsException(errorMessage);
}
return obj; return obj;
} }
@@ -315,7 +347,9 @@ public class AssertTools {
*/ */
public static <T> T checkExists(@Nullable T obj, Supplier<String> errorMessageSupplier) public static <T> T checkExists(@Nullable T obj, Supplier<String> errorMessageSupplier)
throws DataNotExistsException { throws DataNotExistsException {
checkCondition(obj != null, () -> new DataNotExistsException(errorMessageSupplier.get())); if (obj == null) {
throw new DataNotExistsException(errorMessageSupplier.get());
}
return obj; return obj;
} }
@@ -332,8 +366,9 @@ public class AssertTools {
public static <T> T checkExists(@Nullable T obj, public static <T> T checkExists(@Nullable T obj,
String errorMessageTemplate, Object... errorMessageArgs) String errorMessageTemplate, Object... errorMessageArgs)
throws DataNotExistsException { throws DataNotExistsException {
checkCondition(obj != null, if (obj == null) {
() -> new DataNotExistsException(String.format(errorMessageTemplate, errorMessageArgs))); throw new DataNotExistsException(String.format(errorMessageTemplate, errorMessageArgs));
}
return obj; return obj;
} }
@@ -347,7 +382,9 @@ public class AssertTools {
*/ */
public static <T> T checkExists(Optional<T> optional) public static <T> T checkExists(Optional<T> optional)
throws DataNotExistsException { throws DataNotExistsException {
checkCondition(optional.isPresent(), DataNotExistsException::new); if (!optional.isPresent()) {
throw new DataNotExistsException();
}
return optional.get(); return optional.get();
} }
@@ -362,7 +399,9 @@ public class AssertTools {
*/ */
public static <T> T checkExists(Optional<T> optional, String errorMessage) public static <T> T checkExists(Optional<T> optional, String errorMessage)
throws DataNotExistsException { throws DataNotExistsException {
checkCondition(optional.isPresent(), () -> new DataNotExistsException(errorMessage)); if (!optional.isPresent()) {
throw new DataNotExistsException(errorMessage);
}
return optional.get(); return optional.get();
} }
@@ -377,7 +416,9 @@ public class AssertTools {
*/ */
public static <T> T checkExists(Optional<T> optional, Supplier<String> errorMessageSupplier) public static <T> T checkExists(Optional<T> optional, Supplier<String> errorMessageSupplier)
throws DataNotExistsException { throws DataNotExistsException {
checkCondition(optional.isPresent(), () -> new DataNotExistsException(errorMessageSupplier.get())); if (!optional.isPresent()) {
throw new DataNotExistsException(errorMessageSupplier.get());
}
return optional.get(); return optional.get();
} }
@@ -394,8 +435,9 @@ public class AssertTools {
public static <T> T checkExists(Optional<T> optional, public static <T> T checkExists(Optional<T> optional,
String errorMessageTemplate, Object... errorMessageArgs) String errorMessageTemplate, Object... errorMessageArgs)
throws DataNotExistsException { throws DataNotExistsException {
checkCondition(optional.isPresent(), if (!optional.isPresent()) {
() -> new DataNotExistsException(String.format(errorMessageTemplate, errorMessageArgs))); throw new DataNotExistsException(String.format(errorMessageTemplate, errorMessageArgs));
}
return optional.get(); return optional.get();
} }

View File

@@ -17,10 +17,10 @@
package xyz.zhouxy.plusone.commons.util; package xyz.zhouxy.plusone.commons.util;
import static xyz.zhouxy.plusone.commons.util.AssertTools.checkArgument; import static xyz.zhouxy.plusone.commons.util.AssertTools.checkArgument;
import static xyz.zhouxy.plusone.commons.util.AssertTools.checkArgumentNotNull;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom; import java.security.SecureRandom;
import java.util.Objects;
import java.util.Random; import java.util.Random;
import java.util.concurrent.ThreadLocalRandom; import java.util.concurrent.ThreadLocalRandom;
@@ -69,20 +69,20 @@ public final class RandomTools {
* @return 随机字符串 * @return 随机字符串
*/ */
public static String randomStr(Random random, char[] sourceCharacters, int length) { public static String randomStr(Random random, char[] sourceCharacters, int length) {
checkArgument(Objects.nonNull(random), "Random cannot be null."); checkArgumentNotNull(random, "Random cannot be null.");
checkArgument(Objects.nonNull(sourceCharacters), "Source characters cannot be null."); checkArgumentNotNull(sourceCharacters, "Source characters cannot be null.");
checkArgument(length >= 0, "The length should be greater than or equal to zero."); checkArgument(length >= 0, "The length should be greater than or equal to zero.");
return randomStrInternal(random, sourceCharacters, length); return randomStrInternal(random, sourceCharacters, length);
} }
public static String randomStr(char[] sourceCharacters, int length) { public static String randomStr(char[] sourceCharacters, int length) {
checkArgument(Objects.nonNull(sourceCharacters), "Source characters cannot be null."); checkArgumentNotNull(sourceCharacters, "Source characters cannot be null.");
checkArgument(length >= 0, "The length should be greater than or equal to zero."); checkArgument(length >= 0, "The length should be greater than or equal to zero.");
return randomStrInternal(ThreadLocalRandom.current(), sourceCharacters, length); return randomStrInternal(ThreadLocalRandom.current(), sourceCharacters, length);
} }
public static String secureRandomStr(char[] sourceCharacters, int length) { public static String secureRandomStr(char[] sourceCharacters, int length) {
checkArgument(Objects.nonNull(sourceCharacters), "Source characters cannot be null."); checkArgumentNotNull(sourceCharacters, "Source characters cannot be null.");
checkArgument(length >= 0, "The length should be greater than or equal to zero."); checkArgument(length >= 0, "The length should be greater than or equal to zero.");
return randomStrInternal(DEFAULT_SECURE_RANDOM, sourceCharacters, length); return randomStrInternal(DEFAULT_SECURE_RANDOM, sourceCharacters, length);
} }
@@ -98,20 +98,20 @@ public final class RandomTools {
* @return 随机字符串 * @return 随机字符串
*/ */
public static String randomStr(Random random, String sourceCharacters, int length) { public static String randomStr(Random random, String sourceCharacters, int length) {
checkArgument(Objects.nonNull(random), "Random cannot be null."); checkArgumentNotNull(random, "Random cannot be null.");
checkArgument(Objects.nonNull(sourceCharacters), "Source characters cannot be null."); checkArgumentNotNull(sourceCharacters, "Source characters cannot be null.");
checkArgument(length >= 0, "The length should be greater than or equal to zero."); checkArgument(length >= 0, "The length should be greater than or equal to zero.");
return randomStrInternal(random, sourceCharacters, length); return randomStrInternal(random, sourceCharacters, length);
} }
public static String randomStr(String sourceCharacters, int length) { public static String randomStr(String sourceCharacters, int length) {
checkArgument(Objects.nonNull(sourceCharacters), "Source characters cannot be null."); checkArgumentNotNull(sourceCharacters, "Source characters cannot be null.");
checkArgument(length >= 0, "The length should be greater than or equal to zero."); checkArgument(length >= 0, "The length should be greater than or equal to zero.");
return randomStrInternal(ThreadLocalRandom.current(), sourceCharacters, length); return randomStrInternal(ThreadLocalRandom.current(), sourceCharacters, length);
} }
public static String secureRandomStr(String sourceCharacters, int length) { public static String secureRandomStr(String sourceCharacters, int length) {
checkArgument(Objects.nonNull(sourceCharacters), "Source characters cannot be null."); checkArgumentNotNull(sourceCharacters, "Source characters cannot be null.");
checkArgument(length >= 0, "The length should be greater than or equal to zero."); checkArgument(length >= 0, "The length should be greater than or equal to zero.");
return randomStrInternal(DEFAULT_SECURE_RANDOM, sourceCharacters, length); return randomStrInternal(DEFAULT_SECURE_RANDOM, sourceCharacters, length);
} }

View File

@@ -17,10 +17,10 @@
package xyz.zhouxy.plusone.commons.util; package xyz.zhouxy.plusone.commons.util;
import static xyz.zhouxy.plusone.commons.util.AssertTools.checkArgument; import static xyz.zhouxy.plusone.commons.util.AssertTools.checkArgument;
import static xyz.zhouxy.plusone.commons.util.AssertTools.checkArgumentNotNull;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.util.Objects;
import javax.annotation.Nullable; import javax.annotation.Nullable;
@@ -112,7 +112,7 @@ public class StringTools {
* @return 结果 * @return 结果
*/ */
public static String repeat(final String str, int times, int maxLength) { public static String repeat(final String str, int times, int maxLength) {
checkArgument(Objects.nonNull(str)); checkArgumentNotNull(str);
return String.valueOf(ArrayTools.repeat(str.toCharArray(), times, maxLength)); return String.valueOf(ArrayTools.repeat(str.toCharArray(), times, maxLength));
} }