1 package org.simantics.structural.synchronization.protocol;
\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
8 import org.simantics.databoard.binding.mutable.Variant;
\r
10 public class SerializedVariable {
\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
17 public SerializedVariable(String name, Variant value) {
\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
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
38 public Collection<SerializedVariable> getProperties(){
\r
39 if(properties == null) return Collections.emptyList();
\r
40 return properties.values();
\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
49 for(int i=0;i<indent;i++) b.append(" ");
\r
50 b.append("/" + print(child.getValue(), indent+2));
\r
53 if(var.properties != null) {
\r
54 for(Map.Entry<String, SerializedVariable> property : var.properties.entrySet()) {
\r
56 for(int i=0;i<indent;i++) b.append(" ");
\r
57 b.append("#" + print(property.getValue(), indent+2));
\r
60 return b.toString();
\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