java封装的比较器,比较是否全相同,获取不同字段名字

 非常实用的java比较器,贴上代码:

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.BooleanUtils;
import org.apache.commons.lang.ObjectUtils;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.mchange.v1.lang.NullUtils;

@SuppressWarnings("deprecation")
public class JVisaCommonCompare {
   
   private static Logger LOGGER = LoggerFactory.getLogger(JVisaCommonCompare.class);
   
   /** 获取不同值的字段(可以不同类型) */
   public static <S extends Object, T extends Object> Set<String> getDifferentFieldName(S source, T target, List<String> excludeFieldName) {
      LOGGER.info("======================getDifferentFieldName,source:[{}],target:[{}]",
            ToStringBuilder.reflectionToString(source), ToStringBuilder.reflectionToString(target));
      Set<String> fieldName = new HashSet<>();
      if (BooleanUtils.isTrue(NullUtils.equalsOrBothNull(source, target))) {
         return fieldName;
      }
      JsonConfig config = new JsonConfig();
      if (!CollectionUtils.isEmpty(excludeFieldName)) {
         String[] excludeArray = excludeFieldName.toArray(new String[excludeFieldName.size()]);
         config.setExcludes(excludeArray);
      }
      JSONObject sourceObj = JSONObject.fromObject(source, config);
      JSONObject targetObj = JSONObject.fromObject(target, config);
      fieldName = getAllDifferentFieldName(sourceObj, targetObj, config);
      LOGGER.info("======================getDifferentFieldName,result:[{}]", fieldName);
      return fieldName;
   }
   
   /** 判断两个对象是否每个字段都相等 */
   public static <S extends Object, T extends Object> Boolean isAllTheSameOfObject(S source, T target, List<String> excludeFieldName) {
      LOGGER.info("======================isAllTheSameOfObject,source:[{}],target:[{}]",
            ToStringBuilder.reflectionToString(source), ToStringBuilder.reflectionToString(target));
      Set<String> fieldName = new HashSet<>();
      if (BooleanUtils.isTrue(NullUtils.equalsOrBothNull(source, target))) {
         return true;
      }
      JsonConfig config = new JsonConfig();
      if (!CollectionUtils.isEmpty(excludeFieldName)) {
         String[] excludeArray = excludeFieldName.toArray(new String[excludeFieldName.size()]);
         config.setExcludes(excludeArray);
      }
      JSONObject sourceObj = JSONObject.fromObject(source, config);
      JSONObject targetObj = JSONObject.fromObject(target, config);
      fieldName = getAllDifferentFieldName(sourceObj, targetObj, config);
      if (CollectionUtils.isEmpty(fieldName)) {
         return true;
      }
      return false;
   }
   
   /** 获取不同值的字段名字(任何不同类型比较) */
   @SuppressWarnings({ "unused", "unchecked" })
   private static Set<String> getAllDifferentFieldName(JSONObject source, JSONObject target, JsonConfig config) {
      Set<String> fieldName = new HashSet<>();
      if (BooleanUtils.isTrue(NullUtils.equalsOrBothNull(source, target))) {
         return fieldName;
      }
      // if exist JSONObject is null

      if (BooleanUtils.isTrue(source.isNullObject())) {
         fieldName = new HashSet<>(JSONArray.toCollection(config == null ? target.names() : target.names(config)));
         return fieldName;
      }
      if (BooleanUtils.isTrue(target.isNullObject())) {
         fieldName = new HashSet<>(JSONArray.toCollection(config == null ? source.names() : source.names(config)));
         return fieldName;
      }
      // set the config of field

      JSONArray fieldKeySource = null;
      JSONArray fieldKeyTarget = null;
      if (null != config) {
         fieldKeySource = source.names(config);
         fieldKeyTarget = target.names(config);
      } else {
         fieldKeySource = source.names();
         fieldKeyTarget = source.names();
      }
      // compare

      for (int sindex = 0; null != fieldKeySource && sindex < fieldKeySource.size(); sindex++) {
         Object key = fieldKeySource.get(sindex);
         if (!BooleanUtils.isTrue(target.containsKey(key))) {
            fieldName.add((String) key);
            continue;
         }
         Object value1 = source.get(key);
         Object value2 = target.get(key);
         if (!ObjectUtils.equals(value1, value2)) {
            fieldName.add((String) key);
         }
      }
      for (int tindex = 0; null != fieldKeyTarget && tindex < fieldKeyTarget.size(); tindex++) {
         Object key = fieldKeyTarget.get(tindex);
         if (!BooleanUtils.isTrue(source.containsKey(key))) {
            fieldName.add((String) key);
            continue;
         }
         Object value2 = target.get(key);
         Object value1 = source.get(key);
         if (!ObjectUtils.equals(value2, value1)) {
            fieldName.add((String) key);
         }
      }
      return fieldName;
   }
   
}

 

你可能感兴趣的:(java封装的比较器,比较是否全相同,获取不同字段名字)