From dbb86148a7b53d729085b4ace612e211d7a876ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kjetil=20=C3=98rbekk?= Date: Tue, 28 Feb 2012 15:08:58 +0100 Subject: Add support for more than one listener to variables. --- same/src/main/java/com/orbekk/same/Variable.java | 3 ++- same/src/main/java/com/orbekk/same/VariableFactory.java | 17 ++++++++++++----- .../main/java/com/orbekk/same/VariableUpdaterTask.java | 6 +++++- .../test/java/com/orbekk/same/VariableFactoryTest.java | 2 +- 4 files changed, 20 insertions(+), 8 deletions(-) (limited to 'same') 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 get(); DelayedOperation set(T value); void update(); - void setOnChangeListener(OnChangeListener listener); + void addOnChangeListener(OnChangeListener listener); + void removeOnChangeListener(OnChangeListener 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 type; T value; long revision = 0; - OnChangeListener listener = null; + ArrayList> listeners = + new ArrayList>(); public VariableImpl(String identifier, TypeReference type) { this.identifier = identifier; @@ -66,14 +68,19 @@ public class VariableFactory { } @Override - public void setOnChangeListener(OnChangeListener listener) { - this.listener = listener; + public synchronized void addOnChangeListener(OnChangeListener listener) { + listeners.add(listener); + } + + @Override + public synchronized void removeOnChangeListener(OnChangeListener 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 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 extends Thread implements Variable.OnChangeListener { private Variable variable; - private T newValue; + private volatile T newValue; private AtomicBoolean hasNewValue = new AtomicBoolean(false); private AtomicBoolean isReady = new AtomicBoolean(true); public VariableUpdaterTask(Variable variable) { + super("VariableUpdater"); this.variable = variable; } @@ -47,7 +48,9 @@ public class VariableUpdaterTask extends Thread } } + @Override public void run() { + variable.addOnChangeListener(this); while (true) { waitFor(isReady); waitFor(hasNewValue); @@ -56,6 +59,7 @@ public class VariableUpdaterTask extends Thread } performWork(); } + variable.removeOnChangeListener(this); } @Override diff --git a/same/src/test/java/com/orbekk/same/VariableFactoryTest.java b/same/src/test/java/com/orbekk/same/VariableFactoryTest.java index bd0f923..66d181e 100644 --- a/same/src/test/java/com/orbekk/same/VariableFactoryTest.java +++ b/same/src/test/java/com/orbekk/same/VariableFactoryTest.java @@ -72,7 +72,7 @@ public class VariableFactoryTest { Variable.OnChangeListener listener = mock(Variable.OnChangeListener.class); Variable v = vf.create("z", intType); - v.setOnChangeListener(listener); + v.addOnChangeListener(listener); ((StateChangedListener) v).stateChanged( new State.Component("z", 1, "abc")); verify(listener).valueChanged(v); -- cgit v1.2.3