diff options
Diffstat (limited to 'same/src')
-rw-r--r-- | same/src/main/java/com/orbekk/same/State.java | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/same/src/main/java/com/orbekk/same/State.java b/same/src/main/java/com/orbekk/same/State.java index 70ffa83..6b7c824 100644 --- a/same/src/main/java/com/orbekk/same/State.java +++ b/same/src/main/java/com/orbekk/same/State.java @@ -4,12 +4,69 @@ import java.util.Map; import java.util.HashMap; public class State { + private Map<String, Component> state = new HashMap<String, Component>(); + private long stateIteration = 0; private Map<String, String> participants = new HashMap<String, String>(); private String networkName = ""; private String masterId = ""; private String data = ""; + public boolean update(String componentName, String data, long revision) { + Component component = null; + if (!state.containsKey(componentName)) { + component = new Component(0, ""); + } else { + component = state.get(componentName); + } + + if (revision == component.getRevision()) { + component.setRevision(revision + 1); + component.setData(data); + state.put(componentName, component); + return true; + } else { + return false; + } + } + + public String getDataOf(String componentName) { + Component component = state.get(componentName); + if (component != null) { + return component.getData(); + } else { + return null; + } + } + + public static class Component { + private long revision; + private String data; + + public Component(long revision, String data) { + this.revision = revision; + this.data = data; + } + + public long getRevision() { + return revision; + } + public void setRevision(long revision) { + this.revision = revision; + } + + public String getData() { + return data; + } + public void setData(String data) { + this.data = data; + } + + @Override public String toString() { + return this.data + " @" + revision; + } + } + public long getStateIteration() { return stateIteration; } |