summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--same/src/main/java/com/orbekk/same/SameController.java4
-rw-r--r--same/src/main/java/com/orbekk/same/Variable.java17
-rw-r--r--same/src/main/java/com/orbekk/same/VariableFactory.java54
-rw-r--r--same/src/test/java/com/orbekk/same/VariableFactoryTest.java52
4 files changed, 127 insertions, 0 deletions
diff --git a/same/src/main/java/com/orbekk/same/SameController.java b/same/src/main/java/com/orbekk/same/SameController.java
index 8cc37fe..58dfc6f 100644
--- a/same/src/main/java/com/orbekk/same/SameController.java
+++ b/same/src/main/java/com/orbekk/same/SameController.java
@@ -148,4 +148,8 @@ public class SameController {
public Master getMaster() {
return master;
}
+
+ public VariableFactory createVariableFactory() {
+ return new VariableFactory(client.getInterface());
+ }
}
diff --git a/same/src/main/java/com/orbekk/same/Variable.java b/same/src/main/java/com/orbekk/same/Variable.java
new file mode 100644
index 0000000..63f8063
--- /dev/null
+++ b/same/src/main/java/com/orbekk/same/Variable.java
@@ -0,0 +1,17 @@
+package com.orbekk.same;
+
+public interface Variable<T> {
+ public interface OnChangeListener<T> {
+ /**
+ * A notification that 'variable' has been changed.
+ *
+ * The user must run variable.update() to get the updated value.
+ */
+ void valueChanged(Variable<T> variable);
+ }
+
+ T get();
+ void set(T value);
+ void update();
+ void setOnChangeListener(OnChangeListener<T> listener);
+}
diff --git a/same/src/main/java/com/orbekk/same/VariableFactory.java b/same/src/main/java/com/orbekk/same/VariableFactory.java
new file mode 100644
index 0000000..81b7615
--- /dev/null
+++ b/same/src/main/java/com/orbekk/same/VariableFactory.java
@@ -0,0 +1,54 @@
+package com.orbekk.same;
+
+import org.codehaus.jackson.type.TypeReference;
+
+/**
+ * TODO: Use WeakReference in order to make variables GC-able.
+ */
+public class VariableFactory {
+ Client.ClientInterface client;
+
+ private class VariableImpl<T> implements Variable<T> {
+ String identifier;
+ TypeReference<T> type;
+ T value;
+
+ public VariableImpl(String identifier, TypeReference<T> type) {
+ this.identifier = identifier;
+ this.type = type;
+ }
+
+ @Override
+ public T get() {
+ return value;
+ }
+
+ @Override
+ public void set(T value) {
+ }
+
+ @Override
+ public void update() {
+ value = client.getState().getParsedData(identifier, type);
+ }
+
+ @Override
+ public void setOnChangeListener(Variable.OnChangeListener<T> listener) {
+ throw new RuntimeException("Not implemented.");
+ }
+ }
+
+ public static VariableFactory create(Client.ClientInterface client) {
+ return new VariableFactory(client);
+ }
+
+ VariableFactory(Client.ClientInterface client) {
+ this.client = client;
+ }
+
+ public <T> Variable<T> create(String identifier, TypeReference<T> type) {
+ Variable<T> variable = new VariableImpl<T>(identifier, type);
+ variable.update();
+ return variable;
+ }
+}
diff --git a/same/src/test/java/com/orbekk/same/VariableFactoryTest.java b/same/src/test/java/com/orbekk/same/VariableFactoryTest.java
new file mode 100644
index 0000000..eade799
--- /dev/null
+++ b/same/src/test/java/com/orbekk/same/VariableFactoryTest.java
@@ -0,0 +1,52 @@
+package com.orbekk.same;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import java.util.List;
+
+import org.codehaus.jackson.type.TypeReference;
+import org.junit.Before;
+import org.junit.Test;
+
+public class VariableFactoryTest {
+ Client.ClientInterface client;
+ VariableFactory vf;
+ State sampleState;
+
+ TypeReference<Integer> intType = new TypeReference<Integer>() {};
+ TypeReference<List<String>> listType = new TypeReference<List<String>>() {};
+
+ @Before
+ public void setUp() {
+ client = mock(Client.ClientInterface.class);
+ vf = new VariableFactory(client);
+ initializeSampleState();
+ }
+
+ public void initializeSampleState() {
+ sampleState = new State("TestState");
+ sampleState.update("TestVariable", "1", 1);
+ sampleState.update("TestList", "[]", 1);
+ }
+
+ @Test
+ public void getsInitialValue() {
+ when(client.getState()).thenReturn(sampleState);
+ Variable<Integer> testVariable = vf.create("TestVariable", intType);
+ assertEquals(1, (int)testVariable.get());
+ }
+
+ @Test
+ public void updatesValue() {
+ when(client.getState()).thenReturn(sampleState);
+ Variable<List<String>> list = vf.create("TestList", listType);
+ assertTrue(list.get().isEmpty());
+ sampleState.update("TestList", "[\"CONTENT\"]", 2);
+ list.update();
+ assertEquals(1, list.get().size());
+ assertEquals("CONTENT", list.get().get(0));
+ }
+}