]> gerrit.simantics Code Review - simantics/platform.git/blob - 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
1 package org.simantics.structural.synchronization.protocol;\r
2 \r
3 import java.util.Collection;\r
4 import java.util.Collections;\r
5 import java.util.HashMap;\r
6 import java.util.Map;\r
7 \r
8 import org.simantics.databoard.binding.mutable.Variant;\r
9 \r
10 public class SerializedVariable {\r
11 \r
12         private Map<String,SerializedVariable> children;\r
13         private Map<String,SerializedVariable> properties;\r
14         final public Variant value;\r
15         final public String name;\r
16         \r
17         public SerializedVariable(String name, Variant value) {\r
18                 this.name = name;\r
19                 this.value = value;\r
20         }\r
21         \r
22         @SuppressWarnings("unchecked")\r
23         public <T> T getPossiblePropertyValue(String name) {\r
24                 if(properties == null) return null;\r
25                 SerializedVariable p = properties.get(name);\r
26                 if(p == null) return null;\r
27                 else return (T)p.value.getValue();\r
28         }\r
29 \r
30         @SuppressWarnings("unchecked")\r
31         public <T> T getPropertyValue(String name) {\r
32                 if(properties == null) throw new AssertionError("Property '" + name + "' does not exist.");\r
33                 SerializedVariable p = properties.get(name);\r
34                 if(p == null) throw new AssertionError("Property '" + name + "' does not exist.");\r
35                 else return (T)p.value.getValue();\r
36         }\r
37         \r
38         public Collection<SerializedVariable> getProperties(){\r
39                 if(properties == null) return Collections.emptyList();\r
40                 return properties.values();\r
41         }\r
42         \r
43         public static String print(SerializedVariable var, int indent) {\r
44                 StringBuilder b = new StringBuilder();\r
45                 b.append(var.name + " " + var.value.getValue());\r
46                 if(var.children != null) {\r
47                         for(Map.Entry<String, SerializedVariable> child : var.children.entrySet()) {\r
48                                 b.append("\n");\r
49                                 for(int i=0;i<indent;i++) b.append(" ");\r
50                                 b.append("/" + print(child.getValue(), indent+2));\r
51                         }\r
52                 }\r
53                 if(var.properties != null) {\r
54                         for(Map.Entry<String, SerializedVariable> property : var.properties.entrySet()) {\r
55                                 b.append("\n");\r
56                                 for(int i=0;i<indent;i++) b.append(" ");\r
57                                 b.append("#" + print(property.getValue(), indent+2));\r
58                         }\r
59                 }\r
60                 return b.toString();\r
61         }\r
62         \r
63         public void addProperty(String name, SerializedVariable property) {\r
64                 if(properties == null) properties = new HashMap<String,SerializedVariable>();\r
65                 properties.put(name, property);\r
66         }\r
67         \r
68 }\r