]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/variable/AbstractConstantPropertyVariable.java
Merge "Fixes to variable implementations"
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / variable / AbstractConstantPropertyVariable.java
1 package org.simantics.db.layer0.variable;
2
3 import java.util.Collection;
4 import java.util.Collections;
5 import java.util.HashMap;
6 import java.util.Map;
7 import java.util.Set;
8
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;
16
17 abstract public class AbstractConstantPropertyVariable extends AbstractPropertyVariable {
18
19     final protected Variable parent;
20     final String name;
21     final Binding binding;
22     final Map<String, Variable> properties;
23
24     public AbstractConstantPropertyVariable(Variable parent, String name, Binding binding, Collection<ConstantPropertyVariableBuilder> propertyBuilders, Set<String> classifications) {
25         assert parent != null;
26         assert name != null;
27         this.parent = parent;
28         this.name = name;
29         this.binding = binding;
30         this.properties = new HashMap<String, Variable>(propertyBuilders.size());
31         for(ConstantPropertyVariableBuilder builder : propertyBuilders) {
32                 properties.put(builder.getName(), builder.build(this));
33         }
34         if(!classifications.isEmpty())
35                 properties.put(Variables.CLASSIFICATIONS, new ConstantPropertyVariable(this, Variables.CLASSIFICATIONS, classifications, null));
36     }
37
38     public AbstractConstantPropertyVariable(Variable parent, String name, Binding binding) {
39         this(parent, name, binding, Collections.<ConstantPropertyVariableBuilder>emptyList(), Collections.<String>emptySet());
40     }
41
42     @Override
43     public void setValue(WriteGraph graph, Object value, Binding binding)
44             throws DatabaseException {
45         throw new DatabaseException("Value is constant.");
46     }
47
48     @Override
49     public String getName(ReadGraph graph) throws DatabaseException {
50         return name;
51     }
52
53     @Override
54     public Resource getType(ReadGraph graph) throws DatabaseException {
55         return null;
56     }
57
58     @Override
59     public Resource getPossibleType(ReadGraph graph) throws DatabaseException {
60         return null;
61     }
62
63     @Override
64     public Resource getType(ReadGraph graph, Resource baseType) throws DatabaseException {
65         return null;
66     }
67
68     @Override
69     public Resource getPossibleType(ReadGraph graph, Resource baseType) throws DatabaseException {
70         return null;
71     }
72
73 //    @Override
74 //    public Object getSerialized(ReadGraph graph) throws DatabaseException {
75 //        return name;
76 //    }
77
78     @Override
79     public Variable getParent(ReadGraph graph) throws DatabaseException {
80         return parent;
81     }
82
83     @Override
84     public Resource getRepresents(ReadGraph graph) throws DatabaseException {
85         return null;
86     }
87     
88     @Override
89     public Datatype getDatatype(ReadGraph graph) throws DatabaseException {
90         return binding != null ? binding.type() : null;
91     }
92     
93     @Override
94     public Resource getPropertyResource(ReadGraph graph) throws DatabaseException {
95         return null;
96     }
97
98     @Override
99     public Resource getContainerResource(ReadGraph graph) throws DatabaseException {
100         return null;
101     }
102     
103     @Override
104     public Variable getPredicate(ReadGraph graph) throws DatabaseException {
105         return null;
106     }
107     
108     @Override
109     protected Variable getPossibleDomainProperty(ReadGraph graph, String name)
110                 throws DatabaseException {
111         return properties.get(name);
112     }
113
114     @Override
115     public Map<String, Variable> collectDomainProperties(ReadGraph graph,
116                 Map<String, Variable> properties) throws DatabaseException {
117         if(!this.properties.isEmpty()) {
118                 if(properties == null) properties = new HashMap<String,Variable>(this.properties.size());
119                 properties.putAll(this.properties);
120         }
121         return properties;
122     }
123     
124     @Override
125     public Set<String> getClassifications(ReadGraph graph) throws DatabaseException {
126         Variable property = getPossibleDomainProperty(graph, Variables.CLASSIFICATIONS);
127         if(property != null) return property.getValue(graph);
128         else return Collections.emptySet();
129     }
130     
131     @Override
132     public int hashCode() {
133         final int prime = 31;
134         int result = 1;
135         result = prime * result + name.hashCode();
136         result = prime * result + parent.hashCode();
137         result = prime * result + ((binding == null) ? 0 : binding.hashCode());
138         return result;
139     }
140
141     @Override
142     public boolean equals(Object obj) {
143         if (this == obj)
144             return true;
145         if (obj == null)
146             return false;
147         if (getClass() != obj.getClass())
148             return false;
149         AbstractConstantPropertyVariable other = (AbstractConstantPropertyVariable) obj;
150         if (!name.equals(other.name))
151             return false;
152         if (!parent.equals(other.parent))
153             return false;
154         return ObjectUtils.objectEquals(binding, other.binding);
155     }
156
157 }