summaryrefslogtreecommitdiff
path: root/same
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
parentef8563e3106f1774b05fe5ea94f538e9983b0182 (diff)
Implement listeners in Variables.
Diffstat (limited to 'same')
-rw-r--r--same/src/main/java/com/orbekk/same/VariableFactory.java21
-rw-r--r--same/src/test/java/com/orbekk/same/VariableFactoryTest.java18
2 files changed, 35 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;
}
diff --git a/same/src/test/java/com/orbekk/same/VariableFactoryTest.java b/same/src/test/java/com/orbekk/same/VariableFactoryTest.java
index 7d20fa7..a6efa4b 100644
--- a/same/src/test/java/com/orbekk/same/VariableFactoryTest.java
+++ b/same/src/test/java/com/orbekk/same/VariableFactoryTest.java
@@ -59,4 +59,22 @@ public class VariableFactoryTest {
string.set("NewValue");
verify(client).set("X", "\"NewValue\"", 0);
}
+
+ @Test
+ public void addsListener() throws Exception {
+ Variable<String> v = vf.create("X", stringType);
+ verify(client).addStateListener((StateChangedListener)v);
+ }
+
+ @Test
+ public void listenerNotifies() throws Exception {
+ @SuppressWarnings("unchecked")
+ Variable.OnChangeListener<Integer> listener =
+ mock(Variable.OnChangeListener.class);
+ Variable<Integer> v = vf.create("z", intType);
+ v.setOnChangeListener(listener);
+ ((StateChangedListener) v).stateChanged(
+ new State.Component("z", 1, "abc"));
+ verify(listener).valueChanged(v);
+ }
}