diff --git a/hutool-core/src/main/java/cn/hutool/core/lang/Assert.java b/hutool-core/src/main/java/cn/hutool/core/lang/Assert.java index a33b2a4ed..b0b347c02 100644 --- a/hutool-core/src/main/java/cn/hutool/core/lang/Assert.java +++ b/hutool-core/src/main/java/cn/hutool/core/lang/Assert.java @@ -3,6 +3,7 @@ package cn.hutool.core.lang; import cn.hutool.core.collection.CollUtil; import cn.hutool.core.map.MapUtil; import cn.hutool.core.util.ArrayUtil; +import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.StrUtil; import java.util.Map; @@ -1003,6 +1004,99 @@ public class Assert { return value; } + /** + * 断言两个对象是否不相等,如果两个对象相等 抛出IllegalArgumentException 异常 + *
+	 *   Assert.notEquals(obj1,obj2);
+	 * 
+ * + * @param obj1 对象1 + * @param obj2 对象2 + * @throws IllegalArgumentException obj1 must be not equals obj2 + */ + public static void notEquals(Object obj1, Object obj2) { + notEquals(obj1, obj2, "({}) must be not equals ({})", obj1, obj2); + } + + /** + * 断言两个对象是否不相等,如果两个对象相等 抛出IllegalArgumentException 异常 + *
+	 *   Assert.notEquals(obj1,obj2,"obj1 must be not equals obj2");
+	 * 
+ * + * @param obj1 对象1 + * @param obj2 对象2 + * @param errorMsgTemplate 异常信息模板,类似于"aa{}bb{}cc" + * @param params 异常信息参数,用于替换"{}"占位符 + * @throws IllegalArgumentException obj1 must be not equals obj2 + */ + public static void notEquals(Object obj1, Object obj2, String errorMsgTemplate, Object... params) throws IllegalArgumentException { + notEquals(obj1, obj2, () -> new IllegalArgumentException(StrUtil.format(errorMsgTemplate, params))); + } + + /** + * 断言两个对象是否不相等,如果两个对象相等,抛出指定类型异常,并使用指定的函数获取错误信息返回 + * + * @param obj1 对象1 + * @param obj2 对象2 + * @param errorSupplier 错误抛出异常附带的消息生产接口 + * @param 异常类型 + * @throws X obj1 must be not equals obj2 + */ + public static void notEquals(Object obj1, Object obj2, Supplier errorSupplier) throws X { + if (ObjectUtil.equals(obj1, obj2)) { + throw errorSupplier.get(); + } + } + // ----------------------------------------------------------------------------------------------------------- Check not equals + + /** + * 断言两个对象是否相等,如果两个对象不相等 抛出IllegalArgumentException 异常 + *
+	 *   Assert.isEquals(obj1,obj2);
+	 * 
+ * + * @param obj1 对象1 + * @param obj2 对象2 + * @throws IllegalArgumentException obj1 must be equals obj2 + */ + public static void isEquals(Object obj1, Object obj2) { + isEquals(obj1, obj2, "({}) must be equals ({})", obj1, obj2); + } + + /** + * 断言两个对象是否相等,如果两个对象不相等 抛出IllegalArgumentException 异常 + *
+	 *   Assert.isEquals(obj1,obj2,"obj1 must be equals obj2");
+	 * 
+ * + * @param obj1 对象1 + * @param obj2 对象2 + * @param errorMsgTemplate 异常信息模板,类似于"aa{}bb{}cc" + * @param params 异常信息参数,用于替换"{}"占位符 + * @throws IllegalArgumentException obj1 must be equals obj2 + */ + public static void isEquals(Object obj1, Object obj2, String errorMsgTemplate, Object... params) throws IllegalArgumentException { + isEquals(obj1, obj2, () -> new IllegalArgumentException(StrUtil.format(errorMsgTemplate, params))); + } + + /** + * 断言两个对象是否相等,如果两个对象不相等,抛出指定类型异常,并使用指定的函数获取错误信息返回 + * + * @param obj1 对象1 + * @param obj2 对象2 + * @param errorSupplier 错误抛出异常附带的消息生产接口 + * @param 异常类型 + * @throws X obj1 must be equals obj2 + */ + public static void isEquals(Object obj1, Object obj2, Supplier errorSupplier) throws X { + if (ObjectUtil.notEqual(obj1, obj2)) { + throw errorSupplier.get(); + } + } + + // ----------------------------------------------------------------------------------------------------------- Check is equals + // -------------------------------------------------------------------------------------------------------------------------------------------- Private method start /** diff --git a/hutool-core/src/test/java/cn/hutool/core/lang/AssertTest.java b/hutool-core/src/test/java/cn/hutool/core/lang/AssertTest.java index e20ef6e96..be095d835 100755 --- a/hutool-core/src/test/java/cn/hutool/core/lang/AssertTest.java +++ b/hutool-core/src/test/java/cn/hutool/core/lang/AssertTest.java @@ -1,9 +1,10 @@ package cn.hutool.core.lang; +import cn.hutool.core.util.StrUtil; import org.junit.Test; public class AssertTest { - + @Test public void isNullTest(){ String a = null; @@ -33,6 +34,30 @@ public class AssertTest { public void isTrueTest3() { int i = -1; //noinspection ConstantConditions - Assert.isTrue(i > 0, ()-> new IndexOutOfBoundsException("relation message to return")); + Assert.isTrue(i > 0, () -> new IndexOutOfBoundsException("relation message to return")); } + + @Test + public void isEqualsTest() { + //String a="ab"; + //final String b = new String("abc"); + String a = null; + final String b = null; + Assert.isEquals(a, b); + Assert.isEquals(a, b, "{}不等于{}", a, b); + Assert.isEquals(a, b, () -> new RuntimeException(StrUtil.format("{}和{}不相等", a, b))); + } + + @Test + public void notEqualsTest() { + //String c="19"; + //final String d = new String("19"); + String c = null; + final String d = null; + //Assert.notEquals(c,d); + //Assert.notEquals(c,d,"{}等于{}",c,d); + Assert.notEquals(c, d, () -> new RuntimeException(StrUtil.format("{}和{}相等", c, d))); + + } + }