Skip to content

Commit

Permalink
Add more fluent configuration flow for event bus
Browse files Browse the repository at this point in the history
  • Loading branch information
rchomczyk committed Nov 14, 2024
1 parent 15b48f7 commit 1601f3e
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 73 deletions.
11 changes: 1 addition & 10 deletions dew-common/src/dev/shiza/dew/event/EventBus.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,13 @@
package dev.shiza.dew.event;

import dev.shiza.dew.result.ResultProcessor;
import dev.shiza.dew.subscription.Subscriber;
import dev.shiza.dew.subscription.SubscribingException;
import org.jetbrains.annotations.Contract;

public sealed interface EventBus permits EventBusImpl {

@Contract("_ -> this")
EventBus publisher(final EventPublisher eventPublisher);

@Contract("_, _ -> this")
<E extends Event, T> EventBus result(
final Class<T> resultType, final ResultProcessor<E, T> resultProcessor);

void subscribe(final Subscriber subscriber) throws SubscribingException;

void publish(final EventPublisher eventPublisher, final Event event, final String... targets)
void publish(final EventExecutor eventExecutor, final Event event, final String... targets)
throws EventPublishingException;

void publish(final Event event, final String... targets) throws EventPublishingException;
Expand Down
74 changes: 74 additions & 0 deletions dew-common/src/dev/shiza/dew/event/EventBusConfigurator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package dev.shiza.dew.event;

import dev.shiza.dew.result.ResultProcessor;
import dev.shiza.dew.result.ResultProcessorFacade;
import dev.shiza.dew.result.ResultProcessorFacadeFactory;
import dev.shiza.dew.result.registry.ResultProcessorRegistry;
import dev.shiza.dew.result.registry.ResultProcessorRegistryFactory;
import dev.shiza.dew.subscription.SubscriptionFacadeFactory;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Consumer;

public final class EventBusConfigurator {

private final EventExecuteConfig eventExecuteConfig = new EventExecuteConfig();
private final ResultProcessingConfig resultProcessingConfig = new ResultProcessingConfig();

private EventBusConfigurator() {}

public static EventBus configure(final Consumer<EventBusConfigurator> consumer) {
final EventBusConfigurator eventBusConfigurator = new EventBusConfigurator();
consumer.accept(eventBusConfigurator);

final EventExecuteConfig eventExecuteConfig = eventBusConfigurator.eventExecuteConfig;
final EventExecutor eventExecutor = eventExecuteConfig.eventExecutor;

final ResultProcessingConfig resultProcessingConfig =
eventBusConfigurator.resultProcessingConfig;
final ResultProcessorRegistry resultProcessorRegistry =
ResultProcessorRegistryFactory.createMapBasedRegistry(
resultProcessingConfig.resultProcessors);
final ResultProcessorFacade resultProcessorFacade =
ResultProcessorFacadeFactory.create(resultProcessorRegistry);

return new EventBusImpl(
SubscriptionFacadeFactory.create(),
resultProcessorFacade,
resultProcessorRegistry,
eventExecutor);
}

public void execute(final Consumer<EventExecuteConfig> consumer) {
consumer.accept(eventExecuteConfig);
}

public void process(final Consumer<ResultProcessingConfig> consumer) {
consumer.accept(resultProcessingConfig);
}

public static final class EventExecuteConfig {

private EventExecutor eventExecutor = Runnable::run;

private EventExecuteConfig() {}

public void executor(final EventExecutor eventExecutor) {
this.eventExecutor = eventExecutor;
}
}

public static final class ResultProcessingConfig {

private final Map<Class<?>, ResultProcessor<? extends Event, ?>> resultProcessors;

private ResultProcessingConfig() {
this.resultProcessors = new HashMap<>();
}

public <E extends Event, T> void processor(
final Class<T> resultType, final ResultProcessor<E, T> resultProcessor) {
resultProcessors.put(resultType, resultProcessor);
}
}
}
29 changes: 0 additions & 29 deletions dew-common/src/dev/shiza/dew/event/EventBusFactory.java

This file was deleted.

33 changes: 10 additions & 23 deletions dew-common/src/dev/shiza/dew/event/EventBusImpl.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package dev.shiza.dew.event;

import dev.shiza.dew.result.ResultProcessor;
import dev.shiza.dew.result.ResultProcessorFacade;
import dev.shiza.dew.result.registry.ResultProcessorRegistry;
import dev.shiza.dew.subscription.Subscriber;
Expand All @@ -15,28 +14,17 @@ final class EventBusImpl implements EventBus {
private final SubscriptionFacade subscriptionFacade;
private final ResultProcessorFacade resultProcessorFacade;
private final ResultProcessorRegistry resultProcessorRegistry;
private EventPublisher eventPublisher;
private final EventExecutor eventExecutor;

EventBusImpl(
final SubscriptionFacade subscriptionFacade,
final ResultProcessorFacade resultProcessorFacade,
final ResultProcessorRegistry resultProcessorRegistry) {
final ResultProcessorRegistry resultProcessorRegistry,
final EventExecutor eventExecutor) {
this.subscriptionFacade = subscriptionFacade;
this.resultProcessorFacade = resultProcessorFacade;
this.resultProcessorRegistry = resultProcessorRegistry;
}

@Override
public EventBus publisher(final EventPublisher eventPublisher) {
this.eventPublisher = eventPublisher;
return this;
}

@Override
public <E extends Event, T> EventBus result(
final Class<T> resultType, final ResultProcessor<E, T> resultProcessor) {
resultProcessorRegistry.register(resultType, resultProcessor);
return this;
this.eventExecutor = eventExecutor;
}

@Override
Expand All @@ -45,29 +33,28 @@ public void subscribe(final Subscriber subscriber) {
}

@Override
public void publish(
final EventPublisher eventPublisher, final Event event, final String... targets)
public void publish(final EventExecutor eventExecutor, final Event event, final String... targets)
throws EventPublishingException {
final Set<Subscription> subscriptions =
subscriptionFacade.getSubscriptionsByEventType(event.getClass());
for (final Subscription subscription : subscriptions) {
notifySubscription(subscription, eventPublisher, event, targets);
notifySubscription(subscription, eventExecutor, event, targets);
}
}

@Override
public void publish(final Event event, final String... targets) {
if (eventPublisher == null) {
if (eventExecutor == null) {
throw new EventPublishingException(
"Could not publish event, because of not specifying default event publisher.");
}

publish(eventPublisher, event, targets);
publish(eventExecutor, event, targets);
}

private void notifySubscription(
final Subscription subscription,
final EventPublisher eventPublisher,
final EventExecutor eventExecutor,
final Event event,
final String[] targets) {
final Subscriber subscriber = subscription.subscriber();
Expand All @@ -76,7 +63,7 @@ private void notifySubscription(
}

for (final MethodHandle invocation : subscription.invocations()) {
eventPublisher.execute(() -> notifySubscribedMethods(invocation, subscriber, event));
eventExecutor.execute(() -> notifySubscribedMethods(invocation, subscriber, event));
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package dev.shiza.dew.event;

@FunctionalInterface
public interface EventPublisher {
public interface EventExecutor {

void execute(final Runnable task);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,31 @@

import dev.shiza.dew.event.Event;
import dev.shiza.dew.result.ResultProcessor;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

final class MapBasedResultProcessorRegistry implements ResultProcessorRegistry {

private final Map<Class<?>, ResultProcessor<?, ?>> handlers;
private final Map<Class<?>, ResultProcessor<?, ?>> resultProcessors;

MapBasedResultProcessorRegistry() {
this.handlers = new HashMap<>();
MapBasedResultProcessorRegistry(final Map<Class<?>, ResultProcessor<?, ?>> resultProcessors) {
this.resultProcessors = resultProcessors;
}

@Override
public <E extends Event, T> void register(
final Class<T> resultType, final ResultProcessor<E, T> resultProcessor) {
handlers.put(resultType, resultProcessor);
resultProcessors.put(resultType, resultProcessor);
}

@Override
public ResultProcessor<?, ?> getResultHandlerByClass(final Class<?> resultType) {
final ResultProcessor<?, ?> resultProcessor = handlers.get(resultType);
final ResultProcessor<?, ?> resultProcessor = resultProcessors.get(resultType);
if (resultProcessor != null) {
return resultProcessor;
}

for (final Entry<Class<?>, ResultProcessor<?, ?>> entry : handlers.entrySet()) {
for (final Entry<Class<?>, ResultProcessor<?, ?>> entry : resultProcessors.entrySet()) {
if (isAssignableFrom(entry.getKey(), resultType)) {
return entry.getValue();
}
Expand All @@ -38,7 +37,7 @@ public <E extends Event, T> void register(

@Override
public boolean isProcessingRequired() {
return !handlers.isEmpty();
return !resultProcessors.isEmpty();
}

private boolean isAssignableFrom(final Class<?> type, final Class<?> otherType) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package dev.shiza.dew.result.registry;

import dev.shiza.dew.event.Event;
import dev.shiza.dew.result.ResultProcessor;
import java.util.Map;

public final class ResultProcessorRegistryFactory {

private ResultProcessorRegistryFactory() {}

public static ResultProcessorRegistry createMapBasedRegistry() {
return new MapBasedResultProcessorRegistry();
public static ResultProcessorRegistry createMapBasedRegistry(
final Map<Class<?>, ResultProcessor<? extends Event, ?>> resultProcessors) {
return new MapBasedResultProcessorRegistry(resultProcessors);
}
}

0 comments on commit 1601f3e

Please sign in to comment.