-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add more fluent configuration flow for event bus
- Loading branch information
Showing
7 changed files
with
100 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
dew-common/src/dev/shiza/dew/event/EventBusConfigurator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...c/dev/shiza/dew/event/EventPublisher.java → ...rc/dev/shiza/dew/event/EventExecutor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 7 additions & 2 deletions
9
dew-common/src/dev/shiza/dew/result/registry/ResultProcessorRegistryFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |