Skip to content

Commit 7cb9d3a

Browse files
committed
Tried to improve comparison
1 parent 1b6c9f1 commit 7cb9d3a

File tree

1 file changed

+25
-29
lines changed

1 file changed

+25
-29
lines changed

src/main/java/com/APIprotector/apiprotector/service/JsonComparator.java

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -76,48 +76,44 @@ else if (node1 != null && node1.isArray() && node2 != null && node2.isArray()) {
7676
List<JsonNode> list2 = new ArrayList<>();
7777
node1.forEach(list1::add);
7878
node2.forEach(list2::add);
79+
list1.sort(Comparator.comparing(JsonNode::toString));
80+
list2.sort(Comparator.comparing(JsonNode::toString));
7981
int i = 0;
8082

81-
// Identify removed items (exist in list1 but not in list2)
82-
for (JsonNode node : list1) {
83-
if (!containsNode(list2, node)) {
84-
String newPath = path + "[" + i + "]";
85-
JsonNode childNode = compareJsonNodes(node, null, newPath, nodeKey + "[" + i + "]");
86-
children.add(childNode);
87-
i++;
88-
}
89-
}
90-
91-
// Identify added items (exist in list2 but not in list1)
92-
for (JsonNode node : list2) {
93-
if (!containsNode(list1, node)) {
94-
String newPath = path + "[" + i + "]";
95-
JsonNode childNode = compareJsonNodes(null, node, newPath, nodeKey + "[" + i + "]");
96-
children.add(childNode);
97-
i++;
98-
}
99-
}
83+
List<JsonNode> list1rem = new ArrayList<>();
84+
List<JsonNode> list2rem = new ArrayList<>();
10085

10186
// Identify changed items (same structure but different values)
10287
for (JsonNode nodeNo1 : list1) {
10388
for (JsonNode nodeNo2 : list2) {
104-
if (isSimilar(nodeNo1, nodeNo2) && !nodeNo1.equals(nodeNo2)) {
89+
if (nodeNo1.equals(nodeNo2) || isSimilar(nodeNo1, nodeNo2)){
10590
String newPath = path + "[" + i + "]";
10691
JsonNode childNode = compareJsonNodes(nodeNo1, nodeNo2, newPath, nodeKey + "[" + i + "]");
10792
children.add(childNode);
10893
i++;
94+
// Remove the matched node from list2 and list1 to avoid duplicate comparisons
95+
list1rem.add(nodeNo1);
96+
list2rem.add(nodeNo2);
10997
}
11098
}
11199
}
112-
// Identify unchanged items (same structure and same values)
113-
for (JsonNode nodeNo1 : list1) {
114-
for (JsonNode nodeNo2 : list2) {
115-
if (nodeNo1.equals(nodeNo2)) {
116-
String newPath = path + "[" + i + "]";
117-
JsonNode childNode = compareJsonNodes(nodeNo1, nodeNo2, newPath, nodeKey + "[" + i + "]");
118-
children.add(childNode);
119-
i++;
120-
}
100+
101+
// Remove matched nodes from the original lists
102+
list1.removeAll(list1rem);
103+
list2.removeAll(list2rem);
104+
105+
for (JsonNode node : list1) {
106+
// Identify removed items (exist in list1 but not in list2)
107+
if (!containsNode(list2, node)) {
108+
String newPath = path + "[" + i + "]";
109+
JsonNode childNode = compareJsonNodes(node, null, newPath, nodeKey + "[" + i + "]");
110+
children.add(childNode);
111+
i++;
112+
} else {// Identify added items (exist in list2 but not in list1)
113+
String newPath = path + "[" + i + "]";
114+
JsonNode childNode = compareJsonNodes(null, node, newPath, nodeKey + "[" + i + "]");
115+
children.add(childNode);
116+
i++;
121117
}
122118
}
123119
}

0 commit comments

Comments
 (0)