]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.structural.synchronization/src/org/simantics/structural/synchronization/protocol/SerializedVariable.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.structural.synchronization / src / org / simantics / structural / synchronization / protocol / SerializedVariable.java
diff --git a/bundles/org.simantics.structural.synchronization/src/org/simantics/structural/synchronization/protocol/SerializedVariable.java b/bundles/org.simantics.structural.synchronization/src/org/simantics/structural/synchronization/protocol/SerializedVariable.java
new file mode 100644 (file)
index 0000000..f1888e1
--- /dev/null
@@ -0,0 +1,68 @@
+package org.simantics.structural.synchronization.protocol;\r
+\r
+import java.util.Collection;\r
+import java.util.Collections;\r
+import java.util.HashMap;\r
+import java.util.Map;\r
+\r
+import org.simantics.databoard.binding.mutable.Variant;\r
+\r
+public class SerializedVariable {\r
+\r
+       private Map<String,SerializedVariable> children;\r
+       private Map<String,SerializedVariable> properties;\r
+       final public Variant value;\r
+       final public String name;\r
+       \r
+       public SerializedVariable(String name, Variant value) {\r
+               this.name = name;\r
+               this.value = value;\r
+       }\r
+       \r
+       @SuppressWarnings("unchecked")\r
+       public <T> T getPossiblePropertyValue(String name) {\r
+               if(properties == null) return null;\r
+               SerializedVariable p = properties.get(name);\r
+               if(p == null) return null;\r
+               else return (T)p.value.getValue();\r
+       }\r
+\r
+       @SuppressWarnings("unchecked")\r
+       public <T> T getPropertyValue(String name) {\r
+               if(properties == null) throw new AssertionError("Property '" + name + "' does not exist.");\r
+               SerializedVariable p = properties.get(name);\r
+               if(p == null) throw new AssertionError("Property '" + name + "' does not exist.");\r
+               else return (T)p.value.getValue();\r
+       }\r
+       \r
+       public Collection<SerializedVariable> getProperties(){\r
+               if(properties == null) return Collections.emptyList();\r
+               return properties.values();\r
+       }\r
+       \r
+       public static String print(SerializedVariable var, int indent) {\r
+               StringBuilder b = new StringBuilder();\r
+               b.append(var.name + " " + var.value.getValue());\r
+               if(var.children != null) {\r
+                       for(Map.Entry<String, SerializedVariable> child : var.children.entrySet()) {\r
+                               b.append("\n");\r
+                               for(int i=0;i<indent;i++) b.append(" ");\r
+                               b.append("/" + print(child.getValue(), indent+2));\r
+                       }\r
+               }\r
+               if(var.properties != null) {\r
+                       for(Map.Entry<String, SerializedVariable> property : var.properties.entrySet()) {\r
+                               b.append("\n");\r
+                               for(int i=0;i<indent;i++) b.append(" ");\r
+                               b.append("#" + print(property.getValue(), indent+2));\r
+                       }\r
+               }\r
+               return b.toString();\r
+       }\r
+       \r
+       public void addProperty(String name, SerializedVariable property) {\r
+               if(properties == null) properties = new HashMap<String,SerializedVariable>();\r
+               properties.put(name, property);\r
+       }\r
+       \r
+}\r