PalladioSimulator / Palladio-Analyzer-Slingshot

0 stars 1 forks source link

Add possibility to create subscribers at runtime #9

Closed kjuli closed 1 year ago

kjuli commented 1 year ago

This PR is meant to add the possibility to add subscribers at runtime. In this PR, there are some major changes in the code though:

There is no direct AbstractSubscriber anymore. Instead, the code only contains a Subscriber<T> class with the generic parameter T which is the actual event type. The subscriber references an EventHandler<T>, which is a functional interface, meaning that there is only one method that needs to be implemented. An event handler (subscriber) on a bus bus can now be added at runtime with the following:

final List<SubscriberContract> contracts = List.of(new SubscriberContract(
    Event.class, // when
    List.of(EventB.class, EventC.class), // then
    EventCardinality.MANY) // Cardinality
);

bus.register(Subscriber.ofEvent(Event.class)
                       .handler(event -> { /* do something */ })
                       .associatedContract(contracts)
                       .name("identifier for this particular subscriber"));

Of course, the standard way of adding subscribers and specifying annotations is still there. The bus will encapsulate the Java method with this Subscriber and create an EventHandler that simply calls the method.

This PR also contains some minor fixes, such as the empty Result<T> type with correct type.