Parameter.kt

  1. package com.hexagontk.shell

  2. import com.hexagontk.core.text.parseOrNull
  3. import kotlin.reflect.KClass

  4. class Parameter<T : Any>(
  5.     override val type: KClass<T>,
  6.     val name: String,
  7.     override val description: String? = null,
  8.     override val regex: Regex? = null,
  9.     override val optional: Boolean = true,
  10.     override val multiple: Boolean = false,
  11.     override val tag: String? = null,
  12.     override val values: List<T> = emptyList(),
  13. ) : Property<T> {

  14.     override val names: Set<String> = setOf(name)

  15.     companion object {
  16.         private val parameterRegex = "[a-z0-9\\-]{2,}".toRegex()
  17.     }

  18.     constructor(
  19.         type: KClass<T>,
  20.         name: String,
  21.         description: String? = null,
  22.         regex: Regex? = null,
  23.         tag: String? = null,
  24.         value: T,
  25.     ) : this(type, name, description, regex, true, false, tag, listOf(value))

  26.     constructor(
  27.         type: KClass<T>,
  28.         name: String,
  29.         description: String? = null,
  30.         regex: Regex? = null,
  31.         tag: String? = null,
  32.         values: List<T>,
  33.     ) : this(type, name, description, regex, true, true, tag, values)

  34.     init {
  35.         check("Parameter", parameterRegex)
  36.     }

  37.     @Suppress("UNCHECKED_CAST") // Types checked at runtime
  38.     override fun addValues(value: Property<*>): Property<T> =
  39.         Parameter(
  40.             type,
  41.             name,
  42.             description,
  43.             regex,
  44.             optional,
  45.             multiple,
  46.             tag,
  47.             values + value.values as List<T>
  48.         )

  49.     override fun addValue(value: String): Parameter<T> =
  50.         value.parseOrNull(type)
  51.             ?.let {
  52.                 Parameter(type, name, description, regex, optional, multiple, tag, values + it)
  53.             }
  54.             ?: error("Parameter '$name' of type '${typeText()}' can not hold '$value'")

  55.     override fun clearValues(): Parameter<T> =
  56.         Parameter(type, name, description, regex, optional, multiple, tag, emptyList())

  57.     // TODO Only used in tests
  58.     fun copy(
  59.         type: KClass<T> = this.type,
  60.         name: String = this.name,
  61.         description: String? = this.description,
  62.         regex: Regex? = this.regex,
  63.         optional: Boolean = this.optional,
  64.         multiple: Boolean = this.multiple,
  65.         tag: String? = this.tag,
  66.         values: List<T> = this.values,
  67.     ): Parameter<T> =
  68.         Parameter(type, name, description, regex, optional, multiple, tag, values)

  69.     // TODO Only used in tests
  70.     override fun equals(other: Any?): Boolean {
  71.         if (this === other) return true
  72.         if (javaClass != other?.javaClass) return false

  73.         other as Parameter<*>

  74.         if (type != other.type) return false
  75.         if (name != other.name) return false
  76.         if (description != other.description) return false
  77.         if (regex != other.regex) return false
  78.         if (optional != other.optional) return false
  79.         if (multiple != other.multiple) return false
  80.         if (tag != other.tag) return false
  81.         if (values != other.values) return false

  82.         return true
  83.     }

  84.     // TODO Only used in tests
  85.     override fun hashCode(): Int {
  86.         var result = type.hashCode()
  87.         result = 31 * result + name.hashCode()
  88.         result = 31 * result + (description?.hashCode() ?: 0)
  89.         result = 31 * result + (regex?.hashCode() ?: 0)
  90.         result = 31 * result + optional.hashCode()
  91.         result = 31 * result + multiple.hashCode()
  92.         result = 31 * result + (tag?.hashCode() ?: 0)
  93.         result = 31 * result + values.hashCode()
  94.         return result
  95.     }
  96. }