diff options
author | Kjetil Ørbekk <kjetil.orbekk@gmail.com> | 2012-01-13 13:04:29 +0100 |
---|---|---|
committer | Kjetil Ørbekk <kjetil.orbekk@gmail.com> | 2012-01-13 13:04:29 +0100 |
commit | 6ec163993d1baf4ebcddde287c0ade7fb4cf20c6 (patch) | |
tree | c999160ce62db4900ba58c5083bd2c636607664f | |
parent | 008f40dcd76f7eedc0360f94dd42d2d12e3a8793 (diff) |
Maintain a set of recently updated components in State.
-rw-r--r-- | same/src/main/java/com/orbekk/same/State.java | 28 |
1 files changed, 27 insertions, 1 deletions
diff --git a/same/src/main/java/com/orbekk/same/State.java b/same/src/main/java/com/orbekk/same/State.java index 303caca..49ee8c4 100644 --- a/same/src/main/java/com/orbekk/same/State.java +++ b/same/src/main/java/com/orbekk/same/State.java @@ -5,6 +5,8 @@ import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.HashMap; +import java.util.Set; +import java.util.TreeSet; import org.codehaus.jackson.JsonGenerationException; import org.codehaus.jackson.JsonParseException; @@ -14,11 +16,26 @@ import org.codehaus.jackson.type.TypeReference; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +/** + * This class is not thread-safe. In particular, modifications to this class + * while getUpdatedComponents() is being used is unsafe. When accessing + * updatedComponents, synchronize on State: + * + * <code> + * synchronize (state) { + * for (String component : state.updatedComponents()) { + * // Do work. + * } + * state.clearUpdatedComponents(); + * } + * </code> + */ public class State { private Logger logger = LoggerFactory.getLogger(getClass()); private Map<String, Component> state = new HashMap<String, Component>(); private ObjectMapper mapper = new ObjectMapper(); - + private Set<String> updatedComponents = new TreeSet<String>(); + public State(String networkName) { update(".networkName", networkName, 0); updateFromObject(".participants", new ArrayList<String>(), 0); @@ -36,6 +53,7 @@ public class State { component.setRevision(revision + 1); component.setData(data); state.put(componentName, component); + updatedComponents.add(componentName); return true; } else { return false; @@ -110,6 +128,14 @@ public class State { } } + public Set<String> getUpdatedComponents() { + return updatedComponents; + } + + public void clearUpdatedComponents() { + this.updatedComponents.clear(); + } + public static class Component { private long revision; private String data; |