1 package org.simantics.document.server.io;
3 import java.util.ArrayList;
4 import java.util.Collections;
5 import java.util.HashMap;
9 public class CommandContextImpl implements CommandContextMutable {
11 private Map<String,List<List<Object>>> data = new HashMap<String,List<List<Object>>>();
14 public boolean containsKey(String key) {
15 return data.containsKey(key);
19 public List<List<Object>> getRows(String key) {
20 List<List<Object>> rows = data.get(key);
24 return Collections.emptyList();
28 public CommandContextMutable putRow(String key, List<Object> row) {
29 List<List<Object>> rows = ensureRowsAvailable(key);
35 public String getString(String key) {
36 Object o = getValue(key);
41 public CommandContextMutable putString(String key, String value) {
42 return putValue(key, value);
45 private List<List<Object>> ensureRowsAvailable(String key) {
46 List<List<Object>> rows = data.get(key);
48 rows = new ArrayList<List<Object>>();
55 public Map<String, List<List<Object>>> getData() {
60 public List<String> getKeys() {
61 return new ArrayList<String>(data.keySet());
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());
76 public static CommandContextMutable create() {
77 return new CommandContextImpl();
81 public String toString() {
82 StringBuilder sb = new StringBuilder();
83 for (Map.Entry<String, List<List<Object>>> entry : data.entrySet()) {
84 sb.append(entry.getKey());
86 sb.append(entry.getValue());
93 public int hashCode() {
96 result = prime * result + ((data == null) ? 0 : data.hashCode());
101 public boolean equals(Object obj) {
106 if (getClass() != obj.getClass())
108 CommandContextImpl other = (CommandContextImpl) obj;
110 if (other.data != null)
112 } else if (!data.equals(other.data))
118 public CommandContextMutable putValue(String key, Object value) {
119 if(data.containsKey(key)) data.remove(key);
120 List<List<Object>> rows = ensureRowsAvailable(key);
121 List<Object> t = new ArrayList<Object>();
129 public <T> T getValue(String key) {
130 List<List<Object>> rows = getRows(key);
131 if((rows == null) || (rows.size() != 1)) return null;
132 List<Object> t = (List<Object>)rows.get(0);
133 if(t.size() != 2) return null;
134 @SuppressWarnings("unchecked")