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