package jetbrains.mps.util;
/*Generated by MPS */
import org.jetbrains.mps.openapi.model.SNode;
import java.util.Map;
import jetbrains.mps.internal.collections.runtime.SetSequence;
import jetbrains.mps.internal.collections.runtime.MapSequence;
import org.jetbrains.mps.openapi.language.SReferenceLink;
import org.jetbrains.mps.openapi.model.SReference;
import java.util.HashMap;
import jetbrains.mps.internal.collections.runtime.ListSequence;
import jetbrains.mps.internal.collections.runtime.IVisitor;
import jetbrains.mps.smodel.DynamicReference;
import java.util.Set;
import org.jetbrains.mps.openapi.language.SContainmentLink;
import java.util.HashSet;
import java.util.List;
public class SNodeCompare {
private SNodeCompare() {
}
private static boolean nodePropertiesEquals(SNode a, SNode b) {
Map<String, String> aMap = (Map<String, String>) SNodeOperations.getProperties(a);
Map<String, String> bMap = (Map<String, String>) SNodeOperations.getProperties(b);
for (String p : SetSequence.fromSet(MapSequence.fromMap(aMap).keySet()).union(SetSequence.fromSet(MapSequence.fromMap(bMap).keySet()))) {
if (neq_acety0_a0a0c0b(MapSequence.fromMap(aMap).get(p), MapSequence.fromMap(bMap).get(p))) {
return false;
}
}
return true;
}
private static boolean nodeReferencesEquals(SNode a, SNode b) {
final Map<SReferenceLink, SReference> aMap = MapSequence.fromMap(new HashMap<SReferenceLink, SReference>());
final Map<SReferenceLink, SReference> bMap = MapSequence.fromMap(new HashMap<SReferenceLink, SReference>());
ListSequence.fromList(jetbrains.mps.lang.smodel.generator.smodelAdapter.SNodeOperations.getReferences((SNode) a)).visitAll(new IVisitor<SReference>() {
public void visit(SReference ref) {
MapSequence.fromMap(aMap).put(ref.getLink(), ref);
}
});
ListSequence.fromList(jetbrains.mps.lang.smodel.generator.smodelAdapter.SNodeOperations.getReferences((SNode) b)).visitAll(new IVisitor<SReference>() {
public void visit(SReference ref) {
MapSequence.fromMap(bMap).put(ref.getLink(), ref);
}
});
for (SReferenceLink r : SetSequence.fromSet(MapSequence.fromMap(aMap).keySet()).union(SetSequence.fromSet(MapSequence.fromMap(bMap).keySet()))) {
SReference aRef = a.getReference(r);
SReference bRef = b.getReference(r);
if (aRef == null || bRef == null) {
// cannot be both null
return false;
}
if (neq_acety0_a0e0f0c(aRef.getTargetSModelReference(), bRef.getTargetSModelReference())) {
return false;
}
if (neq_acety0_a0f0f0c(((jetbrains.mps.smodel.SReference) aRef).getResolveInfo(), ((jetbrains.mps.smodel.SReference) bRef).getResolveInfo())) {
return false;
}
if (aRef instanceof DynamicReference || bRef instanceof DynamicReference) {
if (aRef instanceof DynamicReference && bRef instanceof DynamicReference) {
// both dynamic, ok
} else {
// dynamic and static
return false;
}
} else {
// both static
if (neq_acety0_a0b0a6a5a2(aRef.getTargetNodeId(), bRef.getTargetNodeId())) {
return false;
}
}
}
return true;
}
private static boolean nodeChildrenEquals(SNode a, SNode b) {
Set<SContainmentLink> roles = SetSequence.fromSetWithValues(new HashSet<SContainmentLink>(), SNodeOperations.getChildRoles(a, true));
SetSequence.fromSet(roles).addSequence(SetSequence.fromSet(SNodeOperations.getChildRoles(b, true)));
for (SContainmentLink r : SetSequence.fromSet(roles)) {
List<? extends SNode> aChildren = IterableUtil.asList(a.getChildren(r));
List<? extends SNode> bChildren = IterableUtil.asList(b.getChildren(r));
if (aChildren.size() != bChildren.size()) {
return false;
}
for (int i = 0; i < aChildren.size(); i++) {
if (!(nodeEquals(aChildren.get(i), bChildren.get(i)))) {
return false;
}
}
}
return true;
}
public static boolean nodeEquals(SNode a, SNode b) {
return eq_acety0_a0a0a0a0e(a.getConcept(), b.getConcept()) && eq_acety0_a0a0a0a0e_0(a.getNodeId(), b.getNodeId()) && nodePropertiesEquals(a, b) && nodeReferencesEquals(a, b) && nodeChildrenEquals(a, b);
}
private static boolean neq_acety0_a0a0c0b(Object a, Object b) {
return !(((a != null ? a.equals(b) : a == b)));
}
private static boolean neq_acety0_a0e0f0c(Object a, Object b) {
return !(((a != null ? a.equals(b) : a == b)));
}
private static boolean neq_acety0_a0f0f0c(Object a, Object b) {
return !(((a != null ? a.equals(b) : a == b)));
}
private static boolean neq_acety0_a0b0a6a5a2(Object a, Object b) {
return !(((a != null ? a.equals(b) : a == b)));
}
private static boolean eq_acety0_a0a0a0a0e(Object a, Object b) {
return (a != null ? a.equals(b) : a == b);
}
private static boolean eq_acety0_a0a0a0a0e_0(Object a, Object b) {
return (a != null ? a.equals(b) : a == b);
}
}