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

[19] Avoid costly test during reference serialisation #44

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2020, 2023 Obeo.
* Copyright (c) 2020, 2024 Obeo.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
Expand Down Expand Up @@ -28,7 +28,6 @@
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -79,22 +78,6 @@
* @author <a href="mailto:[email protected]">Stephane Begaudeau</a>
*/
public class GsonEObjectSerializer implements JsonSerializer<List<EObject>> {

/**
* In case of serialize non containment references, where the reference is not found.
*/
private static final int SKIP = 0;

/**
* In case of serialize non containment references, where the reference is in the same document.
*/
private static final int SAME_DOC = 1;

/**
* In case of serialize non containment references, where the reference is in an other document.
*/
private static final int CROSS_DOC = 2;

/**
* Hexadecimal digits.
*/
Expand Down Expand Up @@ -1415,63 +1398,12 @@ private JsonElement serializeMultipleContainmentEReference(EObject eObject, ERef
*/
@SuppressWarnings("unchecked")
private JsonElement serializeMultipleNonContainmentEReference(EObject eObject, EReference eReference) {
JsonElement jsonElement = null;
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:
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, eReference)));
}
break;
default:
}
jsonArray.add(this.serializeReferenceValue(eObject, eReference, value));
}

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;
return jsonArray;
}

/**
Expand All @@ -1484,27 +1416,23 @@ private int docKindMany(EObject eObject, EReference eReference) {
* @return A JsonObject with the property IGsonConstants.REF and as the value, the URI of the object.
*/
private JsonElement serializeSingleNonContainmentEReference(EObject eObject, EReference eReference) {
JsonElement jsonElement = null;
EObject referenceValue = (EObject) this.helper.getValue(eObject, eReference);
return this.serializeReferenceValue(eObject, eReference, referenceValue);
}

private JsonElement serializeReferenceValue(EObject eObject, EReference eReference, EObject referenceValue) {
String value = ""; //$NON-NLS-1$
Object referenceValue = this.helper.getValue(eObject, eReference);
switch (this.docKindSingle(eObject, eReference)) {
case SAME_DOC:
// same document
value = this.helper.getIDREF((EObject) referenceValue);
value = this.removeFragmentSeparator(value);
break;
case CROSS_DOC:
// cross document
EObject object = (EObject) this.helper.getValue(eObject, eReference);
if (object != null) {
value = this.saveHref(object, eReference);
}
break;
default:
if (this.isFromDifferentResource(referenceValue)) {
value = this.saveHref(referenceValue, eReference);
} else {
value = this.removeFragmentSeparator(this.helper.getIDREF(referenceValue));
}
jsonElement = new JsonPrimitive(value);
return new JsonPrimitive(value);
}

return jsonElement;
private boolean isFromDifferentResource(EObject referenceValue) {
InternalEObject value = (InternalEObject) referenceValue;
return value == null || value.eIsProxy() || value.eResource() != this.helper.getResource();
}

/**
Expand Down Expand Up @@ -1563,31 +1491,6 @@ private boolean shouldSaveType(EClass eClass) {
return shouldSaveType.booleanValue();
}

/**
* Return where the given ERference of the given EObject is.
*
* @param eObject
* the given EObject
* @param eReference
* the given EReference
* @return where the given ERference of the given EObject is
*/
private int docKindSingle(EObject eObject, EReference eReference) {
InternalEObject value = (InternalEObject) this.helper.getValue(eObject, eReference);
int typeOfDocument = GsonEObjectSerializer.SKIP;
if (value != null && value.eIsProxy()) {
typeOfDocument = GsonEObjectSerializer.CROSS_DOC;
} else if (value != null) {
Resource res = value.eResource();
if (res == this.helper.getResource() || res == null) {
typeOfDocument = GsonEObjectSerializer.SAME_DOC;
} else {
typeOfDocument = GsonEObjectSerializer.CROSS_DOC;
}
}
return typeOfDocument;
}

/**
* Return the URI fragment if the pointed resource URI schema is the same of the Resource URI schema. Return the all
* URI otherwise.
Expand Down
Loading