George
12/21/2022, 6:25 AMInt
that has range from x to y. Where would you implement the check for the condition? In the constructor or in a factory function or something else. thanks in advance for any answers !George
12/21/2022, 6:25 AMpublic value class RegistrationId(private val value: Int) : Comparable<RegistrationId> {
init {
require(REGISTRATION_ID_RANGE.contains(value)) {
"The registration id is a number between 1 and 16380 but got: $value"
}
}
override fun compareTo(other: RegistrationId): Int = value.compareTo(other.toInt())
override fun toString(): String = value.toString()
/**
* Adds the other value to this value.
*
* @throws IllegalArgumentException if the resulted [RegistrationId] is out of bounds.
*/
public operator fun plus(other: RegistrationId): RegistrationId = RegistrationId(value.plus(other.value))
/**
* Adds the other value to this value.
*
* @throws IllegalArgumentException if the resulted [RegistrationId] is out of bounds.
*/
public operator fun plus(other: Int): RegistrationId = RegistrationId(value.plus(other))
/**
* Subtracts the other value from this value.
*
* @throws IllegalArgumentException if the resulted [RegistrationId] is negative.
*/
public operator fun minus(other: RegistrationId): RegistrationId = RegistrationId(value.minus(other.value))
/** Converts this [RegistrationId] value to [Int].*/
public fun toInt(): Int = value
public companion object {
private const val MIN_VALUE: Int = 1
private const val MAX_VALUE: Int = 16380
/**
* A constant holding the [range][IntRange] an instance of [RegistrationId] can have.
*/
@JvmField
public val REGISTRATION_ID_RANGE: IntRange = IntRange(MIN_VALUE, MAX_VALUE)
}
}
George
12/21/2022, 6:26 AMKlitos Kyriacou
12/22/2022, 10:09 AMinit {
require(value in REGISTRATION_ID_RANGE) {
"The registration id must be in the range $REGISTRATION_ID_RANGE but got: $value"
}
}
Also, I'm sceptical about your definitions of plus
and minus
. Does it really make sense to be able to add or subtract IDs?George
12/22/2022, 10:22 AMplus
and minus
have no value.Giorgos Makris
01/04/2023, 12:15 PMGiorgos Makris
01/04/2023, 12:17 PM