X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.document.server.io%2Fsrc%2Forg%2Fsimantics%2Fdocument%2Fserver%2Fio%2FCommandContextImpl.java;fp=bundles%2Forg.simantics.document.server.io%2Fsrc%2Forg%2Fsimantics%2Fdocument%2Fserver%2Fio%2FCommandContextImpl.java;h=3277bbd1edfe02440a1b944f7f6c192c4808875c;hp=4230d1018a8648f6a6ce3816d4b3515491313fbd;hb=0ae2b770234dfc3cbb18bd38f324125cf0faca07;hpb=24e2b34260f219f0d1644ca7a138894980e25b14 diff --git a/bundles/org.simantics.document.server.io/src/org/simantics/document/server/io/CommandContextImpl.java b/bundles/org.simantics.document.server.io/src/org/simantics/document/server/io/CommandContextImpl.java index 4230d1018..3277bbd1e 100644 --- a/bundles/org.simantics.document.server.io/src/org/simantics/document/server/io/CommandContextImpl.java +++ b/bundles/org.simantics.document.server.io/src/org/simantics/document/server/io/CommandContextImpl.java @@ -1,138 +1,138 @@ -package org.simantics.document.server.io; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -public class CommandContextImpl implements CommandContextMutable { - - private Map>> data = new HashMap>>(); - - @Override - public boolean containsKey(String key) { - return data.containsKey(key); - } - - @Override - public List> getRows(String key) { - List> rows = data.get(key); - if (rows != null) - return rows; - else - return Collections.emptyList(); - } - - @Override - public CommandContextMutable putRow(String key, List row) { - List> rows = ensureRowsAvailable(key); - rows.add(row); - return this; - } - - @Override - public String getString(String key) { - Object o = getValue(key); - return (String)o; - } - - @Override - public CommandContextMutable putString(String key, String value) { - return putValue(key, value); - } - - private List> ensureRowsAvailable(String key) { - List> rows = data.get(key); - if (rows == null) { - rows = new ArrayList>(); - data.put(key, rows); - } - return rows; - } - - @Override - public Map>> getData() { - return data; - } - - @Override - public List getKeys() { - return new ArrayList(data.keySet()); - } - - @Override - public CommandContextMutable merge(CommandContext context) { - if (context != null) { - Map>> from = context.getData(); - for (Map.Entry>> entry : from.entrySet()) { - List> rows = ensureRowsAvailable(entry.getKey()); - rows.addAll(entry.getValue()); - } - } - return this; - } - - public static CommandContextMutable create() { - return new CommandContextImpl(); - } - - @Override - public String toString() { - StringBuilder sb = new StringBuilder(); - for (Map.Entry>> entry : data.entrySet()) { - sb.append(entry.getKey()); - sb.append(":"); - sb.append(entry.getValue()); - sb.append("\n"); - } - return sb.toString(); - } - - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + ((data == null) ? 0 : data.hashCode()); - return result; - } - - @Override - public boolean equals(Object obj) { - if (this == obj) - return true; - if (obj == null) - return false; - if (getClass() != obj.getClass()) - return false; - CommandContextImpl other = (CommandContextImpl) obj; - if (data == null) { - if (other.data != null) - return false; - } else if (!data.equals(other.data)) - return false; - return true; - } - - @Override - public CommandContextMutable putValue(String key, Object value) { - if(data.containsKey(key)) data.remove(key); - List> rows = ensureRowsAvailable(key); - List t = new ArrayList(); - t.add(key); - t.add(value); - rows.add(t); - return this; - } - - @Override - public T getValue(String key) { - List> rows = getRows(key); - if((rows == null) || (rows.size() != 1)) return null; - List t = (List)rows.get(0); - if(t.size() != 2) return null; - @SuppressWarnings("unchecked") - T o = (T) t.get(1); - return o; - } +package org.simantics.document.server.io; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class CommandContextImpl implements CommandContextMutable { + + private Map>> data = new HashMap>>(); + + @Override + public boolean containsKey(String key) { + return data.containsKey(key); + } + + @Override + public List> getRows(String key) { + List> rows = data.get(key); + if (rows != null) + return rows; + else + return Collections.emptyList(); + } + + @Override + public CommandContextMutable putRow(String key, List row) { + List> rows = ensureRowsAvailable(key); + rows.add(row); + return this; + } + + @Override + public String getString(String key) { + Object o = getValue(key); + return (String)o; + } + + @Override + public CommandContextMutable putString(String key, String value) { + return putValue(key, value); + } + + private List> ensureRowsAvailable(String key) { + List> rows = data.get(key); + if (rows == null) { + rows = new ArrayList>(); + data.put(key, rows); + } + return rows; + } + + @Override + public Map>> getData() { + return data; + } + + @Override + public List getKeys() { + return new ArrayList(data.keySet()); + } + + @Override + public CommandContextMutable merge(CommandContext context) { + if (context != null) { + Map>> from = context.getData(); + for (Map.Entry>> entry : from.entrySet()) { + List> rows = ensureRowsAvailable(entry.getKey()); + rows.addAll(entry.getValue()); + } + } + return this; + } + + public static CommandContextMutable create() { + return new CommandContextImpl(); + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + for (Map.Entry>> entry : data.entrySet()) { + sb.append(entry.getKey()); + sb.append(":"); + sb.append(entry.getValue()); + sb.append("\n"); + } + return sb.toString(); + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((data == null) ? 0 : data.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + CommandContextImpl other = (CommandContextImpl) obj; + if (data == null) { + if (other.data != null) + return false; + } else if (!data.equals(other.data)) + return false; + return true; + } + + @Override + public CommandContextMutable putValue(String key, Object value) { + if(data.containsKey(key)) data.remove(key); + List> rows = ensureRowsAvailable(key); + List t = new ArrayList(); + t.add(key); + t.add(value); + rows.add(t); + return this; + } + + @Override + public T getValue(String key) { + List> rows = getRows(key); + if((rows == null) || (rows.size() != 1)) return null; + List t = (List)rows.get(0); + if(t.size() != 2) return null; + @SuppressWarnings("unchecked") + T o = (T) t.get(1); + return o; + } } \ No newline at end of file