Compare two classes are the same entity utils

Compare two entity classes are consistent utils:

import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ClassCompareUtil {

    
    /**
     * Compare two entity attribute values, returns a boolean, true is no difference in the attribute values ​​of the two objects Table
     * @Param OLDobject in comparison attribute of the object 1
     * @Param the newObject attribute comparison object 2
     * @Return property comparison between the results of Boolean
      * / 
    public  static  Boolean compareObject (OLDobject in the Object, the newObject Object) {
        Map<String, Map<String,Object>> resultMap=compareFields(oldObject,newObject);
        
        if(resultMap.size()>0) {
            return true;
        }else {
            return false;
        }
    }
    
    /**
     * Compare two entities property value, returns to a difference map attribute named key, value are stored as a Map oldObject, newObject value of this attribute name
     * @Param OLDobject in comparison attribute of the object 1
     * @Param the newObject attribute comparison object 2
     * @Return property comparison between the results Map
      * / 
    @SuppressWarnings ( "rawtypes" )
     public  static the Map <String, the Map <String, Object >> compareFields (OLDobject in the Object, the newObject Object) {
        Map<String, Map<String, Object>> map = null;
        
        try{    
            /**
             * Only two objects are of the same type are comparable.
             */
            if (oldObject.getClass() == newObject.getClass()) {
                map = new HashMap<String, Map<String,Object>>();
                
                Clazz class = oldObject.getClass ();
                 // get all the attributes of the object of 
                the PropertyDescriptor [] = PDS Introspector.getBeanInfo (clazz, Object. Class ) .getPropertyDescriptors ();
                
                for (the PropertyDescriptor PD: PDS) {
                     // iterate acquired attribute name 
                    String name = pd.getName ();
                    
                    // get property get method 
                    Method, readMethod = pd.getReadMethod ();
                    
                    // call the get method on OLDobject in the equivalent property value obtained OLDobject in the 
                    Object oldValue = readMethod.invoke (OLDobject in the);
                     // call the get method newObject equivalent property value obtained newObject 
                    Object newValue = readMethod.invoke (newObject);
                    
                    if(oldValue instanceof List){
                        continue;
                    }
                    
                    if(newValue instanceof List){
                        continue;
                    }
                    
                    if(oldValue instanceof Timestamp){
                        oldValue = new Date(((Timestamp) oldValue).getTime());
                    }
                    
                    if(newValue instanceof Timestamp){
                        newValue = new Date(((Timestamp) newValue).getTime());
                    }
                    
                    if(oldValue == null && newValue == null){
                        continue;
                    }else if(oldValue == null && newValue != null){
                        Map<String,Object> valueMap = new HashMap<String,Object>();
                            valueMap.put("oldValue",oldValue);
                            valueMap.put("newValue",newValue);
                        
                        map.put(name, valueMap);
                        
                        continue;
                    }
                    // compare two values are equal, unequal can map into the 
                    IF (! OldValue.equals (newValue)) {
                        Map<String,Object> valueMap = new HashMap<String,Object>();
                            valueMap.put("oldValue",oldValue);
                            valueMap.put("newValue",newValue);
                        
                        map.put(name, valueMap);
                    }
                }
            }
        }catch(Exception e){
            e.printStackTrace ();
        }
        
        return map;
    }
}

Guess you like

Origin www.cnblogs.com/wangquanyi/p/12106514.html