package jetbrains.mps.lang.smodel.generator.smodelAdapter; /*Generated by MPS */ import org.jetbrains.mps.openapi.model.SNode; import org.jetbrains.mps.openapi.language.SReferenceLink; import org.jetbrains.mps.openapi.language.SContainmentLink; import jetbrains.mps.util.SNodeOperations; import org.jetbrains.mps.openapi.model.SNodeAccessUtil; import org.jetbrains.mps.openapi.language.SConcept; import java.util.List; import jetbrains.mps.util.IterableUtil; import org.jetbrains.mps.openapi.model.SReference; import jetbrains.mps.internal.collections.runtime.Sequence; import jetbrains.mps.internal.collections.runtime.ISelector; import jetbrains.mps.internal.collections.runtime.IWhereFilter; import jetbrains.mps.internal.collections.runtime.ITranslator2; /** * * @deprecated */ @Deprecated public class SLinkOperations { @Deprecated public SLinkOperations() { } public static SNode findLinkDeclaration(SReferenceLink link) { if (link == null) { return null; } return link.getDeclarationNode(); } public static SNode findLinkDeclaration(SContainmentLink link) { if (link == null) { return null; } return link.getDeclarationNode(); } public static SNode getTarget(SNode node, SContainmentLink role) { if (node == null) { return null; } return SNodeOperations.getChild(node, role); } public static SNode getTarget(SNode node, SReferenceLink role) { if (node == null) { return null; } return node.getReferenceTarget(role); } public static SNode setTarget(SNode node, SContainmentLink role, SNode targetNode) { if (node != null) { SNode oldChild = getTarget(node, role); if (oldChild != null) { node.removeChild(oldChild); } if (targetNode != null) { SNode targetParent = targetNode.getParent(); if (targetParent != null) { targetParent.removeChild(targetNode); } node.addChild(role, targetNode); } } return targetNode; } public static SNode setTarget(SNode node, SReferenceLink role, SNode targetNode) { if (node != null) { SNodeAccessUtil.setReferenceTarget(node, role, targetNode); } return targetNode; } public static SNode setNewChild(SNode node, SContainmentLink role, SConcept childConcept) { if (node != null) { SNode newChild = SModelOperations.createNewNode(node.getModel(), null, childConcept); setTarget(node, role, newChild); return newChild; } return null; } public static List<SNode> getChildren(SNode node, SContainmentLink role) { if (node != null && role != null) { return new AbstractSNodeList.ChildrenSNodesList(node, role); } return jetbrains.mps.lang.smodel.generator.smodelAdapter.SNodeOperations.EMPTY_LIST; } public static SNode addNewChild(SNode node, SContainmentLink role, SConcept childConcept) { if (node != null) { SNode newChild = SModelOperations.createNewNode(node.getModel(), null, childConcept); node.addChild(role, newChild); return newChild; } return null; } public static SNode addChild(SNode parent, SContainmentLink role, SNode child) { if (parent != null && child != null) { SNode childParent = child.getParent(); if (childParent != null) { childParent.removeChild(child); } parent.addChild(role, child); } return child; } public static void addAll(SNode parent, SContainmentLink role, List<SNode> nodeList) { for (SNode node : nodeList) { addChild(parent, role, node); } } public static SNode insertChildFirst(SNode parent, SContainmentLink role, SNode child) { if (parent != null && child != null) { SNode childParent = child.getParent(); if (childParent != null) { childParent.removeChild(child); } parent.addChild(role, child); } return child; } public static List<SNode> removeAllChildren(SNode parent, SContainmentLink role) { if (parent == null) { return jetbrains.mps.lang.smodel.generator.smodelAdapter.SNodeOperations.EMPTY_LIST; } Iterable<? extends SNode> children = parent.getChildren(role); for (SNode child : children) { parent.removeChild(child); } return IterableUtil.asList(children); } public static SNode findLinkDeclaration(SReference reference) { if (reference == null) { return null; } return reference.getLink().getDeclarationNode(); } public static SNode getTargetNode(SReference reference) { if (reference == null) { return null; } return reference.getTargetNode(); } public static String getRole(SReference reference) { if (reference == null) { return null; } return reference.getRole(); } public static SReferenceLink getRefLink(SReference reference) { if (reference == null) { return null; } return reference.getLink(); } public static String getResolveInfo(SReference reference) { if (reference == null) { return null; } return ((jetbrains.mps.smodel.SReference) reference).getResolveInfo(); } /** * For each element of supplied collection, navigate specified reference and collect non-null targets as a resulting sequence. * Null elements in the source collections are tolerated (and ignored) */ public static Iterable<SNode> collect(Iterable<SNode> collection, final SReferenceLink l) { return Sequence.fromIterable(collection).select(new ISelector<SNode, SNode>() { public SNode select(SNode it) { return getTarget(it, l); } }).where(new IWhereFilter<SNode>() { public boolean accept(SNode it) { return it != null; } }); } /** * For each element of supplied collection, take child from specified role (if any), and return these as a sequence. * Result sequence doesn't contain null values. * Null elements in the source collections are tolerated (and ignored) */ public static Iterable<SNode> collect(Iterable<SNode> collection, final SContainmentLink l) { return Sequence.fromIterable(collection).select(new ISelector<SNode, SNode>() { public SNode select(SNode it) { return getTarget(it, l); } }).where(new IWhereFilter<SNode>() { public boolean accept(SNode it) { return it != null; } }); } /** * For each element of supplied collection, collect all children from specified role and return them as flattened sequence. * Result sequence doesn't contain null values. * Null elements in the source collections are tolerated (and ignored) */ public static Iterable<SNode> collectMany(Iterable<SNode> collection, final SContainmentLink l) { return Sequence.fromIterable(collection).translate(new ITranslator2<SNode, SNode>() { public Iterable<SNode> translate(SNode it) { return getChildren(it, l); } }); } }