Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revise map annotations #88

Merged
merged 2 commits into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

<groupId>fr.igred</groupId>
<artifactId>simple-omero-client</artifactId>
<version>5.18.0</version>
<version>5.19.0-SNAPSHOT</version>
<packaging>jar</packaging>

<name>Simple OMERO Client</name>
Expand Down
6 changes: 1 addition & 5 deletions src/main/java/fr/igred/omero/AnnotatableWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import fr.igred.omero.exception.OMEROServerError;
import fr.igred.omero.exception.ServiceException;
import fr.igred.omero.repository.GenericRepositoryObjectWrapper.ReplacePolicy;
import omero.constants.metadata.NSCLIENTMAPANNOTATION;
import omero.gateway.facility.TablesFacility;
import omero.gateway.model.AnnotationData;
import omero.gateway.model.DataObject;
Expand All @@ -39,7 +38,6 @@
import omero.gateway.model.TableData;
import omero.gateway.model.TagAnnotationData;
import omero.model.IObject;
import omero.model.NamedValue;
import omero.model.TagAnnotationI;
import omero.sys.ParametersI;

Expand Down Expand Up @@ -366,9 +364,7 @@ public void addPairKeyValue(Client client, String key, String value)
*/
public void addKeyValuePair(Client client, String key, String value)
throws ServiceException, AccessException, ExecutionException {
List<NamedValue> kv = singletonList(new NamedValue(key, value));
MapAnnotationWrapper pkv = new MapAnnotationWrapper(kv);
pkv.setNameSpace(NSCLIENTMAPANNOTATION.value);
MapAnnotationWrapper pkv = new MapAnnotationWrapper(key, value);
link(client, pkv);
}

Expand Down
129 changes: 128 additions & 1 deletion src/main/java/fr/igred/omero/annotations/MapAnnotationWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,16 @@
import omero.gateway.model.MapAnnotationData;
import omero.model.NamedValue;

import java.util.AbstractMap;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import static java.util.stream.Collectors.groupingBy;
import static java.util.stream.Collectors.mapping;
import static java.util.stream.Collectors.toList;


/**
Expand All @@ -30,6 +39,11 @@
*/
public class MapAnnotationWrapper extends GenericAnnotationWrapper<MapAnnotationData> {

/**
* The name space used to identify MapAnnotations created be the user
*/
public static final String NS_USER_CREATED = MapAnnotationData.NS_CLIENT_CREATED;


/**
* Constructor of the MapAnnotationWrapper class.
Expand All @@ -45,13 +59,44 @@
* Constructor of the MapAnnotationWrapper class. Sets the content of the MapAnnotationData
*
* @param result List of NamedValue(Key-Value pair).
*
* @deprecated This constructor will be removed in a future version.
* <p>Use {@link #MapAnnotationWrapper(Collection)} instead.
*/
@Deprecated
public MapAnnotationWrapper(List<NamedValue> result) {
super(new MapAnnotationData());
data.setContent(result);
}


/**
* Constructor of the MapAnnotationWrapper class. Sets the content of the map annotation.
*
* @param pairs Collection of Key-Value pairs.
*/
public MapAnnotationWrapper(Collection<? extends Entry<String, String>> pairs) {
super(new MapAnnotationData());
List<NamedValue> nv = pairs.stream()
.map(e -> new NamedValue(e.getKey(), e.getValue()))
.collect(toList());
data.setContent(nv);
}


/**
* Constructor of the MapAnnotationWrapper class. Sets the content to a single key-value pair.
* <p>Initializes the namespace to {@link #NS_USER_CREATED}.</p>
*
* @param key The key.
* @param value The value.
*/
public MapAnnotationWrapper(String key, String value) {
this(Collections.singletonList(new AbstractMap.SimpleEntry<>(key, value)));
data.setNameSpace(NS_USER_CREATED);
}


/**
* Constructor of the MapAnnotationWrapper class.
*/
Expand All @@ -60,11 +105,38 @@
}


/**
* Converts a {@link NamedValue} to a {@link Entry}.
*
* @param namedValue The {@link NamedValue}.
*
* @return See above.
*/
private static Entry<String, String> toMapEntry(NamedValue namedValue) {
return new AbstractMap.SimpleEntry<>(namedValue.name, namedValue.value);
}


