基于反射机制的 EMF 模型比较
2010-07-06 00:00:00 来源:WEB开发网比较组合数据类型
对于组合数据类型,需要把其分解为简单数据类型比较。组合数据类型的字段有可能仍是组合数据类型,所以需要进行递归分解。另外由于对象之间可能有循环的关联关系,所以需要把已经比较过的对象放进备忘录,在每比较一个对象之前先检查备忘录中是否已经记录了该对象,以避免程序陷入无限循环之中。
列表 (EList) 的比较算法可以简单概括为:求两个列表的差集,差集中的每个元素都是一个比较的差异项;把两个列表的交集元素按组合类型或者简单类型算法进行比较。清单 4 给出了比较列表的代码片断。
清单 4. 比较列表 EList
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);
}
private void compare(List remote, List local, int compareType,
List<CompareInfo> compareLog) {
if (local == null || local.isEmpty()) {
if (remote == null || remote.isEmpty())
return;
for (int i = 0; i < remote.size(); i++) {
EObject eo = (EObject) remote.get(i);
compareLog.add(new CompareInfo(CompareInfo.REMOTE_ADD,
compareType, eo, null));
}
return;
}
for (int i = 0; i < remote.size(); i++) {
EObject remoteObject = (EObject) remote.get(i);
EObject localObject = findElementByID(local, remoteObject);
if (localObject != null) {
compareT((EObject) remoteObject, localObject, compareType,compareLog);
} else {
compareLog.add(new CompareInfo(CompareInfo.REMOTE_ADD,
compareType, remoteObject, localObject));
}
}
for (int i = 0; i < local.size(); i++) {
EObject localObject = (EObject) local.get(i);
EObject remoteObject = findElementByID(remote, localObject);
if (remoteObject == null) { //差集 local-remote 元素
compareLog.add(new CompareInfo(CompareInfo.REMOTE_DELETE,
compareType, remoteObject, localObject));
}
}
}
更多精彩
赞助商链接