Skip to content

Commit 29d8064

Browse files
committed
Support TreeDoc.dedupeNodes(); update TDNode.hashCode() to not include key
1 parent bb7db42 commit 29d8064

File tree

6 files changed

+75
-29
lines changed

6 files changed

+75
-29
lines changed

pom.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@
8585
<dependency>
8686
<groupId>org.projectlombok</groupId>
8787
<artifactId>lombok</artifactId>
88-
<version>1.18.16</version>
88+
<version>1.18.24</version>
8989
<scope>provided</scope>
9090
</dependency>
9191
<dependency>
@@ -124,7 +124,7 @@
124124
<path>
125125
<groupId>org.projectlombok</groupId>
126126
<artifactId>lombok</artifactId>
127-
<version>1.18.2</version>
127+
<version>1.18.24</version>
128128
</path>
129129
</annotationProcessorPaths>
130130
</configuration>
@@ -182,7 +182,7 @@
182182
<plugin>
183183
<groupId>org.apache.maven.plugins</groupId>
184184
<artifactId>maven-surefire-plugin</artifactId>
185-
<version>2.21.0</version>
185+
<version>2.12.4</version>
186186
<configuration>
187187
<skipTests>true</skipTests>
188188
</configuration>
@@ -218,7 +218,7 @@
218218
<plugin>
219219
<groupId>org.apache.maven.plugins</groupId>
220220
<artifactId>maven-javadoc-plugin</artifactId>
221-
<version>3.1.0</version>
221+
<version>3.4.1</version>
222222
<executions>
223223
<execution>
224224
<id>attach-javadocs</id>

treedoc/src/main/java/org/jsonex/treedoc/TDNode.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
import static org.jsonex.core.util.LangUtil.orElse;
2929
import static org.jsonex.core.util.ListUtil.last;
30+
import static org.jsonex.core.util.ListUtil.map;
3031

3132
/** A Node in TreeDoc */
3233
@RequiredArgsConstructor
@@ -249,11 +250,11 @@ public StringBuilder toString(StringBuilder sb, boolean includeRootKey, boolean
249250
}
250251

251252
public List<Object> childrenValueAsList() {
252-
return getChildren() == null ? Collections.emptyList() : ListUtil.map(getChildren(), c -> c.getValue());
253+
return getChildren() == null ? Collections.emptyList() : map(getChildren(), c -> c.getValue());
253254
}
254255

255256
public List<List<Object>> childrenValueAsListOfList() {
256-
return getChildren() == null ? Collections.emptyList() : ListUtil.map(getChildren(), c -> c.childrenValueAsList());
257+
return getChildren() == null ? Collections.emptyList() : map(getChildren(), c -> c.childrenValueAsList());
257258
}
258259

259260
@Override public boolean equals(Object o) {
@@ -267,7 +268,8 @@ public List<List<Object>> childrenValueAsListOfList() {
267268
return Objects.equals(key, tdNode.key) && Objects.equals(value, tdNode.value) && Objects.equals(children, tdNode.children);
268269
}
269270

271+
/** Hash code of value and children, key is not included */
270272
@Override public int hashCode() {
271-
return hash.getOrCompute(() -> Objects.hash(key, value, children));
273+
return hash.getOrCompute(() -> Objects.hash(value, children, map(children, TDNode::getKey)));
272274
}
273275
}

treedoc/src/main/java/org/jsonex/treedoc/TreeDoc.java

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@
66
import lombok.experimental.Accessors;
77

88
import java.net.URI;
9-
import java.util.Collection;
10-
import java.util.HashMap;
11-
import java.util.Map;
9+
import java.util.*;
1210

11+
import static java.lang.String.format;
1312
import static org.jsonex.core.util.LangUtil.doIfNotNull;
1413
import static org.jsonex.core.util.ListUtil.mapKeys;
1514

@@ -64,4 +63,32 @@ public static TreeDoc merge(Collection<TDNode> nodes) {
6463

6564
return result;
6665
}
66+
67+
/**
68+
* Dedupe the nodes that are with the same contends by comparing the hash
69+
*/
70+
public void dedupeNodes() {
71+
Map<Integer, TDNode> hashToNodeMap = new LinkedHashMap<>();
72+
Queue<TDNode> queue = new LinkedList<>();
73+
queue.add(root);
74+
while(!queue.isEmpty()) {
75+
TDNode n = queue.poll();
76+
if (n.isLeaf())
77+
continue; // Don't dedupe leaf node and array node
78+
int hash = n.hashCode();
79+
TDNode refNode = hashToNodeMap.get(hash);
80+
if ( refNode != null && refNode.getType() == TDNode.Type.MAP) {
81+
if (refNode.getChild(TDNode.ID_KEY) == null)
82+
refNode.createChild(TDNode.ID_KEY).setValue(format("%x", hash));
83+
n.children.clear();;
84+
n.setType(TDNode.Type.MAP).createChild(TDNode.REF_KEY).setValue(format("#%x", hash));
85+
} else {
86+
hashToNodeMap.put(hash, n);
87+
if (n.hasChildren())
88+
for (TDNode cn : n.children) {
89+
queue.add(cn);
90+
}
91+
}
92+
}
93+
}
6794
}

treedoc/src/test/java/org/jsonex/treeedoc/TDPathTest.java

Lines changed: 0 additions & 19 deletions
This file was deleted.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"a": 1,
3+
"b": {
4+
"c": [1,2,3,4],
5+
"d": {
6+
"e": 1,
7+
"f": 2
8+
}
9+
},
10+
"e": {
11+
"c": [1, 2, 3, 4],
12+
"d": {
13+
"e": 1,
14+
"f": 2
15+
}
16+
}
17+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
a:1,
3+
b:{
4+
c:[
5+
1,
6+
2,
7+
3,
8+
4
9+
],
10+
d:{
11+
e:1,
12+
f:2
13+
},
14+
$id:"5a9c3cc0"
15+
},
16+
e:{
17+
$ref:"#5a9c3cc0"
18+
}
19+
}

0 commit comments

Comments
 (0)