java 物件屬性比較工具類
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
import com.google.common.collect.Maps;
/**
* BeanUtil 物件屬性比較工具類
* @author zzg
*
**/
public class BeanUtil {
public static void main(String[] args) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
// TODO Auto-generated method stub
BeanUtil util = new BeanUtil();
//excel 讀取資料
Person excel = util.new Person();
excel.setId("1");
excel.setAge("18");
excel.setName("知學雲1");
//database 讀取資料
Person database = util.new Person();
database.setId("1");
database.setAge("16");
database.setName("知學雲");
Map<String,Map<String,Object>> map = util.getMap(excel);
//遍歷map
// for (Map.Entry<String,Object> entry : map.entrySet()) {
// System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
// }
boolean flag = util.getFieldEqual(database, map);
System.out.println("屬性值是否發生改變:"+ flag);
Map<String,Map<String,Object>> fields = util.getFieldUpdate(database, map);
Map<String,Object> c = fields.get(database.getId());
for (Map.Entry<String,Object> entry : c.entrySet()) {
System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
}
}
// 指定Bean物件 關聯屬性轉換為Map
public Map<String,Map<String,Object>> getMap(Person object) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException{
Map<String,Map<String,Object>> obj = new HashMap<String,Map<String,Object>>();
Map<String,Object> map = Maps.newHashMap();
Class<?> cls = object.getClass();
Field[] fields = cls.getDeclaredFields();
for(Field field : fields){
Object value = getFieldValue(object, field.getName());
String key = field.getName();
map.put(key, value);
}
obj.put(object.getId(), map);
return obj;
}
// Bean 關聯屬性轉換為Map
public Map<String,Object> getMapProperties(Person object) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException{
Map<String,Object> map = Maps.newHashMap();
Class<?> cls = object.getClass();
Field[] fields = cls.getDeclaredFields();
for(Field field : fields){
Object value = getFieldValue(object, field.getName());
String key = field.getName();
map.put(key, value);
}
return map;
}
// 獲取指定Field 屬性值
public Object getFieldValue(Object obj, String fieldName) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException{
Class<?> cls = obj.getClass();
Object value = null;
Field field = cls.getDeclaredField(fieldName);;
field.setAccessible(true);
value = field.get(obj);
return value;
}
//判斷指定物件的屬性值是否相等
public boolean getFieldEqual(Person object, Map<String,Map<String, Object>> map) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException{
boolean flag = false;
Class<?> cls = object.getClass();
Field[] fields = cls.getDeclaredFields();
if(map.containsKey(object.getId())){
Map<String, Object> content = map.get(object.getId());
for(Field field : fields){
Object excel = content.get(field.getName());
Object database = getFieldValue(object,field.getName());
if(excel == null && database == null){
flag = false;
}else if(excel != null && database == null){
flag = true;
break;
}else if(excel == null && database != null){
flag = true;
break;
}else if(excel != null && database != null){
if(excel.equals(database)){
flag = false;
}else{
flag = true;
break;
}
}
}
}
return flag;
}
// 返回匹配物件修改的屬性值
public Map<String,Map<String,Object>> getFieldUpdate(Person object, Map<String,Map<String, Object>> map) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException{
Map<String,Map<String,Object>> result =new HashMap<String,Map<String,Object>>();
Class<?> cls = object.getClass();
Field[] fields = cls.getDeclaredFields();
Map<String, Object> content = map.get(object.getId());
if(map.containsKey(object.getId())){
Map<String,Object> f = new HashMap<String,Object>();
for(Field field : fields){
Object excel = content.get(field.getName());
Object database = getFieldValue(object,field.getName());
//屬性值判斷:四種情況
if(excel != null && database!= null){
if(!excel.equals(database)){
f.put(field.getName(), excel);
}
}else if(excel == null && database == null){
}else if(excel != null && database == null){
f.put(field.getName(), excel);
}else if(excel == null && database != null){
f.put(field.getName(), null);
}
}
result.put(object.getId(), f);
}
return result;
}
public class Person{
private String id;
private String name;
private String age;
private String sex;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
}