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