Bus { public void unregister(Object object) { if (object == null) { throw new NullPointerException("Object to unregister must not be null."); } enforcer.enforce(this); Map<Class<?>, EventProducer> producersInListener = handlerFinder.findAllProducers(object); for (Map.Entry<Class<?>, EventProducer> entry : producersInListener.entrySet()) { final Class<?> key = entry.getKey(); EventProducer producer = getProducerForEventType(key); EventProducer value = entry.getValue(); if (value == null || !value.equals(producer)) { throw new IllegalArgumentException( "Missing event producer for an annotated method. Is " + object.getClass() + " registered?"); } producersByType.remove(key).invalidate(); } Map<Class<?>, Set<EventHandler>> handlersInListener = handlerFinder.findAllSubscribers(object); for (Map.Entry<Class<?>, Set<EventHandler>> entry : handlersInListener.entrySet()) { Set<EventHandler> currentHandlers = getHandlersForEventType(entry.getKey()); Collection<EventHandler> eventMethodsInListener = entry.getValue(); if (currentHandlers == null || !currentHandlers.containsAll(eventMethodsInListener)) { throw new IllegalArgumentException( "Missing event handler for an annotated method. Is " + object.getClass() + " registered?"); } for (EventHandler handler : currentHandlers) { if (eventMethodsInListener.contains(handler)) { handler.invalidate(); } } currentHandlers.removeAll(eventMethodsInListener); } } Bus();  Bus(String identifier);  Bus(ThreadEnforcer enforcer);  Bus(ThreadEnforcer enforcer, String identifier);   Bus(ThreadEnforcer enforcer, String identifier, HandlerFinder handlerFinder); @Override String toString(); void register(Object object); void unregister(Object object); void post(Object event); static final String DEFAULT_IDENTIFIER; }
@Test public void unregister() { StringCatcher catcher1 = new StringCatcher(); StringCatcher catcher2 = new StringCatcher(); try { bus.unregister(catcher1); fail("Attempting to unregister an unregistered object succeeded"); } catch (IllegalArgumentException expected) { } bus.register(catcher1); bus.post(EVENT); bus.register(catcher2); bus.post(EVENT); List<String> expectedEvents = new ArrayList<String>(); expectedEvents.add(EVENT); expectedEvents.add(EVENT); assertEquals("Two correct events should be delivered.", expectedEvents, catcher1.getEvents()); assertEquals("One correct event should be delivered.", Arrays.asList(EVENT), catcher2.getEvents()); bus.unregister(catcher1); bus.post(EVENT); assertEquals("Shouldn't catch any more events when unregistered.", expectedEvents, catcher1.getEvents()); assertEquals("Two correct events should be delivered.", expectedEvents, catcher2.getEvents()); try { bus.unregister(catcher1); fail("Attempting to unregister an unregistered object succeeded"); } catch (IllegalArgumentException expected) { } bus.unregister(catcher2); bus.post(EVENT); assertEquals("Shouldn't catch any more events when unregistered.", expectedEvents, catcher1.getEvents()); assertEquals("Shouldn't catch any more events when unregistered.", expectedEvents, catcher2.getEvents()); }