add method

This commit is contained in:
Looly 2022-06-14 16:43:05 +08:00
parent cda942d402
commit e9b6f7b2af
3 changed files with 14 additions and 19 deletions

View File

@ -7,6 +7,7 @@
### 🐣新特性 ### 🐣新特性
* 【extra 】 Sftp增加构造重载支持超时pr#653@Gitee * 【extra 】 Sftp增加构造重载支持超时pr#653@Gitee
* 【core 】 BeanUtil增加isCommonFieldsEqualpr#653@Gitee
* *
### 🐞Bug修复 ### 🐞Bug修复

View File

@ -968,10 +968,13 @@ public class BeanUtil {
/** /**
* 判断source与target的所有公共字段的值是否相同 * 判断source与target的所有公共字段的值是否相同
*
* @param source 待检测对象1 * @param source 待检测对象1
* @param target 待检测对象2 * @param target 待检测对象2
* @param ignoreProperties 不需要检测的字段 * @param ignoreProperties 不需要检测的字段
* @return 判断结果如果为true则证明所有字段的值都相同 * @return 判断结果如果为true则证明所有字段的值都相同
* @since 5.8.4
* @author Takak11
*/ */
public static boolean isCommonFieldsEqual(Object source, Object target, String...ignoreProperties) { public static boolean isCommonFieldsEqual(Object source, Object target, String...ignoreProperties) {
@ -989,13 +992,7 @@ public class BeanUtil {
sourceFields.removeAll(Arrays.asList(ignoreProperties)); sourceFields.removeAll(Arrays.asList(ignoreProperties));
for (String field : sourceFields) { for (String field : sourceFields) {
Object sourceValue = sourceFieldsMap.get(field); if(ObjectUtil.notEqual(sourceFieldsMap.get(field), targetFieldsMap.get(field))){
Object targetValue = targetFieldsMap.get(field);
if (null == sourceValue && null == targetValue) {
continue;
}
if (!sourceValue.equals(targetValue)) {
return false; return false;
} }
} }

View File

@ -859,8 +859,8 @@ public class BeanUtilTest {
} }
@Test @Test
public void isCommonFieldsEqualTest() { public void isCommonFieldsEqualTest() {
TestUserEntity userEntity = new TestUserEntity(); final TestUserEntity userEntity = new TestUserEntity();
TestUserDTO userDTO = new TestUserDTO(); final TestUserDTO userDTO = new TestUserDTO();
userDTO.setAge(20); userDTO.setAge(20);
userDTO.setName("takaki"); userDTO.setName("takaki");
@ -869,20 +869,17 @@ public class BeanUtilTest {
BeanUtil.copyProperties(userDTO, userEntity); BeanUtil.copyProperties(userDTO, userEntity);
System.out.println("相同字段值测试" + BeanUtil.isCommonFieldsEqual(userDTO, userEntity)); Assert.assertTrue(BeanUtil.isCommonFieldsEqual(userDTO, userEntity));
userEntity.setAge(13); userEntity.setAge(13);
System.out.println("修改age字段值后测试" + BeanUtil.isCommonFieldsEqual(userDTO, userEntity)); Assert.assertFalse(BeanUtil.isCommonFieldsEqual(userDTO, userEntity));
System.out.println("忽略age字段后测试" + BeanUtil.isCommonFieldsEqual(userDTO, userEntity, "age")); Assert.assertTrue(BeanUtil.isCommonFieldsEqual(userDTO, userEntity, "age"));
System.out.println("全null值测试" + BeanUtil.isCommonFieldsEqual(null, null)); Assert.assertTrue(BeanUtil.isCommonFieldsEqual(null, null));
System.out.println("部分null值测试1" + BeanUtil.isCommonFieldsEqual(null, userEntity)); Assert.assertFalse(BeanUtil.isCommonFieldsEqual(null, userEntity));
System.out.println("部分null值测试2" + BeanUtil.isCommonFieldsEqual(userEntity, null)); Assert.assertFalse(BeanUtil.isCommonFieldsEqual(userEntity, null));
userEntity.setSex(0); userEntity.setSex(0);
System.out.println( Assert.assertTrue(BeanUtil.isCommonFieldsEqual(userDTO, userEntity, "age", "sex"));
"修改age、sex字段修改后并忽略测试"
+ BeanUtil.isCommonFieldsEqual(userDTO, userEntity, "age", "sex")
);
} }
} }