]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/variable/ProxyChildVariable.java
Merge commit 'ad8333027322fda6b9a8a524c7a7e15a54c52f38'
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / variable / ProxyChildVariable.java
1 package org.simantics.db.layer0.variable;\r
2 \r
3 import java.util.ArrayList;\r
4 import java.util.Collection;\r
5 \r
6 import org.simantics.db.ReadGraph;\r
7 import org.simantics.db.exception.DatabaseException;\r
8 \r
9 public class ProxyChildVariable extends AbstractChildVariable {\r
10 \r
11         public static final String CONTEXT_BEGIN = ">>>";\r
12         public static final String CONTEXT_END = "<<<";\r
13 \r
14         // This is the variable that shall be returned to\r
15         protected final Variable base;\r
16         // Actual parent of this variable\r
17         protected final Variable parent;\r
18         // This is the accumulated context variable that is queries for children\r
19         protected final Variable other;\r
20         protected final String name;\r
21         \r
22         public ProxyChildVariable(Variable base, Variable parent, Variable other, String name) {\r
23                 assert(base != null);\r
24                 assert(parent != null);\r
25                 assert(other != null);\r
26                 assert(name != null);\r
27                 this.base = base;\r
28                 this.parent = parent;\r
29                 this.other = other;\r
30                 this.name = name;\r
31         }\r
32         \r
33         public Variable other() {\r
34                 return other;\r
35         }\r
36         \r
37         public Variable base() {\r
38                 return base;\r
39         }\r
40         \r
41         @Override\r
42         public String getName(ReadGraph graph) throws DatabaseException {\r
43                 return name;\r
44         }\r
45         @Override\r
46         public Variable getParent(ReadGraph graph) throws DatabaseException {\r
47                 return parent;\r
48         }\r
49         \r
50     public Variable getPossibleChild(ReadGraph graph, String name) throws DatabaseException {\r
51         Variable otherChild = other.getPossibleChild(graph, name);\r
52         if(otherChild == null) return null;\r
53         return create(base, this, otherChild, name);\r
54     }\r
55     \r
56     public Collection<Variable> getChildren(ReadGraph graph) throws DatabaseException {\r
57         \r
58         ArrayList<Variable> result = new ArrayList<Variable>();\r
59         Collection<Variable> otherChildren = other.browseChildren(graph);\r
60         for(Variable o : otherChildren) {\r
61                 result.add(create(base, this, o, o.getName(graph)));\r
62         }\r
63         return result;\r
64         \r
65     }\r
66     \r
67     public Variable getPossibleProperty(ReadGraph graph, String name) throws DatabaseException {\r
68         Variable otherChild = other.getPossibleProperty(graph, name);\r
69         if(otherChild == null) return null;\r
70         return create(base, this, otherChild, name);\r
71     }\r
72 \r
73     public Variable create(Variable base, Variable parent, Variable other, String name) {\r
74         return new ProxyChildVariable(base, parent, other, name);\r
75     }\r
76 \r
77         @Override\r
78         public int hashCode() {\r
79                 final int prime = 31;\r
80                 int result = 1;\r
81                 result = prime * result + (base != null ? base.hashCode() : 0);\r
82                 result = prime * result + (parent != null ? parent.hashCode() : 0);\r
83                 result = prime * result + (other != null ? other.hashCode() : 0);\r
84                 result = prime * result + (name != null ? name.hashCode() : 0);\r
85                 return result;\r
86         }\r
87     \r
88         @Override\r
89         public boolean equals(Object obj) {\r
90                 \r
91                 if (this == obj)\r
92                         return true;\r
93                 if (obj == null)\r
94                         return false;\r
95                 if (getClass() != obj.getClass())\r
96                         return false;\r
97                 \r
98                 ProxyChildVariable proxy = (ProxyChildVariable) obj;\r
99                 \r
100         if(base != null) {\r
101                 if(!base.equals(proxy.base)) return false;\r
102         } else if (proxy.base != null) return false;\r
103         \r
104         if(parent != null) {\r
105                 if(!parent.equals(proxy.parent)) return false;\r
106         } else if (proxy.parent != null) return false;\r
107 \r
108         if(other != null) {\r
109                 if(!other.equals(proxy.other)) return false;\r
110         } else if (proxy.other != null) return false;\r
111 \r
112         if(name != null) {\r
113                 if(!name.equals(proxy.name)) return false;\r
114         } else if (proxy.name != null) return false;\r
115 \r
116         return true;\r
117                 \r
118         }\r
119     \r
120 }\r