nodetypedefdiff.java
来自「jsr170接口的java实现。是个apache的开源项目。」· Java 代码 · 共 615 行 · 第 1/2 页
JAVA
615 行
NodeDefId id = (NodeDefId) iter.next(); NodeDef def1 = (NodeDef) defs1.get(id); NodeDef def2 = (NodeDef) defs2.get(id); ChildNodeDefDiff diff = new ChildNodeDefDiff(def1, def2); if (diff.getType() > maxType) { maxType = diff.getType(); } childNodeDefDiffs.add(diff); defs2.remove(id); } /** * defs2 by now only contains entries found in defs2 only; * walk through defs2 and process all remaining entries */ iter = defs2.keySet().iterator(); while (iter.hasNext()) { NodeDefId id = (NodeDefId) iter.next(); NodeDef def = (NodeDef) defs2.get(id); ChildNodeDefDiff diff = new ChildNodeDefDiff(null, def); if (diff.getType() > maxType) { maxType = diff.getType(); } childNodeDefDiffs.add(diff); } return maxType; } public String toString() { String result = getClass().getName() + "[\n\tnodeTypeName=" + oldDef.getName(); result += ",\n\tmixinFlagDiff=" + modificationTypeToString(mixinFlagDiff()); result += ",\n\tsupertypesDiff=" + modificationTypeToString(supertypesDiff()); result += ",\n\tpropertyDifferences=[\n"; result += toString(propDefDiffs); result += "\t]"; result += ",\n\tchildNodeDifferences=[\n"; result += toString(childNodeDefDiffs); result += "\t]\n"; result += "]\n"; return result; } private String toString(List childItemDefDiffs) { String result = ""; for (Iterator iter = childItemDefDiffs.iterator(); iter.hasNext();) { ChildItemDefDiff propDefDiff = (ChildItemDefDiff) iter.next(); result += "\t\t" + propDefDiff; if (iter.hasNext()) { result += ","; } result += "\n"; } return result; } private String modificationTypeToString(int modifcationType) { String typeString = "unknown"; switch (modifcationType) { case NONE: typeString = "NONE"; break; case TRIVIAL: typeString = "TRIVIAL"; break; case MINOR: typeString = "MINOR"; break; case MAJOR: typeString = "MAJOR"; break; } return typeString; } //--------------------------------------------------------< inner classes > abstract class ChildItemDefDiff { protected final ItemDef oldDef; protected final ItemDef newDef; protected int type; ChildItemDefDiff(ItemDef oldDef, ItemDef newDef) { this.oldDef = oldDef; this.newDef = newDef; init(); } protected void init() { // determine type of modification if (isAdded()) { if (!newDef.isMandatory()) { // adding a non-mandatory child item is a TRIVIAL change type = TRIVIAL; } else { // adding a mandatory child item is a MAJOR change type = MAJOR; } } else if (isRemoved()) { // removing a child item is a MAJOR change type = MAJOR; } else { /** * neither added nor removed => has to be either identical * or modified */ if (oldDef.equals(newDef)) { // identical type = NONE; } else { // modified if (oldDef.isMandatory() != newDef.isMandatory() && newDef.isMandatory()) { // making a child item mandatory is a MAJOR change type = MAJOR; } else { if (!oldDef.definesResidual() && newDef.definesResidual()) { // just making a child item residual is a MINOR change type = MINOR; } else { if (!oldDef.getName().equals(newDef.getName())) { // changing the name of a child item is a MAJOR change type = MAJOR; } else { // all other changes are TRIVIAL type = TRIVIAL; } } } } } } public int getType() { return type; } public boolean isAdded() { return oldDef == null && newDef != null; } public boolean isRemoved() { return oldDef != null && newDef == null; } public boolean isModified() { return oldDef != null && newDef != null && !oldDef.equals(newDef); } public String toString() { String typeString = modificationTypeToString(getType()); String operationString; if (isAdded()) { operationString = "ADDED"; } else if (isModified()) { operationString = "MODIFIED"; } else if (isRemoved()) { operationString = "REMOVED"; } else { operationString = "NONE"; } ItemDef itemDefinition = (oldDef != null) ? oldDef : newDef; return getClass().getName() + "[itemName=" + itemDefinition.getName() + ", type=" + typeString + ", operation=" + operationString + "]"; } } public class PropDefDiff extends ChildItemDefDiff { PropDefDiff(PropDef oldDef, PropDef newDef) { super(oldDef, newDef); } public PropDef getOldDef() { return (PropDef) oldDef; } public PropDef getNewDef() { return (PropDef) newDef; } protected void init() { super.init(); /** * only need to do comparison if base class implementation * detected a non-MAJOR modification (i.e. TRIVIAL or MINOR); * no need to check for additions or removals as this is already * handled in base class implementation. */ if (isModified() && type != NONE && type != MAJOR) { /** * check if valueConstraints were made more restrictive * (constraints are ORed) */ ValueConstraint[] vca1 = getOldDef().getValueConstraints(); HashSet set1 = new HashSet(); for (int i = 0; i < vca1.length; i++) { set1.add(vca1[i].getDefinition()); } ValueConstraint[] vca2 = getNewDef().getValueConstraints(); HashSet set2 = new HashSet(); for (int i = 0; i < vca2.length; i++) { set2.add(vca2[i].getDefinition()); } if (set1.isEmpty() && !set2.isEmpty()) { // added constraint where there was no constraint (MAJOR change) type = MAJOR; } else if (!set2.containsAll(set1) && !set2.isEmpty()) { // removed existing constraint (MAJOR change) type = MAJOR; } // no need to check defaultValues (TRIVIAL change) if (type == TRIVIAL) { int t1 = getOldDef().getRequiredType(); int t2 = getNewDef().getRequiredType(); if (t1 != t2) { if (t2 == PropertyType.UNDEFINED) { // changed getRequiredType to UNDEFINED (MINOR change) type = MINOR; } else { // changed getRequiredType to specific type (MAJOR change) type = MAJOR; } } boolean b1 = getOldDef().isMultiple(); boolean b2 = getNewDef().isMultiple(); if (b1 != b2) { if (b2) { // changed multiple flag to true (MINOR change) type = MINOR; } else { // changed multiple flag to false (MAJOR change) type = MAJOR; } } } } } } public class ChildNodeDefDiff extends ChildItemDefDiff { ChildNodeDefDiff(NodeDef oldDef, NodeDef newDef) { super(oldDef, newDef); } public NodeDef getOldDef() { return (NodeDef) oldDef; } public NodeDef getNewDef() { return (NodeDef) newDef; } protected void init() { super.init(); /** * only need to do comparison if base class implementation * detected a non-MAJOR modification (i.e. TRIVIAL or MINOR); * no need to check for additions or removals as this is already * handled in base class implementation. */ if (isModified() && type != NONE && type != MAJOR) { boolean b1 = getOldDef().allowsSameNameSiblings(); boolean b2 = getNewDef().allowsSameNameSiblings(); if (b1 != b2 && !b2) { // changed sameNameSiblings flag to false (MAJOR change) type = MAJOR; } // no need to check defaultPrimaryType (TRIVIAL change) if (type == TRIVIAL) { List l1 = Arrays.asList(getOldDef().getRequiredPrimaryTypes()); List l2 = Arrays.asList(getNewDef().getRequiredPrimaryTypes()); if (!l1.equals(l2)) { if (l1.containsAll(l2)) { // removed requiredPrimaryType (MINOR change) type = MINOR; } else { // added requiredPrimaryType (MAJOR change) type = MAJOR; } } } } } }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?