X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.modeling%2Fsrc%2Forg%2Fsimantics%2Fmodeling%2FImmutableComponentVariable.java;fp=bundles%2Forg.simantics.modeling%2Fsrc%2Forg%2Fsimantics%2Fmodeling%2FImmutableComponentVariable.java;h=6ed9273906e090dc805e52d7583f5d139cd99988;hb=3850fec72035293b9a4ede780d01aedc5fbc9056;hp=0000000000000000000000000000000000000000;hpb=1e957fc9da518f3bef8a2c19cad72772087e1b6a;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.modeling/src/org/simantics/modeling/ImmutableComponentVariable.java b/bundles/org.simantics.modeling/src/org/simantics/modeling/ImmutableComponentVariable.java new file mode 100644 index 000000000..6ed927390 --- /dev/null +++ b/bundles/org.simantics.modeling/src/org/simantics/modeling/ImmutableComponentVariable.java @@ -0,0 +1,122 @@ +package org.simantics.modeling; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; + +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.layer0.variable.AbstractChildVariable; +import org.simantics.db.layer0.variable.StandardGraphChildVariable; +import org.simantics.db.layer0.variable.Variable; + +public class ImmutableComponentVariable extends AbstractChildVariable { + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((content == null) ? 0 : content.hashCode()); + result = prime * result + ((parent == null) ? 0 : parent.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + ImmutableComponentVariable other = (ImmutableComponentVariable) obj; + if (content == null) { + if (other.content != null) + return false; + } else if (!content.equals(other.content)) + return false; + if (parent == null) { + if (other.parent != null) + return false; + } else if (!parent.equals(other.parent)) + return false; + return true; + } + + final private Variable parent; + final private ImmutableComponentVariableContent content; + + ImmutableComponentVariable(Variable parent, ImmutableComponentVariableContent content) { + this.parent = parent; + this.content = content; + } + + @Override + public String getName(ReadGraph graph) throws DatabaseException { + return content.name; + } + + @Override + public Variable getParent(ReadGraph graph) throws DatabaseException { + return parent; + } + + @Override + public Resource getRepresents(ReadGraph graph) throws DatabaseException { + return content.resource; + } + + @Override + public Variable getPossibleChild(ReadGraph graph, String name) throws DatabaseException { + if(content.children == null) return null; + ImmutableComponentVariableContent c = content.children.get(name); + if(c == null) return null; + return new ImmutableComponentVariable(this, c); + } + + @Override + public Collection getChildren(ReadGraph graph) throws DatabaseException { + if(content.children == null) return Collections.emptyList(); + ArrayList result = new ArrayList(content.children.size()); + for(ImmutableComponentVariableContent c : content.children.values()) { + result.add(new ImmutableComponentVariable(this, c)); + } + return result; + } + + @Override + protected Variable getPossibleDomainProperty(ReadGraph graph, String name) throws DatabaseException { + + if(content.properties == null) + return null; + + ImmutableComponentPropertyContent p = content.properties.get(name); + if(p != null) + return new ImmutableComponentPropertyVariable(this, p); + + return new StandardGraphChildVariable(parent, node, content.resource).getPossibleProperty(graph, name); + + } + + @Override + public Map collectDomainProperties(ReadGraph graph, Map map) + throws DatabaseException { + + if(content.properties == null) + return map; + + if(map == null) + map = new HashMap<>(); + + for(ImmutableComponentPropertyContent p : content.properties.values()) { + map.put(p.pi.name, new ImmutableComponentPropertyVariable(this, p)); + } + + return map; + + } + +}