From aa0c7aa2418528b3307827e82f90a14e348ce822 Mon Sep 17 00:00:00 2001 From: Pierre Pouchin Date: Thu, 4 Apr 2024 14:39:48 +0200 Subject: [PATCH] Refactor code --- .../igred/ij/plugin/OMEROMacroExtension.java | 50 ++++++++++--------- .../igred/ij/plugin/OMEROExtensionTest.java | 15 +++--- 2 files changed, 35 insertions(+), 30 deletions(-) diff --git a/src/main/java/fr/igred/ij/plugin/OMEROMacroExtension.java b/src/main/java/fr/igred/ij/plugin/OMEROMacroExtension.java index 43bb7a5..fa4f563 100644 --- a/src/main/java/fr/igred/ij/plugin/OMEROMacroExtension.java +++ b/src/main/java/fr/igred/ij/plugin/OMEROMacroExtension.java @@ -20,7 +20,6 @@ import fr.igred.omero.GenericObjectWrapper; import fr.igred.omero.annotations.TableWrapper; import fr.igred.omero.annotations.TagAnnotationWrapper; -import fr.igred.omero.annotations.MapAnnotationWrapper; import fr.igred.omero.exception.AccessException; import fr.igred.omero.exception.OMEROServerError; import fr.igred.omero.exception.ServiceException; @@ -1201,38 +1200,38 @@ public int saveROIs(ImagePlus imp, long id, String property) { * @return The concatenated string of all key-value pairs for the specified repository object. */ public String getKeyValuePairs(String type, long id, String separator) { - Map keyValuePairs = null; + Map keyValuePairs = new TreeMap<>(); - if (separator == null) - separator = "\t"; + String sep = separator == null ? "\t" : separator; - GenericObjectWrapper object = getObject(type, id); - try{ - keyValuePairs = ((GenericRepositoryObjectWrapper) object).getKeyValuePairs(client); + GenericRepositoryObjectWrapper object = getRepositoryObject(type, id); + try { + if (object != null) { + keyValuePairs = new TreeMap<>(object.getKeyValuePairs(client)); + } } catch (ServiceException | AccessException | ExecutionException e) { IJ.error("Could not retrieve object: " + e.getMessage()); } - // Convert to a TreeMap for predictable alphabetical order - keyValuePairs = new TreeMap(keyValuePairs); + int size = 10 * keyValuePairs.size(); - StringBuilder concatenatedString = new StringBuilder(); + StringBuilder concatenation = new StringBuilder(size); for (Map.Entry entry : keyValuePairs.entrySet()) { - concatenatedString.append(entry.getKey()) - .append(separator) - .append(entry.getValue()) - .append(separator); + concatenation.append(entry.getKey()) + .append(sep) + .append(entry.getValue()) + .append(sep); } - if (concatenatedString.length() > 0) { - concatenatedString.setLength(concatenatedString.length() - separator.length()); + if (concatenation.length() > 0) { + concatenation.setLength(concatenation.length() - sep.length()); } - return concatenatedString.toString(); + return concatenation.toString(); } /** - * Retrieves the Value associated to the given Key of a Map annotation. If no - * defaultValue is provided, generates an error. + * Retrieves the Value associated to the given Key of a Map annotation. + *

If no defaultValue is provided, generates an error. * * @param type The object type. * @param id The object ID. @@ -1244,14 +1243,17 @@ public String getKeyValuePairs(String type, long id, String separator) { public String getValue(String type, long id, String key, String defaultValue) { String result = null; - GenericObjectWrapper object = getObject(type, id); - try{ - result = ((GenericRepositoryObjectWrapper) object).getValue(client, key); + GenericRepositoryObjectWrapper object = getRepositoryObject(type, id); + try { + if (object != null) { + result = object.getValue(client, key); + } } catch (NoSuchElementException e) { - if (defaultValue != null) + if (defaultValue != null) { result = defaultValue; - else + } else { IJ.error("Could not retrieve value: " + e.getMessage()); + } } catch (ServiceException | AccessException | ExecutionException e) { IJ.error("Could not retrieve value: " + e.getMessage()); } diff --git a/src/test/java/fr/igred/ij/plugin/OMEROExtensionTest.java b/src/test/java/fr/igred/ij/plugin/OMEROExtensionTest.java index ce83b49..7fc8d36 100644 --- a/src/test/java/fr/igred/ij/plugin/OMEROExtensionTest.java +++ b/src/test/java/fr/igred/ij/plugin/OMEROExtensionTest.java @@ -348,19 +348,22 @@ void testSaveAndGetROIs() { @CsvSource(delimiter = ';', value = {"image;1;null;testKey1\ttestValue1\ttestKey2\t20", "image;3;' ';testKey1 testValue1 testKey2 20", "image;2;&&;testKey1&&testValue2&&testKey2&&30", - "image;4;'';''"}, nullValues={"null"}) - void testgetKeyValuePairs(String type, Long id, String separator, String output) { - String result = ext.getKeyValuePairs(type, id, separator); + "image;4;'';''"}, nullValues = {"null"}) + void testGetKeyValuePairs(String type, Double id, String separator, String output) { + Object[] args = {type, id, separator}; + String result = ext.handleExtension("getKeyValuePairs", args); assertEquals(output, result); } + @ParameterizedTest @CsvSource(delimiter = ';', value = {"image;1;testKey1;null;testValue1", "image;3;testKey2;null;20", "image;2;testKey2;null;30", - "image;2;notExist;default;default"}, nullValues={"null"}) - void testgetValue(String type, Long id, String key, String defaultValue, String output) { - String result = ext.getValue(type, id, key, defaultValue); + "image;2;notExist;default;default"}, nullValues = {"null"}) + void testGetValue(String type, Double id, String key, String defaultValue, String output) { + Object[] args = {type, id, key, defaultValue}; + String result = ext.handleExtension("getValue", args); assertEquals(output, result); }