Class CommonAdaptingEventBusWire<P,C>
- Type Parameters:
P- The type of the platform-specific event.C- The type of the common event.
- All Implemented Interfaces:
IEventBusWire
In other words, an instance of this class will wire its associated IEventBus by first cascading to another
specified wiring implementation (usually a platform-specific one), and then setting up a series of listeners that
allow redirection of events from the targeted bus to another one, specified when constructing the wire.
To allow for the double-posting to happen, a converting Function and a BiConsumer have to be
provided, as further explained in the of(IEventBusWire, IEventBus, Function) and
of(IEventBusWire, IEventBus, Function, BiConsumer) factory methods, refer to those for more information.
The general usage pattern of this class sees it being wired accordingly to a scheme similar to the following snippet of code:
class MyCommonEvent {
final IEventBus<MyCommonEvent> BUS = makeCommonBus();
}
class MyPlatformEvent {
final IEventBus<MyPlatformEvent> BUS = makeBusWithWire(
CommonAdaptingEventBusWire.of(platformSpecificWire(), MyCommonEvent.BUS, MyCommonEvent::new)
);
}
- Since:
- 11.0.0
-
Method Summary
Modifier and TypeMethodDescriptionstatic <C,P> IEventBusWire of(IEventBusWire delegate, IEventBus<C> commonBus, Function<P, C> platformToCommon) Constructs a newCommonAdaptingEventBusWirewith the given parameters.static <C,P> IEventBusWire of(IEventBusWire delegate, IEventBus<C> commonBus, Function<P, C> platformToCommon, BiConsumer<C, P> commonToPlatform) Constructs a newCommonAdaptingEventBusWirewith the given parameters.<T> voidregisterBusForDispatch(com.google.common.reflect.TypeToken<T> eventType, IEventBus<T> bus) Performs the necessary work to wire the givenIEventBusto its platform-specific counterpart.
-
Method Details
-
of
public static <C,P> IEventBusWire of(IEventBusWire delegate, IEventBus<C> commonBus, Function<P, C> platformToCommon) Constructs a newCommonAdaptingEventBusWirewith the given parameters.The newly built wire will first delegate to the given
delegatefor wiring, and then set up a listener that will redirect an event of typeCto one of typePon the givencommonBus. The conversion will happen accordingly to the specifiedplatformToCommonFunction.Usage of this method means that no further processing is required once the event dispatch is done to move back information from the
Cevent to thePevent, either because objects are shared between the two or because the events are both immutable. If additional behavior is required, please refer toof(IEventBusWire, IEventBus, Function, BiConsumer)instead.- Type Parameters:
C- The type of the common event.P- The type of the platform event.- Parameters:
delegate- TheIEventBusWirethat will manage the initial wiring of the target event bus.commonBus- TheIEventBusthat will be used to dispatch the event, once converted from its platform-specific form to the common one.platformToCommon- AFunctionthat, given as argument an instance of the platform-specific event, will handle the transformation to a common event instance.- Returns:
- An
IEventBusWirethat will perform the requested operations. - Since:
- 11.0.0
-
of
public static <C,P> IEventBusWire of(IEventBusWire delegate, IEventBus<C> commonBus, Function<P, C> platformToCommon, BiConsumer<C, P> commonToPlatform) Constructs a newCommonAdaptingEventBusWirewith the given parameters.The newly built wire will first delegate to the given
delegatefor wiring, and then set up a listener that will redirect an event of typeCto one of typePon the givencommonBus. The conversion will happen accordingly to the specifiedplatformToCommonFunction. Once the dispatch has been completed, thecommonToPlatformBiConsumerwill be invoked to allow copying of data from the instance ofCto the one ofP.If no further processing is required, refer to
of(IEventBusWire, IEventBus, Function)instead.- Type Parameters:
C- The type of the common event.P- The type of the platform event.- Parameters:
delegate- TheIEventBusWirethat will manage the initial wiring of the target event bus.commonBus- TheIEventBusthat will be used to dispatch the event, once converted from its platform-specific form to the common one.platformToCommon- AFunctionthat, given as argument an instance of the platform-specific event, will handle the transformation to a common event instance.commonToPlatform- ABiConsumerthat, given as arguments an instance of the common event and one of the platform-specific event, will handle copying of data between the first and the second as necessary.- Returns:
- An
IEventBusWirethat will perform the requested operations. - Since:
- 11.0.0
-
registerBusForDispatch
public <T> void registerBusForDispatch(com.google.common.reflect.TypeToken<T> eventType, IEventBus<T> bus) Description copied from interface:IEventBusWirePerforms the necessary work to wire the givenIEventBusto its platform-specific counterpart.- Specified by:
registerBusForDispatchin interfaceIEventBusWire- Type Parameters:
T- The type of the events fired in the bus.- Parameters:
eventType- The type of the events fired in the bus; this corresponds to invokingIEventBus.eventType()onbus.bus- TheIEventBusthat should be wired.
-