--- /dev/null
+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