/**
* Converts a {@link Entry} to a {@link NamedValue}.
*
* @param entry The {@link Entry}.
*
* @return See above.
*/
private static NamedValue toNamedValue(Entry<String, String> entry) {
return new NamedValue(entry.getKey(), entry.getValue());
}


/**
* Gets the List of NamedValue contained in the map annotation.
*
* @return MapAnnotationData content.
*
* @deprecated This method will be removed in a future version.
*/
@Deprecated
@SuppressWarnings("unchecked")
public List<NamedValue> getContent() {
return (List<NamedValue>) data.getContent();
Expand All @@ -75,13 +147,68 @@
* Sets the content of the map annotation.
*
* @param result List of NamedValue(Key-Value pair).
*
* @deprecated This method will be replaced by {@link #setContent(Collection)} in a future version.
*/
@Deprecated
public void setContent(List<NamedValue> result) {
data = new MapAnnotationData();
data.setContent(result);
}


/**
* Sets the content of the map annotation.
*
* @param pairs Collection of Key-Value pairs.
*/
public void setContent(Collection<? extends Entry<String, String>> pairs) {
List<NamedValue> nv = pairs.stream()
.map(MapAnnotationWrapper::toNamedValue)
.collect(toList());
data.setContent(nv);
}


/**
* Gets the List of Key-Value pairs contained in the map annotation.
*
* @return MapAnnotationData content.
*
* @deprecated This method will be renamed to {@link #getContent()} in a future version.
*/
@Deprecated
@SuppressWarnings("unchecked")
public List<Entry<String, String>> getContentAsEntryList() {
return ((Collection<NamedValue>) data.getContent()).stream()
.map(MapAnnotationWrapper::toMapEntry)
.collect(toList());
}


/**
* Gets the List of Key-Value pairs contained in the map annotation as a map.
* <p>As keys may not be unique, the map contains values as a list.</p>
*
* @return See above.
*/
public Map<String, List<String>> getContentAsMap() {
return this.getContentAsEntryList()
Dismissed Show dismissed Hide dismissed
.stream()
.collect(groupingBy(Entry::getKey,
mapping(Entry::getValue, toList())));
}


/**
* Gets the content of the map annotation as a string.
*
* @return See above.
*/
public String getContentAsString() {
return data.getContentAsString();
}


/**
* @return the {@link MapAnnotationData} contained.
*
Expand Down
50 changes: 50 additions & 0 deletions src/test/java/fr/igred/omero/annotations/MapAnnotationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,15 @@


import fr.igred.omero.UserTest;
import omero.model.NamedValue;
import org.junit.jupiter.api.Test;

import java.util.AbstractMap.SimpleEntry;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import static org.junit.jupiter.api.Assertions.assertEquals;

Expand Down Expand Up @@ -61,4 +67,48 @@
assertEquals(1, maps.size());
}


@Test
void testGetContentAsMap() throws Exception {
MapAnnotationWrapper map = client.getMapAnnotation(4L);
Map<String, List<String>> content = map.getContentAsMap();
assertEquals("testValue1", content.get("testKey1").get(0));
assertEquals("20", content.get("testKey2").get(0));
}


@Test
void testGetContentAsString() throws Exception {
MapAnnotationWrapper map = client.getMapAnnotation(4L);

String expected = "testKey1=testValue1;testKey2=20";
assertEquals(expected, map.getContentAsString());
}


@Test
void testSetContent() throws Exception {
MapAnnotationWrapper map = client.getMapAnnotation(4L);
List<NamedValue> content = map.getContent();
Dismissed Show dismissed Hide dismissed

Collection<Entry<String, String>> pairs = new ArrayList<>(2);
pairs.add(new SimpleEntry<>("testKey2", "testValue2"));
pairs.add(new SimpleEntry<>("testKey3", "testValue3"));

map.setContent(pairs);
map.saveAndUpdate(client);

int size = map.getContent().size();
Dismissed Show dismissed Hide dismissed
String expected = "testKey2=testValue2;testKey3=testValue3";
String actual = map.getContentAsString();

map.setContent(content);
Dismissed Show dismissed Hide dismissed
map.saveAndUpdate(client);

assertEquals(2, size);
assertEquals(expected, actual);
assertEquals("testKey1", map.getContent().get(0).name);
Dismissed Show dismissed Hide dismissed
assertEquals("testValue1", map.getContent().get(0).value);
Dismissed Show dismissed Hide dismissed
}

}
Loading