summaryrefslogtreecommitdiff
path: root/same/src/main/java/com
diff options
context:
space:
mode:
authorKjetil Ørbekk <kjetil.orbekk@gmail.com>2012-02-16 20:26:57 +0100
committerKjetil Ørbekk <kjetil.orbekk@gmail.com>2012-02-16 20:26:57 +0100
commiteffce9ac69de33364f6e0dc78ced189fc32ebd38 (patch)
tree2254fd049102b8d5774188c44f096464a858b672 /same/src/main/java/com
parent863941aa16a5e4a9eeee07ea843a81257e21a038 (diff)
Implement Variable.
State can now be edited with Variable objects.
Diffstat (limited to 'same/src/main/java/com')
-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
3 files changed, 75 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;
+ }
+}