Flag.kt

  1. package com.hexagontk.shell

  2. import com.hexagontk.shell.Option.Companion.optionRegex
  3. import kotlin.reflect.KClass

  4. class Flag(
  5.     override val names: Set<String>,
  6.     override val description: String? = null,
  7.     override val multiple: Boolean = false,
  8.     override val tag: String? = null,
  9.     override val values: List<Boolean> = emptyList(),
  10. ) : Property<Boolean> {

  11.     override val optional: Boolean = true
  12.     override val regex: Regex? = null
  13.     override val type: KClass<Boolean> = Boolean::class

  14.     constructor(
  15.         shortName: Char? = null,
  16.         name: String? = null,
  17.         description: String? = null,
  18.         multiple: Boolean = false,
  19.     ) : this(setOfNotNull(shortName?.toString(), name), description, multiple)

  20.     init {
  21.         check("Flag", optionRegex)
  22.     }

  23.     @Suppress("UNCHECKED_CAST") // Types checked at runtime
  24.     override fun addValues(value: Property<*>): Property<Boolean> =
  25.         Flag(names, description, multiple, tag, values + value.values as List<Boolean>)

  26.     override fun addValue(value: String): Flag =
  27.         Flag(names, description, multiple, tag, values + true)

  28.     override fun clearValues(): Flag =
  29.         Flag(names, description, multiple, tag, emptyList())

  30.     // TODO Only used in tests
  31.     override fun equals(other: Any?): Boolean {
  32.         if (this === other) return true
  33.         if (javaClass != other?.javaClass) return false

  34.         other as Flag

  35.         if (names != other.names) return false
  36.         if (description != other.description) return false
  37.         if (multiple != other.multiple) return false
  38.         if (tag != other.tag) return false
  39.         if (values != other.values) return false

  40.         return true
  41.     }

  42.     // TODO Only used in tests
  43.     override fun hashCode(): Int {
  44.         var result = names.hashCode()
  45.         result = 31 * result + (description?.hashCode() ?: 0)
  46.         result = 31 * result + multiple.hashCode()
  47.         result = 31 * result + (tag?.hashCode() ?: 0)
  48.         result = 31 * result + values.hashCode()
  49.         return result
  50.     }
  51. }