From effce9ac69de33364f6e0dc78ced189fc32ebd38 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kjetil=20=C3=98rbekk?= Date: Thu, 16 Feb 2012 20:26:57 +0100 Subject: Implement Variable. State can now be edited with Variable objects. --- .../main/java/com/orbekk/same/SameController.java | 4 ++ same/src/main/java/com/orbekk/same/Variable.java | 17 +++++++ .../main/java/com/orbekk/same/VariableFactory.java | 54 ++++++++++++++++++++++ 3 files changed, 75 insertions(+) create mode 100644 same/src/main/java/com/orbekk/same/Variable.java create mode 100644 same/src/main/java/com/orbekk/same/VariableFactory.java (limited to 'same/src/main/java') 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 { + public interface OnChangeListener { + /** + * A notification that 'variable' has been changed. + * + * The user must run variable.update() to get the updated value. + */ + void valueChanged(Variable variable); + } + + T get(); + void set(T value); + void update(); + void setOnChangeListener(OnChangeListener 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 implements Variable { + String identifier; + TypeReference type; + T value; + + public VariableImpl(String identifier, TypeReference 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 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 Variable create(String identifier, TypeReference type) { + Variable variable = new VariableImpl(identifier, type); + variable.update(); + return variable; + } +} -- cgit v1.2.3