package jetbrains.mps.lang.structure.util;
/*Generated by MPS */
import org.jetbrains.annotations.Nullable;
import org.jetbrains.mps.openapi.model.SModel;
import org.jetbrains.mps.openapi.model.SNode;
import jetbrains.mps.baseLanguage.closures.runtime.Wrappers;
import jetbrains.mps.internal.collections.runtime.ListSequence;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SModelOperations;
import jetbrains.mps.smodel.adapter.structure.MetaAdapterFactory;
import jetbrains.mps.internal.collections.runtime.IWhereFilter;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SPropertyOperations;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SLinkOperations;
import jetbrains.mps.smodel.SNodeId;
/**
* Generates unique ids. Ids differ from ids of any other entity in the scope of uniqueness.
* If optional parameter is specified, the first id to try is got from node id.
*/
public class ConceptIdHelper {
public static long generateConceptId(@Nullable SModel m, @Nullable final SNode c) {
final Wrappers._long result = new Wrappers._long(ConceptIdHelper.getDefaultIdFromNode(c));
while (ListSequence.fromList(SModelOperations.roots(m, MetaAdapterFactory.getConcept(0xc72da2b97cce4447L, 0x8389f407dc1158b7L, 0x1103553c5ffL, "jetbrains.mps.lang.structure.structure.AbstractConceptDeclaration"))).any(new IWhereFilter<SNode>() {
public boolean accept(SNode it) {
return it != c && eq_huw3go_a0a0a0a0a0a1a0(SPropertyOperations.getString(it, MetaAdapterFactory.getProperty(0xc72da2b97cce4447L, 0x8389f407dc1158b7L, 0x1103553c5ffL, 0x5d2e6079771f8cc0L, "conceptId")), result.value + "");
}
})) {
result.value = randomLong();
}
return result.value;
}
public static long generatePropertyId(@Nullable SNode c, final SNode p) {
final Wrappers._long result = new Wrappers._long(ConceptIdHelper.getDefaultIdFromNode(p));
while (ListSequence.fromList(SLinkOperations.getChildren(c, MetaAdapterFactory.getContainmentLink(0xc72da2b97cce4447L, 0x8389f407dc1158b7L, 0x1103553c5ffL, 0xf979c3ba6cL, "propertyDeclaration"))).any(new IWhereFilter<SNode>() {
public boolean accept(SNode it) {
return it != p && eq_huw3go_a0a0a0a0a0a1a2(SPropertyOperations.getString(it, MetaAdapterFactory.getProperty(0xc72da2b97cce4447L, 0x8389f407dc1158b7L, 0xf979bd086bL, 0x35a81382d82a4d9L, "propertyId")), result.value + "");
}
})) {
result.value = randomLong();
}
return result.value;
}
public static long generateLinkId(@Nullable SNode c, final SNode l) {
final Wrappers._long result = new Wrappers._long(ConceptIdHelper.getDefaultIdFromNode(l));
while (ListSequence.fromList(SLinkOperations.getChildren(c, MetaAdapterFactory.getContainmentLink(0xc72da2b97cce4447L, 0x8389f407dc1158b7L, 0x1103553c5ffL, 0xf979c3ba6bL, "linkDeclaration"))).any(new IWhereFilter<SNode>() {
public boolean accept(SNode it) {
return it != l && eq_huw3go_a0a0a0a0a0a1a4(SPropertyOperations.getString(it, MetaAdapterFactory.getProperty(0xc72da2b97cce4447L, 0x8389f407dc1158b7L, 0xf979bd086aL, 0x35a81382d82a4e4L, "linkId")), result.value + "");
}
})) {
result.value = randomLong();
}
return result.value;
}
@Nullable
public static String getNodeIdString(SNode n) {
jetbrains.mps.smodel.SNode sn = as_huw3go_a0a0a6(n, jetbrains.mps.smodel.SNode.class);
if (sn == null) {
return null;
}
SNodeId.Regular ni = as_huw3go_a0a3a6(sn.getNodeId(), SNodeId.Regular.class);
if (ni == null) {
return null;
}
return ni.getId() + "";
}
private static long getDefaultIdFromNode(SNode c) {
long result = randomLong();
if (c != null) {
org.jetbrains.mps.openapi.model.SNodeId nid = c.getNodeId();
if (nid instanceof SNodeId.Regular) {
result = ((SNodeId.Regular) nid).getId();
}
}
return result;
}
private static long randomLong() {
return ((long) (Math.random() * Long.MAX_VALUE));
}
private static boolean eq_huw3go_a0a0a0a0a0a1a0(Object a, Object b) {
return (a != null ? a.equals(b) : a == b);
}
private static boolean eq_huw3go_a0a0a0a0a0a1a2(Object a, Object b) {
return (a != null ? a.equals(b) : a == b);
}
private static boolean eq_huw3go_a0a0a0a0a0a1a4(Object a, Object b) {
return (a != null ? a.equals(b) : a == b);
}
private static <T> T as_huw3go_a0a0a6(Object o, Class<T> type) {
return (type.isInstance(o) ? (T) o : null);
}
private static <T> T as_huw3go_a0a3a6(Object o, Class<T> type) {
return (type.isInstance(o) ? (T) o : null);
}
}