-
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.
Merge pull request #4 from MechDancer/generic
Rework generic finding algorithm, making unique component and named component interfaces
- Loading branch information
Showing
11 changed files
with
327 additions
and
78 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
25 changes: 25 additions & 0 deletions
25
src/main/kotlin/org/mechdancer/dependency/INamedComponent.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,25 @@ | ||
package org.mechdancer.dependency | ||
|
||
import kotlin.reflect.KClass | ||
import kotlin.reflect.safeCast | ||
|
||
/** | ||
* [INamedComponent] is a type of [Component] associated with unique [name] and [T] | ||
* | ||
* [INamedComponent]s with the same type and name can not coexist in the scope | ||
*/ | ||
interface INamedComponent<T : INamedComponent<T>> : Component { | ||
|
||
val name: String | ||
|
||
val type: KClass<out INamedComponent<*>> | ||
|
||
fun defaultType() = javaClass.kotlin.findSuperGenericTypeRecursively(INamedComponent::class) | ||
|
||
fun defaultEquals(other: Any?) = | ||
this === other || type.safeCast(other)?.name == name | ||
|
||
fun defaultHashCode() = | ||
(type.hashCode() shl 31) + name.hashCode() | ||
|
||
} |
19 changes: 19 additions & 0 deletions
19
src/main/kotlin/org/mechdancer/dependency/IUniqueComponent.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,19 @@ | ||
package org.mechdancer.dependency | ||
|
||
import kotlin.reflect.KClass | ||
import kotlin.reflect.safeCast | ||
|
||
/** | ||
* [IUniqueComponent] is a type of [Component] associated with unique type [T] | ||
* | ||
* [IUniqueComponent]s with the same type can not coexist in the scope | ||
*/ | ||
interface IUniqueComponent<T : IUniqueComponent<T>> : Component { | ||
val type: KClass<out IUniqueComponent<*>> | ||
|
||
fun defaultType() = javaClass.kotlin.findSuperGenericTypeRecursively(IUniqueComponent::class) | ||
|
||
fun defaultHashCode() = type.hashCode() | ||
|
||
fun defaultEquals(other: Any?) = this === other || type.safeCast(other) !== null | ||
} |
20 changes: 7 additions & 13 deletions
20
src/main/kotlin/org/mechdancer/dependency/NamedComponent.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,22 +1,16 @@ | ||
package org.mechdancer.dependency | ||
|
||
import kotlin.reflect.KClass | ||
import kotlin.reflect.safeCast | ||
|
||
/** | ||
* [NamedComponent] is a type of [Component] associated with unique [name] and [T] | ||
* | ||
* [NamedComponent]s with the same type and name can not coexist in the scope | ||
* Class version of [INamedComponent] for convenience | ||
*/ | ||
abstract class NamedComponent<T : NamedComponent<T>> | ||
(val name: String, type: KClass<T>? = null) : Component { | ||
abstract class NamedComponent<T : NamedComponent<T>>(override val name: String) : | ||
INamedComponent<T> { | ||
override fun equals(other: Any?) = defaultEquals(other) | ||
|
||
@Suppress("UNCHECKED_CAST") | ||
val type = type ?: javaClass.kotlin.firstGenericType(NamedComponent::class) as KClass<T> | ||
override fun hashCode(): Int = defaultHashCode() | ||
|
||
override fun equals(other: Any?) = | ||
this === other || type.safeCast(other)?.name == name | ||
override val type: KClass<out INamedComponent<*>> by lazy { defaultType() } | ||
|
||
override fun hashCode() = | ||
(type.hashCode() shl 31) + name.hashCode() | ||
} | ||
} |
18 changes: 6 additions & 12 deletions
18
src/main/kotlin/org/mechdancer/dependency/UniqueComponent.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,20 +1,14 @@ | ||
package org.mechdancer.dependency | ||
|
||
import kotlin.reflect.KClass | ||
import kotlin.reflect.safeCast | ||
|
||
/** | ||
* [UniqueComponent] is a type of [Component] associated with unique type [T] | ||
* | ||
* [UniqueComponent]s with the same type can not coexist in the scope | ||
* Class version of [IUniqueComponent] for convenience | ||
*/ | ||
abstract class UniqueComponent<T : UniqueComponent<T>>(type: KClass<T>? = null) : Component { | ||
abstract class UniqueComponent<T : UniqueComponent<T>> : | ||
IUniqueComponent<T> { | ||
|
||
val type = type ?: javaClass.kotlin.firstGenericType(UniqueComponent::class) | ||
override val type by lazy { defaultType() } | ||
|
||
override fun equals(other: Any?) = | ||
this === other || type.safeCast(other) !== null | ||
override fun equals(other: Any?) = defaultEquals(other) | ||
|
||
override fun hashCode() = | ||
type.hashCode() | ||
override fun hashCode() = defaultHashCode() | ||
} |
21 changes: 21 additions & 0 deletions
21
src/main/kotlin/org/mechdancer/dependency/UniqueComponentWrapper.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,21 @@ | ||
package org.mechdancer.dependency | ||
|
||
import kotlin.reflect.KClass | ||
import kotlin.reflect.safeCast | ||
|
||
class UniqueComponentWrapper<T : Any> @PublishedApi internal constructor( | ||
val type: KClass<*>, | ||
val wrapped: T | ||
) : Component { | ||
|
||
companion object { | ||
inline operator fun <reified T : Any> invoke(wrapped: T) = | ||
UniqueComponentWrapper(T::class, wrapped) | ||
} | ||
|
||
override fun equals(other: Any?): Boolean = | ||
this === other || (other is UniqueComponentWrapper<*> && type.safeCast(other.wrapped) !== null) | ||
|
||
override fun hashCode(): Int = type.hashCode() | ||
|
||
} |
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
Oops, something went wrong.