]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.structural2/src/org/simantics/structural2/variables/PairConnectionDescriptor.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.structural2 / src / org / simantics / structural2 / variables / PairConnectionDescriptor.java
diff --git a/bundles/org.simantics.structural2/src/org/simantics/structural2/variables/PairConnectionDescriptor.java b/bundles/org.simantics.structural2/src/org/simantics/structural2/variables/PairConnectionDescriptor.java
new file mode 100644 (file)
index 0000000..0273dbd
--- /dev/null
@@ -0,0 +1,80 @@
+package org.simantics.structural2.variables;\r
+\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.common.procedure.adapter.TransientCacheAsyncListener;\r
+import org.simantics.db.common.request.UnaryRead;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.layer0.variable.Variable;\r
+import org.simantics.utils.datastructures.Pair;\r
+\r
+public class PairConnectionDescriptor extends AbstractVariableConnectionPointDescriptor {\r
+       \r
+       public Variable base;\r
+       public Pair<String,Resource> pair;\r
+       \r
+       public PairConnectionDescriptor(Variable base, Pair<String,Resource> pair) {\r
+               this.base = base;\r
+               this.pair = pair;\r
+       }\r
+       @Override\r
+       public int hashCode() {\r
+               return base.hashCode() + 31*pair.hashCode();\r
+       }\r
+       @Override\r
+       public boolean equals(Object object) {\r
+        if (this == object)\r
+            return true;\r
+        else if (object == null)\r
+            return false;\r
+        else if (!(object instanceof PairConnectionDescriptor))\r
+            return false;\r
+        PairConnectionDescriptor r = (PairConnectionDescriptor)object;\r
+        if(!r.pair.equals(pair)) return false;\r
+        if(!r.base.equals(base)) return false;\r
+        return true;\r
+       }\r
+       \r
+       static class ComputeVariable extends UnaryRead<PairConnectionDescriptor, Variable> {\r
+\r
+               public ComputeVariable(PairConnectionDescriptor desc) {\r
+                       super(desc);\r
+               }\r
+\r
+               @Override\r
+               public Variable perform(ReadGraph graph) throws DatabaseException {\r
+                       return parameter.base.getChild(graph, parameter.pair.first).getProperty(graph, parameter.pair.second);\r
+               }\r
+               \r
+       }\r
+       @Override\r
+       public Variable getVariable(ReadGraph graph) throws DatabaseException {\r
+               return graph.syncRequest(new ComputeVariable(this), TransientCacheAsyncListener.<Variable>instance());\r
+       }\r
+       public String getURI(ReadGraph graph) throws DatabaseException {\r
+               return getVariable(graph).getURI(graph);\r
+       }\r
+       @Override\r
+       public Resource getConnectionPointResource(ReadGraph graph) throws DatabaseException {\r
+               return getVariable(graph).getPossiblePredicateResource(graph);\r
+       }\r
+       \r
+       @Override\r
+       public boolean isFlattenedFrom(ReadGraph graph, Variable possiblyStructuralCp) throws DatabaseException {\r
+               \r
+               Variable parent = possiblyStructuralCp.getParent(graph);\r
+               \r
+               String descURI = getURI(graph);\r
+               String parentURI = parent.getURI(graph);\r
+               if(descURI.startsWith(parentURI)) {\r
+                       // Children are in substructure\r
+                       if('/' == descURI.charAt(parentURI.length())) return true;\r
+                       // Otherwise require exact match\r
+                       return descURI.endsWith(possiblyStructuralCp.getName(graph));\r
+               } else {\r
+                       return false;\r
+               }\r
+               \r
+       }\r
+       \r
+}
\ No newline at end of file