]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/variable/ProxyChildVariable.java
89664698ac790a5a4c77baf8f16a26639ec309fc
[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         System.err.println("other: " + other.getURI(graph) + " " + name );
52         Variable otherChild = other.getPossibleChild(graph, name);
53         if(otherChild == null) return null;
54         return create(base, this, otherChild, name);
55     }
56     
57     public Collection<Variable> getChildren(ReadGraph graph) throws DatabaseException {
58         
59         ArrayList<Variable> result = new ArrayList<Variable>();
60         Collection<Variable> otherChildren = other.browseChildren(graph);
61         for(Variable o : otherChildren) {
62                 result.add(create(base, this, o, o.getName(graph)));
63         }
64         return result;
65         
66     }
67     
68     public Variable getPossibleProperty(ReadGraph graph, String name) throws DatabaseException {
69         Variable otherChild = other.getPossibleProperty(graph, name);
70         if(otherChild == null) return null;
71         return create(base, this, otherChild, name);
72     }
73
74     public Variable create(Variable base, Variable parent, Variable other, String name) {
75         return new ProxyChildVariable(base, parent, other, name);
76     }
77
78         @Override
79         public int hashCode() {
80                 final int prime = 31;
81                 int result = 1;
82                 result = prime * result + (base != null ? base.hashCode() : 0);
83                 result = prime * result + (parent != null ? parent.hashCode() : 0);
84                 result = prime * result + (other != null ? other.hashCode() : 0);
85                 result = prime * result + (name != null ? name.hashCode() : 0);
86                 return result;
87         }
88     
89         @Override
90         public boolean equals(Object obj) {
91                 
92                 if (this == obj)
93                         return true;
94                 if (obj == null)
95                         return false;
96                 if (getClass() != obj.getClass())
97                         return false;
98                 
99                 ProxyChildVariable proxy = (ProxyChildVariable) obj;
100                 
101         if(base != null) {
102                 if(!base.equals(proxy.base)) return false;
103         } else if (proxy.base != null) return false;
104         
105         if(parent != null) {
106                 if(!parent.equals(proxy.parent)) return false;
107         } else if (proxy.parent != null) return false;
108
109         if(other != null) {
110                 if(!other.equals(proxy.other)) return false;
111         } else if (proxy.other != null) return false;
112
113         if(name != null) {
114                 if(!name.equals(proxy.name)) return false;
115         } else if (proxy.name != null) return false;
116
117         return true;
118                 
119         }
120     
121 }