]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.document.server.io/src/org/simantics/document/server/io/CommandContextImpl.java
5b49810fad9f8109c5a4a637ab24444ea3bf059c
[simantics/platform.git] / bundles / org.simantics.document.server.io / src / org / simantics / document / server / io / CommandContextImpl.java
1 package org.simantics.document.server.io;
2
3 import java.util.ArrayList;
4 import java.util.Collections;
5 import java.util.HashMap;
6 import java.util.List;
7 import java.util.Map;
8
9 public class CommandContextImpl implements CommandContextMutable {
10
11         private Map<String,List<List<Object>>> data = new HashMap<String,List<List<Object>>>();
12
13         @Override
14         public boolean containsKey(String key) {
15                 return data.containsKey(key);
16         }
17         
18         @Override
19         public List<List<Object>> getRows(String key) {
20                 List<List<Object>> rows = data.get(key);
21                 if (rows != null) 
22                         return rows;
23                 else
24                         return Collections.emptyList();
25         }
26
27         @Override
28         public CommandContextMutable putRow(String key, List<Object> row) {
29                 List<List<Object>> rows = ensureRowsAvailable(key);
30                 rows.add(row);
31                 return this;
32         }
33
34         @Override
35         public String getString(String key) {
36                 Object o = getValue(key);
37                 return (String)o;
38         }
39         
40         @Override
41         public CommandContextMutable putString(String key, String value) {
42             return putValue(key, value);
43         }
44
45         private List<List<Object>> ensureRowsAvailable(String key) {
46                 List<List<Object>> rows = data.get(key);
47                 if (rows == null) {
48                         rows = new ArrayList<List<Object>>();
49                         data.put(key, rows);
50                 }
51                 return rows;
52         }
53
54         @Override
55         public Map<String, List<List<Object>>> getData() {
56                 return data;
57         }
58         
59         @Override
60         public List<String> getKeys() {
61                 return new ArrayList<String>(data.keySet());
62         }
63         
64         @Override
65         public CommandContextMutable merge(CommandContext context) {
66                 if (context != null) {
67                         Map<String,List<List<Object>>> from = context.getData(); 
68                         for (Map.Entry<String, List<List<Object>>> entry : from.entrySet()) {
69                                 List<List<Object>> rows = ensureRowsAvailable(entry.getKey());
70                                 rows.addAll(entry.getValue());
71                         }
72                 }
73                 return this;
74         }
75         
76         public static CommandContextMutable create() {
77                 return new CommandContextImpl();
78         }
79         
80         @Override
81         public String toString() {
82                 StringBuilder sb = new StringBuilder();
83                 sb.append("CommandContext:\n");
84                 for (Map.Entry<String, List<List<Object>>> entry : data.entrySet()) {
85                         String key = entry.getKey();
86                         if(key.startsWith("__")) continue;
87                         sb.append(key);
88                         sb.append(":");
89                         List<List<Object>> value = entry.getValue();
90                         if(value.size() == 1) {
91                                 List<Object> t = (List<Object>)value.get(0);
92                                 if(t.size() == 2) {
93                                         sb.append(t.get(1));
94                                 } else {
95                                         sb.append(t);
96                                 }
97                         } else {
98                                 sb.append(value);
99                         }
100                         sb.append("\n");
101                 }
102                 return sb.toString();
103         }
104         
105         @Override
106         public int hashCode() {
107                 final int prime = 31;
108                 int result = 1;
109                 result = prime * result + ((data == null) ? 0 : data.hashCode());
110                 return result;
111         }
112
113         @Override
114         public boolean equals(Object obj) {
115                 if (this == obj)
116                         return true;
117                 if (obj == null)
118                         return false;
119                 if (getClass() != obj.getClass())
120                         return false;
121                 CommandContextImpl other = (CommandContextImpl) obj;
122                 if (data == null) {
123                         if (other.data != null)
124                                 return false;
125                 } else if (!data.equals(other.data))
126                         return false;
127                 return true;
128         }
129
130     @Override
131     public CommandContextMutable putValue(String key, Object value) {
132         if(data.containsKey(key)) data.remove(key);
133         List<List<Object>> rows = ensureRowsAvailable(key);
134         List<Object> t = new ArrayList<Object>();
135         t.add(key);
136         t.add(value);
137         rows.add(t);
138         return this;
139     }
140     
141     @Override
142     public <T> T getValue(String key) {
143         List<List<Object>> rows = getRows(key);
144         if((rows == null) || (rows.size() != 1)) return null;
145         List<Object> t = (List<Object>)rows.get(0);
146         if(t.size() != 2) return null;
147         @SuppressWarnings("unchecked")
148         T o = (T) t.get(1);
149         return o;
150     }
151 }