Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.neroyun</groupId>
<artifactId>mediator</artifactId>
<version>1.0.1</version>
<version>1.0.2</version>
<name>Mediator</name>
<description>A simple mediator pattern implementation in Java</description>
<url>https://github.com/nerosoftdev/mediator</url>
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/com/neroyun/mediator/Mediator.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package com.neroyun.mediator;

import com.neroyun.mediator.internal.QueryCallback;

/**
* Defines the Mediator interface for handling commands, queries, and events.
* The Mediator pattern promotes loose coupling between components by centralizing communication.
* This interface can be implemented to create a concrete mediator that manages the interactions between various components in the system.
*/
@SuppressWarnings("unused")
public interface Mediator {

/**
Expand All @@ -26,11 +29,11 @@ public interface Mediator {
/**
* Executes a query and provides the result to the specified response handler.
* @param query the query to be executed
* @param response the response handler
* @param callback the callback to handle the result of the query
* @param <T> the type of the query
* @param <R> the type of the result
*/
<T extends Query<R>, R> void execute(T query, R response);
<T extends Query<R>, R> void execute(T query, QueryCallback<R> callback);

/**
* Publishes an event to all interested handlers.
Expand Down
30 changes: 11 additions & 19 deletions src/main/java/com/neroyun/mediator/PipelinedMediator.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,22 +73,22 @@ public <T extends Query<R>, R> R execute(@NotNull T query) {
}

@Override
public <T extends Query<R>, R> void execute(@NotNull T query, R response) {
public <T extends Query<R>, R> void execute(@NotNull T query, QueryCallback<R> callback) {
checkArguments(query, "Query can not be null.");
var result = execute(query);
if (callback != null) {
callback.onCompleted(result);
}
}

@Override
public <T extends Event> void publish(@NotNull T event) {
checkArguments(event, "Event can not be null.");

List<Runnable> tasks = handlers.supply()
.filter(handler -> handler.matches(event))
.map(handler -> (Handler<Event, Void>) handler)
.map(handler -> (Runnable) () -> {
var pipeline = buildMiddlewarePipeline(event, () -> handler.handle(event));
pipeline.invoke();
})
.toList();
List<Runnable> tasks = handlers.supply().filter(handler -> handler.matches(event)).map(handler -> (Handler<Event, Void>) handler).map(handler -> (Runnable) () -> {
var pipeline = buildMiddlewarePipeline(event, () -> handler.handle(event));
pipeline.invoke();
}).toList();

if (tasks.isEmpty()) {
return;
Expand Down Expand Up @@ -134,11 +134,7 @@ public <T extends Event> void publish(@NotNull T event) {
*/
private <T extends Message<R>, R> Handler<T, R> resolveHandler(T message) {
// resolve handler from handlers stream
return handlers.supply()
.filter(handler -> handler.matches(message))
.map(handler -> (Handler<T, R>) handler)
.findFirst()
.orElseThrow(() -> new RuntimeException("No handler found for message: " + message.getClass().getName()));
return handlers.supply().filter(handler -> handler.matches(message)).map(handler -> (Handler<T, R>) handler).findFirst().orElseThrow(() -> new RuntimeException("No handler found for message: " + message.getClass().getName()));
}

/**
Expand All @@ -150,11 +146,7 @@ private <T extends Message<R>, R> Handler<T, R> resolveHandler(T message) {
* @param <R> the type of the response produced by the message handler
*/
private <T extends Validatable & Message<R>, R> void validate(T message) {
var errors = validators.supply()
.map(validator -> validator.validate(message))
.filter(ValidationResult::isFailure)
.flatMap(result -> result.errors().stream())
.toList();
var errors = validators.supply().map(validator -> validator.validate(message)).filter(ValidationResult::isFailure).flatMap(result -> result.errors().stream()).toList();
if (!errors.isEmpty()) {
throw new ValidationException(errors);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.neroyun.mediator.internal;

@FunctionalInterface
public interface QueryCallback<R> {
void onCompleted(R results);
}
Loading