]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/variable/ConstantPropertyVariable.java
Merge commit 'ad8333027322fda6b9a8a524c7a7e15a54c52f38'
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / variable / ConstantPropertyVariable.java
1 package org.simantics.db.layer0.variable;\r
2 \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
8 \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
16 \r
17 public class ConstantPropertyVariable extends AbstractPropertyVariable {\r
18 \r
19     final protected Variable parent;\r
20     final String name;\r
21     final Object value;\r
22     final Binding binding;\r
23     final Map<String, Variable> properties;\r
24 \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
29         this.name = name;\r
30         this.value = value;\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
35         }\r
36         if(!classifications.isEmpty())\r
37                 properties.put(Variables.CLASSIFICATIONS, new ConstantPropertyVariable(this, Variables.CLASSIFICATIONS, classifications, null));\r
38     }\r
39 \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
42     }\r
43 \r
44     @Override\r
45     public <T> T getValue(ReadGraph graph) throws DatabaseException {\r
46         return (T)value;\r
47     }\r
48 \r
49     @Override\r
50     public <T> T getValue(ReadGraph graph, Binding binding)\r
51             throws DatabaseException {        \r
52         return (T)value;\r
53     }\r
54 \r
55     @Override\r
56     public void setValue(WriteGraph graph, Object value, Binding binding)\r
57             throws DatabaseException {\r
58         throw new DatabaseException("Value is constant.");\r
59     }\r
60 \r
61     @Override\r
62     public String getName(ReadGraph graph) throws DatabaseException {\r
63         return name;\r
64     }\r
65 \r
66     @Override\r
67     public Resource getType(ReadGraph graph) throws DatabaseException {\r
68         return null;\r
69     }\r
70 \r
71     @Override\r
72     public Resource getPossibleType(ReadGraph graph) throws DatabaseException {\r
73         return null;\r
74     }\r
75 \r
76     @Override\r
77     public Resource getType(ReadGraph graph, Resource baseType) throws DatabaseException {\r
78         return null;\r
79     }\r
80 \r
81     @Override\r
82     public Resource getPossibleType(ReadGraph graph, Resource baseType) throws DatabaseException {\r
83         return null;\r
84     }\r
85 \r
86 //    @Override\r
87 //    public Object getSerialized(ReadGraph graph) throws DatabaseException {\r
88 //        return name;\r
89 //    }\r
90 \r
91     @Override\r
92     public Variable getParent(ReadGraph graph) throws DatabaseException {\r
93         return parent;\r
94     }\r
95 \r
96     @Override\r
97     public Resource getRepresents(ReadGraph graph) throws DatabaseException {\r
98         return null;\r
99     }\r
100     \r
101     @Override\r
102     public Datatype getDatatype(ReadGraph graph) throws DatabaseException {\r
103         return binding != null ? binding.type() : null;\r
104     }\r
105     \r
106     @Override\r
107     public Resource getPropertyResource(ReadGraph graph) throws DatabaseException {\r
108         return null;\r
109     }\r
110 \r
111     @Override\r
112     public Resource getContainerResource(ReadGraph graph) throws DatabaseException {\r
113         return null;\r
114     }\r
115     \r
116     @Override\r
117     public Variable getPredicate(ReadGraph graph) throws DatabaseException {\r
118         return null;\r
119     }\r
120     \r
121     @Override\r
122     protected Variable getPossibleDomainProperty(ReadGraph graph, String name)\r
123                 throws DatabaseException {\r
124         return properties.get(name);\r
125     }\r
126 \r
127     @Override\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
133         }\r
134         return properties;\r
135     }\r
136     \r
137     @Override\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
142     }\r
143     \r
144     @Override\r
145     public int hashCode() {\r
146         final int prime = 31;\r
147         int result = 1;\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
152         return result;\r
153     }\r
154 \r
155     @Override\r
156     public boolean equals(Object obj) {\r
157         if (this == obj)\r
158             return true;\r
159         if (obj == null)\r
160             return false;\r
161         if (getClass() != obj.getClass())\r
162             return false;\r
163         ConstantPropertyVariable other = (ConstantPropertyVariable) obj;\r
164         if (!name.equals(other.name))\r
165             return false;\r
166         if (!parent.equals(other.parent))\r
167             return false;\r
168         if (!ObjectUtils.objectEquals(value, other.value))\r
169             return false;\r
170         return ObjectUtils.objectEquals(binding, other.binding);\r
171     }\r
172 \r
173 }\r