In the latest version (and in the previous versions too), Guava eventbus module is not extensible. Currently, it uses Subscriber and SubscriberRegistry
internally to decide on dispatching the event. But these classes are made package-private and hence not extensible. If Subscriber
and SubscriberRegistry
were made as a public interface, it would let others to provide their own implementation in deciding the Subscriber
.
Let me explain my usecase to make more sense here. Lets say I have a hierarchy of classes which have a common set of events (create/updated/delete). And there are listeners who listen to one or more of these events for one or more of these classes. To be able to use EventBus
model, now, I have to have three event classes created for each type of the classes.
If I have class A
and class B extends A
and class C extends B
and class D extends A
, and each of A, B, C, and D could be created/updated/deleted. Then I have to have ACreatedEvent
, AUpdatedEvent
, ADeletedEvent
, BCreatedEvent
, BUpdatedEvent
, BDeletedEvent
, and so on for all the classes. (I know that subscribing for ACreatedEvent
will be notified if BCreatedEvent
is posted, assuming BCreatedEvent extends ACreatedEvent
and so on. But there could be listeners who are specifically interested in a sub class object event and not super class object event. So, this bloated hierarchy of events is necessary to give the flexibility for the listeners).
This forces me to created so many Event classes which is very clumsy. What I thought was, to introduce another annotation, lets say, @Requires(classTypes={B.class})
, which should be added along with the @Subscribe
. This kind of an auxiliary/associate annotation could then be used when constructing the Subscriber
/SubscriberRegistry
(possibly using a Predicate<Class>
). In this case, the listeners could simply have
/**
* Listens for B or C created event
*/
@Subscribe
@Requires(classTypes={B.class, C.class})
public void handleCreatedEvent(ACreatedEvent event) {
//handle B and C created event
}
and the I would only need to have the three Event
classes for the base class (ACreatedEvent
, AUpdatedEvent
, ADeletedEvent
) and listeners who are specifically interested in the subclass events can have @Requires
annotation.
But the problem is, Guava doesn't expose those core classes.
So, my questions are:
- Why Guava wanted the logic of Subscribers private?
- I am working on a commercial product. Can I copy the eventbus module into my code base and give my own implementation (a new implementation without changing the existing implemantation) for
Subscriber
andSubscriberRegistry
and changing theEventBus
to use mySubscriberRegistry
class? (without changing the rest of the classes and their copyright statements) Will that violate the Apache License 2.0?
[PS: Sorry for this long question. @Requires
is just an example. The idea of the question was the possible extensible behavior for @Subscribe
]
来源:https://stackoverflow.com/questions/28104575/why-guava-eventbus-the-module-is-not-extensible