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; import org.simantics.structural.stubs.StructuralResource2; 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.procedural) { return new StandardGraphChildVariable(parent, null, content.resource).getPossibleChild(graph, name); } 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.procedural) { return new StandardGraphChildVariable(parent, null, content.resource).getChildren(graph); } 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; } }