Skip to content

Commit

Permalink
Avoid nested iteration when serializing 'many' ref
Browse files Browse the repository at this point in the history
  • Loading branch information
cbrun committed Mar 17, 2023
1 parent 2238697 commit 052666c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -1336,61 +1335,21 @@ private JsonElement serializeMultipleNonContainmentEReference(EObject eObject, E
JsonArray jsonArray = new JsonArray();
Object referenceValue = this.helper.getValue(eObject, eReference);
for (EObject value : (InternalEList<? extends EObject>) referenceValue) {
switch (this.docKindMany(eObject, eReference)) {
case SAME_DOC:
if (!value.eIsProxy() && value.eResource() == this.helper.getResource()) {
String id = this.helper.getIDREF(value);
id = this.removeFragmentSeparator(id);
if (!id.isEmpty()) {
jsonArray.add(new JsonPrimitive(id));
}
break;
case CROSS_DOC:
if (value != null) {
jsonArray.add(new JsonPrimitive(this.saveHref(value, null)));
}
break;
default:
} else {
jsonArray.add(new JsonPrimitive(this.saveHref(value, null)));
}
}

jsonElement = jsonArray;
return jsonElement;
}

/**
* Return where given ERference of the given EObject are. If at least one reference is in an other resource, all
* references are treated as references to other resources.
*
* @param eObject
* the given EObject
* @param eReference
* the given ERference
* @return where given ERference of the given EObject are
*/
private int docKindMany(EObject eObject, EReference eReference) {
int referenceType = GsonEObjectSerializer.SAME_DOC;
@SuppressWarnings("unchecked")
InternalEList<? extends InternalEObject> internalEList = (InternalEList<? extends InternalEObject>) this.helper.getValue(eObject, eReference);

if (internalEList.isEmpty()) {
referenceType = GsonEObjectSerializer.SKIP;
}

Iterator<? extends InternalEObject> it = internalEList.iterator();
while (referenceType != GsonEObjectSerializer.SKIP && referenceType != GsonEObjectSerializer.CROSS_DOC && it.hasNext()) {
InternalEObject internalEObject = it.next();
if (internalEObject.eIsProxy()) {
referenceType = GsonEObjectSerializer.CROSS_DOC;
} else {
Resource resource = internalEObject.eResource();
if (resource != this.helper.getResource() && resource != null) {
referenceType = GsonEObjectSerializer.CROSS_DOC;
}
}
}
return referenceType;
}

/**
* Returns a JsonElement representing a single non containment reference value.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -685,18 +685,21 @@ public String getHREF(EObject obj) {
InternalEObject o = (InternalEObject) obj;

URI objectURI = o.eProxyURI();

if (objectURI == null) {
Resource otherResource = obj.eResource();
if (otherResource == null) {
if (this.resource != null) {
objectURI = this.getHREF(otherResource, obj);
} else {
if (this.resource == null) {
this.handleDanglingHREF(obj);
return null;
}
// also check for getID ?
objectURI = this.getHREF(this.resource, obj);

} else {
objectURI = this.getHREF(otherResource, obj);
}

}

objectURI = this.deresolve(objectURI);
Expand Down

0 comments on commit 052666c

Please sign in to comment.