Skip to content

Commit

Permalink
Add more flexible result handler lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
shitzuu committed Sep 11, 2024
1 parent 6808139 commit e5b6a51
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion dew-common/src/dev/shiza/dew/result/ResultHandlerService.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dev.shiza.dew.result;

import java.util.Map;
import java.util.Map.Entry;

final class ResultHandlerService implements ResultHandlerFacade {

Expand All @@ -21,7 +22,7 @@ public <T> void process(final T value) {
return;
}

final ResultHandler<?> resultHandler = handlers.get(value.getClass());
final ResultHandler<?> resultHandler = getResultHandler(value.getClass());
if (resultHandler == null) {
throw new ResultHandlingException(
"Could not handle result of type %s, because of missing result handler."
Expand All @@ -30,4 +31,20 @@ public <T> void process(final T value) {

((ResultHandler<T>) resultHandler).handle(value);
}

private ResultHandler<?> getResultHandler(final Class<?> clazz) {
final ResultHandler<?> resultHandler = handlers.get(clazz);
if (resultHandler != null) {
return resultHandler;
}

for (final Entry<Class<?>, ResultHandler<?>> entry : handlers.entrySet()) {
final Class<?> resultType = entry.getKey();
if (resultType.isAssignableFrom(clazz) || clazz.isAssignableFrom(resultType)) {
return entry.getValue();
}
}

return null;
}
}

0 comments on commit e5b6a51

Please sign in to comment.