Skip to content

Commit 49649fa

Browse files
committed
added hasSubscriberForEvent (greenrobot#81)
1 parent d50c3ef commit 49649fa

File tree

3 files changed

+49
-5
lines changed

3 files changed

+49
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* Fixed performance regression sneaked into V2.2.x affecting (first time) registration of subscribers
88
* Updated to Gradle 2.1, using wrapper
99
* EventBusTest and EventBusPerformance use Gradle to build
10+
* Added hasSubscriberForEvent to check if currently subscribers exist registered to a given event type
1011
* Improved README.md and extracted an extended HOWTO.md and CHANGELOG.md from it
1112
* Ignore compiler generated methods (#76)
1213
* Various small code improvements (#120 among many others)

EventBus/src/de/greenrobot/event/EventBus.java

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -443,9 +443,27 @@ public void removeAllStickyEvents() {
443443
}
444444
}
445445

446+
public boolean hasSubscriberForEvent(Class<?> eventClass) {
447+
List<Class<?>> eventTypes = lookupAllEventTypes(eventClass);
448+
if (eventTypes != null) {
449+
int countTypes = eventTypes.size();
450+
for (int h = 0; h < countTypes; h++) {
451+
Class<?> clazz = eventTypes.get(h);
452+
CopyOnWriteArrayList<Subscription> subscriptions;
453+
synchronized (this) {
454+
subscriptions = subscriptionsByEventType.get(clazz);
455+
}
456+
if (subscriptions != null && !subscriptions.isEmpty()) {
457+
return true;
458+
}
459+
}
460+
}
461+
return false;
462+
}
463+
446464
private void postSingleEvent(Object event, PostingThreadState postingState) throws Error {
447465
Class<?> eventClass = event.getClass();
448-
List<Class<?>> eventTypes = findEventTypes(eventClass);
466+
List<Class<?>> eventTypes = lookupAllEventTypes(eventClass);
449467
boolean subscriptionFound = false;
450468
int countTypes = eventTypes.size();
451469
for (int h = 0; h < countTypes; h++) {
@@ -512,8 +530,8 @@ private void postToSubscription(Subscription subscription, Object event, boolean
512530
}
513531
}
514532

515-
/** Finds all Class objects including super classes and interfaces. */
516-
private List<Class<?>> findEventTypes(Class<?> eventClass) {
533+
/** Looks up all Class objects including super classes and interfaces. Should also work for interfaces. */
534+
private List<Class<?>> lookupAllEventTypes(Class<?> eventClass) {
517535
synchronized (eventTypesCache) {
518536
List<Class<?>> eventTypes = eventTypesCache.get(eventClass);
519537
if (eventTypes == null) {
@@ -567,7 +585,7 @@ void invokeSubscriber(Subscription subscription, Object event) {
567585

568586
private void handleSubscriberException(Subscription subscription, Object event, Throwable cause) {
569587
if (event instanceof SubscriberExceptionEvent) {
570-
if(logSubscriberExceptions) {
588+
if (logSubscriberExceptions) {
571589
// Don't send another SubscriberExceptionEvent to avoid infinite event recursion, just log
572590
Log.e(TAG, "SubscriberExceptionEvent subscriber " + subscription.subscriber.getClass()
573591
+ " threw an exception", cause);
@@ -583,7 +601,7 @@ private void handleSubscriberException(Subscription subscription, Object event,
583601
Log.e(TAG, "Could not dispatch event: " + event.getClass() + " to subscribing class "
584602
+ subscription.subscriber.getClass(), cause);
585603
}
586-
if(sendSubscriberExceptionEvent) {
604+
if (sendSubscriberExceptionEvent) {
587605
SubscriberExceptionEvent exEvent = new SubscriberExceptionEvent(this, cause, event,
588606
subscription.subscriber);
589607
post(exEvent);

EventBusTest/src/de/greenrobot/event/test/EventBusBasicTest.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,31 @@ public void testPostInEventHandler() {
222222
assertEquals(10, reposter.lastEvent);
223223
}
224224

225+
public void testHasSubscriberForEvent() {
226+
assertFalse(eventBus.hasSubscriberForEvent(String.class));
227+
eventBus.register(this);
228+
assertTrue(eventBus.hasSubscriberForEvent(String.class));
229+
}
230+
231+
public void testHasSubscriberForEventSuperclass() {
232+
assertFalse(eventBus.hasSubscriberForEvent(String.class));
233+
eventBus.register(new Object() {
234+
public void onEvent(Object event) {
235+
}
236+
});
237+
assertTrue(eventBus.hasSubscriberForEvent(String.class));
238+
}
239+
240+
public void testHasSubscriberForEventImplementedInterface() {
241+
assertFalse(eventBus.hasSubscriberForEvent(String.class));
242+
eventBus.register(new Object() {
243+
public void onEvent(CharSequence event) {
244+
}
245+
});
246+
assertTrue(eventBus.hasSubscriberForEvent(CharSequence.class));
247+
assertTrue(eventBus.hasSubscriberForEvent(String.class));
248+
}
249+
225250
public void onEvent(String event) {
226251
lastStringEvent = event;
227252
countStringEvent++;

0 commit comments

Comments
 (0)