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