summaryrefslogtreecommitdiff
path: root/same/src/main/java
diff options
context:
space:
mode:
Diffstat (limited to 'same/src/main/java')
-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