diff --git a/plusone-commons/src/main/java/xyz/zhouxy/plusone/commons/model/ValidatableStringRecord.java b/plusone-commons/src/main/java/xyz/zhouxy/plusone/commons/model/ValidatableStringRecord.java index 6c0ebae..212ec85 100644 --- a/plusone-commons/src/main/java/xyz/zhouxy/plusone/commons/model/ValidatableStringRecord.java +++ b/plusone-commons/src/main/java/xyz/zhouxy/plusone/commons/model/ValidatableStringRecord.java @@ -17,6 +17,7 @@ package xyz.zhouxy.plusone.commons.model; 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.function.Supplier; @@ -75,8 +76,8 @@ public abstract class ValidatableStringRecord= 0, "The number of times must be greater than or equal to zero"); checkArgument(maxLength >= 0, @@ -526,7 +527,7 @@ public class ArrayTools { * @return 重复后的数组 */ public static byte[] repeat(byte[] arr, int times, int maxLength) { - checkArgument(Objects.nonNull(arr)); + checkArgumentNotNull(arr); checkArgument(times >= 0, "The number of times must be greater than or equal to zero"); checkArgument(maxLength >= 0, @@ -561,7 +562,7 @@ public class ArrayTools { * @return 重复后的数组 */ public static short[] repeat(short[] arr, int times, int maxLength) { - checkArgument(Objects.nonNull(arr)); + checkArgumentNotNull(arr); checkArgument(times >= 0, "The number of times must be greater than or equal to zero"); checkArgument(maxLength >= 0, @@ -596,7 +597,7 @@ public class ArrayTools { * @return 重复后的数组 */ public static int[] repeat(int[] arr, int times, int maxLength) { - checkArgument(Objects.nonNull(arr)); + checkArgumentNotNull(arr); checkArgument(times >= 0, "The number of times must be greater than or equal to zero"); checkArgument(maxLength >= 0, @@ -631,7 +632,7 @@ public class ArrayTools { * @return 重复后的数组 */ public static long[] repeat(long[] arr, int times, int maxLength) { - checkArgument(Objects.nonNull(arr)); + checkArgumentNotNull(arr); checkArgument(times >= 0, "The number of times must be greater than or equal to zero"); checkArgument(maxLength >= 0, @@ -666,7 +667,7 @@ public class ArrayTools { * @return 重复后的数组 */ public static float[] repeat(float[] arr, int times, int maxLength) { - checkArgument(Objects.nonNull(arr)); + checkArgumentNotNull(arr); checkArgument(times >= 0, "The number of times must be greater than or equal to zero"); checkArgument(maxLength >= 0, @@ -701,7 +702,7 @@ public class ArrayTools { * @return 重复后的数组 */ public static double[] repeat(double[] arr, int times, int maxLength) { - checkArgument(Objects.nonNull(arr)); + checkArgumentNotNull(arr); checkArgument(times >= 0, "The number of times must be greater than or equal to zero"); checkArgument(maxLength >= 0, @@ -750,7 +751,7 @@ public class ArrayTools { * @param 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) { return; } @@ -793,7 +794,7 @@ public class ArrayTools { * @param 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) { return; } @@ -836,7 +837,7 @@ public class ArrayTools { * @param 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) { return; } @@ -879,7 +880,7 @@ public class ArrayTools { * @param 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) { return; } @@ -922,7 +923,7 @@ public class ArrayTools { * @param 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) { return; } @@ -965,7 +966,7 @@ public class ArrayTools { * @param 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) { return; } @@ -1008,7 +1009,7 @@ public class ArrayTools { * @param 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) { return; } @@ -1063,7 +1064,7 @@ public class ArrayTools { * @param values 填充内容 */ private static void fillInternal(T[] a, int fromIndex, int toIndex, @Nullable T[] values) { - checkArgument(Objects.nonNull(a)); + checkArgumentNotNull(a); if (values == null || values.length == 0) { return; } diff --git a/plusone-commons/src/main/java/xyz/zhouxy/plusone/commons/util/AssertTools.java b/plusone-commons/src/main/java/xyz/zhouxy/plusone/commons/util/AssertTools.java index cefd599..ade6530 100644 --- a/plusone-commons/src/main/java/xyz/zhouxy/plusone/commons/util/AssertTools.java +++ b/plusone-commons/src/main/java/xyz/zhouxy/plusone/commons/util/AssertTools.java @@ -54,7 +54,9 @@ public class AssertTools { * @throws IllegalArgumentException 当条件不满足时抛出 */ public static void checkArgument(boolean condition) { - checkCondition(condition, IllegalArgumentException::new); + if (!condition) { + throw new IllegalArgumentException(); + } } /** @@ -65,7 +67,9 @@ public class AssertTools { * @throws IllegalArgumentException 当条件不满足时抛出 */ 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 当条件不满足时抛出 */ public static void checkArgument(boolean condition, Supplier 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, String errorMessageTemplate, Object... errorMessageArgs) { - checkCondition(condition, - () -> new IllegalArgumentException(String.format(errorMessageTemplate, errorMessageArgs))); + if (!condition) { + throw new IllegalArgumentException(String.format(errorMessageTemplate, errorMessageArgs)); + } } // ================================ @@ -109,7 +116,9 @@ public class AssertTools { * @throws IllegalArgumentException 当 {@code obj} 为 {@code null} 时抛出 */ public static T checkArgumentNotNull(@Nullable T obj) { - checkCondition(obj != null, IllegalArgumentException::new); + if (obj == null) { + throw new IllegalArgumentException(); + } return obj; } @@ -122,7 +131,9 @@ public class AssertTools { * @throws IllegalArgumentException 当 {@code obj} 为 {@code null} 时抛出 */ public static T checkArgumentNotNull(@Nullable T obj, String errorMessage) { - checkCondition(obj != null, () -> new IllegalArgumentException(errorMessage)); + if (obj == null) { + throw new IllegalArgumentException(errorMessage); + } return obj; } @@ -135,7 +146,9 @@ public class AssertTools { * @throws IllegalArgumentException 当 {@code obj} 为 {@code null} 时抛出 */ public static T checkArgumentNotNull(@Nullable T obj, Supplier errorMessageSupplier) { - checkCondition(obj != null, () -> new IllegalArgumentException(errorMessageSupplier.get())); + if (obj == null) { + throw new IllegalArgumentException(errorMessageSupplier.get()); + } return obj; } @@ -150,8 +163,9 @@ public class AssertTools { */ public static T checkArgumentNotNull(@Nullable T obj, String errorMessageTemplate, Object... errorMessageArgs) { - checkCondition(obj != null, - () -> new IllegalArgumentException(String.format(errorMessageTemplate, errorMessageArgs))); + if (obj == null) { + throw new IllegalArgumentException(String.format(errorMessageTemplate, errorMessageArgs)); + } return obj; } @@ -170,7 +184,9 @@ public class AssertTools { * @throws IllegalStateException 当条件不满足时抛出 */ public static void checkState(boolean condition) { - checkCondition(condition, IllegalStateException::new); + if (!condition) { + throw new IllegalStateException(); + } } /** @@ -181,7 +197,9 @@ public class AssertTools { * @throws IllegalStateException 当条件不满足时抛出 */ 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 当条件不满足时抛出 */ public static void checkState(boolean condition, Supplier 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, String errorMessageTemplate, Object... errorMessageArgs) { - checkCondition(condition, - () -> new IllegalStateException(String.format(errorMessageTemplate, errorMessageArgs))); + if (!condition) { + throw new IllegalStateException(String.format(errorMessageTemplate, errorMessageArgs)); + } } // ================================ @@ -225,7 +246,9 @@ public class AssertTools { * @throws NullPointerException 当 {@code obj} 为 {@code null} 时抛出 */ public static 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} 时抛出 */ public static 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} 时抛出 */ public static void checkNotNull(@Nullable T obj, Supplier 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 void checkNotNull(@Nullable T obj, String errorMessageTemplate, Object... errorMessageArgs) { - checkCondition(obj != null, - () -> new NullPointerException(String.format(errorMessageTemplate, errorMessageArgs))); + if (obj == null) { + throw new NullPointerException(String.format(errorMessageTemplate, errorMessageArgs)); + } } // ================================ @@ -285,7 +313,9 @@ public class AssertTools { */ public static T checkExists(@Nullable T obj) throws DataNotExistsException { - checkCondition(obj != null, DataNotExistsException::new); + if (obj == null) { + throw new DataNotExistsException(); + } return obj; } @@ -300,7 +330,9 @@ public class AssertTools { */ public static T checkExists(@Nullable T obj, String errorMessage) throws DataNotExistsException { - checkCondition(obj != null, () -> new DataNotExistsException(errorMessage)); + if (obj == null) { + throw new DataNotExistsException(errorMessage); + } return obj; } @@ -315,7 +347,9 @@ public class AssertTools { */ public static T checkExists(@Nullable T obj, Supplier errorMessageSupplier) throws DataNotExistsException { - checkCondition(obj != null, () -> new DataNotExistsException(errorMessageSupplier.get())); + if (obj == null) { + throw new DataNotExistsException(errorMessageSupplier.get()); + } return obj; } @@ -332,8 +366,9 @@ public class AssertTools { public static T checkExists(@Nullable T obj, String errorMessageTemplate, Object... errorMessageArgs) throws DataNotExistsException { - checkCondition(obj != null, - () -> new DataNotExistsException(String.format(errorMessageTemplate, errorMessageArgs))); + if (obj == null) { + throw new DataNotExistsException(String.format(errorMessageTemplate, errorMessageArgs)); + } return obj; } @@ -347,7 +382,9 @@ public class AssertTools { */ public static T checkExists(Optional optional) throws DataNotExistsException { - checkCondition(optional.isPresent(), DataNotExistsException::new); + if (!optional.isPresent()) { + throw new DataNotExistsException(); + } return optional.get(); } @@ -362,7 +399,9 @@ public class AssertTools { */ public static T checkExists(Optional optional, String errorMessage) throws DataNotExistsException { - checkCondition(optional.isPresent(), () -> new DataNotExistsException(errorMessage)); + if (!optional.isPresent()) { + throw new DataNotExistsException(errorMessage); + } return optional.get(); } @@ -377,7 +416,9 @@ public class AssertTools { */ public static T checkExists(Optional optional, Supplier errorMessageSupplier) throws DataNotExistsException { - checkCondition(optional.isPresent(), () -> new DataNotExistsException(errorMessageSupplier.get())); + if (!optional.isPresent()) { + throw new DataNotExistsException(errorMessageSupplier.get()); + } return optional.get(); } @@ -394,8 +435,9 @@ public class AssertTools { public static T checkExists(Optional optional, String errorMessageTemplate, Object... errorMessageArgs) throws DataNotExistsException { - checkCondition(optional.isPresent(), - () -> new DataNotExistsException(String.format(errorMessageTemplate, errorMessageArgs))); + if (!optional.isPresent()) { + throw new DataNotExistsException(String.format(errorMessageTemplate, errorMessageArgs)); + } return optional.get(); } diff --git a/plusone-commons/src/main/java/xyz/zhouxy/plusone/commons/util/RandomTools.java b/plusone-commons/src/main/java/xyz/zhouxy/plusone/commons/util/RandomTools.java index c2f43c3..376cbca 100644 --- a/plusone-commons/src/main/java/xyz/zhouxy/plusone/commons/util/RandomTools.java +++ b/plusone-commons/src/main/java/xyz/zhouxy/plusone/commons/util/RandomTools.java @@ -17,10 +17,10 @@ package xyz.zhouxy.plusone.commons.util; 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.SecureRandom; -import java.util.Objects; import java.util.Random; import java.util.concurrent.ThreadLocalRandom; @@ -69,20 +69,20 @@ public final class RandomTools { * @return 随机字符串 */ public static String randomStr(Random random, char[] sourceCharacters, int length) { - checkArgument(Objects.nonNull(random), "Random cannot be null."); - checkArgument(Objects.nonNull(sourceCharacters), "Source characters cannot be null."); + checkArgumentNotNull(random, "Random cannot be null."); + checkArgumentNotNull(sourceCharacters, "Source characters cannot be null."); checkArgument(length >= 0, "The length should be greater than or equal to zero."); return randomStrInternal(random, sourceCharacters, 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."); return randomStrInternal(ThreadLocalRandom.current(), sourceCharacters, 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."); return randomStrInternal(DEFAULT_SECURE_RANDOM, sourceCharacters, length); } @@ -98,20 +98,20 @@ public final class RandomTools { * @return 随机字符串 */ public static String randomStr(Random random, String sourceCharacters, int length) { - checkArgument(Objects.nonNull(random), "Random cannot be null."); - checkArgument(Objects.nonNull(sourceCharacters), "Source characters cannot be null."); + checkArgumentNotNull(random, "Random cannot be null."); + checkArgumentNotNull(sourceCharacters, "Source characters cannot be null."); checkArgument(length >= 0, "The length should be greater than or equal to zero."); return randomStrInternal(random, sourceCharacters, 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."); return randomStrInternal(ThreadLocalRandom.current(), sourceCharacters, 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."); return randomStrInternal(DEFAULT_SECURE_RANDOM, sourceCharacters, length); } diff --git a/plusone-commons/src/main/java/xyz/zhouxy/plusone/commons/util/StringTools.java b/plusone-commons/src/main/java/xyz/zhouxy/plusone/commons/util/StringTools.java index 23fa3bb..01b6ee4 100644 --- a/plusone-commons/src/main/java/xyz/zhouxy/plusone/commons/util/StringTools.java +++ b/plusone-commons/src/main/java/xyz/zhouxy/plusone/commons/util/StringTools.java @@ -17,10 +17,10 @@ package xyz.zhouxy.plusone.commons.util; 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.URL; -import java.util.Objects; import javax.annotation.Nullable; @@ -112,7 +112,7 @@ public class StringTools { * @return 结果 */ 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)); }