Java获取两个对象属性值变化
1.定义比较结果类
ComparbleResult.java
package com.izhouy.learn.util;
import lombok.Data;
/**
* title ComparbleResult
* Description
* CreateDate 2022/5/31 23:04
*
* @author izhouy
*/
@Data
public class ComparbleResult {
/**
* 变更字段
*/
private String fieldName;
/**
* 变更前类的内容容
*/
private Object fieldContent;
/**
* 变更后类的内容容
*/
private Object newFieldContent;
/**
* 变更的枚举类型
*/
private String handerType;
}
2.创建比较对象
HeadParam.java
package com.izhouy.learn.config;
import lombok.Data;
/**
* title HeadParam
* Description
* CreateDate 2022/5/30 23:02
*
* @author izhouy
*/
@Data
public class HeadParam {
private String param;
private Boolean required = false;
}
3.创建比较工具类
ComparbleUtils.java
package com.izhouy.learn.util;
import com.izhouy.learn.config.HeadParam;
import java.lang.reflect.Field;
import java.util.*;
import java.util.stream.Stream;
/**
* 比较器工具类
*/
public class ComparbleUtils {
/**
* 返回所有字段
*
* @param target
* @return
*/
public static Map<String, Field> getFields(Object target) {
if (target == null) {
return Collections.EMPTY_MAP;
}
Field[] fields = target.getClass().getDeclaredFields();
Map<String, Field> rsMap = new HashMap<>(fields.length);
Stream.of(fields).forEach(field -> {
field.setAccessible(true);
rsMap.put(field.getName(), field);
});
return rsMap;
}
/**
* 比较两个对象属性值是否相同,如果不同返回修改过的属性信息集合,包括:字段名,原始数据值,新值,更改类型
*
* @param source 原始对象
* @param target 新对象
* @return List<ComparbleResult> 变化后的数据集合
*/
public static List<ComparbleResult> compareInstance(Object source, Object target) {
List<ComparbleResult> compareResultList = new ArrayList<>();
try {
// 获取字段集合
Map<String, Field> filedsSource = getFields(source);
Map<String, Field> fieldsTarget = getFields(target);
// 先遍历source集合,处理两种情况:
// source中有的,target中没有的->字段被删除
// source中有的,target中有但是内容变化->字段内容被更新
for (Field field : filedsSource.values()) {
ComparbleResult comparbleResult = new ComparbleResult();
Object v1 = field.get(source);
// source中有的,target中没有的->字段被删除
if (!fieldsTarget.containsKey(field.getName())) {
comparbleResult.setFieldName(field.getName());
comparbleResult.setHanderType(EnumDataChangeHanderType.DELETE.getCode());
compareResultList.add(comparbleResult);
continue;
}
Object v2 = fieldsTarget.get(field.getName()).get(target);
comparbleResult.setFieldName(field.getName());
comparbleResult.setFieldContent(v1);
comparbleResult.setNewFieldContent(v2);
if (v1 == null && v2 == null) {
continue;
}
// source中有的,target中有但是内容变化->字段内容被更新
if (v1 == null || !v1.equals(v2)) {
comparbleResult.setHanderType(EnumDataChangeHanderType.UPDATE.getCode());
compareResultList.add(comparbleResult);
continue;
}
}
// 遍历target集合,处理一种情况
// source中没有,target有的->新增字段
for (Field field : fieldsTarget.values()) {
ComparbleResult comparbleResult = new ComparbleResult();
if (!filedsSource.containsKey(field.getName())) {
comparbleResult.setFieldName(field.getName());
comparbleResult.setHanderType(EnumDataChangeHanderType.SAVE.getCode());
compareResultList.add(comparbleResult);
continue;
}
}
} catch (Exception ex) {
throw new RuntimeException(ex);
}
return compareResultList;
}
public static void main(String[] args) {
HeadParam headParam = new HeadParam();
HeadParam headParam1 = new HeadParam();
headParam.setParam(null);
headParam1.setParam("new");
headParam1.setRequired(null);
List<ComparbleResult> comparbleResults = compareInstance(headParam, headParam1);
if (!comparbleResults.isEmpty()) {
comparbleResults.forEach(item -> System.out.println(item.toString()));
}
}
}
4.属性变更类型枚举
EnumDataChangeHanderType.java
package com.izhouy.learn.util;
import lombok.Getter;
/**
* title EnumDataChangeHanderType
* Description 属性变更类型枚举
* CreateDate 2022/5/31 23:06
*
* @author izhouy
*/
@Getter
public enum EnumDataChangeHanderType {
// 属性变更类型枚举
DELETE("DEL", "删除"),
UPDATE("UPD", "修改"),
SAVE("SAVE", "新增");
private String code;
private String name;
EnumDataChangeHanderType(String code, String name) {
this.code = code;
this.name = name;
}
}