summaryrefslogtreecommitdiff
path: root/same/src/main/java
diff options
context:
space:
mode:
authorKjetil Ørbekk <kjetil.orbekk@gmail.com>2012-01-13 11:24:39 +0100
committerKjetil Ørbekk <kjetil.orbekk@gmail.com>2012-01-13 11:24:39 +0100
commita3389e3e7782f56650760b07bb2f452e78406459 (patch)
tree1d8e6e82215de20b0e305812fd3fc3d2d7b34bfd /same/src/main/java
parentc033e0a7e8c224020fbb2e9735a7b2f6de0e1af6 (diff)
New object model in State.
– Currently mixed with old code. TODO: Remove old code.
Diffstat (limited to 'same/src/main/java')
-rw-r--r--same/src/main/java/com/orbekk/same/State.java57
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;
}