mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
enhancement in BeanUtil
This commit is contained in:
parent
2b7403162e
commit
b1a93a3345
@ -23,13 +23,7 @@ import java.beans.PropertyEditor;
|
||||
import java.beans.PropertyEditorManager;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.*;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.stream.Collectors;
|
||||
@ -971,4 +965,41 @@ public class BeanUtil {
|
||||
throw new IllegalArgumentException("Invalid Getter or Setter name: " + getterOrSetterName);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断source与target的所有公共字段的值是否相同
|
||||
* @param source 待检测对象1
|
||||
* @param target 待检测对象2
|
||||
* @param ignoreProperties 不需要检测的字段
|
||||
* @return 判断结果,如果为true则证明所有字段的值都相同
|
||||
*/
|
||||
public static boolean isCommonFieldsEqual(Object source, Object target, String...ignoreProperties) {
|
||||
|
||||
if (null == source && null == target) {
|
||||
return true;
|
||||
}
|
||||
if (null == source || null == target) {
|
||||
return false;
|
||||
}
|
||||
|
||||
Map<String, Object> sourceFieldsMap = BeanUtil.beanToMap(source);
|
||||
Map<String, Object> targetFieldsMap = BeanUtil.beanToMap(target);
|
||||
|
||||
Set<String> sourceFields = sourceFieldsMap.keySet();
|
||||
sourceFields.removeAll(Arrays.asList(ignoreProperties));
|
||||
|
||||
for (String field : sourceFields) {
|
||||
Object sourceValue = sourceFieldsMap.get(field);
|
||||
Object targetValue = targetFieldsMap.get(field);
|
||||
|
||||
if (null == sourceValue && null == targetValue) {
|
||||
continue;
|
||||
}
|
||||
if (!sourceValue.equals(targetValue)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
@ -11,10 +11,7 @@ import cn.hutool.core.thread.ThreadUtil;
|
||||
import cn.hutool.core.util.ArrayUtil;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import lombok.Data;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import lombok.ToString;
|
||||
import lombok.*;
|
||||
import lombok.experimental.Accessors;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Ignore;
|
||||
@ -840,4 +837,52 @@ public class BeanUtilTest {
|
||||
}, copyOptions);
|
||||
Assert.assertEquals("123", pojo.getName());
|
||||
}
|
||||
|
||||
@Data
|
||||
@EqualsAndHashCode
|
||||
private static class TestUserEntity {
|
||||
private String username;
|
||||
private String name;
|
||||
private Integer age;
|
||||
private Integer sex;
|
||||
private String mobile;
|
||||
private Date createTime;
|
||||
}
|
||||
|
||||
@Data
|
||||
@EqualsAndHashCode
|
||||
private static class TestUserDTO {
|
||||
private String name;
|
||||
private Integer age;
|
||||
private Integer sex;
|
||||
private String mobile;
|
||||
}
|
||||
@Test
|
||||
public void isCommonFieldsEqualTest() {
|
||||
TestUserEntity userEntity = new TestUserEntity();
|
||||
TestUserDTO userDTO = new TestUserDTO();
|
||||
|
||||
userDTO.setAge(20);
|
||||
userDTO.setName("takaki");
|
||||
userDTO.setSex(1);
|
||||
userDTO.setMobile("17812312023");
|
||||
|
||||
BeanUtil.copyProperties(userDTO, userEntity);
|
||||
|
||||
System.out.println("相同字段值测试" + BeanUtil.isCommonFieldsEqual(userDTO, userEntity));
|
||||
|
||||
userEntity.setAge(13);
|
||||
System.out.println("修改age字段值后测试" + BeanUtil.isCommonFieldsEqual(userDTO, userEntity));
|
||||
System.out.println("忽略age字段后测试" + BeanUtil.isCommonFieldsEqual(userDTO, userEntity, "age"));
|
||||
|
||||
System.out.println("全null值测试" + BeanUtil.isCommonFieldsEqual(null, null));
|
||||
System.out.println("部分null值测试1" + BeanUtil.isCommonFieldsEqual(null, userEntity));
|
||||
System.out.println("部分null值测试2" + BeanUtil.isCommonFieldsEqual(userEntity, null));
|
||||
|
||||
userEntity.setSex(0);
|
||||
System.out.println(
|
||||
"修改age、sex字段修改后并忽略测试"
|
||||
+ BeanUtil.isCommonFieldsEqual(userDTO, userEntity, "age", "sex")
|
||||
);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user