From c6d3cbeaf55a1f46ee212c605735859aaabae3f0 Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Fri, 21 Apr 2023 01:46:46 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20Assert=20=E7=B1=BB?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../zhouxy/plusone/commons/util/Assert.java | 121 ++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100644 src/main/java/xyz/zhouxy/plusone/commons/util/Assert.java diff --git a/src/main/java/xyz/zhouxy/plusone/commons/util/Assert.java b/src/main/java/xyz/zhouxy/plusone/commons/util/Assert.java new file mode 100644 index 0000000..d7f0bf4 --- /dev/null +++ b/src/main/java/xyz/zhouxy/plusone/commons/util/Assert.java @@ -0,0 +1,121 @@ +package xyz.zhouxy.plusone.commons.util; + +import java.util.Collection; +import java.util.Objects; +import java.util.function.Supplier; + +import javax.annotation.Nullable; + +public class Assert { + + // isTrue + public static void isTrue(boolean conditions, Supplier e) throws E { + if (!conditions) { + throw e.get(); + } + } + + public static void isTrue(boolean conditions, String errorMessage) { + Assert.isTrue(conditions, () -> new IllegalArgumentException(errorMessage)); + } + + public static void isTrue(boolean conditions, String errorMessageTemplate, Object... args) { + Assert.isTrue(conditions, String.format(errorMessageTemplate, args)); + } + + // isFalse + public static void isFalse(boolean conditions, Supplier e) throws E { + Assert.isTrue(!conditions, e); + } + + public static void isFalse(boolean conditions, String errorMessage) { + Assert.isTrue(!conditions, () -> new IllegalArgumentException(errorMessage)); + } + + public static void isFalse(boolean conditions, String errorMessageTemplate, Object... args) { + Assert.isTrue(!conditions, String.format(errorMessageTemplate, args)); + } + + // between - int + public static void between(int value, int min, int max, Supplier e) throws E { + Assert.isTrue((value >= min && value < max), e); + } + + public static void between(int value, int min, int max, String errorMessage) { + Assert.isTrue((value >= min && value < max), errorMessage); + } + + public static void between(int value, int min, int max, String errorMessageTemplate, Object... args) { + Assert.isTrue((value >= min && value < max), errorMessageTemplate, args); + } + + // between - long + public static void between(long value, long min, long max, Supplier e) throws E { + Assert.isTrue((value >= min && value < max), e); + } + + public static void between(long value, long min, long max, String errorMessage) { + Assert.isTrue((value >= min && value < max), errorMessage); + } + + public static void between(long value, long min, long max, String errorMessageTemplate, Object... args) { + Assert.isTrue((value >= min && value < max), errorMessageTemplate, args); + } + + // between - double + public static void between(double value, double min, double max, Supplier e) throws E { + Assert.isTrue((value >= min && value < max), e); + } + + public static void between(double value, double min, double max, String errorMessage) { + Assert.isTrue((value >= min && value < max), errorMessage); + } + + public static void between(double value, double min, double max, String errorMessageTemplate, Object... args) { + Assert.isTrue((value >= min && value < max), errorMessageTemplate, args); + } + + // notNull + public static void notNull(Object value, Supplier e) throws E { + Assert.isTrue(Objects.nonNull(value), e); + } + + public static void notNull(Object value, String errorMessage) throws E { + Assert.isTrue(Objects.nonNull(value), errorMessage); + } + + public static void notNull(Object value, String errorMessageTemplate, Object... args) { + Assert.isTrue(Objects.nonNull(value), errorMessageTemplate, args); + } + + // isEmpty + public static void isEmpty(@Nullable Collection collection, Supplier e) throws E { + Assert.isTrue((collection == null || collection.isEmpty()), e); + } + + public static void isEmpty(@Nullable Collection collection, String errorMessage) { + Assert.isTrue((collection == null || collection.isEmpty()), errorMessage); + } + + public static void isEmpty(@Nullable Collection collection, String errorMessageTemplate, Object... args) { + Assert.isTrue((collection == null || collection.isEmpty()), errorMessageTemplate, args); + } + + // isNotEmpty + public static void isNotEmpty(@Nullable Collection collection, Supplier e) throws E { + Assert.isFalse((collection == null || collection.isEmpty()), e); + } + + public static void isNotEmpty(@Nullable Collection collection, String errorMessage) { + Assert.isFalse((collection == null || collection.isEmpty()), errorMessage); + } + + public static void isNotEmpty(@Nullable Collection collection, String errorMessageTemplate, Object... args) { + Assert.isFalse((collection == null || collection.isEmpty()), errorMessageTemplate, args); + } + + // private consrtuctor + private Assert() { + throw new IllegalStateException("Utility class"); + } +}