summaryrefslogtreecommitdiff
path: root/same/src/main
diff options
context:
space:
mode:
authorKjetil Ørbekk <kjetil.orbekk@gmail.com>2012-02-28 15:08:58 +0100
committerKjetil Ørbekk <kjetil.orbekk@gmail.com>2012-02-28 15:08:58 +0100
commitdbb86148a7b53d729085b4ace612e211d7a876ed (patch)
tree95e8542f0996dd2a7d9e4c92c6330b4ed124594e /same/src/main
parente5418063a31eec06835f294ec6e1054ea42f846d (diff)
Add support for more than one listener to variables.
Diffstat (limited to 'same/src/main')
-rw-r--r--same/src/main/java/com/orbekk/same/Variable.java3
-rw-r--r--same/src/main/java/com/orbekk/same/VariableFactory.java17
-rw-r--r--same/src/main/java/com/orbekk/same/VariableUpdaterTask.java6
3 files changed, 19 insertions, 7 deletions
diff --git a/same/src/main/java/com/orbekk/same/Variable.java b/same/src/main/java/com/orbekk/same/Variable.java
index 4d42909..7f9f4d5 100644
--- a/same/src/main/java/com/orbekk/same/Variable.java
+++ b/same/src/main/java/com/orbekk/same/Variable.java
@@ -15,5 +15,6 @@ public interface Variable<T> {
T get();
DelayedOperation set(T value);
void update();
- void setOnChangeListener(OnChangeListener<T> listener);
+ void addOnChangeListener(OnChangeListener<T> listener);
+ void removeOnChangeListener(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
index fa9eeeb..e04e36b 100644
--- a/same/src/main/java/com/orbekk/same/VariableFactory.java
+++ b/same/src/main/java/com/orbekk/same/VariableFactory.java
@@ -1,6 +1,7 @@
package com.orbekk.same;
import java.io.IOException;
+import java.util.ArrayList;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
@@ -25,7 +26,8 @@ public class VariableFactory {
TypeReference<T> type;
T value;
long revision = 0;
- OnChangeListener<T> listener = null;
+ ArrayList<OnChangeListener<T>> listeners =
+ new ArrayList<OnChangeListener<T>>();
public VariableImpl(String identifier, TypeReference<T> type) {
this.identifier = identifier;
@@ -66,14 +68,19 @@ public class VariableFactory {
}
@Override
- public void setOnChangeListener(OnChangeListener<T> listener) {
- this.listener = listener;
+ public synchronized void addOnChangeListener(OnChangeListener<T> listener) {
+ listeners.add(listener);
+ }
+
+ @Override
+ public synchronized void removeOnChangeListener(OnChangeListener<T> listener) {
+ listeners.remove(listener);
}
@Override
- public void stateChanged(Component component) {
+ public synchronized void stateChanged(Component component) {
if (component.getName().equals(identifier)) {
- if (listener != null) {
+ for (OnChangeListener<T> listener : listeners) {
listener.valueChanged(this);
}
}
diff --git a/same/src/main/java/com/orbekk/same/VariableUpdaterTask.java b/same/src/main/java/com/orbekk/same/VariableUpdaterTask.java
index 471dbc6..f696e17 100644
--- a/same/src/main/java/com/orbekk/same/VariableUpdaterTask.java
+++ b/same/src/main/java/com/orbekk/same/VariableUpdaterTask.java
@@ -7,11 +7,12 @@ import java.util.concurrent.atomic.AtomicBoolean;
public class VariableUpdaterTask<T> extends Thread
implements Variable.OnChangeListener<T> {
private Variable<T> variable;
- private T newValue;
+ private volatile T newValue;
private AtomicBoolean hasNewValue = new AtomicBoolean(false);
private AtomicBoolean isReady = new AtomicBoolean(true);
public VariableUpdaterTask(Variable<T> variable) {
+ super("VariableUpdater");
this.variable = variable;
}
@@ -47,7 +48,9 @@ public class VariableUpdaterTask<T> extends Thread
}
}
+ @Override
public void run() {
+ variable.addOnChangeListener(this);
while (true) {
waitFor(isReady);
waitFor(hasNewValue);
@@ -56,6 +59,7 @@ public class VariableUpdaterTask<T> extends Thread
}
performWork();
}
+ variable.removeOnChangeListener(this);
}
@Override