]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/variable/AbstractConstantPropertyVariable.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / variable / AbstractConstantPropertyVariable.java
diff --git a/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/variable/AbstractConstantPropertyVariable.java b/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/variable/AbstractConstantPropertyVariable.java
new file mode 100644 (file)
index 0000000..5bf91e7
--- /dev/null
@@ -0,0 +1,157 @@
+package org.simantics.db.layer0.variable;\r
+\r
+import java.util.Collection;\r
+import java.util.Collections;\r
+import java.util.HashMap;\r
+import java.util.Map;\r
+import java.util.Set;\r
+\r
+import org.simantics.databoard.binding.Binding;\r
+import org.simantics.databoard.type.Datatype;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.WriteGraph;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.utils.ObjectUtils;\r
+\r
+abstract public class AbstractConstantPropertyVariable extends AbstractPropertyVariable {\r
+\r
+    final protected Variable parent;\r
+    final String name;\r
+    final Binding binding;\r
+    final Map<String, Variable> properties;\r
+\r
+    public AbstractConstantPropertyVariable(Variable parent, String name, Binding binding, Collection<ConstantPropertyVariableBuilder> propertyBuilders, Set<String> classifications) {\r
+        assert parent != null;\r
+        assert name != null;\r
+        this.parent = parent;\r
+        this.name = name;\r
+        this.binding = binding;\r
+        this.properties = new HashMap<String, Variable>(propertyBuilders.size());\r
+        for(ConstantPropertyVariableBuilder builder : propertyBuilders) {\r
+               properties.put(builder.getName(), builder.build(this));\r
+        }\r
+        if(!classifications.isEmpty())\r
+               properties.put(Variables.CLASSIFICATIONS, new ConstantPropertyVariable(this, Variables.CLASSIFICATIONS, classifications, null));\r
+    }\r
+\r
+    public AbstractConstantPropertyVariable(Variable parent, String name, Binding binding) {\r
+       this(parent, name, binding, Collections.<ConstantPropertyVariableBuilder>emptyList(), Collections.<String>emptySet());\r
+    }\r
+\r
+    @Override\r
+    public void setValue(WriteGraph graph, Object value, Binding binding)\r
+            throws DatabaseException {\r
+        throw new DatabaseException("Value is constant.");\r
+    }\r
+\r
+    @Override\r
+    public String getName(ReadGraph graph) throws DatabaseException {\r
+        return name;\r
+    }\r
+\r
+    @Override\r
+    public Resource getType(ReadGraph graph) throws DatabaseException {\r
+        return null;\r
+    }\r
+\r
+    @Override\r
+    public Resource getPossibleType(ReadGraph graph) throws DatabaseException {\r
+        return null;\r
+    }\r
+\r
+    @Override\r
+    public Resource getType(ReadGraph graph, Resource baseType) throws DatabaseException {\r
+        return null;\r
+    }\r
+\r
+    @Override\r
+    public Resource getPossibleType(ReadGraph graph, Resource baseType) throws DatabaseException {\r
+        return null;\r
+    }\r
+\r
+//    @Override\r
+//    public Object getSerialized(ReadGraph graph) throws DatabaseException {\r
+//        return name;\r
+//    }\r
+\r
+    @Override\r
+    public Variable getParent(ReadGraph graph) throws DatabaseException {\r
+        return parent;\r
+    }\r
+\r
+    @Override\r
+    public Resource getRepresents(ReadGraph graph) throws DatabaseException {\r
+        return null;\r
+    }\r
+    \r
+    @Override\r
+    public Datatype getDatatype(ReadGraph graph) throws DatabaseException {\r
+        return binding != null ? binding.type() : null;\r
+    }\r
+    \r
+    @Override\r
+    public Resource getPropertyResource(ReadGraph graph) throws DatabaseException {\r
+       return null;\r
+    }\r
+\r
+    @Override\r
+    public Resource getContainerResource(ReadGraph graph) throws DatabaseException {\r
+       return null;\r
+    }\r
+    \r
+    @Override\r
+    public Variable getPredicate(ReadGraph graph) throws DatabaseException {\r
+       return null;\r
+    }\r
+    \r
+    @Override\r
+    protected Variable getPossibleDomainProperty(ReadGraph graph, String name)\r
+               throws DatabaseException {\r
+       return properties.get(name);\r
+    }\r
+\r
+    @Override\r
+    public Map<String, Variable> collectDomainProperties(ReadGraph graph,\r
+               Map<String, Variable> properties) throws DatabaseException {\r
+       if(!this.properties.isEmpty()) {\r
+               if(properties == null) properties = new HashMap<String,Variable>(this.properties.size());\r
+               properties.putAll(this.properties);\r
+       }\r
+       return properties;\r
+    }\r
+    \r
+    @Override\r
+    public Set<String> getClassifications(ReadGraph graph) throws DatabaseException {\r
+       Variable property = getPossibleDomainProperty(graph, Variables.CLASSIFICATIONS);\r
+       if(property != null) return property.getValue(graph);\r
+       else return Collections.emptySet();\r
+    }\r
+    \r
+    @Override\r
+    public int hashCode() {\r
+        final int prime = 31;\r
+        int result = 1;\r
+        result = prime * result + name.hashCode();\r
+        result = prime * result + parent.hashCode();\r
+        result = prime * result + ((binding == null) ? 0 : binding.hashCode());\r
+        return result;\r
+    }\r
+\r
+    @Override\r
+    public boolean equals(Object obj) {\r
+        if (this == obj)\r
+            return true;\r
+        if (obj == null)\r
+            return false;\r
+        if (getClass() != obj.getClass())\r
+            return false;\r
+        AbstractConstantPropertyVariable other = (AbstractConstantPropertyVariable) obj;\r
+        if (!name.equals(other.name))\r
+            return false;\r
+        if (!parent.equals(other.parent))\r
+            return false;\r
+        return ObjectUtils.objectEquals(binding, other.binding);\r
+    }\r
+\r
+}\r