From: Mateusz Warowny Date: Wed, 19 Jan 2022 13:45:41 +0000 (+0100) Subject: JAL-3878 Move listeners list from the WebServiceDiscoverer interface. X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=4076999c53315133e8d4ff9023b23131bf001e87;p=jalview.git JAL-3878 Move listeners list from the WebServiceDiscoverer interface. Listeners list defined in the interface was globally shared among all discoverer instances. Listeners should be kept by the discoverer instance instead. --- diff --git a/src/jalview/ws2/OperationsChangeListenerList.java b/src/jalview/ws2/OperationsChangeListenerList.java new file mode 100644 index 0000000..275fab3 --- /dev/null +++ b/src/jalview/ws2/OperationsChangeListenerList.java @@ -0,0 +1,62 @@ +package jalview.ws2; + +import java.util.List; +import java.util.concurrent.CopyOnWriteArrayList; +import jalview.ws2.WebServiceDiscovererI.OperationsChangeListener; +import jalview.ws2.operations.Operation; + +/** + * A helper class that manages a list of OperationsChangeListeners + * Used by web service discoverers to store listeners. + * @author mmwarowny + * + */ +public class OperationsChangeListenerList +{ + private List listeners = new CopyOnWriteArrayList<>(); + private WebServiceDiscovererI discoverer; + + public OperationsChangeListenerList(WebServiceDiscovererI discoverer) + { + this.discoverer = discoverer; + } + + /** + * Add an operations list observer that will be notified of any changes. + * + * @param listener + * operations list listener + */ + public void addListener(OperationsChangeListener listener) + { + listeners.add(listener); + } + + /** + * Remove the listener from the observers list. + * + * @param listener + * listener to be removed + */ + public void removeListener(OperationsChangeListener listener) + { + listeners.remove(listener); + } + + /** + * Called whenever the list of operations changes. Notifies all listeners of + * the change to the operations list. Typically, should be called with an + * empty list at the beginning of the service discovery process and for the + * second time with the list of discovered operations after that. + * + * @param list + * new list of discovered operations + */ + public void fireOperationsChanged(List list) + { + for (var listener : listeners) + { + listener.operationsChanged(discoverer, list); + } + } +} diff --git a/src/jalview/ws2/WebServiceDiscovererI.java b/src/jalview/ws2/WebServiceDiscovererI.java index 30cc22e..f70fee9 100644 --- a/src/jalview/ws2/WebServiceDiscovererI.java +++ b/src/jalview/ws2/WebServiceDiscovererI.java @@ -123,19 +123,13 @@ public interface WebServiceDiscovererI List operations); } - List serviceListeners = new CopyOnWriteArrayList<>(); - /** * Add an operations list observer that will be notified of any changes. * * @param listener * operations list listener */ - public default void addOperationsChangeListener( - OperationsChangeListener listener) - { - serviceListeners.add(listener); - } + public void addOperationsChangeListener(OperationsChangeListener listener); /** * Remove the listener from the observers list. @@ -143,26 +137,5 @@ public interface WebServiceDiscovererI * @param listener * listener to be removed */ - public default void removeOperationsChangeListener( - OperationsChangeListener listener) - { - serviceListeners.remove(listener); - } - - /** - * Called whenever the list of operations changes. Notifies all listeners of - * the change to the operations list. Typically, should be called with an - * empty list at the beginning of the service discovery process and for the - * second time with the list of discovered operations after that. - * - * @param list - * new list of discovered operations - */ - default void fireOperationsChanged(List list) - { - for (var listener : serviceListeners) - { - listener.operationsChanged(this, list); - } - } + public void removeOperationsChangeListener(OperationsChangeListener listener); } diff --git a/src/jalview/ws2/slivka/SlivkaWSDiscoverer.java b/src/jalview/ws2/slivka/SlivkaWSDiscoverer.java index 4cbd1a2..f39559c 100644 --- a/src/jalview/ws2/slivka/SlivkaWSDiscoverer.java +++ b/src/jalview/ws2/slivka/SlivkaWSDiscoverer.java @@ -231,4 +231,26 @@ public class SlivkaWSDiscoverer implements WebServiceDiscovererI return ""; } + + private OperationsChangeListenerList operationsChangeListeners = + new OperationsChangeListenerList(this); + + @Override + public void addOperationsChangeListener(OperationsChangeListener listener) + { + operationsChangeListeners.addListener(listener); + } + + @Override + public void removeOperationsChangeListener( + OperationsChangeListener listener) + { + operationsChangeListeners.removeListener(listener); + } + + private void fireOperationsChanged(List list) + { + operationsChangeListeners.fireOperationsChanged(list); + } + }