]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling/src/org/simantics/modeling/ImmutableComponentPropertyVariable.java
Multiple readers and variable optimization
[simantics/platform.git] / bundles / org.simantics.modeling / src / org / simantics / modeling / ImmutableComponentPropertyVariable.java
1 package org.simantics.modeling;
2
3 import java.util.HashMap;
4 import java.util.Map;
5 import java.util.Set;
6
7 import org.simantics.Simantics;
8 import org.simantics.databoard.binding.Binding;
9 import org.simantics.databoard.type.Datatype;
10 import org.simantics.db.ReadGraph;
11 import org.simantics.db.Resource;
12 import org.simantics.db.WriteGraph;
13 import org.simantics.db.exception.DatabaseException;
14 import org.simantics.db.layer0.util.Layer0Utils;
15 import org.simantics.db.layer0.variable.AbstractPropertyVariable;
16 import org.simantics.db.layer0.variable.Variable;
17
18 class ImmutableComponentPropertyVariable extends AbstractPropertyVariable {
19
20         final Variable parent;
21         final ImmutableComponentPropertyContent content;
22
23         @Override
24         public int hashCode() {
25                 final int prime = 31;
26                 int result = 1;
27                 result = prime * result + ((content == null) ? 0 : content.hashCode());
28                 result = prime * result + ((parent == null) ? 0 : parent.hashCode());
29                 return result;
30         }
31
32         @Override
33         public boolean equals(Object obj) {
34                 if (this == obj)
35                         return true;
36                 if (obj == null)
37                         return false;
38                 if (getClass() != obj.getClass())
39                         return false;
40                 ImmutableComponentPropertyVariable other = (ImmutableComponentPropertyVariable) obj;
41                 if (content == null) {
42                         if (other.content != null)
43                                 return false;
44                 } else if (!content.equals(other.content))
45                         return false;
46                 if (parent == null) {
47                         if (other.parent != null)
48                                 return false;
49                 } else if (!parent.equals(other.parent))
50                         return false;
51                 return true;
52         }
53
54         ImmutableComponentPropertyVariable(Variable parent, ImmutableComponentPropertyContent content) {
55                 this.parent = parent;
56                 this.content = content;
57         }
58
59         @Override
60         public Resource getPropertyResource(ReadGraph graph) throws DatabaseException {
61                 return content.pi.predicate;
62         }
63
64         @Override
65         public Resource getPossiblePredicateResource(ReadGraph graph) throws DatabaseException {
66                 return content.pi.predicate;
67         }
68
69         @Override
70         public Resource getContainerResource(ReadGraph graph) throws DatabaseException {
71                 throw new IllegalStateException();
72         }
73
74         @Override
75         public Datatype getDatatype(ReadGraph graph) throws DatabaseException {
76                 return Layer0Utils.getDatatype(graph, this);
77         }
78
79         @Override
80         public Resource getPredicateResource(ReadGraph graph) throws DatabaseException {
81                 return content.pi.predicate;
82         }
83
84         @Override
85         public Variable getPredicate(ReadGraph graph) throws DatabaseException {
86                 throw new IllegalStateException();
87         }
88
89         @Override
90         public <T> T getValue(ReadGraph graph) throws DatabaseException {
91                 if(content.value != null) return (T)content.value;
92                 return (T)Simantics.applySCLRead(graph, content.expression, this);
93         }
94
95         @Override
96         public <T> T getValue(ReadGraph graph, Binding binding) throws DatabaseException {
97                 if(content.value != null) return (T)content.value;
98                 return (T)Simantics.applySCLRead(graph, content.expression, this);
99         }
100
101         @Override
102         public void setValue(WriteGraph graph, Object value, Binding binding) throws DatabaseException {
103                 throw new IllegalStateException();
104         }
105
106         @Override
107         public String getName(ReadGraph graph) throws DatabaseException {
108                 return content.pi.name;
109         }
110
111         @Override
112         public Variable getParent(ReadGraph graph) throws DatabaseException {
113                 return parent;
114         }
115
116         @Override
117         public Set<String> getClassifications(ReadGraph graph) throws DatabaseException {
118                 return content.pi.classifications;
119         }
120
121         @Override
122         protected Variable getPossibleDomainProperty(ReadGraph graph, String name) throws DatabaseException {
123
124                 if(content.properties == null)
125                         return null;
126
127                 ImmutableComponentPropertyContent p = content.properties.get(name);
128                 if(p != null)
129                         return new ImmutableComponentPropertyVariable(this, p);
130
131                 return null;
132
133         }
134
135         @Override
136         public Map<String, Variable> collectDomainProperties(ReadGraph graph, Map<String, Variable> map)
137                         throws DatabaseException {
138
139                 if(content.properties == null)
140                         return map;
141
142                 if(map == null)
143                         map = new HashMap<>();
144
145                 for(ImmutableComponentPropertyContent p : content.properties.values()) {
146                         map.put(p.pi.name, new ImmutableComponentPropertyVariable(this, p));
147                 }
148
149                 return map;
150
151         }
152
153 }