diff --git a/src/main/java/org/openflow/util/IProducer.java b/src/main/java/org/openflow/util/IProducer.java index 789f5a2ba9ebb0a41f3bf3a54ffd3cde08636d56..52ae79a52b5dc47af63f71d0aceeb3ee26105727 100644 --- a/src/main/java/org/openflow/util/IProducer.java +++ b/src/main/java/org/openflow/util/IProducer.java @@ -2,7 +2,8 @@ package org.openflow.util; public interface IProducer { - public void registerConsumer (Class<?> iface, Object anObj); - public void deregisterConsumer (Class<?> iface, Object anObj); + public void registerConsumer(Class<?> iface, Object anObj); + + public void deregisterConsumer(Class<?> iface, Object anObj); } diff --git a/src/main/java/org/openflow/util/ProducerConsumer.java b/src/main/java/org/openflow/util/ProducerConsumer.java index 16f71868f07ee94c7feb3def7bf1b3539e7030bb..f2244ef3524483b0a2fc70e8f2ba433efd363c01 100644 --- a/src/main/java/org/openflow/util/ProducerConsumer.java +++ b/src/main/java/org/openflow/util/ProducerConsumer.java @@ -5,213 +5,219 @@ import java.util.Hashtable; import java.util.Map; import java.util.Set; - /** - * The following implement a producer/consumer design pattern in which - * both producers and consumers explicitly employ a centralized - * registration mechanism, and java Interfaces are used as contracts.<br> - * + * The following implement a producer/consumer design pattern in which both + * producers and consumers explicitly employ a centralized registration + * mechanism, and java Interfaces are used as contracts.<br> */ public class ProducerConsumer { - /* - * Class variables - */ - protected static ProducerConsumer singleton; - - /* - * Default constructor - */ - protected ProducerConsumer () - { - producerMap = new Hashtable<Class<?>, Set<IProducer>> (); - } - - /* - * Instance variables - */ - - // Interface/IProducer map - protected Map<Class<?>, Set<IProducer>> producerMap; - - - /* - * Protected methods - */ - - protected void _registerConsumer (Object consumer, Class<?> [] interfaces, Set<Class<?>> iSet, Set<Class<?>> iUniqueSet) - { - //*...Process all interfaces...*/ - for (Class<?> iface: interfaces) { - - //*...Protect against repeated interfaces...*/ - if (!iUniqueSet.contains (iface)) { - iUniqueSet.add (iface); - - Set<IProducer> producers = producerMap.get (iface); - - if (producers != null) { - for (IProducer producer: producers) - producer.registerConsumer (iface, consumer); - iSet.add (iface); - } - - //*...Recurse...*/ - _registerConsumer (consumer, iface.getInterfaces (), iSet, iUniqueSet); - } - } - } - - protected void _registerConsumer (Object consumer, Class<?> clazz, Set<Class<?>> iSet, Set<Class<?>> iUniqueSet) - { - if (clazz != null) { - //*...Process all interfaces...*/ - _registerConsumer (consumer, clazz.getInterfaces (), iSet, iUniqueSet); - - //*...Recurse the class hierarchy...*/ - _registerConsumer (consumer, clazz.getSuperclass (), iSet, iUniqueSet); - } - } - - protected int _deregisterConsumer (Object consumer, Class<?> [] interfaces, Set<Class<?>> iUniqueSet) - { - int count = 0; - - //*...Process all interfaces...*/ - for (Class<?> iface: interfaces) { - - //*...Protect against repeated interfaces...*/ - if (!iUniqueSet.contains (iface)) { - iUniqueSet.add (iface); - - Set<IProducer> producers = producerMap.get (iface); - - if (producers != null) { - for (IProducer producer: producers) - producer.deregisterConsumer (iface, consumer); - - count ++; - } - - //*...Recurse...*/ - count += _deregisterConsumer (consumer, iface.getInterfaces (), iUniqueSet); - } - } - - return count; - } - - protected int _deregisterConsumer (Object consumer, Class<?> clazz, Set<Class<?>> iUniqueSet) - { - int count = 0; - - if (clazz != null) { - //*...Process all interfaces...*/ - count += _deregisterConsumer (consumer, clazz.getInterfaces (), iUniqueSet); - - //*...Recurse the class hierarchy...*/ - count += _deregisterConsumer (consumer, clazz.getSuperclass (), iUniqueSet); - } - - return count; - } - - /* - * Singleton API - */ - - /** - * @return singleton ProducerConsumer - */ - public static synchronized ProducerConsumer getSingleton () - { - if (singleton == null) - singleton = new ProducerConsumer (); - - return singleton; - } - - /* - * Producer APIs - */ - - /** - * Producer registration - * - * @param producer object that implements IProducer - * @param iface interface supported by the producer - * @return whether there was a previously registered producer, - * or true if one or more the arguments were invalid - */ - public boolean registerProducer (IProducer producer, Class<?> iface) - { - if (producer != null && iface != null && iface.isInterface ()) { - Set<IProducer> producers = producerMap.get (iface); - - if (producers == null) { - producers = new HashSet<IProducer> (); - producerMap.put (iface, producers); - } - - return producers.add (producer); - } - else - return true; - } - - /** - * Producer deregistration - * - * @param producer object that implements IProducer - * @param iface interface supported by the producer - * @return whether the interface/producer pair was removed, - * or false if one or more the arguments were invalid - */ - public boolean deregisterProducer (IProducer producer, Class<?> iface) - { - if (producer != null && iface != null && iface.isInterface ()) { - Set<IProducer> producers = producerMap.get (iface); - - if (producers != null) - return producers.remove (producer); - } - - return false; - } - - - /* - * Consumer APIs - */ - - /** - * Consumer registration - * - * @param consumer object that implements producer-specific interfaces - * @return set of supported interfaces - */ - public Set<Class<?>> registerConsumer (Object consumer) - { - Set<Class<?>> iSet = new HashSet<Class<?>> (); - - if (consumer != null) - _registerConsumer (consumer, consumer.getClass (), iSet, new HashSet<Class<?>> ()); - - return iSet; - } - - /** - * Consumer deregistration - * - * @param consumer object to deregister - * @return number of unregistered interfaces - */ - public int deregisterConsumer (Object consumer) - { - if (consumer != null) - return _deregisterConsumer (consumer, consumer.getClass (), new HashSet<Class<?>> ()); - else - return 0; - } + /* + * Class variables + */ + protected static ProducerConsumer singleton; + + /* + * Default constructor + */ + protected ProducerConsumer() { + producerMap = new Hashtable<Class<?>, Set<IProducer>>(); + } + + /* + * Instance variables + */ + + // Interface/IProducer map + protected Map<Class<?>, Set<IProducer>> producerMap; + + /* + * Protected methods + */ + + protected void _registerConsumer(Object consumer, Class<?>[] interfaces, + Set<Class<?>> iSet, + Set<Class<?>> iUniqueSet) { + // *...Process all interfaces...*/ + for (Class<?> iface : interfaces) { + + // *...Protect against repeated interfaces...*/ + if (!iUniqueSet.contains(iface)) { + iUniqueSet.add(iface); + + Set<IProducer> producers = producerMap.get(iface); + + if (producers != null) { + for (IProducer producer : producers) + producer.registerConsumer(iface, consumer); + iSet.add(iface); + } + + // *...Recurse...*/ + _registerConsumer(consumer, iface.getInterfaces(), iSet, + iUniqueSet); + } + } + } + + protected void _registerConsumer(Object consumer, Class<?> clazz, + Set<Class<?>> iSet, + Set<Class<?>> iUniqueSet) { + if (clazz != null) { + // *...Process all interfaces...*/ + _registerConsumer(consumer, clazz.getInterfaces(), iSet, + iUniqueSet); + + // *...Recurse the class hierarchy...*/ + _registerConsumer(consumer, clazz.getSuperclass(), iSet, + iUniqueSet); + } + } + + protected int _deregisterConsumer(Object consumer, + Class<?>[] interfaces, + Set<Class<?>> iUniqueSet) { + int count = 0; + + // *...Process all interfaces...*/ + for (Class<?> iface : interfaces) { + + // *...Protect against repeated interfaces...*/ + if (!iUniqueSet.contains(iface)) { + iUniqueSet.add(iface); + + Set<IProducer> producers = producerMap.get(iface); + + if (producers != null) { + for (IProducer producer : producers) + producer.deregisterConsumer(iface, consumer); + + count++; + } + + // *...Recurse...*/ + count += _deregisterConsumer(consumer, + iface.getInterfaces(), + iUniqueSet); + } + } + + return count; + } + + protected int _deregisterConsumer(Object consumer, Class<?> clazz, + Set<Class<?>> iUniqueSet) { + int count = 0; + + if (clazz != null) { + // *...Process all interfaces...*/ + count += _deregisterConsumer(consumer, clazz.getInterfaces(), + iUniqueSet); + + // *...Recurse the class hierarchy...*/ + count += _deregisterConsumer(consumer, clazz.getSuperclass(), + iUniqueSet); + } + + return count; + } + + /* + * Singleton API + */ + + /** + * @return singleton ProducerConsumer + */ + public static synchronized ProducerConsumer getSingleton() { + if (singleton == null) singleton = new ProducerConsumer(); + + return singleton; + } + + /* + * Producer APIs + */ + + /** + * Producer registration + * + * @param producer + * object that implements IProducer + * @param iface + * interface supported by the producer + * @return whether there was a previously registered producer, or true if + * one or more the arguments were invalid + */ + public boolean registerProducer(IProducer producer, Class<?> iface) { + if (producer != null && iface != null && iface.isInterface()) { + Set<IProducer> producers = producerMap.get(iface); + + if (producers == null) { + producers = new HashSet<IProducer>(); + producerMap.put(iface, producers); + } + + return producers.add(producer); + } else + return true; + } + + /** + * Producer deregistration + * + * @param producer + * object that implements IProducer + * @param iface + * interface supported by the producer + * @return whether the interface/producer pair was removed, or false if one + * or more the arguments were invalid + */ + public boolean deregisterProducer(IProducer producer, Class<?> iface) { + if (producer != null && iface != null && iface.isInterface()) { + Set<IProducer> producers = producerMap.get(iface); + + if (producers != null) return producers.remove(producer); + } + + return false; + } + + /* + * Consumer APIs + */ + + /** + * Consumer registration + * + * @param consumer + * object that implements producer-specific interfaces + * @return set of supported interfaces + */ + public Set<Class<?>> registerConsumer(Object consumer) { + Set<Class<?>> iSet = new HashSet<Class<?>>(); + + if (consumer != null) + _registerConsumer(consumer, + consumer.getClass(), iSet, + new HashSet<Class<?>>()); + + return iSet; + } + + /** + * Consumer deregistration + * + * @param consumer + * object to deregister + * @return number of unregistered interfaces + */ + public int deregisterConsumer(Object consumer) { + if (consumer != null) + return _deregisterConsumer(consumer, consumer.getClass(), + new HashSet<Class<?>>()); + else + return 0; + } }