-
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.
Move mapping mediator builder into core package
- Loading branch information
Showing
5 changed files
with
68 additions
and
55 deletions.
There are no files selected for viewing
9 changes: 9 additions & 0 deletions
9
projects/kediatr-core/src/main/kotlin/com/trendyol/kediatr/DependencyProvider.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 |
---|---|---|
@@ -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>> | ||
} |
56 changes: 56 additions & 0 deletions
56
projects/kediatr-core/src/main/kotlin/com/trendyol/kediatr/MappingDependencyProvider.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,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 | ||
} | ||
} | ||
} |
3 changes: 2 additions & 1 deletion
3
projects/kediatr-core/src/test/kotlin/com/trendyol/kediatr/MediatorTests.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
46 changes: 0 additions & 46 deletions
46
...tr-core/src/testFixtures/kotlin/com/trendyol/kediatr/testing/MappingDependencyProvider.kt
This file was deleted.
Oops, something went wrong.
9 changes: 1 addition & 8 deletions
9
projects/kediatr-core/src/testFixtures/kotlin/com/trendyol/kediatr/testing/convention.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 |
---|---|---|
@@ -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 | ||
} | ||
} |