-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
88 additions
and
59 deletions.
There are no files selected for viewing
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
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,32 @@ | ||
package io.nexure.capsule | ||
|
||
internal class Dependency | ||
private constructor( | ||
val key: Key, | ||
val priority: Int, | ||
private val constructor: DependencyProvider.() -> Any, | ||
): Comparable<Dependency> { | ||
private val instance: Memoized<Any> = Memoized() | ||
|
||
fun getInstance(provider: DependencyProvider): Any { | ||
return try { | ||
instance.getOnce { | ||
provider.constructor() | ||
} | ||
} catch (e: Exception) { | ||
throw DependencyException(key, e) | ||
} | ||
} | ||
|
||
fun copy(): Dependency = Dependency(key, priority, constructor) | ||
|
||
override fun toString(): String = key.toString() | ||
|
||
override fun compareTo(other: Dependency): Int = this.priority.compareTo(other.priority) | ||
|
||
companion object { | ||
fun fromClass(clazz: Class<*>, priority: Int, constructor: DependencyProvider.() -> Any): Dependency { | ||
return Dependency(key = Key(clazz), priority = priority, constructor = constructor) | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
lib/src/main/kotlin/io/nexure/capsule/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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package io.nexure.capsule | ||
|
||
interface DependencyProvider { | ||
fun <T : Any> tryGet(clazz: Class<T>): T? | ||
fun <T : Any> get(clazz: Class<T>): T = tryGet(clazz) ?: throw DependencyException(clazz) | ||
fun <T : Any> getMany(clazz: Class<T>): List<T> | ||
} | ||
|
||
inline fun <reified T : Any> DependencyProvider.get(): T = get(T::class.java) | ||
inline fun <reified T : Any> DependencyProvider.tryGet(): T? = tryGet(T::class.java) | ||
inline fun <reified T : Any> DependencyProvider.getMany(): List<T> = getMany(T::class.java) |
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,34 @@ | ||
package io.nexure.capsule | ||
|
||
import java.util.LinkedList | ||
|
||
internal class Registry( | ||
private val dependencies: MutableMap<Key, MutableList<Dependency>> = HashMap() | ||
) { | ||
fun getDependencies(key: Key): List<Dependency> = dependencies[key]?.sorted() ?: emptyList() | ||
|
||
fun add(dependency: Dependency) { | ||
dependencies.compute(dependency.key) { _: Key, value: MutableList<Dependency>? -> | ||
val list: MutableList<Dependency> = value ?: LinkedList() | ||
list.add(dependency) | ||
list | ||
} | ||
} | ||
|
||
operator fun plus(other: Registry): Registry { | ||
val dependencies: MutableMap<Key, MutableList<Dependency>> = HashMap() | ||
|
||
sequenceOf(this.dependencies.values, other.dependencies.values) | ||
.flatten() | ||
.flatten() | ||
.map { it.copy() } | ||
.groupByTo(dependencies) { it.key } | ||
|
||
return Registry(dependencies) | ||
} | ||
|
||
override fun toString(): String = dependencies.asSequence() | ||
.map { it.value } | ||
.flatten() | ||
.joinToString(separator = "\n", prefix = "\n") { "- ${it.key}" } | ||
} |