1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
| import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import lombok.extern.slf4j.Slf4j;
import java.util.ArrayList; import java.util.Comparator; import java.util.List; import java.util.Map; import java.util.stream.Collectors;
import static com.jiankunking.branchcompare.es.SortUtil.mapComparator;
/** * @Author jiankunking * @Date 2024/9/14 9:48 * @Description: */ @Slf4j public class MapCompareUtil {
public static boolean isMapEquals(Map<String, Object> offlineMap, Map<String, Object> onlineMap) throws JsonProcessingException { offlineMap = MapFlatUtil.flat(offlineMap); onlineMap = MapFlatUtil.flat(onlineMap); if (offlineMap.size() != onlineMap.size()) { return false; }
for (Map.Entry<String, Object> offlineEntry : offlineMap.entrySet()) { String offlineEntryKey = offlineEntry.getKey(); if (!onlineMap.containsKey(offlineEntryKey)) { return false; } Object offlineEntryValue = offlineEntry.getValue(); Object onlineEntryValue = onlineMap.get(offlineEntryKey);
Class offlineEntryValueClass = offlineEntryValue.getClass(); Class onlineEntryValueClass = onlineEntryValue.getClass();
if (offlineEntryValueClass != onlineEntryValueClass) { log.warn("value type not equals,offlineEntryValue:" + offlineEntryValueClass.getName() + ",onlineEntryValue:" + onlineEntryValueClass.getName()); return false; } if (offlineEntryValue instanceof Map) { Map<String, Object> offlineMapValue = (Map<String, Object>) offlineEntryValue; Map<String, Object> onlineMapValue = (Map<String, Object>) onlineEntryValue; if (!isMapEquals(offlineMapValue, onlineMapValue)) { return false; } continue; } else if (offlineEntryValue instanceof List) { List<Object> offlineList = (List<Object>) offlineEntryValue; List<Object> onlineList = (List<Object>) onlineEntryValue; if (offlineList.size() != onlineList.size()) { log.warn("list size not equals,offlineList:" + offlineList.size() + ",onlineList:" + onlineList.size()); return false; }
// List<Map> if (!offlineList.isEmpty() && offlineList.get(0) instanceof Map) { List<Map<String, Object>> offlineEntryValueTmp = (List<Map<String, Object>>) offlineEntryValue; List<Map<String, Object>> onlineEntryValueTmp = (List<Map<String, Object>>) onlineEntryValue;
List<SortUtil.Sort> sorts = new ArrayList<>(); // 按照map 的key 排序 for (Map.Entry<String, Object> entry : offlineEntryValueTmp.get(0).entrySet()) { sorts.add(new SortUtil.Sort(entry.getKey(), SortUtil.Order.ASC)); }
List<Map<String, Object>> offlineEntryValueSorted = offlineEntryValueTmp.stream() .sorted(mapComparator(sorts)) .collect(Collectors.toList()); List<Map<String, Object>> onlineEntryValueSorted = onlineEntryValueTmp.stream() .sorted(mapComparator(sorts)) .collect(Collectors.toList()); for (int i = 0; i < offlineEntryValueSorted.size(); i++) { Object offlineListItem = offlineEntryValueSorted.get(i); Object onlineListItem = onlineEntryValueSorted.get(i); if (!isMapEquals((Map<String, Object>) offlineListItem, (Map<String, Object>) onlineListItem)) { return false; } }
} else { // List<简单类型> offlineList.sort(Comparator.comparing(o -> o.toString())); onlineList.sort(Comparator.comparing(o -> o.toString())); for (int i = 0; i < offlineList.size(); i++) { Object offlineListItem = offlineList.get(i); Object onlineListItem = onlineList.get(i); if (!simpleObjectEquals(offlineListItem, onlineListItem)) { log.warn("list item not equals,offlineListItem:" + offlineListItem + ",onlineListItem:" + onlineListItem); return false; } } } continue; } if (!simpleObjectEquals(offlineEntryValue, onlineEntryValue)) { log.warn("map value not equals,offlineEntryValue:" + offlineEntryValue + ",onlineEntryValue:" + onlineEntryValue); return false; } } return true; }
// 只能处理简单对象 不能处理Map List等复杂类型 private static boolean simpleObjectEquals(Object o1, Object o2) throws JsonProcessingException { String offlineJson = new ObjectMapper().writeValueAsString(o1); String onlineJson = new ObjectMapper().writeValueAsString(o2); if (offlineJson.equals(onlineJson)) { return true; } return false; } }
|