11d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert/*
21d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Copyright (C) 2007 The Guava Authors
31d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *
41d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Licensed under the Apache License, Version 2.0 (the "License");
51d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * you may not use this file except in compliance with the License.
61d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * You may obtain a copy of the License at
71d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *
81d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * http://www.apache.org/licenses/LICENSE-2.0
91d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *
101d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Unless required by applicable law or agreed to in writing, software
111d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * distributed under the License is distributed on an "AS IS" BASIS,
121d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
131d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * See the License for the specific language governing permissions and
141d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * limitations under the License.
151d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert */
161d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
171d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert/**
181d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * The EventBus allows publish-subscribe-style communication between components
191d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * without requiring the components to explicitly register with one another
201d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * (and thus be aware of each other).  It is designed exclusively to replace
211d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * traditional Java in-process event distribution using explicit registration.
221d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * It is <em>not</em> a general-purpose publish-subscribe system, nor is it
231d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * intended for interprocess communication.
241d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *
251d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <h2>One-Minute Guide</h2>
261d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *
271d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Converting an existing EventListener-based system to use the EventBus is
281d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * easy.
291d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *
301d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <h3>For Listeners</h3>
311d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * To listen for a specific flavor of event (say, a CustomerChangeEvent)...
321d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <ul>
331d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <li><strong>...in traditional Java events:</strong> implement an interface
341d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *     defined with the event &mdash; such as CustomerChangeEventListener.</li>
351d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <li><strong>...with EventBus:</strong> create a method that accepts
361d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *     CustomerChangeEvent as its sole argument, and mark it with the
371d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *     {@link com.google.common.eventbus.Subscribe} annotation.</li>
381d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * </ul>
391d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *
401d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <p>To register your listener methods with the event producers...
411d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <ul>
421d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <li><strong>...in traditional Java events:</strong> pass your object to each
431d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *     producer's {@code registerCustomerChangeEventListener} method.  These
441d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *     methods are rarely defined in common interfaces, so in addition to
451d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *     knowing every possible producer, you must also know its type.</li>
461d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <li><strong>...with EventBus:</strong> pass your object to the
471d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *     {@link com.google.common.eventbus.EventBus#register(Object)} method on an
481d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *     EventBus.  You'll need to
491d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *     make sure that your object shares an EventBus instance with the event
501d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *     producers.</li>
511d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * </ul>
521d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *
531d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <p>To listen for a common event supertype (such as EventObject or Object)...
541d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <ul>
551d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <li><strong>...in traditional Java events:</strong> not easy.</li>
561d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <li><strong>...with EventBus:</strong> events are automatically dispatched to
571d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *     listeners of any supertype, allowing listeners for interface types
581d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *     or "wildcard listeners" for Object.</li>
591d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * </ul>
601d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *
611d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <p>To listen for and detect events that were dispatched without listeners...
621d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <ul>
631d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <li><strong>...in traditional Java events:</strong> add code to each
641d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *     event-dispatching method (perhaps using AOP).</li>
651d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <li><strong>...with EventBus:</strong> subscribe to {@link
661d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *     com.google.common.eventbus.DeadEvent}.  The
671d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *     EventBus will notify you of any events that were posted but not
681d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *     delivered.  (Handy for debugging.)</li>
691d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * </ul>
701d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *
711d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <h3>For Producers</h3>
721d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * To keep track of listeners to your events...
731d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <ul>
741d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <li><strong>...in traditional Java events:</strong> write code to manage
751d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *     a list of listeners to your object, including synchronization, or use a
761d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *     utility class like EventListenerList.</li>
771d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <li><strong>...with EventBus:</strong> EventBus does this for you.</li>
781d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * </ul>
791d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *
801d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <p>To dispatch an event to listeners...
811d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <ul>
821d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <li><strong>...in traditional Java events:</strong> write a method to
831d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *     dispatch events to each event listener, including error isolation and
841d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *     (if desired) asynchronicity.</li>
851d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <li><strong>...with EventBus:</strong> pass the event object to an EventBus's
861d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *     {@link com.google.common.eventbus.EventBus#post(Object)} method.</li>
871d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * </ul>
881d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *
891d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <h2>Glossary</h2>
901d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *
911d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * The EventBus system and code use the following terms to discuss event
921d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * distribution:
931d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <dl>
941d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <dt>Event</dt><dd>Any object that may be <em>posted</em> to a bus.</dd>
951d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <dt>Subscribing</dt><dd>The act of registering a <em>listener</em> with an
961d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *     EventBus, so that its <em>handler methods</em> will receive events.</dd>
971d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <dt>Listener</dt><dd>An object that wishes to receive events, by exposing
981d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *     <em>handler methods</em>.</dt>
991d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <dt>Handler method</dt><dd>A public method that the EventBus should use to
1001d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *     deliver <em>posted</em> events.  Handler methods are marked by the
1011d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *     {@link com.google.common.eventbus.Subscribe} annotation.</dd>
1021d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <dt>Posting an event</dt><dd>Making the event available to any
1031d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *     <em>listeners</em> through the EventBus.</dt>
1041d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * </dl>
1051d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *
1061d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <h2>FAQ</h2>
1071d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <h3>Why must I create my own Event Bus, rather than using a singleton?</h3>
1081d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *
1091d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * The Event Bus doesn't specify how you use it; there's nothing stopping your
1101d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * application from having separate EventBus instances for each component, or
1111d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * using separate instances to separate events by context or topic.  This also
1121d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * makes it trivial to set up and tear down EventBus objects in your tests.
1131d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *
1141d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <p>Of course, if you'd like to have a process-wide EventBus singleton,
1151d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * there's nothing stopping you from doing it that way.  Simply have your
1161d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * container (such as Guice) create the EventBus as a singleton at global scope
1171d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * (or stash it in a static field, if you're into that sort of thing).
1181d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *
1191d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <p>In short, the EventBus is not a singleton because we'd rather not make
1201d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * that decision for you.  Use it how you like.
1211d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *
1221d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <h3>Can I unregister a listener from the Event Bus?</h3>
1231d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Currently, no -- a listener registered with an EventBus instance will
1241d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * continue to receive events until the EventBus itself is disposed.
1251d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *
1261d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <p>In the apps using EventBus so far, this has not been a problem:
1271d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <ul>
1281d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *   <li>Most listeners are registered on startup or lazy initialization, and
1291d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *       persist for the life of the application.
1301d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *   <li>Scope-specific EventBus instances can handle temporary event
1311d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *       distribution (e.g. distributing events among request-scoped objects)
1321d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *   <li>For testing, EventBus instances can be easily created and thrown away,
1331d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *       removing the need for explicit unregistration.
1341d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * </ul>
1351d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *
1361d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <h3>Why use an annotation to mark handler methods, rather than requiring the
1371d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * listener to implement an interface?</h3>
1381d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * We feel that the Event Bus's {@code @Subscribe} annotation conveys your
1391d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * intentions just as explicitly as implementing an interface (or perhaps more
1401d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * so), while leaving you free to place event handler methods wherever you wish
1411d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * and give them intention-revealing names.
1421d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *
1431d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <p>Traditional Java Events use a listener interface which typically sports
1441d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * only a handful of methods -- typically one.  This has a number of
1451d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * disadvantages:
1461d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <ul>
1471d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *   <li>Any one class can only implement a single response to a given event.
1481d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *   <li>Listener interface methods may conflict.
1491d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *   <li>The method must be named after the event (e.g. {@code
1501d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *       handleChangeEvent}), rather than its purpose (e.g. {@code
1511d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *       recordChangeInJournal}).
1521d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *   <li>Each event usually has its own interface, without a common parent
1531d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *       interface for a family of events (e.g. all UI events).
1541d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * </ul>
1551d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *
1561d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <p>The difficulties in implementing this cleanly has given rise to a pattern,
1571d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * particularly common in Swing apps, of using tiny anonymous classes to
1581d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * implement event listener interfaces.
1591d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *
1601d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <p>Compare these two cases: <pre>
1611d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *   class ChangeRecorder {
1621d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *     void setCustomer(Customer cust) {
1631d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *       cust.addChangeListener(new ChangeListener() {
1641d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *         void customerChanged(ChangeEvent e) {
1651d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *           recordChange(e.getChange());
1661d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *         }
1671d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *       };
1681d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *     }
1691d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *   }
1701d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *
1711d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *   // Class is typically registered by the container.
1721d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *   class EventBusChangeRecorder {
1731d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *     &#064;Subscribe void recordCustomerChange(ChangeEvent e) {
1741d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *       recordChange(e.getChange());
1751d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *     }
1761d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *   }</pre>
1771d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *
1781d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * The intent is actually clearer in the second case: there's less noise code,
1791d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * and the event handler has a clear and meaningful name.
1801d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *
1811d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <h3>What about a generic {@code Handler<T>} interface?</h3>
1821d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Some have proposed a generic {@code Handler<T>} interface for EventBus
1831d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * listeners.  This runs into issues with Java's use of type erasure, not to
1841d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * mention problems in usability.
1851d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *
1861d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <p>Let's say the interface looked something like the following: <pre>   {@code
1871d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *   interface Handler<T> {
1881d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *     void handleEvent(T event);
1891d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *   }}</pre>
1901d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *
1911d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Due to erasure, no single class can implement a generic interface more than
1921d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * once with different type parameters.  This is a giant step backwards from
1931d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * traditional Java Events, where even if {@code actionPerformed} and {@code
1941d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * keyPressed} aren't very meaningful names, at least you can implement both
1951d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * methods!
1961d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *
1971d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <h3>Doesn't EventBus destroy static typing and eliminate automated
1981d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * refactoring support?</h3>
1991d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Some have freaked out about EventBus's {@code register(Object)} and {@code
2001d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * post(Object)} methods' use of the {@code Object} type.
2011d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *
2021d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <p>{@code Object} is used here for a good reason: the Event Bus library
2031d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * places no restrictions on the types of either your event listeners (as in
2041d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * {@code register(Object)}) or the events themselves (in {@code post(Object)}).
2051d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *
2061d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <p>Event handler methods, on the other hand, must explicitly declare their
2071d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * argument type -- the type of event desired (or one of its supertypes).  Thus,
2081d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * searching for references to an event class will instantly find all handler
2091d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * methods for that event, and renaming the type will affect all handler methods
2101d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * within view of your IDE (and any code that creates the event).
2111d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *
2121d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <p>It's true that you can rename your {@code @Subscribed} event handler
2131d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * methods at will; Event Bus will not stop this or do anything to propagate the
2141d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * rename because, to Event Bus, the names of your handler methods are
2151d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * irrelevant.  Test code that calls the methods directly, of course, will be
2161d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * affected by your renaming -- but that's what your refactoring tools are for.
2171d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *
2181d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <h3>What happens if I {@code register} a listener without any handler
2191d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * methods?</h3>
2201d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Nothing at all.
2211d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *
2221d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <p>The Event Bus was designed to integrate with containers and module
2231d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * systems, with Guice as the prototypical example.  In these cases, it's
2241d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * convenient to have the container/factory/environment pass <i>every</i>
2251d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * created object to an EventBus's {@code register(Object)} method.
2261d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *
2271d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <p>This way, any object created by the container/factory/environment can
2281d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * hook into the system's event model simply by exposing handler methods.
2291d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *
2301d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <h3>What Event Bus problems can be detected at compile time?</h3>
2311d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Any problem that can be unambiguously detected by Java's type system.  For
2321d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * example, defining a handler method for a nonexistent event type.
2331d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *
2341d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <h3>What Event Bus problems can be detected immediately at registration?</h3>
2351d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Immediately upon invoking {@code register(Object)} , the listener being
2361d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * registered is checked for the <i>well-formedness</i> of its handler methods.
2371d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Specifically, any methods marked with {@code @Subscribe} must take only a
2381d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * single argument.
2391d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *
2401d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <p>Any violations of this rule will cause an {@code IllegalArgumentException}
2411d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * to be thrown.
2421d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *
2431d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <p>(This check could be moved to compile-time using APT, a solution we're
2441d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * researching.)
2451d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *
2461d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <h3>What Event Bus problems may only be detected later, at runtime?</h3>
2471d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * If a component posts events with no registered listeners, it <i>may</i>
2481d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * indicate an error (typically an indication that you missed a
2491d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * {@code @Subscribe} annotation, or that the listening component is not loaded).
2501d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *
2511d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <p>(Note that this is <i>not necessarily</i> indicative of a problem.  There
2521d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * are many cases where an application will deliberately ignore a posted event,
2531d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * particularly if the event is coming from code you don't control.)
2541d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *
2551d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <p>To handle such events, register a handler method for the {@code DeadEvent}
2561d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * class.  Whenever EventBus receives an event with no registered handlers, it
2571d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * will turn it into a {@code DeadEvent} and pass it your way -- allowing you to
2581d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * log it or otherwise recover.
2591d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *
2601d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <h3>How do I test event listeners and their handler methods?</h3>
2611d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Because handler methods on your listener classes are normal methods, you can
2621d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * simply call them from your test code to simulate the EventBus.
2631d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert */
2641d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertpackage com.google.common.eventbus;
265