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