Class SubscriberRegistry


  • final class SubscriberRegistry
    extends java.lang.Object
    Registry of subscribers to a single event bus.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private EventBus bus
      The event bus this registry belongs to.
      private static LoadingCache<java.lang.Class<?>,​ImmutableSet<java.lang.Class<?>>> flattenHierarchyCache
      Global cache of classes to their flattened hierarchy of supertypes.
      private static LoadingCache<java.lang.Class<?>,​ImmutableList<java.lang.reflect.Method>> subscriberMethodsCache
      A thread-safe cache that contains the mapping from each class to all methods in that class and all super-classes, that are annotated with @Subscribe.
      private java.util.concurrent.ConcurrentMap<java.lang.Class<?>,​java.util.concurrent.CopyOnWriteArraySet<Subscriber>> subscribers
      All registered subscribers, indexed by event type.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      private Multimap<java.lang.Class<?>,​Subscriber> findAllSubscribers​(java.lang.Object listener)
      Returns all subscribers for the given listener grouped by the type of event they subscribe to.
      (package private) static ImmutableSet<java.lang.Class<?>> flattenHierarchy​(java.lang.Class<?> concreteClass)
      Flattens a class's type hierarchy into a set of Class objects including all superclasses (transitively) and all interfaces implemented by these superclasses.
      private static ImmutableList<java.lang.reflect.Method> getAnnotatedMethods​(java.lang.Class<?> clazz)  
      private static ImmutableList<java.lang.reflect.Method> getAnnotatedMethodsNotCached​(java.lang.Class<?> clazz)  
      (package private) java.util.Iterator<Subscriber> getSubscribers​(java.lang.Object event)
      Gets an iterator representing an immutable snapshot of all subscribers to the given event at the time this method is called.
      (package private) java.util.Set<Subscriber> getSubscribersForTesting​(java.lang.Class<?> eventType)  
      (package private) void register​(java.lang.Object listener)
      Registers all subscriber methods on the given listener object.
      (package private) void unregister​(java.lang.Object listener)
      Unregisters all subscribers on the given listener object.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • subscribers

        private final java.util.concurrent.ConcurrentMap<java.lang.Class<?>,​java.util.concurrent.CopyOnWriteArraySet<Subscriber>> subscribers
        All registered subscribers, indexed by event type.

        The CopyOnWriteArraySet values make it easy and relatively lightweight to get an immutable snapshot of all current subscribers to an event without any locking.

      • bus

        private final EventBus bus
        The event bus this registry belongs to.
      • subscriberMethodsCache

        private static final LoadingCache<java.lang.Class<?>,​ImmutableList<java.lang.reflect.Method>> subscriberMethodsCache
        A thread-safe cache that contains the mapping from each class to all methods in that class and all super-classes, that are annotated with @Subscribe. The cache is shared across all instances of this class; this greatly improves performance if multiple EventBus instances are created and objects of the same class are registered on all of them.
      • flattenHierarchyCache

        private static final LoadingCache<java.lang.Class<?>,​ImmutableSet<java.lang.Class<?>>> flattenHierarchyCache
        Global cache of classes to their flattened hierarchy of supertypes.
    • Constructor Detail

      • SubscriberRegistry

        SubscriberRegistry​(EventBus bus)
    • Method Detail

      • register

        void register​(java.lang.Object listener)
        Registers all subscriber methods on the given listener object.
      • unregister

        void unregister​(java.lang.Object listener)
        Unregisters all subscribers on the given listener object.
      • getSubscribersForTesting

        java.util.Set<Subscriber> getSubscribersForTesting​(java.lang.Class<?> eventType)
      • getSubscribers

        java.util.Iterator<Subscriber> getSubscribers​(java.lang.Object event)
        Gets an iterator representing an immutable snapshot of all subscribers to the given event at the time this method is called.
      • findAllSubscribers

        private Multimap<java.lang.Class<?>,​Subscriber> findAllSubscribers​(java.lang.Object listener)
        Returns all subscribers for the given listener grouped by the type of event they subscribe to.
      • getAnnotatedMethods

        private static ImmutableList<java.lang.reflect.Method> getAnnotatedMethods​(java.lang.Class<?> clazz)
      • getAnnotatedMethodsNotCached

        private static ImmutableList<java.lang.reflect.Method> getAnnotatedMethodsNotCached​(java.lang.Class<?> clazz)
      • flattenHierarchy

        static ImmutableSet<java.lang.Class<?>> flattenHierarchy​(java.lang.Class<?> concreteClass)
        Flattens a class's type hierarchy into a set of Class objects including all superclasses (transitively) and all interfaces implemented by these superclasses.