Author: mes
Date: 2011-01-21 21:13:49 -0800 (Fri, 21 Jan 2011)
New Revision: 23564

Added:
   
core3/event-impl/trunk/impl/src/main/java/org/cytoscape/event/internal/ServiceComparator.java
Modified:
   
core3/event-impl/trunk/impl/src/main/java/org/cytoscape/event/internal/CyListenerAdapter.java
   
core3/event-impl/trunk/impl/src/main/java/org/cytoscape/event/internal/CyMicroListenerAdapter.java
Log:
a change so that listeners are now called in a consistent order

Modified: 
core3/event-impl/trunk/impl/src/main/java/org/cytoscape/event/internal/CyListenerAdapter.java
===================================================================
--- 
core3/event-impl/trunk/impl/src/main/java/org/cytoscape/event/internal/CyListenerAdapter.java
       2011-01-22 01:29:17 UTC (rev 23563)
+++ 
core3/event-impl/trunk/impl/src/main/java/org/cytoscape/event/internal/CyListenerAdapter.java
       2011-01-22 05:13:49 UTC (rev 23564)
@@ -38,6 +38,7 @@
 import java.lang.reflect.Method;
 import java.util.HashMap;
 import java.util.Map;
+import java.util.Arrays;
 import java.util.concurrent.Executor;
 import java.util.concurrent.Executors;
 
@@ -55,6 +56,7 @@
        
        private static final Logger logger = 
LoggerFactory.getLogger(CyListenerAdapter.class);
        private static final Executor EXEC = Executors.newCachedThreadPool();
+       private static final ServiceComparator serviceComparator = new 
ServiceComparator(); 
 
        private final Map<Class<?>,ServiceTracker> serviceTrackers; 
        private final BundleContext bc;
@@ -88,6 +90,7 @@
                        final Method method = 
listenerClass.getMethod("handleEvent", event.getClass());
 
                        for (final Object listener : listeners) {
+                               System.out.println("event: " + 
event.getClass().getName() + "  listener: " + listener.getClass().getName());
                                method.invoke(listenerClass.cast(listener), 
event);
                        }
                } catch (NoSuchMethodException e) {
@@ -140,7 +143,13 @@
                        serviceTrackers.put( listenerClass, st );
                }
 
