BeanUtils compare 对象属性值

/*
 * Copyright 2002-2007 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.euler.util.beans;

import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeansException;
import org.springframework.util.Assert;

import com.euler.orm.Organization;

public abstract class BeanUtils {

    /**
     * 对比对象属性值 返回不相同集合
    * @Title: compareProperties
    * @Description: TODO
    * @param @param source
    * @param @param target
    * @param @return
    * @param @throws Throwable   
    * @return List
    * @throws
     */
    public static List compareProperties(Object source,Object target) throws Throwable {
        return compareProperties(source, target,false);
    }

    /**
     * 对比两个对象之间的属性值 返回
     * List<String,Map<String,Map<String,String>>> 
     * try { 
     * list =BeanUtils.compareProperties(organization, organization2);
     *  } catch(Throwable e) { 
     *  // TODO Auto-generated catch block e.printStackTrace(); 
     *  }
     * for (Map map : list) { 
     * Set<String> set=map.keySet();
     *  for(String key:set){
     *   sb.append("字段名"+key); 
     *   Map nmap=(HashMap)map.get(key); 
     *   Set<Object> nset=nmap.keySet(); 
     *   for(Object value:nset){
     *    sb.append("源:"+value);
     * sb.append("目:"+nmap.get(value)); 
     *          } 
     *      } 
     * }
     * 
     * @author Chaiqingsong
     * @Title: compareProperties
     * @Description: TODO
     * @param
     * @param source
     * @param
     * @param target
     * @param
     * @return
     * @param
     * @throws Throwable
     * @return List
     * @throws
     */
    private static List compareProperties(Object source,
                                          Object target,boolean returnString) throws Throwable {

        Assert.notNull(source, "Source must not be null");
        Assert.notNull(target, "Target must not be null");

        Class actualEditable = target.getClass();
        List result = new ArrayList();
        PropertyDescriptor[] targetPds = getPropertyDescriptors(actualEditable);

        for (int i = 0; i < targetPds.length; i++) {
            PropertyDescriptor targetPd = targetPds[i];

            if (targetPd.getWriteMethod() != null) {
                PropertyDescriptor sourcePd = getPropertyDescriptor(source.getClass(), targetPd.getName());
                if (sourcePd != null && sourcePd.getReadMethod() != null) {
                    try {
                        Method readMethod = sourcePd.getReadMethod();
                        if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers())) {
                            readMethod.setAccessible(true);
                        }
                        //源值 和 目标值 对比
                        Object svalue = readMethod.invoke(source, new Object[0]);
                        Object tvalue = readMethod.invoke(target, new Object[0]);
                        if(svalue==null){
                            svalue="";
                        }
                        if (svalue != null && tvalue != null && !svalue.equals(tvalue)) {
                            Map rs = new HashMap();
                            Map values = new HashMap();
                            values.put(svalue, tvalue);
                            rs.put(sourcePd.getName(), values);
                            result.add(rs);
                        }

                    } catch (Throwable ex) {
                        throw ex;
                    }
                }
            }
        }

        return result;
    }

    public static void main(String[] args) {
        StringBuffer sb = new StringBuffer();
        Organization organization = new Organization();
        organization.setOrgid("123456");
        organization.setOrgname("第一个");
        organization.setXtlb("1");

        Organization organization2 = new Organization();
        organization2.setOrgid("654321");
        organization2.setOrgname("第二个");
        organization2.setXtlb("1");
        List<Map> list = null;
        try {
            list = BeanUtils.compareProperties(organization, organization2);
        } catch (Throwable e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        for (Map map : list) {
            Set<String> set = map.keySet();
            for (String key : set) {
                sb.append("字段名" + key);
                Map nmap = (HashMap) map.get(key);
                Set<Object> nset = nmap.keySet();
                for (Object value : nset) {
                    sb.append("源:" + value);
                    sb.append("目:" + nmap.get(value));
                }
            }
        }
        System.out.println(sb.toString());

    }

}

 

你可能感兴趣的:(BeanUtils)