types icon indicating copy to clipboard operation
types copied to clipboard

✨ New `PositiveInteger` type

Open LVMVRQUXL opened this issue 1 year ago • 2 comments

📝 Description

In the org.kotools.types package of the types subproject, we want to introduce the PositiveInteger experimental type for representing an integer that is greater than zero.

The text parameter specified in the factory functions of this type should match the following pattern: ^\+?[1-9]\d*$.

This type should be available for all Kotlin platforms and Java, but its orNull functions shouldn't be accessible from Java, due to its non-explicit support for nullable types.

class PositiveInteger private constructor() {
    override fun equals(other: Any?): Boolean = TODO()
    override fun hashCode(): Int = TODO()
    override fun toString(): String = TODO()

    companion object {
        fun orNull(number: Int): PositiveInteger? = TODO()
        fun orNull(number: Long): PositiveInteger? = TODO()
        fun orNull(text: String): PositiveInteger? = TODO()

        fun orThrow(number: Int): PositiveInteger = TODO()
        fun orThrow(number: Long): PositiveInteger = TODO()
        fun orThrow(text: String): PositiveInteger = TODO()
    }
}

Also, in the types-kotlinx-serialization subproject, we want to add the PositiveInteger.Companion.stringSerializer() function for serializing the PositiveInteger type as String. This function should be only available for Kotlin platforms.

fun PositiveInteger.Companion.stringSerializer(): KSerializer<PositiveInteger>

✅ Checklist

  • [x] ✨ Add the type with a private constructor.
  • [x] ✨ Add the PositiveInteger.Companion.orNull(Int) function with tests, documentation and samples.
  • [x] ✨ Add the PositiveInteger.Companion.orNull(Long) function with tests, documentation and samples.
  • [x] ✨ Add the PositiveInteger.Companion.orNull(String) function with tests, documentation and samples.
  • [x] ✨ Add the PositiveInteger.Companion.orThrow(Int) function with tests, documentation and samples.
  • [x] ✨ Add the PositiveInteger.Companion.orThrow(Long) function with tests, documentation and samples.
  • [x] ✨ Add the PositiveInteger.Companion.orThrow(String) function with tests, documentation and samples.
  • [x] ✨ Override the PositiveInteger.equals(Any?) function with tests, documentation and samples.
  • [x] ✨ Override the PositiveInteger.hashCode() function with tests, documentation and samples.
  • [x] ✨ Override the PositiveInteger.toString() function with tests, documentation and samples.
  • [x] ✨ Add the PositiveInteger.Companion.stringSerializer() function with tests, documentation and samples.
  • [x] ✨ Update the KotoolsTypesSerializersModule() function by adding a default serializer for the PositiveInteger type.
  • [ ] ♻️ Refactor string serializers by introducing an internal StringSerializer interface.
  • [ ] 📝 Update the unreleased changelog for this issue.
  • [ ] 📝 After closing this issue, update the milestone of tracking issues depending only on this one.

LVMVRQUXL avatar May 18 '24 14:05 LVMVRQUXL

The PositiveInteger.Companion.random() function should use the widest range available, like the Long type's range, for producing a random value.

LVMVRQUXL avatar Jul 20 '24 09:07 LVMVRQUXL