summaryrefslogtreecommitdiff
path: root/same/src/main/java/com
diff options
context:
space:
mode:
authorKjetil Ørbekk <kjetil.orbekk@gmail.com>2012-02-23 14:18:12 +0100
committerKjetil Ørbekk <kjetil.orbekk@gmail.com>2012-02-23 14:18:12 +0100
commit6a893a7ba5563774e66519988398b67b06bcd8b0 (patch)
treee983e464b995a6ac5287d95f18c62c2bdc6797c3 /same/src/main/java/com
parentef8563e3106f1774b05fe5ea94f538e9983b0182 (diff)
Implement listeners in Variables.
Diffstat (limited to 'same/src/main/java/com')
-rw-r--r--same/src/main/java/com/orbekk/same/VariableFactory.java21
1 files changed, 17 insertions, 4 deletions
diff --git a/same/src/main/java/com/orbekk/same/VariableFactory.java b/same/src/main/java/com/orbekk/same/VariableFactory.java
index 87db576..49604c6 100644
--- a/same/src/main/java/com/orbekk/same/VariableFactory.java
+++ b/same/src/main/java/com/orbekk/same/VariableFactory.java
@@ -9,6 +9,8 @@ import org.codehaus.jackson.type.TypeReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import com.orbekk.same.State.Component;
+
/**
* TODO: Use WeakReference in order to make variables GC-able.
*/
@@ -17,11 +19,12 @@ public class VariableFactory {
private Client.ClientInterface client;
private ObjectMapper mapper = new ObjectMapper();
- private class VariableImpl<T> implements Variable<T> {
+ private class VariableImpl<T> implements Variable<T>, StateChangedListener {
String identifier;
TypeReference<T> type;
T value;
long revision = 0;
+ OnChangeListener<T> listener = null;
public VariableImpl(String identifier, TypeReference<T> type) {
this.identifier = identifier;
@@ -59,8 +62,17 @@ public class VariableFactory {
}
@Override
- public void setOnChangeListener(Variable.OnChangeListener<T> listener) {
- throw new RuntimeException("Not implemented.");
+ public void setOnChangeListener(OnChangeListener<T> listener) {
+ this.listener = listener;
+ }
+
+ @Override
+ public void stateChanged(Component component) {
+ if (component.getName().equals(identifier)) {
+ if (listener != null) {
+ listener.valueChanged(this);
+ }
+ }
}
}
@@ -73,8 +85,9 @@ public class VariableFactory {
}
public <T> Variable<T> create(String identifier, TypeReference<T> type) {
- Variable<T> variable = new VariableImpl<T>(identifier, type);
+ VariableImpl<T> variable = new VariableImpl<T>(identifier, type);
variable.update();
+ client.addStateListener(variable);
return variable;
}