eturn true;
}
}
return false;
}
如果一个 EStructuralFeature 适用于两个对象,则用 eGet() 方法获取两 个对象在该字段的值并进行比较。通过 EMF 反射 API 取值和根据得到值的不同 类型进行比较的细节同第 3 章。ModelComparePort 类的列表类型变量 _comparedNodes 是用作记录已经比较过节点的备忘录,在进行比较之前需要查 阅备忘录避免重复比较,在比较之后需要更新备忘录。比较器发现两个对象的不 同便生成一个 CompareInfo 对象,最终得到一个由 CompareInfo 对象组成的差 异项列表,关键代码如清单 7 所示:
清单 7. 比较两个 EObject 对象
public void compareT(EObject remote, EObject local, int compareType,
List<CompareInfo> compareLog)
{
if (remote == null && local != null)
{
compareLog.add(new CompareInfo (CompareInfo.REMOTE_DELETE,
compareType, remote, local));
return;
}
else if (remote != null && local == null)
{
compareLog.add(new CompareInfo(CompareInfo.REMOTE_ADD, compareType,
remote, local));
return;
}
if (((MObject) local).getEditStatus().equals(
EditStatus.DELETED_LITERAL))
{
compareLog.add(new CompareInfo (CompareInfo.REMOTE_MODIFIED,
compareType, remote, local));
return;
}
if (!_comparedNodes.contains(remote))
_comparedNodes.add(remote);
else
return;
// compare all
for (int i = 0; i < _featureList.size(); i++)
{
EStructuralFeature feature = _featureList.get (i);
if (isSuitable(remote, feature))
{
if (remote.eGet(feature) instanceof Elist
|| local.eGet(feature) instanceof Elist)
{ // if the value is a list
compare((EList) remote.eGet(feature), (EList) local
.eGet(feature), compareType, compareLog);
}
else if (remote.eGet(feature) instanceof Eobject
|| local.eGet(feature) instanceof Eobject)
{ // if the value is an Eobject
compareT((EObject) remote.eGet(feature), (EObject) local
.eGet(feature), compareType, compareLog);
}
else
{ // if the value is a simple object
Object remoteValue = remote.eGet (feature);
Object localValue = local.eGet(feature);
if (remoteValue != null && localValue != null)
{
if (!remoteValue.equals(localValue))
{
// the two remote value and local value are
|