-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from traversals/automatic
Automatic injectors
- Loading branch information
Showing
19 changed files
with
433 additions
and
51 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
34 changes: 34 additions & 0 deletions
34
kapsule-core/src/main/kotlin/space/traversal/kapsule/Injects.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,34 @@ | ||
/* | ||
* Copyright 2017 Traversal Space | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
*/ | ||
|
||
package space.traversal.kapsule | ||
|
||
/** | ||
* Injection interface. | ||
*/ | ||
interface Injects<M> { | ||
|
||
/** | ||
* Fetches [Kapsule] instance and calls [Kapsule.required]. | ||
*/ | ||
fun <T> required(initializer: M.() -> T) = Kapsules.get(this).required(initializer) | ||
|
||
/** | ||
* Fetches [Kapsule] instance and calls [Kapsule.optional]. | ||
*/ | ||
fun <T> optional(initializer: M.() -> T?) = Kapsules.get(this).optional(initializer) | ||
|
||
/** | ||
* Fetches [Kapsule] instance and calls [Kapsule.inject]. | ||
*/ | ||
fun <M> Injects<M>.inject(module: M) { | ||
Kapsules.get(this).inject(module) | ||
} | ||
} |
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
kapsule-core/src/main/kotlin/space/traversal/kapsule/Kapsules.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 @@ | ||
/* | ||
* Copyright 2017 Traversal Space | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
*/ | ||
|
||
package space.traversal.kapsule | ||
|
||
import space.traversal.kapsule.util.CallerMap | ||
|
||
/** | ||
* Static storage of [Kapsule] instances. | ||
*/ | ||
object Kapsules { | ||
|
||
internal val instances = CallerMap<Injects<*>, Kapsule<*>>() | ||
|
||
/** | ||
* Retrieves active instance of [Kapsule] or creates a new one. | ||
* | ||
* @param caller Injection caller instance, used as lookup key. | ||
* @return Stored or new instance. | ||
*/ | ||
fun <M> get(caller: Injects<M>) = fetch(caller) ?: Kapsule<M>().apply { instances[caller] = this } | ||
|
||
/** | ||
* Fetches stored instance. | ||
* | ||
* @param caller Injection caller instance, used as lookup key. | ||
* @return Stored instance or null. | ||
*/ | ||
@Suppress("UNCHECKED_CAST") | ||
internal fun <M> fetch(caller: Injects<M>) = instances[caller] as? Kapsule<M> | ||
} |
70 changes: 70 additions & 0 deletions
70
kapsule-core/src/main/kotlin/space/traversal/kapsule/util/CallerMap.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,70 @@ | ||
/* | ||
* Copyright 2017 Traversal Space | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
*/ | ||
|
||
package space.traversal.kapsule.util | ||
|
||
import java.util.* | ||
|
||
/** | ||
* Utility map for storing caller instances. | ||
* | ||
* Implements a cache such that the most recent entry is returned at constant time. | ||
*/ | ||
internal class CallerMap<K, V> : WeakHashMap<K, V>() { | ||
|
||
@Volatile internal var lastKey: K? = null | ||
@Volatile internal var lastValue: V? = null | ||
|
||
override fun containsKey(key: K) = key == lastKey || super.containsKey(key) | ||
|
||
override fun containsValue(value: V): Boolean { | ||
return super.containsValue(value) | ||
} | ||
|
||
operator override fun get(key: K): V? { | ||
return if (key == lastKey) { | ||
lastValue | ||
} else { | ||
val value = super.get(key) | ||
setLast(key, value) | ||
return value | ||
} | ||
} | ||
|
||
operator fun set(key: K, value: V) = put(key, value) | ||
|
||
override fun put(key: K, value: V): V? { | ||
setLast(key, value) | ||
return super.put(key, value) | ||
} | ||
|
||
override fun remove(key: K): V? { | ||
if (key == lastKey) { | ||
setLast() | ||
} | ||
return super.remove(key) | ||
} | ||
|
||
override fun clear() { | ||
super.clear() | ||
setLast() | ||
} | ||
|
||
/** | ||
* Set last key-value pair cache. | ||
* | ||
* @param key Map key. | ||
* @param value Map value. | ||
*/ | ||
private fun setLast(key: K? = null, value: V? = null) { | ||
lastKey = key | ||
lastValue = value | ||
} | ||
} |
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
37 changes: 37 additions & 0 deletions
37
kapsule-core/src/test/kotlin/space/traversal/kapsule/KapsulesTest.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,37 @@ | ||
/* | ||
* Copyright 2017 Traversal Space | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
*/ | ||
|
||
package space.traversal.kapsule | ||
|
||
import junit.framework.TestCase | ||
import org.junit.Test | ||
import org.omg.CORBA.Object | ||
|
||
/** | ||
* Test case for [Kapsules]. | ||
*/ | ||
class KapsulesTest : TestCase() { | ||
|
||
@Test fun testFetch() { | ||
Kapsules.instances.clear() | ||
val caller = object : Injects<Object> {} | ||
assertEquals(null, Kapsules.fetch(caller)) | ||
assertEquals(0, Kapsules.instances.size) | ||
|
||
} | ||
|
||
@Test fun testGet() { | ||
Kapsules.instances.clear() | ||
val caller = object : Injects<Object> {} | ||
val kap = Kapsules.get(caller) | ||
assertEquals(kap, Kapsules.get(caller)) | ||
assertEquals(1, Kapsules.instances.size) | ||
} | ||
} |
Oops, something went wrong.