1 package org.simantics.structural.synchronization.protocol;
3 import java.util.Collection;
4 import java.util.Collections;
5 import java.util.HashMap;
8 import org.simantics.databoard.binding.mutable.Variant;
10 public class SerializedVariable {
12 private Map<String,SerializedVariable> children;
13 private Map<String,SerializedVariable> properties;
14 final public Variant value;
15 final public String name;
17 public SerializedVariable(String name, Variant value) {
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();
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();
38 public Collection<SerializedVariable> getProperties(){
39 if(properties == null) return Collections.emptyList();
40 return properties.values();
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()) {
49 for(int i=0;i<indent;i++) b.append(" ");
50 b.append("/" + print(child.getValue(), indent+2));
53 if(var.properties != null) {
54 for(Map.Entry<String, SerializedVariable> property : var.properties.entrySet()) {
56 for(int i=0;i<indent;i++) b.append(" ");
57 b.append("#" + print(property.getValue(), indent+2));
63 public void addProperty(String name, SerializedVariable property) {
64 if(properties == null) properties = new HashMap<String,SerializedVariable>();
65 properties.put(name, property);