-               return serviceTrackers.get(listenerClass).getServices();
+               Object[] services = 
serviceTrackers.get(listenerClass).getServices();
+
+               if ( services == null )
+                       return null;
+
+               Arrays.sort(services, serviceComparator);
+               return services; 
        }
 
        private static class Runner implements Runnable {

Modified: 
core3/event-impl/trunk/impl/src/main/java/org/cytoscape/event/internal/CyMicroListenerAdapter.java
===================================================================
--- 
core3/event-impl/trunk/impl/src/main/java/org/cytoscape/event/internal/CyMicroListenerAdapter.java
  2011-01-22 01:29:17 UTC (rev 23563)
+++ 
core3/event-impl/trunk/impl/src/main/java/org/cytoscape/event/internal/CyMicroListenerAdapter.java
  2011-01-22 05:13:49 UTC (rev 23564)
@@ -38,9 +38,9 @@
 import java.lang.reflect.Method;
 import java.lang.reflect.Proxy;
 import java.util.HashMap;
-import java.util.HashSet;
+import java.util.TreeSet;
+import java.util.SortedSet;
 import java.util.Map;
-import java.util.Set;
 
 import org.cytoscape.event.CyMicroListener;
 
@@ -51,14 +51,15 @@
 public class CyMicroListenerAdapter {
 
        private final Map<Object,Map<Class<?>,Object>> proxys;
-       private final Map<Object,Map<Class<?>,Set<Object>>> listeners;
+       private final Map<Object,Map<Class<?>,SortedSet<Object>>> listeners;
        private final Map<Class<?>,Object> noOpProxies;
-       
+
+       private static final ServiceComparator serviceComparator = new 
ServiceComparator();
        private final static Logger logger = 
LoggerFactory.getLogger(CyMicroListenerAdapter.class);
 
        public CyMicroListenerAdapter() {
                proxys = new HashMap<Object,Map<Class<?>,Object>>();
-               listeners = new HashMap<Object,Map<Class<?>,Set<Object>>>();
+               listeners = new 
HashMap<Object,Map<Class<?>,SortedSet<Object>>>();
                noOpProxies = new HashMap<Class<?>,Object>();
        }
 
@@ -99,13 +100,13 @@
                // First add the listener service to the set of services
                // for this object and class.
                if ( !listeners.containsKey(source) )
-                       listeners.put(source, new 
HashMap<Class<?>,Set<Object>>());
+                       listeners.put(source, new 
HashMap<Class<?>,SortedSet<Object>>());
 
-               Map<Class<?>,Set<Object>> listenerMap = listeners.get(source);
+               Map<Class<?>,SortedSet<Object>> listenerMap = 
listeners.get(source);
                if ( !listenerMap.containsKey(clazz) )
-                       listenerMap.put(clazz,new HashSet<Object>());
+                       listenerMap.put(clazz,new 
TreeSet<Object>(serviceComparator));
 
-               Set<Object> listenerServices = listenerMap.get(clazz);
+               SortedSet<Object> listenerServices = listenerMap.get(clazz);
                listenerServices.add(service);
                        
                // Now create a Proxy object for this object and class.
@@ -122,9 +123,9 @@
 
        public <L extends CyMicroListener> void removeMicroListener(L service, 
Class<L> clazz, Object source) {
                // clean up the listeners
-               Map<Class<?>,Set<Object>> sourceListeners = 
listeners.get(source);
+               Map<Class<?>,SortedSet<Object>> sourceListeners = 
listeners.get(source);
                if ( sourceListeners != null ) {
-                       Set<Object> classListeners = sourceListeners.get(clazz);
+                       SortedSet<Object> classListeners = 
sourceListeners.get(clazz);
                        if ( classListeners != null ) {
                                classListeners.remove(service);
                                if ( classListeners.size() == 0 )
@@ -153,13 +154,13 @@
        // Simply iterates over the provided list of Listeners and
        // executes the specified method on each Listener.
        private static class ListenerHandler implements InvocationHandler {
-               private final Set<Object> ol; 
+               private final SortedSet<Object> ol; 
 
                public ListenerHandler() {
                        this.ol = null;
                }
 
-               public ListenerHandler(Set<Object> ol) {
+               public ListenerHandler(SortedSet<Object> ol) {
                        this.ol = ol;
                }
 

Added: 
core3/event-impl/trunk/impl/src/main/java/org/cytoscape/event/internal/ServiceComparator.java
===================================================================
--- 
core3/event-impl/trunk/impl/src/main/java/org/cytoscape/event/internal/ServiceComparator.java
                               (rev 0)
+++ 
core3/event-impl/trunk/impl/src/main/java/org/cytoscape/event/internal/ServiceComparator.java
       2011-01-22 05:13:49 UTC (rev 23564)
@@ -0,0 +1,58 @@
+/*
+ Copyright (c) 2008, The Cytoscape Consortium (www.cytoscape.org)
+
+ The Cytoscape Consortium is:
+ - Institute for Systems Biology
+ - University of California San Diego
+ - Memorial Sloan-Kettering Cancer Center
+ - Institut Pasteur
+ - Agilent Technologies
+
+ This library is free software; you can redistribute it and/or modify it
+ under the terms of the GNU Lesser General Public License as published
+ by the Free Software Foundation; either version 2.1 of the License, or
+ any later version.
+
+ This library is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY, WITHOUT EVEN THE IMPLIED WARRANTY OF
+ MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  The software and
+ documentation provided hereunder is on an "as is" basis, and the
+ Institute for Systems Biology and the Whitehead Institute
+ have no obligations to provide maintenance, support,
+ updates, enhancements or modifications.  In no event shall the
+ Institute for Systems Biology and the Whitehead Institute
+ be liable to any party for direct, indirect, special,
+ incidental or consequential damages, including lost profits, arising
+ out of the use of this software and its documentation, even if the
+ Institute for Systems Biology and the Whitehead Institute
+ have been advised of the possibility of such damage.  See
+ the GNU Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with this library; if not, write to the Free Software Foundation,
+ Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+*/
+package org.cytoscape.event.internal;
+
+import java.util.Comparator;
+
+/**
+ * The point of this comparator is to allow services to be ordered in a 
consistent,
+ * albeit arbitrary, way. 
+ */
+class ServiceComparator implements Comparator<Object> {
+       public int compare(Object a, Object b) {
+               final int aId = a.getClass().getName().hashCode();
+               final int bId = b.getClass().getName().hashCode(); 
+               if ( aId < bId ) 
+                       return -1;
+               else if ( aId > bId )
+                       return 1;
+               else
+                       return 0;
+       }
+
+       public boolean equals(Object o) {
+               return (o == this);
+       }
+}

-- 
You received this message because you are subscribed to the Google Groups 
"cytoscape-cvs" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/cytoscape-cvs?hl=en.

Reply via email to