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