Skip to content

Commit

Permalink
Move mapping mediator builder into core package
Browse files Browse the repository at this point in the history
  • Loading branch information
osoykan committed Oct 30, 2024
1 parent 00106de commit 1462db5
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 55 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
package com.trendyol.kediatr

/**
* Dependency provider interface.
*/
interface DependencyProvider {
/**
* Gets a single instance of the specified class.
*/
fun <T> getSingleInstanceOf(clazz: Class<T>): T

/**
* Gets all subtypes of the specified class.
*/
fun <T> getSubTypesOf(clazz: Class<T>): Collection<Class<T>>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
@file:Suppress("UNCHECKED_CAST")

package com.trendyol.kediatr

import java.lang.reflect.ParameterizedType

/**
* Dependency provider that uses a map to resolve dependencies.
* @param handlerMap A map that contains the handlers.
* The key is the handler class and the value is the handler instance.
* @see DependencyProvider
*/
class MappingDependencyProvider(
private val handlerMap: HashMap<Class<*>, Any>
) : DependencyProvider {
override fun <T> getSingleInstanceOf(clazz: Class<T>): T = handlerMap[clazz] as T

override fun <T> getSubTypesOf(clazz: Class<T>): Collection<Class<T>> = handlerMap.keys
.filter { isCompatibleType(it, clazz) }
.map { it as Class<T> }

private fun <T> isCompatibleType(
handler: Class<*>,
interfaceOrBaseClass: Class<T>
): Boolean = when {
interfaceOrBaseClass.isAssignableFrom(handler) -> true
handler.genericInterfaces
.filterIsInstance<ParameterizedType>()
.any { it.rawType == interfaceOrBaseClass } -> true

else -> when (val superclass = handler.genericSuperclass) {
is ParameterizedType -> {
val inheritedHandler = superclass.rawType as Class<*>
inheritedHandler.genericInterfaces
.filterIsInstance<ParameterizedType>()
.any { it.rawType == interfaceOrBaseClass }
}

is Class<*> -> interfaceOrBaseClass.isAssignableFrom(superclass)
else -> false
}
}

companion object {
/**
* Creates a mediator with the given handlers.
* @param handlers The handlers to be used by the mediator. Can also be [PipelineBehavior] instances.
* @return A mediator instance.
*/
fun createMediator(handlers: List<Any> = emptyList()): Mediator {
val provider = MappingDependencyProvider(handlers.associateBy { it.javaClass } as HashMap<Class<*>, Any>)
val mediator = MediatorBuilder(provider).build()
return mediator
}
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package com.trendyol.kediatr

import com.trendyol.kediatr.MappingDependencyProvider.Companion.createMediator
import com.trendyol.kediatr.testing.*
import io.kotest.matchers.shouldBe
import org.junit.jupiter.api.Test

class MediatorTests : MediatorUseCases() {
override fun provideMediator(): Mediator = createMediator(
types = listOf(
handlers = listOf(
TestCommandHandler(mediator = { testMediator }),
TestPipelineCommandHandler(mediator = { testMediator }),
TestCommandWithResultCommandHandler(mediator = { testMediator }),
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
package com.trendyol.kediatr.testing

import com.trendyol.kediatr.*
import com.trendyol.kediatr.Mediator

@Suppress("UNCHECKED_CAST")
abstract class MediatorTestConvention {
abstract fun provideMediator(): Mediator

val testMediator by lazy { provideMediator() }

protected fun createMediator(types: List<Any> = emptyList()): Mediator {
val provider = MappingDependencyProvider(types.associateBy { it.javaClass } as HashMap<Class<*>, Any>)
val mediator = MediatorBuilder(provider).build()
return mediator
}
}

0 comments on commit 1462db5

Please sign in to comment.