package org.simantics.modeling; import java.util.HashMap; import java.util.Map; import java.util.Set; import org.simantics.Simantics; import org.simantics.databoard.binding.Binding; import org.simantics.databoard.type.Datatype; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; import org.simantics.db.WriteGraph; import org.simantics.db.exception.DatabaseException; import org.simantics.db.layer0.util.Layer0Utils; import org.simantics.db.layer0.variable.AbstractPropertyVariable; import org.simantics.db.layer0.variable.Variable; class ImmutableComponentPropertyVariable extends AbstractPropertyVariable { final Variable parent; final ImmutableComponentPropertyContent content; @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; ImmutableComponentPropertyVariable other = (ImmutableComponentPropertyVariable) 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; } ImmutableComponentPropertyVariable(Variable parent, ImmutableComponentPropertyContent content) { this.parent = parent; this.content = content; } @Override public Resource getPropertyResource(ReadGraph graph) throws DatabaseException { return content.pi.predicate; } @Override public Resource getPossiblePredicateResource(ReadGraph graph) throws DatabaseException { return content.pi.predicate; } @Override public Resource getContainerResource(ReadGraph graph) throws DatabaseException { throw new IllegalStateException(); } @Override public Datatype getDatatype(ReadGraph graph) throws DatabaseException { return Layer0Utils.getDatatype(graph, this); } @Override public Resource getPredicateResource(ReadGraph graph) throws DatabaseException { return content.pi.predicate; } @Override public Variable getPredicate(ReadGraph graph) throws DatabaseException { throw new IllegalStateException(); } @Override public T getValue(ReadGraph graph) throws DatabaseException { if(content.value != null) return (T)content.value; return (T)Simantics.applySCLRead(graph, content.expression, this); } @Override public T getValue(ReadGraph graph, Binding binding) throws DatabaseException { if(content.value != null) return (T)content.value; return (T)Simantics.applySCLRead(graph, content.expression, this); } @Override public void setValue(WriteGraph graph, Object value, Binding binding) throws DatabaseException { throw new IllegalStateException(); } @Override public String getName(ReadGraph graph) throws DatabaseException { return content.pi.name; } @Override public Variable getParent(ReadGraph graph) throws DatabaseException { return parent; } @Override public Set getClassifications(ReadGraph graph) throws DatabaseException { return content.pi.classifications; } @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 null; } @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; } }