From fa9d095e4ee5b50c27839a8c1f2e73ed885dbd9d Mon Sep 17 00:00:00 2001 From: Looly Date: Thu, 5 May 2022 11:45:58 +0800 Subject: [PATCH] fix code --- .../main/java/cn/hutool/core/lang/Assert.java | 96 ++++++++++++++++++- .../java/cn/hutool/core/util/ObjUtil.java | 33 ++----- 2 files changed, 102 insertions(+), 27 deletions(-) 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 f492f7870..350b9bb65 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 @@ -2,8 +2,9 @@ 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.text.StrUtil; +import cn.hutool.core.util.ArrayUtil; +import cn.hutool.core.util.ObjUtil; import java.util.Map; import java.util.function.Supplier; @@ -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(final Object obj1, final 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(final Object obj1, final Object obj2, final String errorMsgTemplate, final 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(final Object obj1, final Object obj2, final Supplier errorSupplier) throws X { + if (ObjUtil.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 equals(final Object obj1, final Object obj2) { + equals(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 equals(final Object obj1, final Object obj2, final String errorMsgTemplate, final Object... params) throws IllegalArgumentException { + equals(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 equals(final Object obj1, final Object obj2, final Supplier errorSupplier) throws X { + if (ObjUtil.notEquals(obj1, obj2)) { + throw errorSupplier.get(); + } + } + + // ----------------------------------------------------------------------------------------------------------- Check is equals + // -------------------------------------------------------------------------------------------------------------------------------------------- Private method start /** diff --git a/hutool-core/src/main/java/cn/hutool/core/util/ObjUtil.java b/hutool-core/src/main/java/cn/hutool/core/util/ObjUtil.java index 5ab0dc774..a14147299 100644 --- a/hutool-core/src/main/java/cn/hutool/core/util/ObjUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/util/ObjUtil.java @@ -28,7 +28,7 @@ import java.util.function.Supplier; public class ObjUtil { /** - * 比较两个对象是否相等,此方法是 {@link #equal(Object, Object)}的别名方法。
+ * 比较两个对象是否相等。 * 相同的条件有两个,满足其一即可:
*
    *
  1. obj1 == null && obj2 == null
  2. @@ -39,28 +39,9 @@ public class ObjUtil { * @param obj1 对象1 * @param obj2 对象2 * @return 是否相等 - * @see #equal(Object, Object) * @since 5.4.3 */ public static boolean equals(final Object obj1, final Object obj2) { - return equal(obj1, obj2); - } - - /** - * 比较两个对象是否相等。
    - * 相同的条件有两个,满足其一即可:
    - *
      - *
    1. obj1 == null && obj2 == null
    2. - *
    3. obj1.equals(obj2)
    4. - *
    5. 如果是BigDecimal比较,0 == obj1.compareTo(obj2)
    6. - *
    - * - * @param obj1 对象1 - * @param obj2 对象2 - * @return 是否相等 - * @see Objects#equals(Object, Object) - */ - public static boolean equal(final Object obj1, final Object obj2) { if (obj1 instanceof BigDecimal && obj2 instanceof BigDecimal) { return NumberUtil.equals((BigDecimal) obj1, (BigDecimal) obj2); } @@ -75,8 +56,8 @@ public class ObjUtil { * @return 是否不等 * @since 3.0.7 */ - public static boolean notEqual(final Object obj1, final Object obj2) { - return false == equal(obj1, obj2); + public static boolean notEquals(final Object obj1, final Object obj2) { + return false == equals(obj1, obj2); } /** @@ -169,7 +150,7 @@ public class ObjUtil { final Iterator iter = (Iterator) obj; while (iter.hasNext()) { final Object o = iter.next(); - if (equal(o, element)) { + if (equals(o, element)) { return true; } } @@ -179,17 +160,17 @@ public class ObjUtil { final Enumeration enumeration = (Enumeration) obj; while (enumeration.hasMoreElements()) { final Object o = enumeration.nextElement(); - if (equal(o, element)) { + if (equals(o, element)) { return true; } } return false; } - if (obj.getClass().isArray() == true) { + if (ArrayUtil.isArray(obj)) { final int len = Array.getLength(obj); for (int i = 0; i < len; i++) { final Object o = Array.get(obj, i); - if (equal(o, element)) { + if (equals(o, element)) { return true; } }