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