]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling/src/org/simantics/modeling/ImmutableComponentVariable.java
Multiple readers and variable optimization
[simantics/platform.git] / bundles / org.simantics.modeling / src / org / simantics / modeling / ImmutableComponentVariable.java
1 package org.simantics.modeling;
2
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.Collections;
6 import java.util.HashMap;
7 import java.util.Map;
8
9 import org.simantics.db.ReadGraph;
10 import org.simantics.db.Resource;
11 import org.simantics.db.exception.DatabaseException;
12 import org.simantics.db.layer0.variable.AbstractChildVariable;
13 import org.simantics.db.layer0.variable.StandardGraphChildVariable;
14 import org.simantics.db.layer0.variable.Variable;
15
16 public class ImmutableComponentVariable extends AbstractChildVariable {
17
18         @Override
19         public int hashCode() {
20                 final int prime = 31;
21                 int result = 1;
22                 result = prime * result + ((content == null) ? 0 : content.hashCode());
23                 result = prime * result + ((parent == null) ? 0 : parent.hashCode());
24                 return result;
25         }
26
27         @Override
28         public boolean equals(Object obj) {
29                 if (this == obj)
30                         return true;
31                 if (obj == null)
32                         return false;
33                 if (getClass() != obj.getClass())
34                         return false;
35                 ImmutableComponentVariable other = (ImmutableComponentVariable) obj;
36                 if (content == null) {
37                         if (other.content != null)
38                                 return false;
39                 } else if (!content.equals(other.content))
40                         return false;
41                 if (parent == null) {
42                         if (other.parent != null)
43                                 return false;
44                 } else if (!parent.equals(other.parent))
45                         return false;
46                 return true;
47         }
48
49         final private Variable parent;
50         final private ImmutableComponentVariableContent content;
51         
52         ImmutableComponentVariable(Variable parent, ImmutableComponentVariableContent content) {
53                 this.parent = parent;
54                 this.content = content;
55         }
56         
57         @Override
58         public String getName(ReadGraph graph) throws DatabaseException {
59                 return content.name;
60         }
61         
62         @Override
63         public Variable getParent(ReadGraph graph) throws DatabaseException {
64                 return parent;
65         }
66         
67         @Override
68         public Resource getRepresents(ReadGraph graph) throws DatabaseException {
69                 return content.resource;
70         }
71
72         @Override
73         public Variable getPossibleChild(ReadGraph graph, String name) throws DatabaseException {
74                 if(content.children == null) return null;
75                 ImmutableComponentVariableContent c = content.children.get(name);
76                 if(c == null) return null;
77                 return new ImmutableComponentVariable(this, c);
78         }
79         
80         @Override
81         public Collection<Variable> getChildren(ReadGraph graph) throws DatabaseException {
82                 if(content.children == null) return Collections.emptyList();
83                 ArrayList<Variable> result = new ArrayList<Variable>(content.children.size());
84                 for(ImmutableComponentVariableContent c : content.children.values()) {
85                         result.add(new ImmutableComponentVariable(this, c));
86                 }
87                 return result;
88         }
89         
90         @Override
91         protected Variable getPossibleDomainProperty(ReadGraph graph, String name) throws DatabaseException {
92
93                 if(content.properties == null)
94                         return null;
95
96                 ImmutableComponentPropertyContent p = content.properties.get(name);
97                 if(p != null)
98                         return new ImmutableComponentPropertyVariable(this, p);
99                 
100                 return new StandardGraphChildVariable(parent, node, content.resource).getPossibleProperty(graph, name);
101                 
102         }
103         
104         @Override
105         public Map<String, Variable> collectDomainProperties(ReadGraph graph, Map<String, Variable> map)
106                         throws DatabaseException {
107                 
108                 if(content.properties == null)
109                         return map;
110
111                 if(map == null)
112                         map = new HashMap<>();
113                 
114                 for(ImmutableComponentPropertyContent p : content.properties.values()) {
115                         map.put(p.pi.name, new ImmutableComponentPropertyVariable(this, p));
116                 }
117
118                 return map;
119                 
120         }
121         
122 }