From 86584f7fcfc39aa4cb498bb95d9c68c54365bab0 Mon Sep 17 00:00:00 2001 From: Edwin Jakobs Date: Sun, 2 Feb 2020 21:35:39 +0100 Subject: [PATCH] Add TextParameter annotation --- orx-parameters/README.md | 3 +- orx-parameters/src/main/kotlin/Annotations.kt | 53 ++++++++++++------- .../src/test/kotlin/TestAnnotations.kt | 8 ++- 3 files changed, 44 insertions(+), 20 deletions(-) diff --git a/orx-parameters/README.md b/orx-parameters/README.md index 028573c2..990b193c 100644 --- a/orx-parameters/README.md +++ b/orx-parameters/README.md @@ -7,7 +7,8 @@ Currently orx-parameters supplies the following annotations: - `DoubleParameter` - `IntParameter` - - `DoubleParameter` + - `BooleanParameter` + - `TextParameter` ## Annotation application diff --git a/orx-parameters/src/main/kotlin/Annotations.kt b/orx-parameters/src/main/kotlin/Annotations.kt index 818c8bec..af3a9b12 100644 --- a/orx-parameters/src/main/kotlin/Annotations.kt +++ b/orx-parameters/src/main/kotlin/Annotations.kt @@ -11,7 +11,7 @@ import kotlin.reflect.full.findAnnotation annotation class Description(val title: String, val description: String = "") /** - * DoubleParameter annotation for a double precision Filter parameter + * DoubleParameter annotation for a double precision parameter * @property label a short description of the parameter * @property low the lowest value this parameter should be assigned * @property high the highest value this parameter should be assigned @@ -23,7 +23,7 @@ annotation class Description(val title: String, val description: String = "") annotation class DoubleParameter(val label: String, val low: Double, val high: Double, val precision: Int = 3, val order: Int = Integer.MAX_VALUE) /** - * IntParameter annotation for an integer Filter parameter + * IntParameter annotation for an integer parameter * @property label a short description of the parameter * @property low the lowest value this parameter should be assigned * @property high the highest value this parameter should be assigned @@ -34,7 +34,7 @@ annotation class DoubleParameter(val label: String, val low: Double, val high: D annotation class IntParameter(val label: String, val low: Int, val high: Int, val order: Int = Integer.MAX_VALUE) /** - * BooleanParameter annotation for an integer Filter parameter + * BooleanParameter annotation for a boolean parameter * @property label a short description of the parameter * @property order hint for where to place the parameter in user interfaces */ @@ -42,9 +42,18 @@ annotation class IntParameter(val label: String, val low: Int, val high: Int, va @Retention(AnnotationRetention.RUNTIME) annotation class BooleanParameter(val label: String, val order: Int = Integer.MAX_VALUE) +/** + * Text annotation for a text parameter + * @property label a short description of the parameter + * @property order hint for where to place the parameter in user interfaces + */ +@Target(AnnotationTarget.PROPERTY) +@Retention(AnnotationRetention.RUNTIME) +annotation class TextParameter(val label: String, val order: Int = Integer.MAX_VALUE) + /** - * ButtonParameter annotation for an integer Filter parameter + * ButtonParameter annotation for button parameter * @property label a short description of the parameter * @property order hint for where to place the parameter in user interfaces */ @@ -56,16 +65,18 @@ enum class ParameterType(val annotation: KClass) { Double(DoubleParameter::class), Int(IntParameter::class), Boolean(BooleanParameter::class), - Button(ButtonParameter::class) + Button(ButtonParameter::class), + Text(TextParameter::class) ; companion object { - fun forParameterAnnotationClass(annotation: Annotation) : ParameterType { - return when(annotation) { + fun forParameterAnnotationClass(annotation: Annotation): ParameterType { + return when (annotation) { is DoubleParameter -> Double is IntParameter -> Int is BooleanParameter -> Boolean is ButtonParameter -> Button + is TextParameter -> Text else -> error("no type for $annotation") } } @@ -103,15 +114,20 @@ fun Any.listParameters(): List { (it.findAnnotation() != null || it.findAnnotation() != null || it.findAnnotation() != null || - it.findAnnotation() != null) + it.findAnnotation() != null) || + it.findAnnotation() != null }.map { - val annotations = listOf(it.findAnnotation(), it.findAnnotation(), it.findAnnotation(), it.findAnnotation()).filterNotNull() + val annotations = listOfNotNull(it.findAnnotation(), + it.findAnnotation(), + it.findAnnotation(), + it.findAnnotation(), + it.findAnnotation()) var intRange: IntRange? = null var doubleRange: ClosedRange? = null var order: Int = Integer.MAX_VALUE - var label: String = "" + var label = "" var precision: Int? = null - var type : ParameterType? = null + var type: ParameterType? = null annotations.forEach { type = ParameterType.forParameterAnnotationClass(it) @@ -135,16 +151,17 @@ fun Any.listParameters(): List { label = it.label order = it.order } + is TextParameter -> { + label = it.label + order = it.order + } } } - Parameter(type?:error("no type"), it as KMutableProperty1, label, doubleRange, intRange, precision, order) + Parameter(type + ?: error("no type"), it as KMutableProperty1, label, doubleRange, intRange, precision, order) }.sortedBy { it.order } } -fun Any.title() = this::class.findAnnotation()?.let { - it.title -} +fun Any.title() = this::class.findAnnotation()?.title -fun Any.description() = this::class.findAnnotation()?.let { - it.description -} +fun Any.description() = this::class.findAnnotation()?.description diff --git a/orx-parameters/src/test/kotlin/TestAnnotations.kt b/orx-parameters/src/test/kotlin/TestAnnotations.kt index 70046c09..8c86dd07 100644 --- a/orx-parameters/src/test/kotlin/TestAnnotations.kt +++ b/orx-parameters/src/test/kotlin/TestAnnotations.kt @@ -16,13 +16,16 @@ val a = object { @ButtonParameter("a button parameter", order = 3) var f = {} + + @TextParameter("a text parameter", order = 4) + var t = "test" } object TestAnnotations : Spek({ describe("an annotated object") { it("has listable parameters") { val list = a.listParameters() - list.size `should be equal to` 4 + list.size `should be equal to` 5 list[0].property.name `should be equal to` "d" list[0].parameterType `should be equal to` ParameterType.Double list[0].label `should be equal to` "a double scalar" @@ -47,6 +50,9 @@ object TestAnnotations : Spek({ list[3].parameterType `should be equal to` ParameterType.Button list[3].property.name `should be equal to` "f" + + list[4].parameterType `should be equal to` ParameterType.Text + list[4].property.name `should be equal to` "t" } } })