]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.modeling/src/org/simantics/modeling/ImmutableComponentVariable.java
Multiple readers and variable optimization
[simantics/platform.git] / bundles / org.simantics.modeling / src / org / simantics / modeling / ImmutableComponentVariable.java
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 (file)
index 0000000..6ed9273
--- /dev/null
@@ -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<Variable> getChildren(ReadGraph graph) throws DatabaseException {
+               if(content.children == null) return Collections.emptyList();
+               ArrayList<Variable> result = new ArrayList<Variable>(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<String, Variable> collectDomainProperties(ReadGraph graph, Map<String, Variable> 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;
+               
+       }
+       
+}