Skip to content

Commit

Permalink
add FeatureMap support (require additional tests)
Browse files Browse the repository at this point in the history
  • Loading branch information
yvrng committed Sep 15, 2017
1 parent 45cfe9e commit d7de3e4
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 13 deletions.
Expand Up @@ -15,6 +15,7 @@
import fr.inria.atlanmod.neoemf.core.Id;
import fr.inria.atlanmod.neoemf.core.PersistenceFactory;
import fr.inria.atlanmod.neoemf.core.PersistentEObject;
import fr.inria.atlanmod.neoemf.core.StringId;
import fr.inria.atlanmod.neoemf.data.bean.ClassBean;
import fr.inria.atlanmod.neoemf.data.bean.ManyFeatureBean;
import fr.inria.atlanmod.neoemf.data.bean.SingleFeatureBean;
Expand Down Expand Up @@ -771,9 +772,14 @@ private void refresh(PersistentEObject object) {
* A converter that transforms the value of {@link EAttribute}s.
*/
@ParametersAreNonnullByDefault
// TODO Add support for FeatureMap#Entry
private class AttributeConverter {

/**
* The delimiter used to separate the feature from its value in a serialized {@link FeatureMap.Entry}.
*/
@Nonnull
private static final String FEATURE_MAP_DELIMITER = "#";

/**
* Converts an instance of the {@code attribute} to a string literal representation.
*
Expand All @@ -782,6 +788,7 @@ private class AttributeConverter {
*
* @return the string literal representation of the value
*
* @see #revert(EAttribute, String)
* @see EcoreUtil#convertToString(EDataType, Object)
*/
public String convert(EAttribute attribute, @Nullable Object value) {
Expand All @@ -798,8 +805,38 @@ public String convert(EAttribute attribute, @Nullable Object value) {
return EcoreUtil.convertToString(dataType, value);
}

private String convertEntry(EAttribute attribute, FeatureMap.Entry entry) {
throw new UnsupportedOperationException("FeatureMap.Entry are not supported yet");
/**
* Converts an instance of {@link FeatureMap.Entry} to a string literal representation.
* <p>
* The {@code entry} is serialized as {@code featureName#value}, where {@code value} is the string
* representation of the {@link FeatureMap.Entry#getValue() value} for an attribute, or the string
* representation of the {@link Id} for a reference.
*
* @param attribute the attribute to instantiate
* @param entry the entry of the attribute
*
* @return the string literal representation of the entry
*
* @see #revertEntry(EAttribute, String)
* @see EcoreUtil#convertToString(EDataType, Object)
*/
@Nonnull
private String convertEntry(@SuppressWarnings("unused") EAttribute attribute, FeatureMap.Entry entry) {
EStructuralFeature innerFeature = entry.getEStructuralFeature();
String value;

if (EObjects.isAttribute(innerFeature)) {
EAttribute innerAttribute = EObjects.asAttribute(innerFeature);
value = EcoreUtil.convertToString(innerAttribute.getEAttributeType(), entry.getValue());
}
else {
PersistentEObject referencedObject = PersistentEObject.from(entry.getValue());
updateInstanceOf(referencedObject);

value = referencedObject.id().toString();
}

return innerFeature.getName() + FEATURE_MAP_DELIMITER + value;
}

/**
Expand All @@ -810,6 +847,7 @@ private String convertEntry(EAttribute attribute, FeatureMap.Entry entry) {
*
* @return the value of the attribute
*
* @see #convert(EAttribute, Object)
* @see EcoreUtil#createFromString(EDataType, String)
*/
public Object revert(EAttribute attribute, @Nullable String value) {
Expand All @@ -826,8 +864,37 @@ public Object revert(EAttribute attribute, @Nullable String value) {
return EcoreUtil.createFromString(dataType, value);
}

private FeatureMap.Entry revertEntry(EAttribute attribute, String value) {
throw new UnsupportedOperationException("FeatureMap.Entry are not supported yet");
/**
* Creates an instance of {@link FeatureMap.Entry} from a string literal representation.
* <p>
* The {@code entry} must be serialized as {@code featureName#value}.
*
* @param attribute the attribute to instantiate
* @param entry the string literal representation of the entry
*
* @return the entry of the attribute
*
* @see #convertEntry(EAttribute, FeatureMap.Entry)
* @see EcoreUtil#createFromString(EDataType, String)
*/
@Nonnull
private FeatureMap.Entry revertEntry(EAttribute attribute, String entry) {
String[] splitValues = entry.split(FEATURE_MAP_DELIMITER, 2);

EClass metaClass = attribute.getEContainingClass();
EStructuralFeature innerFeature = metaClass.getEStructuralFeature(splitValues[0]);

Object value;

if (EObjects.isAttribute(innerFeature)) {
EAttribute innerAttribute = EObjects.asAttribute(innerFeature);
value = EcoreUtil.createFromString(innerAttribute.getEAttributeType(), splitValues[1]);
}
else {
value = resolve(StringId.of(splitValues[1]));
}

return FeatureMapUtil.createEntry(innerFeature, value);
}
}
}
Expand Up @@ -5,7 +5,6 @@
import fr.inria.atlanmod.neoemf.tests.sample.TargetObject;

import org.eclipse.emf.ecore.util.FeatureMap;
import org.junit.Ignore;
import org.junit.Test;

import java.util.List;
Expand All @@ -15,7 +14,7 @@
/**
* A test-case that checks the support of {@link FeatureMap}.
*/
@Ignore("Not implemented yet")
// TODO Add tests on FeatureMap.size(), ...indexOf(), ...contains(), ...
public class FeatureMapTest extends AbstractBackendTest {

/**
Expand Down Expand Up @@ -112,10 +111,10 @@ public void testSetAttributes() {
assertThat(featureMapAttributes.getValue(1)).isEqualTo(value1);
assertThat(featureMapAttributes.getValue(2)).isEqualTo(value2);

attributes2.set(0, value3);
attributes2.set(0, value3); // Replace 1 by 3

assertThat(featureMapAttributes.getValue(0)).isEqualTo(value3);
assertThat(featureMapAttributes.getValue(1)).isEqualTo(value1);
assertThat(featureMapAttributes.getValue(0)).isEqualTo(value0);
assertThat(featureMapAttributes.getValue(1)).isEqualTo(value3);
assertThat(featureMapAttributes.getValue(2)).isEqualTo(value2);
}

Expand Down Expand Up @@ -218,10 +217,10 @@ public void testSetReferences() {
assertThat(featureMapReferences.getValue(1)).isEqualTo(target1);
assertThat(featureMapReferences.getValue(2)).isEqualTo(target2);

references2.set(0, target3);
references2.set(0, target3); // Replace 1 by 3

assertThat(featureMapReferences.getValue(0)).isEqualTo(target3);
assertThat(featureMapReferences.getValue(1)).isEqualTo(target1);
assertThat(featureMapReferences.getValue(0)).isEqualTo(target0);
assertThat(featureMapReferences.getValue(1)).isEqualTo(target3);
assertThat(featureMapReferences.getValue(2)).isEqualTo(target2);
}

Expand Down

0 comments on commit d7de3e4

Please sign in to comment.