Skip to content

Commit 64053c4

Browse files
committed
Reduce BlueIdCalculator.calculate complexity from O(N^2) to O(N).
1 parent 3ee9d39 commit 64053c4

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

src/main/java/blue/language/utils/BlueIdCalculator.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@ public static String calculateBlueId(List<Node> nodes) {
3030
}
3131

3232
public String calculate(Object object) {
33-
Object cleanedObject = cleanStructure(object);
33+
// we invoke calculateCleanedObject method only once (for root)
34+
return calculateCleanedObject(cleanStructure(object));
35+
}
36+
37+
private String calculateCleanedObject(Object cleanedObject) {
3438
if (cleanedObject instanceof String || cleanedObject instanceof Number || cleanedObject instanceof Boolean) {
3539
return hashProvider.apply(cleanedObject.toString());
3640
} else if (cleanedObject instanceof Map) {
@@ -53,7 +57,7 @@ private String calculateMap(Map<String, Object> map) {
5357
if (OBJECT_NAME.equals(key) || OBJECT_VALUE.equals(key) || OBJECT_DESCRIPTION.equals(key)) {
5458
hashes.put(key, entry.getValue());
5559
} else {
56-
String blueId = calculate(entry.getValue());
60+
String blueId = calculateCleanedObject(entry.getValue());
5761
hashes.put(key, Collections.singletonMap("blueId", blueId));
5862
}
5963
}
@@ -62,14 +66,14 @@ private String calculateMap(Map<String, Object> map) {
6266

6367
private String calculateList(List<Object> list) {
6468
if (list.size() == 1) {
65-
return calculate(list.get(0));
69+
return calculateCleanedObject(list.get(0));
6670
}
6771

6872
List<Object> subList = list.subList(0, list.size() - 1);
6973
String hashOfSubList = calculateList(subList);
7074

7175
Object lastElement = list.get(list.size() - 1);
72-
String hashOfLastElement = calculate(lastElement);
76+
String hashOfLastElement = calculateCleanedObject(lastElement);
7377

7478
List<Map<String, String>> result = Arrays.asList(
7579
Collections.singletonMap("blueId", hashOfSubList),

0 commit comments

Comments
 (0)