From d8d4e22e8f947156482cbf59f1c0fc7e77652d6c Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Sat, 29 Apr 2023 15:54:00 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AE=80=E5=8D=95=E8=B0=83=E6=95=B4=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../zhouxy/plusone/commons/util/Assert.java | 102 ++++++++++++------ 1 file changed, 70 insertions(+), 32 deletions(-) diff --git a/src/main/java/xyz/zhouxy/plusone/commons/util/Assert.java b/src/main/java/xyz/zhouxy/plusone/commons/util/Assert.java index 25ef053..fea90b6 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/util/Assert.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/util/Assert.java @@ -18,63 +18,85 @@ public class Assert { } public static void isTrue(@Nullable Boolean conditions, String errorMessage) { - Assert.isTrue(conditions, () -> new IllegalArgumentException(errorMessage)); + if (!Boolean.TRUE.equals(conditions)) { + throw new IllegalArgumentException(errorMessage); + } } public static void isTrue(@Nullable Boolean conditions, String errorMessageTemplate, Object... args) { - Assert.isTrue(conditions, String.format(errorMessageTemplate, args)); + if (!Boolean.TRUE.equals(conditions)) { + throw new IllegalArgumentException(String.format(errorMessageTemplate, args)); + } } // isFalse public static void isFalse(@Nullable Boolean conditions, Supplier e) throws E { - Assert.isTrue(Boolean.FALSE.equals(conditions), e); + if (!Boolean.FALSE.equals(conditions)) { + throw e.get(); + } } public static void isFalse(@Nullable Boolean conditions, String errorMessage) { - Assert.isTrue(Boolean.FALSE.equals(conditions), () -> new IllegalArgumentException(errorMessage)); + if (!Boolean.FALSE.equals(conditions)) { + throw new IllegalArgumentException(errorMessage); + } } public static void isFalse(@Nullable Boolean conditions, String errorMessageTemplate, Object... args) { - Assert.isTrue(Boolean.FALSE.equals(conditions), String.format(errorMessageTemplate, args)); + if (!Boolean.FALSE.equals(conditions)) { + throw new IllegalArgumentException(String.format(errorMessageTemplate, args)); + } } // between - int + private static boolean between(int value, int min, int max) { + return value >= min && value < max; + } + public static void between(int value, int min, int max, Supplier e) throws E { - Assert.isTrue((value >= min && value < max), e); + Assert.isTrue(between(value, min, max), e); } public static void between(int value, int min, int max, String errorMessage) { - Assert.isTrue((value >= min && value < max), errorMessage); + Assert.isTrue(between(value, min, max), errorMessage); } public static void between(int value, int min, int max, String errorMessageTemplate, Object... args) { - Assert.isTrue((value >= min && value < max), errorMessageTemplate, args); + Assert.isTrue(between(value, min, max), errorMessageTemplate, args); } // between - long + private static boolean between(long value, long min, long max) { + return value >= min && value < max; + } + public static void between(long value, long min, long max, Supplier e) throws E { - Assert.isTrue((value >= min && value < max), e); + Assert.isTrue(between(value, min, max), e); } public static void between(long value, long min, long max, String errorMessage) { - Assert.isTrue((value >= min && value < max), errorMessage); + Assert.isTrue(between(value, min, max), errorMessage); } public static void between(long value, long min, long max, String errorMessageTemplate, Object... args) { - Assert.isTrue((value >= min && value < max), errorMessageTemplate, args); + Assert.isTrue(between(value, min, max), errorMessageTemplate, args); } // between - double + private static boolean between(double value, double min, double max) { + return value >= min && value < max; + } + public static void between(double value, double min, double max, Supplier e) throws E { - Assert.isTrue((value >= min && value < max), e); + Assert.isTrue(between(value, min, max), e); } public static void between(double value, double min, double max, String errorMessage) { - Assert.isTrue((value >= min && value < max), errorMessage); + Assert.isTrue(between(value, min, max), errorMessage); } public static void between(double value, double min, double max, String errorMessageTemplate, Object... args) { - Assert.isTrue((value >= min && value < max), errorMessageTemplate, args); + Assert.isTrue(between(value, min, max), errorMessageTemplate, args); } // notNull @@ -91,81 +113,97 @@ public class Assert { } // isEmpty - Collection + private static boolean isEmpty(@Nullable Collection collection) { + return collection == null || collection.isEmpty(); + } + public static void isEmpty(@Nullable Collection collection, Supplier e) throws E { - Assert.isTrue((collection == null || collection.isEmpty()), e); + Assert.isTrue(isEmpty(collection), e); } public static void isEmpty(@Nullable Collection collection, String errorMessage) { - Assert.isTrue((collection == null || collection.isEmpty()), errorMessage); + Assert.isTrue(isEmpty(collection), errorMessage); } public static void isEmpty(@Nullable Collection collection, String errorMessageTemplate, Object... args) { - Assert.isTrue((collection == null || collection.isEmpty()), errorMessageTemplate, args); + Assert.isTrue(isEmpty(collection), errorMessageTemplate, args); } // isNotEmpty - Collection + private static boolean isNotEmpty(@Nullable Collection collection) { + return collection != null && !collection.isEmpty(); + } + public static void isNotEmpty(@Nullable Collection collection, Supplier e) throws E { - Assert.isFalse((collection == null || collection.isEmpty()), e); + Assert.isTrue(isNotEmpty(collection), e); } public static void isNotEmpty(@Nullable Collection collection, String errorMessage) { - Assert.isFalse((collection == null || collection.isEmpty()), errorMessage); + Assert.isTrue(isNotEmpty(collection), errorMessage); } public static void isNotEmpty(@Nullable Collection collection, String errorMessageTemplate, Object... args) { - Assert.isFalse((collection == null || collection.isEmpty()), errorMessageTemplate, args); + Assert.isTrue(isNotEmpty(collection), errorMessageTemplate, args); } // isEmpty - Array + private static boolean isEmpty(@Nullable T[] arr) { + return arr == null || arr.length == 0; + } + public static void isEmpty(@Nullable T[] arr, Supplier e) throws E { - Assert.isTrue((arr == null || arr.length == 0), e); + Assert.isTrue(isEmpty(arr), e); } public static void isEmpty(@Nullable T[] arr, String errorMessage) { - Assert.isTrue((arr == null || arr.length == 0), errorMessage); + Assert.isTrue(isEmpty(arr), errorMessage); } public static void isEmpty(@Nullable T[] arr, String errorMessageTemplate, Object... args) { - Assert.isTrue((arr == null || arr.length == 0), errorMessageTemplate, args); + Assert.isTrue(isEmpty(arr), errorMessageTemplate, args); } // isNotEmpty - Array + private static boolean isNotEmpty(@Nullable T[] arr) { + return arr != null && arr.length > 0; + } + public static void isNotEmpty(@Nullable T[] arr, Supplier e) throws E { - Assert.isFalse((arr == null || arr.length == 0), e); + Assert.isTrue(isNotEmpty(arr), e); } public static void isNotEmpty(@Nullable T[] arr, String errorMessage) { - Assert.isFalse((arr == null || arr.length == 0), errorMessage); + Assert.isTrue(isNotEmpty(arr), errorMessage); } public static void isNotEmpty(@Nullable T[] arr, String errorMessageTemplate, Object... args) { - Assert.isFalse((arr == null || arr.length == 0), errorMessageTemplate, args); + Assert.isTrue(isNotEmpty(arr), errorMessageTemplate, args); } // isEmpty - String public static void isEmpty(@Nullable String str, Supplier e) throws E { - Assert.isTrue((str == null || str.isEmpty()), e); + Assert.isTrue(Strings.isNullOrEmpty(str), e); } public static void isEmpty(@Nullable String str, String errorMessage) { - Assert.isTrue((str == null || str.isEmpty()), errorMessage); + Assert.isTrue(Strings.isNullOrEmpty(str), errorMessage); } public static void isEmpty(@Nullable String str, String errorMessageTemplate, Object... args) { - Assert.isTrue((str == null || str.isEmpty()), errorMessageTemplate, args); + Assert.isTrue(Strings.isNullOrEmpty(str), errorMessageTemplate, args); } // isNotEmpty - String public static void isNotEmpty(@Nullable String str, Supplier e) throws E { - Assert.isFalse(Strings.isNullOrEmpty(str), e); + Assert.isTrue(!Strings.isNullOrEmpty(str), e); } public static void isNotEmpty(@Nullable String str, String errorMessage) { - Assert.isFalse(Strings.isNullOrEmpty(str), errorMessage); + Assert.isTrue(!Strings.isNullOrEmpty(str), errorMessage); } public static void isNotEmpty(@Nullable String str, String errorMessageTemplate, Object... args) { - Assert.isFalse(Strings.isNullOrEmpty(str), errorMessageTemplate, args); + Assert.isTrue(!Strings.isNullOrEmpty(str), errorMessageTemplate, args); } // private constructor