com.terheyden.event.ModifiableEventRouterImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of event-router Show documentation
Show all versions of event-router Show documentation
Simple, fast, flexible event router / event bus for Java
The newest version!
package com.terheyden.event;
import java.util.Collection;
import java.util.UUID;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.stream.Collectors;
/**
* EventRouter class.
* Not static, so you can have multiple event routers.
* You can always make it static if they want.
*/
public class ModifiableEventRouterImpl extends BaseEventRouter implements ModifiableEventRouter {
ModifiableEventRouterImpl(SubscriberExceptionHandler eventHandler, ThreadPoolExecutor threadPoolExecutor) {
super(threadPoolExecutor, new ModifiableEventSendStrategy<>(eventHandler));
}
@Override
public UUID subscribe(CheckedFunction eventHandler) {
ModifiableEventSubscription subscription = new ModifiableEventSubscription<>(eventHandler);
getSubscriberManager().subscribe(subscription);
return subscription.getSubscriptionId();
}
@Override
public UUID subscribeReadOnly(CheckedConsumer eventHandler) {
ModifiableEventSubscription subscription = new ModifiableEventSubscription<>(eventObj -> {
eventHandler.accept(eventObj);
return eventObj;
});
getSubscriberManager().subscribe(subscription);
return subscription.getSubscriptionId();
}
@Override
public void unsubscribe(UUID subscriptionId) {
getSubscriberManager().unsubscribe(subscriptionId);
}
@Override
public void publish(T eventObj) {
publishInternal(new EventRequest<>(eventObj));
}
@Override
public ThreadPoolExecutor getThreadPool() {
return super.getThreadPool();
}
@Override
public Collection getSubscriptions() {
return getSubscribers()
.stream()
.map(EventSubscription::getSubscriptionId)
.collect(Collectors.toList());
}
}