From c116bfbc2c39b0268be637d03bdcce82d20264f7 Mon Sep 17 00:00:00 2001 From: GeorgeC Date: Tue, 6 Feb 2024 15:19:32 -0500 Subject: [PATCH 01/10] Update key length handling in limitKeySize The limitKeySize method in the VisualizationUtil class has been updated to ensure key uniqueness when keys exceed a certain length. A more robust method of shortening the keys has been implemented: if the key is longer than 45 characters, it is cut off and replaced with "..." and additional characters are appended to ensure uniqueness if needed. This helps limit key size while preserving uniqueness. --- .../dbmi/avillach/util/VisualizationUtil.java | 56 ++++++++++++++----- 1 file changed, 42 insertions(+), 14 deletions(-) diff --git a/pic-sure-util/src/main/java/edu/harvard/dbmi/avillach/util/VisualizationUtil.java b/pic-sure-util/src/main/java/edu/harvard/dbmi/avillach/util/VisualizationUtil.java index 8a7d99a7..0a2e91cb 100644 --- a/pic-sure-util/src/main/java/edu/harvard/dbmi/avillach/util/VisualizationUtil.java +++ b/pic-sure-util/src/main/java/edu/harvard/dbmi/avillach/util/VisualizationUtil.java @@ -69,25 +69,53 @@ public static Map doProcessResults(Map axisMap } /** - * Replaces long column names with shorter version. + * This method is used to limit the size of the keys in the axisMap to a maximum of 45 characters. If the key is longer + * than 45 characters, it will be shortened to 45 characters and the last 3 characters will be replaced with "...". + * If the shortened key is not unique, we will search for the first unique key by walking through the string and + * grabbing the next 6 characters after the first unique character. We then build a new key with the shortened key and + * the unique characters and add it to the newAxisMap. + *

* - * @param axisMap - * @return + * @param axisMap - Map of the categories and their counts + * @return Map - Map of the categories and their counts with the keys limited to 45 characters */ private static Map limitKeySize(Map axisMap) { - List toRemove = new ArrayList<>(); - Map toAdd = new HashMap<>(); - axisMap.keySet().forEach(key -> { - if (key.length() > MAX_X_LABEL_LINE_LENGTH) { - toRemove.add(key); - toAdd.put( - key.substring(0, MAX_X_LABEL_LINE_LENGTH - 3) + "...", - axisMap.get(key)); + Map newAxisMap = new HashMap<>(); + HashSet keys = new HashSet<>(); + + axisMap.forEach((key, value) -> { + if (key.length() < MAX_X_LABEL_LINE_LENGTH) { + newAxisMap.put(key, value); + keys.add(key); + } else { + String shortKey = key.substring(0, MAX_X_LABEL_LINE_LENGTH - 3); + if (keys.contains(shortKey)) { + for (int i = 0; i < key.length(); i++) { + if (i + MAX_X_LABEL_LINE_LENGTH < key.length()) { + // Walk through the string to find the first unique key. + shortKey = key.substring(i, i + MAX_X_LABEL_LINE_LENGTH - 3); + + // We check if the short key is unique by checking if it is not in the keys set. + if (!keys.contains(shortKey)) { + // Get the next 6 characters after the first unique character. + String uniqueEnd = key.substring(i + MAX_X_LABEL_LINE_LENGTH - 3, i + MAX_X_LABEL_LINE_LENGTH + 3); + // remove 6 characters from the original short key + shortKey = key.substring(i, MAX_X_LABEL_LINE_LENGTH - 3) + "..." + uniqueEnd; + // we can break here because we have found a unique key + break; + } + } + } + } else { + shortKey = shortKey + "..."; + } + + newAxisMap.put(shortKey, value); + keys.add(shortKey); } }); - toRemove.forEach(key -> axisMap.remove(key)); - axisMap.putAll(toAdd); - return axisMap; + + return newAxisMap; } } From 909a19c6772e3d8f04690a89751c2df7def55bfe Mon Sep 17 00:00:00 2001 From: GeorgeC Date: Wed, 7 Feb 2024 09:21:58 -0500 Subject: [PATCH 02/10] Add VisualizationUtilTests and refine key shortening logic Added a new test class, VisualizationUtilTests to validate the functionality of the VisualizationUtil class. Adjusted the 'limitKeySize' method to ensure key uniqueness when keys are shortened to a maximum length of 45 characters. If shortened keys are not unique, additional trailing characters are included until uniqueness is achieved. --- .../service/VisualizationUtilTests.java | 26 ++++++++++ .../dbmi/avillach/util/VisualizationUtil.java | 51 +++++++++++-------- 2 files changed, 56 insertions(+), 21 deletions(-) create mode 100644 pic-sure-resources/pic-sure-visualization-resource/src/test/java/edu/harvard/hms/dbmi/avillach/resource/visualization/service/VisualizationUtilTests.java diff --git a/pic-sure-resources/pic-sure-visualization-resource/src/test/java/edu/harvard/hms/dbmi/avillach/resource/visualization/service/VisualizationUtilTests.java b/pic-sure-resources/pic-sure-visualization-resource/src/test/java/edu/harvard/hms/dbmi/avillach/resource/visualization/service/VisualizationUtilTests.java new file mode 100644 index 00000000..3c48afcb --- /dev/null +++ b/pic-sure-resources/pic-sure-visualization-resource/src/test/java/edu/harvard/hms/dbmi/avillach/resource/visualization/service/VisualizationUtilTests.java @@ -0,0 +1,26 @@ +package edu.harvard.hms.dbmi.avillach.resource.visualization.service; + +import edu.harvard.dbmi.avillach.util.VisualizationUtil; +import org.junit.Test; +import org.junit.jupiter.api.DisplayName; + +import java.util.HashMap; +import java.util.Map; + +public class VisualizationUtilTests { + + @Test + @DisplayName("Test limitKeySize") + public void testLimitKeySizeUniqueness() { + // Testing name uniqueness + Map axisMap = new HashMap<>(); + axisMap.put("Disease-Specific (Asthma, Allergy and Inflammation, PUB)", 1); + axisMap.put("Disease-Specific (Asthma, Allergy and Inflammation, PUB, NPU)", 1); + axisMap.put("Disease-Specific (Asthma, Allergy and Inflammation, NPU)", 1); + axisMap.put("Disease-Specific (Asthma, Allergy and Inflammation)", 1); + + Map stringIntegerMap = VisualizationUtil.limitKeySize(axisMap); + assert (stringIntegerMap.size() == 4); + } + +} diff --git a/pic-sure-util/src/main/java/edu/harvard/dbmi/avillach/util/VisualizationUtil.java b/pic-sure-util/src/main/java/edu/harvard/dbmi/avillach/util/VisualizationUtil.java index 0a2e91cb..0b8be08f 100644 --- a/pic-sure-util/src/main/java/edu/harvard/dbmi/avillach/util/VisualizationUtil.java +++ b/pic-sure-util/src/main/java/edu/harvard/dbmi/avillach/util/VisualizationUtil.java @@ -79,7 +79,7 @@ public static Map doProcessResults(Map axisMap * @param axisMap - Map of the categories and their counts * @return Map - Map of the categories and their counts with the keys limited to 45 characters */ - private static Map limitKeySize(Map axisMap) { + public static Map limitKeySize(Map axisMap) { Map newAxisMap = new HashMap<>(); HashSet keys = new HashSet<>(); @@ -88,30 +88,39 @@ private static Map limitKeySize(Map axisMap) { newAxisMap.put(key, value); keys.add(key); } else { - String shortKey = key.substring(0, MAX_X_LABEL_LINE_LENGTH - 3); - if (keys.contains(shortKey)) { - for (int i = 0; i < key.length(); i++) { - if (i + MAX_X_LABEL_LINE_LENGTH < key.length()) { - // Walk through the string to find the first unique key. - shortKey = key.substring(i, i + MAX_X_LABEL_LINE_LENGTH - 3); - - // We check if the short key is unique by checking if it is not in the keys set. - if (!keys.contains(shortKey)) { - // Get the next 6 characters after the first unique character. - String uniqueEnd = key.substring(i + MAX_X_LABEL_LINE_LENGTH - 3, i + MAX_X_LABEL_LINE_LENGTH + 3); - // remove 6 characters from the original short key - shortKey = key.substring(i, MAX_X_LABEL_LINE_LENGTH - 3) + "..." + uniqueEnd; - // we can break here because we have found a unique key - break; - } - } + // proposed key + StringBuilder proposedKey; + + // Check if the key exists in the keys set. + boolean hasEqual = false; + // Let first check if the proposed key is a leading substring of another key + for (String innerKey : axisMap.keySet()) {// Check if this key will be equal to any future keys. + hasEqual = key.substring(0, MAX_X_LABEL_LINE_LENGTH).equals(innerKey.substring(0, MAX_X_LABEL_LINE_LENGTH)); + if (hasEqual) { + break; } + } + + if (hasEqual) { // if we have an equal we want to create a more unique key + int countFromEnd = 6; + String uniqueness; + do { + // get the original key substring max x + 3 (for the dots) + countFromEnd. + proposedKey = new StringBuilder(key.substring(0, MAX_X_LABEL_LINE_LENGTH - 3 - countFromEnd)); + + // Get the last "countFromEnd" characters + uniqueness = key.substring(key.length() - countFromEnd); + + proposedKey.append("...").append(uniqueness); + countFromEnd++; // increase the number of characters from the end of the string we will use + } while (keys.contains(proposedKey.toString())); + } else { - shortKey = shortKey + "..."; + proposedKey = new StringBuilder(key.substring(0, MAX_X_LABEL_LINE_LENGTH - 3) + "..."); } - newAxisMap.put(shortKey, value); - keys.add(shortKey); + newAxisMap.put(proposedKey.toString(), value); + keys.add(proposedKey.toString()); } }); From 051f0f4bd12905e2f3a57ce951527e283e1edd6f Mon Sep 17 00:00:00 2001 From: GeorgeC Date: Wed, 7 Feb 2024 15:40:47 -0500 Subject: [PATCH 03/10] Add check on length before substring --- .../edu/harvard/dbmi/avillach/util/VisualizationUtil.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/pic-sure-util/src/main/java/edu/harvard/dbmi/avillach/util/VisualizationUtil.java b/pic-sure-util/src/main/java/edu/harvard/dbmi/avillach/util/VisualizationUtil.java index 0b8be08f..66e452dd 100644 --- a/pic-sure-util/src/main/java/edu/harvard/dbmi/avillach/util/VisualizationUtil.java +++ b/pic-sure-util/src/main/java/edu/harvard/dbmi/avillach/util/VisualizationUtil.java @@ -95,9 +95,11 @@ public static Map limitKeySize(Map axisMap) { boolean hasEqual = false; // Let first check if the proposed key is a leading substring of another key for (String innerKey : axisMap.keySet()) {// Check if this key will be equal to any future keys. - hasEqual = key.substring(0, MAX_X_LABEL_LINE_LENGTH).equals(innerKey.substring(0, MAX_X_LABEL_LINE_LENGTH)); - if (hasEqual) { - break; + if (innerKey.length() >= MAX_X_LABEL_LINE_LENGTH) { + hasEqual = key.substring(0, MAX_X_LABEL_LINE_LENGTH).equals(innerKey.substring(0, MAX_X_LABEL_LINE_LENGTH)); + if (hasEqual) { + break; + } } } From e49bcbb2a57b7db8f813070772785e82ce61aa30 Mon Sep 17 00:00:00 2001 From: Gcolon021 <34667267+Gcolon021@users.noreply.github.com> Date: Thu, 8 Feb 2024 10:38:09 -0500 Subject: [PATCH 04/10] Update pic-sure-util/src/main/java/edu/harvard/dbmi/avillach/util/VisualizationUtil.java --- .../java/edu/harvard/dbmi/avillach/util/VisualizationUtil.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pic-sure-util/src/main/java/edu/harvard/dbmi/avillach/util/VisualizationUtil.java b/pic-sure-util/src/main/java/edu/harvard/dbmi/avillach/util/VisualizationUtil.java index 66e452dd..aba4ee2e 100644 --- a/pic-sure-util/src/main/java/edu/harvard/dbmi/avillach/util/VisualizationUtil.java +++ b/pic-sure-util/src/main/java/edu/harvard/dbmi/avillach/util/VisualizationUtil.java @@ -71,7 +71,7 @@ public static Map doProcessResults(Map axisMap /** * This method is used to limit the size of the keys in the axisMap to a maximum of 45 characters. If the key is longer * than 45 characters, it will be shortened to 45 characters and the last 3 characters will be replaced with "...". - * If the shortened key is not unique, we will search for the first unique key by walking through the string and + * If the shortened key is not unique, we will search for the first unique key by walking backward through the string and * grabbing the next 6 characters after the first unique character. We then build a new key with the shortened key and * the unique characters and add it to the newAxisMap. *

From e3e2d67c5e126a4deed495bf3c1ad5753c335df9 Mon Sep 17 00:00:00 2001 From: Gcolon021 <34667267+Gcolon021@users.noreply.github.com> Date: Thu, 8 Feb 2024 10:39:47 -0500 Subject: [PATCH 05/10] Update pic-sure-util/src/main/java/edu/harvard/dbmi/avillach/util/VisualizationUtil.java --- .../edu/harvard/dbmi/avillach/util/VisualizationUtil.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/pic-sure-util/src/main/java/edu/harvard/dbmi/avillach/util/VisualizationUtil.java b/pic-sure-util/src/main/java/edu/harvard/dbmi/avillach/util/VisualizationUtil.java index aba4ee2e..bbdce22b 100644 --- a/pic-sure-util/src/main/java/edu/harvard/dbmi/avillach/util/VisualizationUtil.java +++ b/pic-sure-util/src/main/java/edu/harvard/dbmi/avillach/util/VisualizationUtil.java @@ -71,9 +71,7 @@ public static Map doProcessResults(Map axisMap /** * This method is used to limit the size of the keys in the axisMap to a maximum of 45 characters. If the key is longer * than 45 characters, it will be shortened to 45 characters and the last 3 characters will be replaced with "...". - * If the shortened key is not unique, we will search for the first unique key by walking backward through the string and - * grabbing the next 6 characters after the first unique character. We then build a new key with the shortened key and - * the unique characters and add it to the newAxisMap. + * If the shortened key is not unique, we will create a unique one *

* * @param axisMap - Map of the categories and their counts From 1f4778f7e01fadf3742eb13f5d3f8374df0219df Mon Sep 17 00:00:00 2001 From: Gcolon021 <34667267+Gcolon021@users.noreply.github.com> Date: Thu, 8 Feb 2024 10:40:26 -0500 Subject: [PATCH 06/10] Update pic-sure-util/src/main/java/edu/harvard/dbmi/avillach/util/VisualizationUtil.java --- .../java/edu/harvard/dbmi/avillach/util/VisualizationUtil.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pic-sure-util/src/main/java/edu/harvard/dbmi/avillach/util/VisualizationUtil.java b/pic-sure-util/src/main/java/edu/harvard/dbmi/avillach/util/VisualizationUtil.java index bbdce22b..53a8e23d 100644 --- a/pic-sure-util/src/main/java/edu/harvard/dbmi/avillach/util/VisualizationUtil.java +++ b/pic-sure-util/src/main/java/edu/harvard/dbmi/avillach/util/VisualizationUtil.java @@ -105,7 +105,7 @@ public static Map limitKeySize(Map axisMap) { int countFromEnd = 6; String uniqueness; do { - // get the original key substring max x + 3 (for the dots) + countFromEnd. + // get the original key substring max x - 3 (for the dots) - countFromEnd. proposedKey = new StringBuilder(key.substring(0, MAX_X_LABEL_LINE_LENGTH - 3 - countFromEnd)); // Get the last "countFromEnd" characters From f9ca0651425cdced7fdbe80b1df12734573fbe17 Mon Sep 17 00:00:00 2001 From: Gcolon021 <34667267+Gcolon021@users.noreply.github.com> Date: Thu, 8 Feb 2024 12:37:59 -0500 Subject: [PATCH 07/10] Update pic-sure-resources/pic-sure-visualization-resource/src/test/java/edu/harvard/hms/dbmi/avillach/resource/visualization/service/VisualizationUtilTests.java Co-authored-by: Luke Sikina --- .../service/VisualizationUtilTests.java | 31 ++++++++++++------- 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/pic-sure-resources/pic-sure-visualization-resource/src/test/java/edu/harvard/hms/dbmi/avillach/resource/visualization/service/VisualizationUtilTests.java b/pic-sure-resources/pic-sure-visualization-resource/src/test/java/edu/harvard/hms/dbmi/avillach/resource/visualization/service/VisualizationUtilTests.java index 3c48afcb..076cbf1e 100644 --- a/pic-sure-resources/pic-sure-visualization-resource/src/test/java/edu/harvard/hms/dbmi/avillach/resource/visualization/service/VisualizationUtilTests.java +++ b/pic-sure-resources/pic-sure-visualization-resource/src/test/java/edu/harvard/hms/dbmi/avillach/resource/visualization/service/VisualizationUtilTests.java @@ -9,18 +9,25 @@ public class VisualizationUtilTests { - @Test - @DisplayName("Test limitKeySize") - public void testLimitKeySizeUniqueness() { - // Testing name uniqueness - Map axisMap = new HashMap<>(); - axisMap.put("Disease-Specific (Asthma, Allergy and Inflammation, PUB)", 1); - axisMap.put("Disease-Specific (Asthma, Allergy and Inflammation, PUB, NPU)", 1); - axisMap.put("Disease-Specific (Asthma, Allergy and Inflammation, NPU)", 1); - axisMap.put("Disease-Specific (Asthma, Allergy and Inflammation)", 1); + @Test + @DisplayName("Test limitKeySize") + public void testLimitKeySizeUniqueness() { + Map axisMap = new HashMap<>(Map.of( + "Disease-Specific (Asthma, Allergy and Inflammation, PUB)", 1, + "Disease-Specific (Asthma, Allergy and Inflammation, PUB, NPU)", 1, + "Disease-Specific (Asthma, Allergy and Inflammation, NPU)", 1, + "Disease-Specific (Asthma, Allergy and Inflammation)", 1 + )); - Map stringIntegerMap = VisualizationUtil.limitKeySize(axisMap); - assert (stringIntegerMap.size() == 4); - } + Map actual = VisualizationUtil.limitKeySize(axisMap); + + Map expected = Map.of( + "Disease-Specific (Asthma, Allergy an..., PUB)", 1, + "Disease-Specific (Asthma, Allergy an...ation)", 1, + "Disease-Specific (Asthma, Allergy an..., NPU)", 1, + "Disease-Specific (Asthma, Allergy a...B, NPU)", 1 + ); + assertEquals(expected, actual); + } } From 3e62cbbe7435644bb5d5f42740fdc1f20975666c Mon Sep 17 00:00:00 2001 From: GeorgeC Date: Thu, 8 Feb 2024 12:41:54 -0500 Subject: [PATCH 08/10] Fix import for modified unit test --- .../resource/visualization/service/VisualizationUtilTests.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pic-sure-resources/pic-sure-visualization-resource/src/test/java/edu/harvard/hms/dbmi/avillach/resource/visualization/service/VisualizationUtilTests.java b/pic-sure-resources/pic-sure-visualization-resource/src/test/java/edu/harvard/hms/dbmi/avillach/resource/visualization/service/VisualizationUtilTests.java index 076cbf1e..8a2798d2 100644 --- a/pic-sure-resources/pic-sure-visualization-resource/src/test/java/edu/harvard/hms/dbmi/avillach/resource/visualization/service/VisualizationUtilTests.java +++ b/pic-sure-resources/pic-sure-visualization-resource/src/test/java/edu/harvard/hms/dbmi/avillach/resource/visualization/service/VisualizationUtilTests.java @@ -7,6 +7,8 @@ import java.util.HashMap; import java.util.Map; +import static org.junit.Assert.assertEquals; + public class VisualizationUtilTests { @Test From 9fa4347b869d86c6cadba9726d7b0d6850a97a1c Mon Sep 17 00:00:00 2001 From: GeorgeC Date: Mon, 12 Feb 2024 14:53:07 -0500 Subject: [PATCH 09/10] Refactor and add test cases for key size limiting function Refactors existing code by simplifying key size limiting logic in VisualizationUtil and added several new unit tests to ensure its correct behavior with different input scenarios including long keys, empty maps, null maps, and uniqueness near middle. --- .../service/VisualizationUtilTests.java | 96 +++++++++++++++---- .../dbmi/avillach/util/VisualizationUtil.java | 70 ++++++-------- 2 files changed, 105 insertions(+), 61 deletions(-) diff --git a/pic-sure-resources/pic-sure-visualization-resource/src/test/java/edu/harvard/hms/dbmi/avillach/resource/visualization/service/VisualizationUtilTests.java b/pic-sure-resources/pic-sure-visualization-resource/src/test/java/edu/harvard/hms/dbmi/avillach/resource/visualization/service/VisualizationUtilTests.java index 8a2798d2..3820e181 100644 --- a/pic-sure-resources/pic-sure-visualization-resource/src/test/java/edu/harvard/hms/dbmi/avillach/resource/visualization/service/VisualizationUtilTests.java +++ b/pic-sure-resources/pic-sure-visualization-resource/src/test/java/edu/harvard/hms/dbmi/avillach/resource/visualization/service/VisualizationUtilTests.java @@ -11,25 +11,81 @@ public class VisualizationUtilTests { - @Test - @DisplayName("Test limitKeySize") - public void testLimitKeySizeUniqueness() { - Map axisMap = new HashMap<>(Map.of( - "Disease-Specific (Asthma, Allergy and Inflammation, PUB)", 1, - "Disease-Specific (Asthma, Allergy and Inflammation, PUB, NPU)", 1, - "Disease-Specific (Asthma, Allergy and Inflammation, NPU)", 1, - "Disease-Specific (Asthma, Allergy and Inflammation)", 1 - )); - - Map actual = VisualizationUtil.limitKeySize(axisMap); - - Map expected = Map.of( - "Disease-Specific (Asthma, Allergy an..., PUB)", 1, - "Disease-Specific (Asthma, Allergy an...ation)", 1, - "Disease-Specific (Asthma, Allergy an..., NPU)", 1, - "Disease-Specific (Asthma, Allergy a...B, NPU)", 1 - ); - assertEquals(expected, actual); - } + @Test + @DisplayName("Test limitKeySize") + public void testLimitKeySizeUniqueness() { + Map axisMap = new HashMap<>(Map.of( + "Disease-Specific (Asthma, Allergy and Inflammation, PUB)", 1, + "Disease-Specific (Asthma, Allergy and Inflammation, PUB, NPU)", 1, + "Disease-Specific (Asthma, Allergy and Inflammation, NPU)", 1, + "Disease-Specific (Asthma, Allergy and Inflammation)", 1 + )); + + Map actual = VisualizationUtil.limitKeySize(axisMap); + + Map expected = Map.of( + "Disease-Specific (Asthma, Allergy an..., PUB)", 1, + "Disease-Specific (Asthma, Allergy an...ation)", 1, + "Disease-Specific (Asthma, Allergy an..., NPU)", 1, + "Disease-Specific (Asthma, Allergy a...B, NPU)", 1 + ); + assertEquals(expected, actual); + } + + @Test + @DisplayName("Test Empty Map limitKeySize") + public void testEmptyMapLimitKeySize() { + Map axisMap = new HashMap<>(); + Map actual = VisualizationUtil.limitKeySize(axisMap); + Map expected = new HashMap<>(); + assertEquals(expected, actual); + } + + @Test + @DisplayName("Test null Map limitKeySize") + public void testNullMapLimitKeySize() { + Map axisMap = null; + Map actual = VisualizationUtil.limitKeySize(axisMap); + Map expected = new HashMap<>(); + assertEquals(expected, actual); + } + + @Test + @DisplayName("Test with no long keys limitKeySize") + public void testNoLongKeysLimitKeySize() { + // Test with no long keys + Map axisMap = new HashMap<>(); + for (int i = 0; i < 10; i++) { + axisMap.put("key" + i, 1); + } + Map actual = VisualizationUtil.limitKeySize(axisMap); + Map expected = new HashMap<>(axisMap); + assertEquals(expected, actual); + } + + @Test + @DisplayName("Test with keys of greater than 45 characters and uniqueness is near middle limitKeySize") + public void testKeysOfGreaterLengthAndUniquenessNearMiddleLimitKeySize() { + Map axisMap = new HashMap<>(); + axisMap.put("Hello, this is a long key that is STRING1 greater than 45 characters and is unique", 1); + axisMap.put("Hello, this is a long key that is STRING2 greater than 45 characters and is unique", 1); + axisMap.put("Hello, this is a long key that is STRING3 greater than 45 characters and is unique", 1); + + Map actual = VisualizationUtil.limitKeySize(axisMap); + + // loop through the keys and check if they are less than 45 characters + for (String key : actual.keySet()) { + assertEquals(45, key.length()); + } + + Map expected = Map.of( + "Hello, this is a long key that is ST...unique", 1, + "Hello, this is a long key that is S... unique", 1, + "Hello, this is a long key that is ...s unique", 1 + ); + + assertEquals(expected, actual); + } + } diff --git a/pic-sure-util/src/main/java/edu/harvard/dbmi/avillach/util/VisualizationUtil.java b/pic-sure-util/src/main/java/edu/harvard/dbmi/avillach/util/VisualizationUtil.java index 53a8e23d..38dd8940 100644 --- a/pic-sure-util/src/main/java/edu/harvard/dbmi/avillach/util/VisualizationUtil.java +++ b/pic-sure-util/src/main/java/edu/harvard/dbmi/avillach/util/VisualizationUtil.java @@ -71,60 +71,48 @@ public static Map doProcessResults(Map axisMap /** * This method is used to limit the size of the keys in the axisMap to a maximum of 45 characters. If the key is longer * than 45 characters, it will be shortened to 45 characters and the last 3 characters will be replaced with "...". - * If the shortened key is not unique, we will create a unique one + * If the shortened key is not unique, we will create a unique one *

* * @param axisMap - Map of the categories and their counts * @return Map - Map of the categories and their counts with the keys limited to 45 characters */ public static Map limitKeySize(Map axisMap) { + if (axisMap == null) { + return new HashMap<>(); + } + Map newAxisMap = new HashMap<>(); HashSet keys = new HashSet<>(); - axisMap.forEach((key, value) -> { - if (key.length() < MAX_X_LABEL_LINE_LENGTH) { - newAxisMap.put(key, value); - keys.add(key); - } else { - // proposed key - StringBuilder proposedKey; - - // Check if the key exists in the keys set. - boolean hasEqual = false; - // Let first check if the proposed key is a leading substring of another key - for (String innerKey : axisMap.keySet()) {// Check if this key will be equal to any future keys. - if (innerKey.length() >= MAX_X_LABEL_LINE_LENGTH) { - hasEqual = key.substring(0, MAX_X_LABEL_LINE_LENGTH).equals(innerKey.substring(0, MAX_X_LABEL_LINE_LENGTH)); - if (hasEqual) { - break; - } - } - } - - if (hasEqual) { // if we have an equal we want to create a more unique key - int countFromEnd = 6; - String uniqueness; - do { - // get the original key substring max x - 3 (for the dots) - countFromEnd. - proposedKey = new StringBuilder(key.substring(0, MAX_X_LABEL_LINE_LENGTH - 3 - countFromEnd)); - - // Get the last "countFromEnd" characters - uniqueness = key.substring(key.length() - countFromEnd); + String adjustedKey = key.length() < MAX_X_LABEL_LINE_LENGTH ? key : createAdjustedKey(axisMap, keys, key); + newAxisMap.put(adjustedKey, value); + keys.add(adjustedKey); + }); + return newAxisMap; + } - proposedKey.append("...").append(uniqueness); - countFromEnd++; // increase the number of characters from the end of the string we will use - } while (keys.contains(proposedKey.toString())); + private static String createAdjustedKey(Map axisMap, HashSet keys, String key) { + String keyPrefix = key.substring(0, MAX_X_LABEL_LINE_LENGTH); + return isKeyPrefixInAxisMap(axisMap, keyPrefix) ? generateUniqueKey(keys, key) : appendEllipsis(keyPrefix); + } - } else { - proposedKey = new StringBuilder(key.substring(0, MAX_X_LABEL_LINE_LENGTH - 3) + "..."); - } + private static boolean isKeyPrefixInAxisMap(Map axisMap, String keyPrefix) { + return axisMap.keySet().stream().anyMatch(k -> k.startsWith(keyPrefix)); + } - newAxisMap.put(proposedKey.toString(), value); - keys.add(proposedKey.toString()); - } - }); + private static String generateUniqueKey(HashSet keys, String key) { + int countFromEnd = 6; + String proposedKey; + do { + proposedKey = String.format("%s...%s", key.substring(0, MAX_X_LABEL_LINE_LENGTH - 3 - countFromEnd), key.substring(key.length() - countFromEnd)); + countFromEnd++; + } while (keys.contains(proposedKey)); + return proposedKey; + } - return newAxisMap; + private static String appendEllipsis(String keyPrefixAdjusted) { + return String.format("%s...", keyPrefixAdjusted); } } From 6a81fef70aa648256bd98d8db362016f2864f838 Mon Sep 17 00:00:00 2001 From: GeorgeC Date: Mon, 12 Feb 2024 15:42:36 -0500 Subject: [PATCH 10/10] Update null handling in VisualizationUtil's limitKeySize Changed the handling of null input in VisualizationUtil's limitKeySize from returning a new HashMap to throwing an IllegalArgumentException. Also, updated the corresponding test to check for this exception instead of comparing with an empty map. --- .../visualization/service/VisualizationUtilTests.java | 9 ++++++--- .../harvard/dbmi/avillach/util/VisualizationUtil.java | 2 +- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/pic-sure-resources/pic-sure-visualization-resource/src/test/java/edu/harvard/hms/dbmi/avillach/resource/visualization/service/VisualizationUtilTests.java b/pic-sure-resources/pic-sure-visualization-resource/src/test/java/edu/harvard/hms/dbmi/avillach/resource/visualization/service/VisualizationUtilTests.java index 3820e181..78583922 100644 --- a/pic-sure-resources/pic-sure-visualization-resource/src/test/java/edu/harvard/hms/dbmi/avillach/resource/visualization/service/VisualizationUtilTests.java +++ b/pic-sure-resources/pic-sure-visualization-resource/src/test/java/edu/harvard/hms/dbmi/avillach/resource/visualization/service/VisualizationUtilTests.java @@ -45,9 +45,12 @@ public void testEmptyMapLimitKeySize() { @DisplayName("Test null Map limitKeySize") public void testNullMapLimitKeySize() { Map axisMap = null; - Map actual = VisualizationUtil.limitKeySize(axisMap); - Map expected = new HashMap<>(); - assertEquals(expected, actual); + // this should throw a NullPointerException + try { + VisualizationUtil.limitKeySize(axisMap); + } catch (IllegalArgumentException e) { + assertEquals("axisMap cannot be null", e.getMessage()); + } } @Test diff --git a/pic-sure-util/src/main/java/edu/harvard/dbmi/avillach/util/VisualizationUtil.java b/pic-sure-util/src/main/java/edu/harvard/dbmi/avillach/util/VisualizationUtil.java index 38dd8940..ed32a921 100644 --- a/pic-sure-util/src/main/java/edu/harvard/dbmi/avillach/util/VisualizationUtil.java +++ b/pic-sure-util/src/main/java/edu/harvard/dbmi/avillach/util/VisualizationUtil.java @@ -79,7 +79,7 @@ public static Map doProcessResults(Map axisMap */ public static Map limitKeySize(Map axisMap) { if (axisMap == null) { - return new HashMap<>(); + throw new IllegalArgumentException("axisMap cannot be null"); } Map newAxisMap = new HashMap<>();