2 Commits

6 changed files with 223 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,13 +17,16 @@
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;
import com.google.common.collect.BoundType;
import com.google.common.collect.Range;
/** /**
* 随机工具类 * 随机工具类
* <p> * <p>
@@ -69,20 +72,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,24 +101,70 @@ 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);
} }
/**
* 生成随机整数
*
* @param min 最小值(包含)
* @param max 最大值(不包含)
* @return 在区间 {@code [min, max)} 内的随机整数
*/
public static int randomInt(int min, int max) {
return randomIntInternal(ThreadLocalRandom.current(), min, max);
}
/**
* 生成随机整数
*
* @param min 最小值(包含)
* @param max 最大值(不包含)
* @return 在区间 {@code [min, max)} 内的随机整数
*/
public static int randomInt(Random random, int min, int max) {
checkArgumentNotNull(random, "Random cannot be null.");
return randomIntInternal(random, min, max);
}
/**
* 生成随机整数
*
* @param range 整数区间
* @return 在指定区间内的随机整数
*/
public static int randomInt(Range<Integer> range) {
checkArgumentNotNull(range, "Range cannot be null.");
return randomIntInternal(ThreadLocalRandom.current(), range);
}
/**
* 生成随机整数
*
* @param range 整数区间
* @return 在指定区间内的随机整数
*/
public static int randomInt(Random random, Range<Integer> range) {
checkArgumentNotNull(random, "Random cannot be null.");
checkArgumentNotNull(range, "Range cannot be null.");
return randomIntInternal(random, range);
}
/** /**
* 使用传入的随机数生成器,生成指定长度的字符串 * 使用传入的随机数生成器,生成指定长度的字符串
* *
@@ -158,6 +207,31 @@ public final class RandomTools {
return String.valueOf(result); return String.valueOf(result);
} }
/**
* 生成随机整数
*
* @param min 最小值(包含)
* @param max 最大值(不包含)
* @return 在区间 {@code [min, max)} 内的随机整数
*/
private static int randomIntInternal(Random random, int min, int max) {
return random.nextInt(max - min) + min;
}
/**
* 生成随机整数
*
* @param range 整数区间
* @return 在指定区间内的随机整数
*/
private static int randomIntInternal(Random random, Range<Integer> range) {
Integer lowerEndpoint = range.lowerEndpoint();
Integer upperEndpoint = range.upperEndpoint();
int min = range.lowerBoundType() == BoundType.CLOSED ? lowerEndpoint : lowerEndpoint + 1;
int max = range.upperBoundType() == BoundType.OPEN ? upperEndpoint : upperEndpoint + 1;
return random.nextInt(max - min) + min;
}
private RandomTools() { private RandomTools() {
throw new IllegalStateException("Utility class"); throw new IllegalStateException("Utility class");
} }

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));
} }

View File

@@ -21,6 +21,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertInstanceOf; import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.lang.reflect.Constructor; import java.lang.reflect.Constructor;
import java.security.SecureRandom; import java.security.SecureRandom;
@@ -30,6 +31,8 @@ import java.util.Random;
import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import com.google.common.collect.Range;
@SuppressWarnings("null") @SuppressWarnings("null")
public class RandomToolsTests { public class RandomToolsTests {
@@ -107,6 +110,50 @@ public class RandomToolsTests {
assertEquals(5, result.length()); assertEquals(5, result.length());
} }
@Test
public void randomInt_WithMinAndMax() {
for (int i = 0; i < 1000_0000; i++) {
int r = RandomTools.randomInt(random, -2, 3);
assertTrue(r >= -2 && r < 3);
}
}
@Test
public void randomInt_WithClosedOpenRange() {
Range<Integer> co = Range.closedOpen(-2, 3);
for (int i = 0; i < 1000_0000; i++) {
int rco = RandomTools.randomInt(random, co);
assertTrue(rco >= -2 && rco < 3);
}
}
@Test
public void randomInt_WithClosedRange() {
Range<Integer> cc = Range.closed(-2, 3);
for (int i = 0; i < 1000_0000; i++) {
int rcc = RandomTools.randomInt(random, cc);
assertTrue(rcc >= -2 && rcc <= 3);
}
}
@Test
public void randomInt_WithOpenClosedRange() {
Range<Integer> oc = Range.openClosed(-2, 3);
for (int i = 0; i < 1000_0000; i++) {
int roc = RandomTools.randomInt(random, oc);
assertTrue(roc > -2 && roc <= 3);
}
}
@Test
public void randomInt_WithOpenRange() {
Range<Integer> oo = Range.open(-2, 3);
for (int i = 0; i < 1000_0000; i++) {
int roo = RandomTools.randomInt(random, oo);
assertTrue(roo > -2 && roo < 3);
}
}
@Test @Test
void test_constructor_isNotAccessible_ThrowsIllegalStateException() { void test_constructor_isNotAccessible_ThrowsIllegalStateException() {
Constructor<?>[] constructors = RandomTools.class.getDeclaredConstructors(); Constructor<?>[] constructors = RandomTools.class.getDeclaredConstructors();