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