diff options
Diffstat (limited to 'same/src/main')
-rw-r--r-- | same/src/main/java/com/orbekk/same/SameController.java | 4 | ||||
-rw-r--r-- | same/src/main/java/com/orbekk/same/Variable.java | 17 | ||||
-rw-r--r-- | same/src/main/java/com/orbekk/same/VariableFactory.java | 54 |
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; + } +} |