-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Organize Sync and Async registration #24
- Loading branch information
Showing
4 changed files
with
189 additions
and
147 deletions.
There are no files selected for viewing
43 changes: 43 additions & 0 deletions
43
kediatr-core/src/main/kotlin/com/trendyol/kediatr/AsyncRegistry.kt
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,43 @@ | ||
package com.trendyol.kediatr | ||
|
||
import com.trendyol.kediatr.common.AsyncNotificationProvider | ||
import com.trendyol.kediatr.common.AsyncPipelineProvider | ||
import com.trendyol.kediatr.common.AsyncQueryProvider | ||
|
||
@Suppress("UNCHECKED_CAST") | ||
internal class AsyncRegistry(dependencyProvider: DependencyProvider) : Registrar() { | ||
val commandMap = HashMap<Class<*>, AsyncCommandProvider<AsyncCommandHandler<Command>>>() | ||
val queryMap = HashMap<Class<*>, AsyncQueryProvider<AsyncQueryHandler<*, *>>>() | ||
val notificationMap = | ||
HashMap<Class<*>, MutableList<AsyncNotificationProvider<AsyncNotificationHandler<*>>>>() | ||
val pipelineSet = HashSet<AsyncPipelineProvider<*>>() | ||
val commandWithResultMap = | ||
HashMap<Class<*>, AsyncCommandWithResultProvider<*>>() | ||
|
||
init { | ||
|
||
registerFor<AsyncQueryHandler<Query<*>, *>, Query<*>>(dependencyProvider) { key, value -> | ||
queryMap[key] = AsyncQueryProvider(dependencyProvider, value as Class<AsyncQueryHandler<*, *>>) | ||
} | ||
|
||
registerFor<AsyncCommandHandler<Command>, Command>(dependencyProvider) { key, value -> | ||
commandMap[key] = AsyncCommandProvider(dependencyProvider, value) | ||
} | ||
|
||
registerFor<AsyncCommandWithResultHandler<CommandWithResult<*>, *>, CommandWithResult<*>>(dependencyProvider) { key, value -> | ||
commandWithResultMap[key] = AsyncCommandWithResultProvider( | ||
dependencyProvider, | ||
value as Class<AsyncCommandWithResultHandler<*, *>> | ||
) | ||
} | ||
|
||
registerFor<AsyncNotificationHandler<Notification>, Notification>(dependencyProvider) { key, value -> | ||
notificationMap.getOrPut(key) { mutableListOf() } | ||
.add(AsyncNotificationProvider(dependencyProvider, value as Class<AsyncNotificationHandler<*>>)) | ||
} | ||
|
||
registerFor<AsyncPipelineBehavior>(dependencyProvider) { handler -> | ||
pipelineSet.add(AsyncPipelineProvider(dependencyProvider, handler)) | ||
} | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
kediatr-core/src/main/kotlin/com/trendyol/kediatr/Registrar.kt
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,71 @@ | ||
package com.trendyol.kediatr | ||
|
||
import java.lang.reflect.ParameterizedType | ||
|
||
@Suppress("UNCHECKED_CAST") | ||
abstract class Registrar { | ||
|
||
protected inline fun <reified THandler : Any, TParameter> registerFor( | ||
dependencyProvider: DependencyProvider, | ||
registrar: (key: Class<TParameter>, value: Class<THandler>) -> Unit, | ||
) = dependencyProvider.getSubTypesOf(THandler::class.java).forEach { | ||
registerFor<THandler, TParameter>(it) { key, value -> | ||
registrar(key as Class<TParameter>, value as Class<THandler>) | ||
} | ||
} | ||
|
||
protected inline fun <reified THandler : Any, TParameter> registerFor( | ||
handler: Class<*>, | ||
registrar: (key: Class<*>, value: Class<*>) -> Unit, | ||
) { | ||
val interfaceOrBaseClass = THandler::class.java | ||
if (!interfaceOrBaseClass.isAssignableFrom(handler)) return | ||
|
||
handler.genericInterfaces | ||
.filterIsInstance<ParameterizedType>() | ||
.map { extractParameter<TParameter>(it) } | ||
.forEach { registrar(it, handler) } | ||
|
||
when (handler.genericSuperclass) { | ||
is ParameterizedType -> { | ||
val inheritedHandler = (handler.genericSuperclass as ParameterizedType).rawType as Class<*> | ||
inheritedHandler.genericInterfaces | ||
.filterIsInstance<ParameterizedType>() | ||
.map { extractParameter<TParameter>(handler.genericSuperclass as ParameterizedType) } | ||
.forEach { registrar(it, handler) } | ||
} | ||
|
||
is Class<*> -> { | ||
val inheritedHandler = (handler.genericSuperclass as Class<*>) | ||
if (interfaceOrBaseClass.isAssignableFrom(inheritedHandler)) { | ||
inheritedHandler.genericInterfaces | ||
.filterIsInstance<ParameterizedType>() | ||
.map { extractParameter<TParameter>(it) } | ||
.forEach { registrar(it, handler) } | ||
} | ||
} | ||
} | ||
} | ||
|
||
protected inline fun <reified T> registerFor( | ||
dependencyProvider: DependencyProvider, | ||
registrar: (value: Class<T>) -> Unit, | ||
) = dependencyProvider.getSubTypesOf(T::class.java).forEach { handler -> | ||
registerFor<T>(handler) { value -> registrar(value as Class<T>) } | ||
} | ||
|
||
protected inline fun <reified T> registerFor( | ||
handler: Class<*>, | ||
registrar: (value: Class<*>) -> Unit, | ||
) { | ||
val interfaceOrBaseClass = T::class.java | ||
if (!interfaceOrBaseClass.isAssignableFrom(handler)) return | ||
registrar(handler) | ||
} | ||
|
||
protected fun <T> extractParameter(genericInterface: ParameterizedType): Class<out T> = | ||
when (val typeArgument = genericInterface.actualTypeArguments[0]) { | ||
is ParameterizedType -> typeArgument.rawType as Class<out T> | ||
else -> typeArgument as Class<out T> | ||
} | ||
} |
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
38 changes: 38 additions & 0 deletions
38
kediatr-core/src/main/kotlin/com/trendyol/kediatr/SyncRegistry.kt
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,38 @@ | ||
package com.trendyol.kediatr | ||
|
||
import com.trendyol.kediatr.common.NotificationProvider | ||
import com.trendyol.kediatr.common.PipelineProvider | ||
import com.trendyol.kediatr.common.QueryProvider | ||
|
||
@Suppress("UNCHECKED_CAST") | ||
internal class SyncRegistry(dependencyProvider: DependencyProvider) : Registrar() { | ||
val queryMap = HashMap<Class<*>, QueryProvider<QueryHandler<*, *>>>() | ||
val commandMap = HashMap<Class<*>, CommandProvider<CommandHandler<Command>>>() | ||
val notificationMap = HashMap<Class<*>, MutableList<NotificationProvider<NotificationHandler<*>>>>() | ||
val pipelineSet = HashSet<PipelineProvider<*>>() | ||
val commandWithResultMap = HashMap<Class<*>, CommandWithResultProvider<CommandWithResultHandler<*, *>>>() | ||
|
||
init { | ||
registerFor<QueryHandler<Query<*>, *>, Query<*>>(dependencyProvider) { key, value -> | ||
queryMap[key] = QueryProvider(dependencyProvider, value as Class<QueryHandler<*, *>>) | ||
} | ||
|
||
registerFor<CommandHandler<Command>, Command>(dependencyProvider) { key, value -> | ||
commandMap[key] = CommandProvider(dependencyProvider, value) | ||
} | ||
|
||
registerFor<CommandWithResultHandler<CommandWithResult<*>, *>, CommandWithResult<*>>(dependencyProvider) { key, value -> | ||
commandWithResultMap[key] = | ||
CommandWithResultProvider(dependencyProvider, value as Class<CommandWithResultHandler<*, *>>) | ||
} | ||
|
||
registerFor<NotificationHandler<Notification>, Notification>(dependencyProvider) { key, value -> | ||
notificationMap.getOrPut(key) { mutableListOf() } | ||
.add(NotificationProvider(dependencyProvider, value as Class<NotificationHandler<*>>)) | ||
} | ||
|
||
registerFor<PipelineBehavior>(dependencyProvider) { handler -> | ||
pipelineSet.add(PipelineProvider(dependencyProvider, handler)) | ||
} | ||
} | ||
} |