mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
fix code
This commit is contained in:
parent
77f2cedf76
commit
fa9d095e4e
@ -2,8 +2,9 @@ package cn.hutool.core.lang;
|
|||||||
|
|
||||||
import cn.hutool.core.collection.CollUtil;
|
import cn.hutool.core.collection.CollUtil;
|
||||||
import cn.hutool.core.map.MapUtil;
|
import cn.hutool.core.map.MapUtil;
|
||||||
import cn.hutool.core.util.ArrayUtil;
|
|
||||||
import cn.hutool.core.text.StrUtil;
|
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.Map;
|
||||||
import java.util.function.Supplier;
|
import java.util.function.Supplier;
|
||||||
@ -1003,6 +1004,99 @@ public class Assert {
|
|||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 断言两个对象是否不相等,如果两个对象相等 抛出IllegalArgumentException 异常
|
||||||
|
* <pre class="code">
|
||||||
|
* Assert.notEquals(obj1,obj2);
|
||||||
|
* </pre>
|
||||||
|
*
|
||||||
|
* @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 异常
|
||||||
|
* <pre class="code">
|
||||||
|
* Assert.notEquals(obj1,obj2,"obj1 must be not equals obj2");
|
||||||
|
* </pre>
|
||||||
|
*
|
||||||
|
* @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 <X> 异常类型
|
||||||
|
* @throws X obj1 must be not equals obj2
|
||||||
|
*/
|
||||||
|
public static <X extends Throwable> void notEquals(final Object obj1, final Object obj2, final Supplier<X> errorSupplier) throws X {
|
||||||
|
if (ObjUtil.equals(obj1, obj2)) {
|
||||||
|
throw errorSupplier.get();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// ----------------------------------------------------------------------------------------------------------- Check not equals
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 断言两个对象是否相等,如果两个对象不相等 抛出IllegalArgumentException 异常
|
||||||
|
* <pre class="code">
|
||||||
|
* Assert.isEquals(obj1,obj2);
|
||||||
|
* </pre>
|
||||||
|
*
|
||||||
|
* @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 异常
|
||||||
|
* <pre class="code">
|
||||||
|
* Assert.isEquals(obj1,obj2,"obj1 must be equals obj2");
|
||||||
|
* </pre>
|
||||||
|
*
|
||||||
|
* @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 <X> 异常类型
|
||||||
|
* @throws X obj1 must be equals obj2
|
||||||
|
*/
|
||||||
|
public static <X extends Throwable> void equals(final Object obj1, final Object obj2, final Supplier<X> errorSupplier) throws X {
|
||||||
|
if (ObjUtil.notEquals(obj1, obj2)) {
|
||||||
|
throw errorSupplier.get();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ----------------------------------------------------------------------------------------------------------- Check is equals
|
||||||
|
|
||||||
// -------------------------------------------------------------------------------------------------------------------------------------------- Private method start
|
// -------------------------------------------------------------------------------------------------------------------------------------------- Private method start
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -28,7 +28,7 @@ import java.util.function.Supplier;
|
|||||||
public class ObjUtil {
|
public class ObjUtil {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 比较两个对象是否相等,此方法是 {@link #equal(Object, Object)}的别名方法。<br>
|
* 比较两个对象是否相等。
|
||||||
* 相同的条件有两个,满足其一即可:<br>
|
* 相同的条件有两个,满足其一即可:<br>
|
||||||
* <ol>
|
* <ol>
|
||||||
* <li>obj1 == null && obj2 == null</li>
|
* <li>obj1 == null && obj2 == null</li>
|
||||||
@ -39,28 +39,9 @@ public class ObjUtil {
|
|||||||
* @param obj1 对象1
|
* @param obj1 对象1
|
||||||
* @param obj2 对象2
|
* @param obj2 对象2
|
||||||
* @return 是否相等
|
* @return 是否相等
|
||||||
* @see #equal(Object, Object)
|
|
||||||
* @since 5.4.3
|
* @since 5.4.3
|
||||||
*/
|
*/
|
||||||
public static boolean equals(final Object obj1, final Object obj2) {
|
public static boolean equals(final Object obj1, final Object obj2) {
|
||||||
return equal(obj1, obj2);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 比较两个对象是否相等。<br>
|
|
||||||
* 相同的条件有两个,满足其一即可:<br>
|
|
||||||
* <ol>
|
|
||||||
* <li>obj1 == null && obj2 == null</li>
|
|
||||||
* <li>obj1.equals(obj2)</li>
|
|
||||||
* <li>如果是BigDecimal比较,0 == obj1.compareTo(obj2)</li>
|
|
||||||
* </ol>
|
|
||||||
*
|
|
||||||
* @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) {
|
if (obj1 instanceof BigDecimal && obj2 instanceof BigDecimal) {
|
||||||
return NumberUtil.equals((BigDecimal) obj1, (BigDecimal) obj2);
|
return NumberUtil.equals((BigDecimal) obj1, (BigDecimal) obj2);
|
||||||
}
|
}
|
||||||
@ -75,8 +56,8 @@ public class ObjUtil {
|
|||||||
* @return 是否不等
|
* @return 是否不等
|
||||||
* @since 3.0.7
|
* @since 3.0.7
|
||||||
*/
|
*/
|
||||||
public static boolean notEqual(final Object obj1, final Object obj2) {
|
public static boolean notEquals(final Object obj1, final Object obj2) {
|
||||||
return false == equal(obj1, obj2);
|
return false == equals(obj1, obj2);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -169,7 +150,7 @@ public class ObjUtil {
|
|||||||
final Iterator<?> iter = (Iterator<?>) obj;
|
final Iterator<?> iter = (Iterator<?>) obj;
|
||||||
while (iter.hasNext()) {
|
while (iter.hasNext()) {
|
||||||
final Object o = iter.next();
|
final Object o = iter.next();
|
||||||
if (equal(o, element)) {
|
if (equals(o, element)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -179,17 +160,17 @@ public class ObjUtil {
|
|||||||
final Enumeration<?> enumeration = (Enumeration<?>) obj;
|
final Enumeration<?> enumeration = (Enumeration<?>) obj;
|
||||||
while (enumeration.hasMoreElements()) {
|
while (enumeration.hasMoreElements()) {
|
||||||
final Object o = enumeration.nextElement();
|
final Object o = enumeration.nextElement();
|
||||||
if (equal(o, element)) {
|
if (equals(o, element)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (obj.getClass().isArray() == true) {
|
if (ArrayUtil.isArray(obj)) {
|
||||||
final int len = Array.getLength(obj);
|
final int len = Array.getLength(obj);
|
||||||
for (int i = 0; i < len; i++) {
|
for (int i = 0; i < len; i++) {
|
||||||
final Object o = Array.get(obj, i);
|
final Object o = Array.get(obj, i);
|
||||||
if (equal(o, element)) {
|
if (equals(o, element)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user