magritte.wrappers.Morph Maven / Gradle / Ivy
package magritte.wrappers;
import magritte.Intention;
import magritte.Node;
import magritte.Wrapper;
import magritte.editors.NodeProducer;
import magritte.filters.CasesOf;
import magritte.helpers.ClassName;
import magritte.helpers.NodeSetWrapper;
import magritte.helpers.NodeWrapper;
import static magritte.Link.AsHolder;
public class Morph implements Wrapper {
protected Node node;
@Override
public Node node() {
return node;
}
@Override
public void setNode(Node node) {
this.node = node;
}
public String name() {
return node.name();
}
public boolean is(java.lang.Class typeClass) {
return node.is(typeClass);
}
public T as(Class morphClass) {
return NodeWrapper.wrap(node).with(morphClass);
}
public C get(String name) {
return node.get(name);
}
public void set(String name, T value) {
node.set(name, value);
}
protected T owner(Class morphClass) {
return NodeWrapper.wrap(this.node.owner()).with(morphClass);
}
protected T property(Class definitionClass) {
return definition().property(definitionClass);
}
protected T[] components(Class morphClass) {
return NodeSetWrapper.wrap(node.components().select(CasesOf.type(morphClass))).with(morphClass);
}
protected T[] holdings(Class morphClass) {
return NodeSetWrapper.wrap(node.holdings().select(CasesOf.type(morphClass))).with(morphClass);
}
public T[] usages(Class morphClass) {
return NodeSetWrapper.wrap(node.usages().select(CasesOf.type(morphClass))).with(morphClass);
}
public T[] references(Class morphClass) {
return NodeSetWrapper.wrap(node.references().select(CasesOf.type(morphClass))).with(morphClass);
}
protected T[] extensions(Class intentionClass) {
return NodeWrapper.wrap(node).withIntention(intentionClass);
}
public Definition definition() {
Definition definition = new Definition();
definition.setNode(type());
definition.setTarget(node);
return definition;
}
private Node type() {
for (Node type : node.types())
if (type.is(this.getClass())) return type;
return node.type();
}
protected NodeWrapper addNode(Node type) {
return NodeWrapper.wrap(createHolderNode(type));
}
private Node createHolderNode(Node type) {
return NodeProducer.produce(createRootNode()).with(type).link(node, AsHolder).reindex().node();
}
private Node createRootNode() {
return node.repository().createRootNode();
}
@Override
public String toString() {
return ClassName.withoutPackage(this) + " - " + node.toString();
}
}