Skip to content

Commit

Permalink
Refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
ppouchin committed Apr 4, 2024
1 parent 2b1e922 commit aa0c7aa
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 30 deletions.
50 changes: 26 additions & 24 deletions src/main/java/fr/igred/ij/plugin/OMEROMacroExtension.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String, String> keyValuePairs = null;
Map<String, String> 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<String, String> 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.
* <p> If no defaultValue is provided, generates an error.
*
* @param type The object type.
* @param id The object ID.
Expand All @@ -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());
}
Expand Down
15 changes: 9 additions & 6 deletions src/test/java/fr/igred/ij/plugin/OMEROExtensionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down

0 comments on commit aa0c7aa

Please sign in to comment.