1 package org.simantics.db.layer0.variable;
3 import java.util.Collection;
4 import java.util.Collections;
5 import java.util.HashMap;
9 import org.simantics.databoard.binding.Binding;
10 import org.simantics.databoard.type.Datatype;
11 import org.simantics.db.ReadGraph;
12 import org.simantics.db.Resource;
13 import org.simantics.db.WriteGraph;
14 import org.simantics.db.exception.DatabaseException;
15 import org.simantics.utils.ObjectUtils;
17 public class ConstantPropertyVariable extends AbstractPropertyVariable {
19 final protected Variable parent;
22 final Binding binding;
23 final Map<String, Variable> properties;
25 public ConstantPropertyVariable(Variable parent, String name, Object value, Binding binding, Collection<ConstantPropertyVariableBuilder> propertyBuilders, Set<String> classifications) {
26 assert parent != null;
31 this.binding = binding;
32 this.properties = new HashMap<String, Variable>(propertyBuilders.size());
33 for(ConstantPropertyVariableBuilder builder : propertyBuilders) {
34 properties.put(builder.getName(), builder.build(this));
36 if(!classifications.isEmpty())
37 properties.put(Variables.CLASSIFICATIONS, new ConstantPropertyVariable(this, Variables.CLASSIFICATIONS, classifications, null));
40 public ConstantPropertyVariable(Variable parent, String name, Object value, Binding binding) {
41 this(parent, name, value, binding, Collections.<ConstantPropertyVariableBuilder>emptyList(), Collections.<String>emptySet());
45 public <T> T getValue(ReadGraph graph) throws DatabaseException {
50 public <T> T getValue(ReadGraph graph, Binding binding)
51 throws DatabaseException {
56 public void setValue(WriteGraph graph, Object value, Binding binding)
57 throws DatabaseException {
58 throw new DatabaseException("Value is constant.");
62 public String getName(ReadGraph graph) throws DatabaseException {
67 public Resource getType(ReadGraph graph) throws DatabaseException {
72 public Resource getPossibleType(ReadGraph graph) throws DatabaseException {
77 public Resource getType(ReadGraph graph, Resource baseType) throws DatabaseException {
82 public Resource getPossibleType(ReadGraph graph, Resource baseType) throws DatabaseException {
87 // public Object getSerialized(ReadGraph graph) throws DatabaseException {
92 public Variable getParent(ReadGraph graph) throws DatabaseException {
97 public Resource getRepresents(ReadGraph graph) throws DatabaseException {
102 public Datatype getDatatype(ReadGraph graph) throws DatabaseException {
103 return binding != null ? binding.type() : null;
107 public Resource getPropertyResource(ReadGraph graph) throws DatabaseException {
112 public Resource getContainerResource(ReadGraph graph) throws DatabaseException {
117 public Variable getPredicate(ReadGraph graph) throws DatabaseException {
122 protected Variable getPossibleDomainProperty(ReadGraph graph, String name)
123 throws DatabaseException {
124 return properties.get(name);
128 public Map<String, Variable> collectDomainProperties(ReadGraph graph,
129 Map<String, Variable> properties) throws DatabaseException {
130 if(!this.properties.isEmpty()) {
131 if(properties == null) properties = new HashMap<String,Variable>(this.properties.size());
132 properties.putAll(this.properties);
138 public Set<String> getClassifications(ReadGraph graph) throws DatabaseException {
139 Variable property = getPossibleDomainProperty(graph, Variables.CLASSIFICATIONS);
140 if(property != null) return property.getValue(graph);
141 else return Collections.emptySet();
145 public int hashCode() {
146 final int prime = 31;
148 result = prime * result + name.hashCode();
149 result = prime * result + parent.hashCode();
150 result = prime * result + ((value == null) ? 0 : value.hashCode());
151 result = prime * result + ((binding == null) ? 0 : binding.hashCode());
156 public boolean equals(Object obj) {
161 if (getClass() != obj.getClass())
163 ConstantPropertyVariable other = (ConstantPropertyVariable) obj;
164 if (!name.equals(other.name))
166 if (!parent.equals(other.parent))
168 if (!ObjectUtils.objectEquals(value, other.value))
170 return ObjectUtils.objectEquals(binding, other.binding);