Bus { public void register(Object object) { if (object == null) { throw new NullPointerException("Object to register must not be null."); } enforcer.enforce(this); Map<EventType, ProducerEvent> foundProducers = finder.findAllProducers(object); for (EventType type : foundProducers.keySet()) { final ProducerEvent producer = foundProducers.get(type); ProducerEvent previousProducer = producersByType.putIfAbsent(type, producer); if (previousProducer != null) { throw new IllegalArgumentException("Producer method for type " + type + " found on type " + producer.getTarget().getClass() + ", but already registered by type " + previousProducer.getTarget().getClass() + "."); } Set<SubscriberEvent> subscribers = subscribersByType.get(type); if (subscribers != null && !subscribers.isEmpty()) { for (SubscriberEvent subscriber : subscribers) { dispatchProducerResult(subscriber, producer); } } } Map<EventType, Set<SubscriberEvent>> foundSubscribersMap = finder.findAllSubscribers(object); for (EventType type : foundSubscribersMap.keySet()) { Set<SubscriberEvent> subscribers = subscribersByType.get(type); if (subscribers == null) { Set<SubscriberEvent> SubscribersCreation = new CopyOnWriteArraySet<>(); subscribers = subscribersByType.putIfAbsent(type, SubscribersCreation); if (subscribers == null) { subscribers = SubscribersCreation; } } final Set<SubscriberEvent> foundSubscribers = foundSubscribersMap.get(type); if (!subscribers.addAll(foundSubscribers)) { throw new IllegalArgumentException("Object already registered."); } } for (Map.Entry<EventType, Set<SubscriberEvent>> entry : foundSubscribersMap.entrySet()) { EventType type = entry.getKey(); ProducerEvent producer = producersByType.get(type); if (producer != null && producer.isValid()) { Set<SubscriberEvent> subscriberEvents = entry.getValue(); for (SubscriberEvent subscriberEvent : subscriberEvents) { if (!producer.isValid()) { break; } if (subscriberEvent.isValid()) { dispatchProducerResult(subscriberEvent, producer); } } } } } Bus();  Bus(String identifier);  Bus(ThreadEnforcer enforcer);  Bus(ThreadEnforcer enforcer, String identifier);   Bus(ThreadEnforcer enforcer, String identifier, Finder finder); @Override String toString(); void register(Object object); @Deprecated boolean hasRegistered(Object object); void unregister(Object object); void post(Object event); void post(String tag, Object event); static final String DEFAULT_IDENTIFIER; }
@Test public void registeringSameObjectTwiceFails() { StringCatcher catcher = new StringCatcher(); bus.register(catcher); try { bus.register(catcher); fail(); } catch (IllegalArgumentException e) { assertEquals("Object already registered.", e.getMessage()); } }
@Test public void producerCalledForExistingSubscribers() { StringCatcher catcher = new StringCatcher(); StringProducer producer = new StringProducer(); bus.register(catcher); bus.register(producer); assertEquals(Arrays.asList(StringProducer.VALUE), catcher.getEvents()); }
@Test public void subscribingOrProducingOnlyAllowedOnPublicMethods() { try { bus.register(new Object() { @Subscribe protected void method(Object o) { } }); fail(); } catch (IllegalArgumentException expected) { } try { bus.register(new Object() { @Subscribe void method(Object o) { } }); fail(); } catch (IllegalArgumentException expected) { } try { bus.register(new Object() { @Subscribe private void method(Object o) { } }); fail(); } catch (IllegalArgumentException expected) { } try { bus.register(new Object() { @Produce protected Object method() { return null; } }); fail(); } catch (IllegalArgumentException expected) { } try { bus.register(new Object() { @Produce Object method() { return null; } }); fail(); } catch (IllegalArgumentException expected) { } try { bus.register(new Object() { @Produce private Object method() { return null; } }); fail(); } catch (IllegalArgumentException expected) { } }
@Test(expected = IllegalArgumentException.class) public void voidProducerThrowsException() throws Exception { class VoidProducer { @Produce public void things() { } } bus.register(new VoidProducer()); }
@Test public void missingSubscribe() { bus.register(new Object()); }
@Test public void producingNullIsInvalid() { try { bus.register(new NullProducer()); } catch (IllegalStateException expected) { } }