Skip to content

Commit

Permalink
refactor(#12)!: convert builder of integers to extension function
Browse files Browse the repository at this point in the history
  • Loading branch information
LVMVRQUXL committed Jan 3, 2023
1 parent 97d6797 commit 84c1607
Show file tree
Hide file tree
Showing 15 changed files with 82 additions and 99 deletions.
Expand Up @@ -11,7 +11,7 @@ import kotlinx.serialization.encoding.Encoder
import kotools.types.Package
import kotools.types.SinceKotoolsTypes
import kotools.types.number.StrictlyPositiveInt
import kotools.types.number.asStrictlyPositiveInt
import kotools.types.number.toStrictlyPositiveInt
import kotools.types.toSuccessfulResult

/**
Expand All @@ -36,7 +36,7 @@ public data class NotEmptyList<out E> internal constructor(

/** The size of this list. */
public val size: StrictlyPositiveInt by lazy(
asList.size.asStrictlyPositiveInt::getOrThrow
asList.size.toStrictlyPositiveInt()::getOrThrow
)

/** Returns the string representation of this list. */
Expand Down
4 changes: 2 additions & 2 deletions src/commonMain/kotlin/kotools/types/collection/NotEmptyMap.kt
Expand Up @@ -11,7 +11,7 @@ import kotlinx.serialization.encoding.Encoder
import kotools.types.Package
import kotools.types.SinceKotoolsTypes
import kotools.types.number.StrictlyPositiveInt
import kotools.types.number.asStrictlyPositiveInt
import kotools.types.number.toStrictlyPositiveInt
import kotools.types.toSuccessfulResult

/**
Expand Down Expand Up @@ -59,7 +59,7 @@ public data class NotEmptyMap<K, out V> internal constructor(

/** The size of this map. */
public val size: StrictlyPositiveInt by lazy(
asMap.size.asStrictlyPositiveInt::getOrThrow
asMap.size.toStrictlyPositiveInt()::getOrThrow
)

/** Returns the string representation of this map. */
Expand Down
4 changes: 2 additions & 2 deletions src/commonMain/kotlin/kotools/types/collection/NotEmptySet.kt
Expand Up @@ -11,7 +11,7 @@ import kotlinx.serialization.encoding.Encoder
import kotools.types.Package
import kotools.types.SinceKotoolsTypes
import kotools.types.number.StrictlyPositiveInt
import kotools.types.number.asStrictlyPositiveInt
import kotools.types.number.toStrictlyPositiveInt
import kotools.types.toSuccessfulResult

/**
Expand All @@ -36,7 +36,7 @@ public data class NotEmptySet<out E> internal constructor(

/** The size of this set. */
public val size: StrictlyPositiveInt by lazy(
asSet.size.asStrictlyPositiveInt::getOrThrow
asSet.size.toStrictlyPositiveInt()::getOrThrow
)

/** Returns the string representation of this set. */
Expand Down
4 changes: 2 additions & 2 deletions src/commonMain/kotlin/kotools/types/number/AnyInt.kt
Expand Up @@ -56,7 +56,7 @@ internal object AnyIntSerializerImplementation : AnyIntSerializer<AnyInt> {

override fun deserialize(value: Int): AnyInt = when {
value == ZeroInt.toInt() -> Result.success(ZeroInt)
value > ZeroInt.toInt() -> value.asStrictlyPositiveInt
else -> value.asStrictlyNegativeInt
value > ZeroInt.toInt() -> value.toStrictlyPositiveInt()
else -> value.toStrictlyNegativeInt()
}.getOrThrow()
}
21 changes: 8 additions & 13 deletions src/commonMain/kotlin/kotools/types/number/NegativeInt.kt
Expand Up @@ -7,11 +7,7 @@ import kotools.types.SinceKotoolsTypes
import kotools.types.text.NotBlankString
import kotools.types.text.toNotBlankString

/**
* Representation of negative integers including [zero][ZeroInt].
*
* See the [asNegativeInt] property for building a [NegativeInt].
*/
/** Representation of negative integers including [zero][ZeroInt]. */
@Serializable(NegativeIntSerializer::class)
@SinceKotoolsTypes("1.1")
public sealed interface NegativeInt : AnyInt {
Expand All @@ -30,7 +26,7 @@ public sealed interface NegativeInt : AnyInt {
public fun random(): NegativeInt {
val range: IntRange = min.toInt()..max.toInt()
return range.random()
.asNegativeInt
.toNegativeInt()
.getOrThrow()
}
}
Expand All @@ -42,19 +38,18 @@ public sealed interface NegativeInt : AnyInt {
* [strictly positive][StrictlyPositiveInt].
*/
@SinceKotoolsTypes("4.0")
public val Int.asNegativeInt: Result<NegativeInt>
get() = when {
this == ZeroInt.toInt() -> Result.success(ZeroInt)
this < ZeroInt.toInt() -> asStrictlyNegativeInt
else -> Result.failure(this shouldBe aNegativeNumber)
}
public fun Int.toNegativeInt(): Result<NegativeInt> = when {
this == ZeroInt.toInt() -> Result.success(ZeroInt)
this < ZeroInt.toInt() -> toStrictlyNegativeInt()
else -> Result.failure(this shouldBe aNegativeNumber)
}

internal object NegativeIntSerializer : AnyIntSerializer<NegativeInt> {
override val serialName: Result<NotBlankString> by lazy(
"${Package.number}.NegativeInt"::toNotBlankString
)

override fun deserialize(value: Int): NegativeInt = value.asNegativeInt
override fun deserialize(value: Int): NegativeInt = value.toNegativeInt()
.getOrNull()
?: throw SerializationException(value shouldBe aNegativeNumber)
}
21 changes: 8 additions & 13 deletions src/commonMain/kotlin/kotools/types/number/NonZeroInt.kt
Expand Up @@ -9,11 +9,7 @@ import kotools.types.collection.notEmptySetOf
import kotools.types.text.NotBlankString
import kotools.types.text.toNotBlankString

/**
* Representation of integers other than [zero][ZeroInt].
*
* See the [asNonZeroInt] property for building a [NonZeroInt].
*/
/** Representation of integers other than [zero][ZeroInt]. */
@Serializable(NonZeroIntSerializer::class)
@SinceKotoolsTypes("1.1")
public sealed interface NonZeroInt : AnyInt {
Expand All @@ -38,7 +34,7 @@ public sealed interface NonZeroInt : AnyInt {
)
return ranges.asSet.random()
.random()
.asNonZeroInt
.toNonZeroInt()
.getOrThrow()
}
}
Expand All @@ -49,19 +45,18 @@ public sealed interface NonZeroInt : AnyInt {
* [IllegalArgumentException] if this integer equals [zero][ZeroInt].
*/
@SinceKotoolsTypes("4.0")
public val Int.asNonZeroInt: Result<NonZeroInt>
get() = when {
this > ZeroInt.toInt() -> asStrictlyPositiveInt
this < ZeroInt.toInt() -> asStrictlyNegativeInt
else -> Result.failure(this shouldBe otherThanZero)
}
public fun Int.toNonZeroInt(): Result<NonZeroInt> = when {
this > ZeroInt.toInt() -> toStrictlyPositiveInt()
this < ZeroInt.toInt() -> toStrictlyNegativeInt()
else -> Result.failure(this shouldBe otherThanZero)
}

internal object NonZeroIntSerializer : AnyIntSerializer<NonZeroInt> {
override val serialName: Result<NotBlankString> by lazy(
"${Package.number}.NonZeroInt"::toNotBlankString
)

override fun deserialize(value: Int): NonZeroInt = value.asNonZeroInt
override fun deserialize(value: Int): NonZeroInt = value.toNonZeroInt()
.getOrNull()
?: throw SerializationException(value shouldBe otherThanZero)
}
21 changes: 8 additions & 13 deletions src/commonMain/kotlin/kotools/types/number/PositiveInt.kt
Expand Up @@ -7,11 +7,7 @@ import kotools.types.SinceKotoolsTypes
import kotools.types.text.NotBlankString
import kotools.types.text.toNotBlankString

/**
* Representation of positive integers including [zero][ZeroInt].
*
* See the [asPositiveInt] property for building a [PositiveInt].
*/
/** Representation of positive integers including [zero][ZeroInt]. */
@Serializable(PositiveIntSerializer::class)
@SinceKotoolsTypes("1.1")
public sealed interface PositiveInt : AnyInt {
Expand All @@ -28,7 +24,7 @@ public sealed interface PositiveInt : AnyInt {
/** Returns a random [PositiveInt]. */
@SinceKotoolsTypes("3.0")
public fun random(): PositiveInt = (min.toInt()..max.toInt()).random()
.asPositiveInt
.toPositiveInt()
.getOrThrow()
}
}
Expand All @@ -39,19 +35,18 @@ public sealed interface PositiveInt : AnyInt {
* [strictly negative][StrictlyNegativeInt].
*/
@SinceKotoolsTypes("4.0")
public val Int.asPositiveInt: Result<PositiveInt>
get() = when {
this == ZeroInt.toInt() -> Result.success(ZeroInt)
this > ZeroInt.toInt() -> asStrictlyPositiveInt
else -> Result.failure(this shouldBe aPositiveNumber)
}
public fun Int.toPositiveInt(): Result<PositiveInt> = when {
this == ZeroInt.toInt() -> Result.success(ZeroInt)
this > ZeroInt.toInt() -> toStrictlyPositiveInt()
else -> Result.failure(this shouldBe aPositiveNumber)
}

internal object PositiveIntSerializer : AnyIntSerializer<PositiveInt> {
override val serialName: Result<NotBlankString> by lazy(
"${Package.number}.PositiveInt"::toNotBlankString
)

override fun deserialize(value: Int): PositiveInt = value.asPositiveInt
override fun deserialize(value: Int): PositiveInt = value.toPositiveInt()
.getOrNull()
?: throw SerializationException(value shouldBe aPositiveNumber)
}
19 changes: 7 additions & 12 deletions src/commonMain/kotlin/kotools/types/number/StrictlyNegativeInt.kt
Expand Up @@ -9,12 +9,7 @@ import kotools.types.text.toNotBlankString
import kotools.types.toSuccessfulResult
import kotlin.jvm.JvmInline

/**
* Representation of negative integers excluding [zero][ZeroInt].
*
* See the [asStrictlyNegativeInt] property for building a
* [StrictlyNegativeInt].
*/
/** Representation of negative integers excluding [zero][ZeroInt]. */
@JvmInline
@Serializable(StrictlyNegativeIntSerializer::class)
@SinceKotoolsTypes("1.1")
Expand All @@ -26,12 +21,12 @@ private constructor(private val value: Int) : NonZeroInt, NegativeInt {
public companion object {
/** The minimum value a [StrictlyNegativeInt] can have. */
public val min: StrictlyNegativeInt by lazy(
Int.MIN_VALUE.asStrictlyNegativeInt::getOrThrow
Int.MIN_VALUE.toStrictlyNegativeInt()::getOrThrow
)

/** The maximum value a [StrictlyNegativeInt] can have. */
public val max: StrictlyNegativeInt by lazy(
(-1).asStrictlyNegativeInt::getOrThrow
(-1).toStrictlyNegativeInt()::getOrThrow
)

internal infix fun of(value: Int): Result<StrictlyNegativeInt> = value
Expand All @@ -43,7 +38,7 @@ private constructor(private val value: Int) : NonZeroInt, NegativeInt {
@SinceKotoolsTypes("3.0")
public fun random(): StrictlyNegativeInt = (min.value..max.value)
.random()
.asStrictlyNegativeInt
.toStrictlyNegativeInt()
.getOrThrow()
}

Expand All @@ -56,8 +51,8 @@ private constructor(private val value: Int) : NonZeroInt, NegativeInt {
* [IllegalArgumentException] if this integer is [positive][PositiveInt].
*/
@SinceKotoolsTypes("4.0")
public val Int.asStrictlyNegativeInt: Result<StrictlyNegativeInt>
get() = StrictlyNegativeInt of this
public fun Int.toStrictlyNegativeInt(): Result<StrictlyNegativeInt> =
StrictlyNegativeInt of this

internal object StrictlyNegativeIntSerializer :
AnyIntSerializer<StrictlyNegativeInt> {
Expand All @@ -66,7 +61,7 @@ internal object StrictlyNegativeIntSerializer :
)

override fun deserialize(value: Int): StrictlyNegativeInt = value
.asStrictlyNegativeInt
.toStrictlyNegativeInt()
.getOrNull()
?: throw SerializationException(value shouldBe aStrictlyNegativeNumber)
}
19 changes: 7 additions & 12 deletions src/commonMain/kotlin/kotools/types/number/StrictlyPositiveInt.kt
Expand Up @@ -9,12 +9,7 @@ import kotools.types.text.toNotBlankString
import kotools.types.toSuccessfulResult
import kotlin.jvm.JvmInline

/**
* Representation of positive integers excluding [zero][ZeroInt].
*
* See the [asStrictlyPositiveInt] property for building a
* [StrictlyPositiveInt].
*/
/** Representation of positive integers excluding [zero][ZeroInt]. */
@JvmInline
@Serializable(StrictlyPositiveIntSerializer::class)
@SinceKotoolsTypes("1.1")
Expand All @@ -26,12 +21,12 @@ private constructor(private val value: Int) : NonZeroInt, PositiveInt {
public companion object {
/** The minimum value a [StrictlyPositiveInt] can have. */
public val min: StrictlyPositiveInt by lazy(
1.asStrictlyPositiveInt::getOrThrow
1.toStrictlyPositiveInt()::getOrThrow
)

/** The maximum value a [StrictlyPositiveInt] can have. */
public val max: StrictlyPositiveInt by lazy(
Int.MAX_VALUE.asStrictlyPositiveInt::getOrThrow
Int.MAX_VALUE.toStrictlyPositiveInt()::getOrThrow
)

internal infix fun of(value: Int): Result<StrictlyPositiveInt> = value
Expand All @@ -43,7 +38,7 @@ private constructor(private val value: Int) : NonZeroInt, PositiveInt {
@SinceKotoolsTypes("3.0")
public fun random(): StrictlyPositiveInt = (min.value..max.value)
.random()
.asStrictlyPositiveInt
.toStrictlyPositiveInt()
.getOrThrow()
}

Expand All @@ -56,8 +51,8 @@ private constructor(private val value: Int) : NonZeroInt, PositiveInt {
* [IllegalArgumentException] if this integer is [negative][NegativeInt].
*/
@SinceKotoolsTypes("4.0")
public val Int.asStrictlyPositiveInt: Result<StrictlyPositiveInt>
get() = StrictlyPositiveInt of this
public fun Int.toStrictlyPositiveInt(): Result<StrictlyPositiveInt> =
StrictlyPositiveInt of this

internal object StrictlyPositiveIntSerializer :
AnyIntSerializer<StrictlyPositiveInt> {
Expand All @@ -66,7 +61,7 @@ internal object StrictlyPositiveIntSerializer :
)

override fun deserialize(value: Int): StrictlyPositiveInt = value
.asStrictlyPositiveInt
.toStrictlyPositiveInt()
.getOrNull()
?: throw SerializationException(value shouldBe aStrictlyPositiveNumber)
}
5 changes: 3 additions & 2 deletions src/commonMain/kotlin/kotools/types/text/NotBlankString.kt
Expand Up @@ -11,7 +11,7 @@ import kotlinx.serialization.encoding.Encoder
import kotools.types.Package
import kotools.types.SinceKotoolsTypes
import kotools.types.number.StrictlyPositiveInt
import kotools.types.number.asStrictlyPositiveInt
import kotools.types.number.toStrictlyPositiveInt
import kotools.types.toSuccessfulResult
import kotlin.jvm.JvmInline

Expand All @@ -34,7 +34,8 @@ public value class NotBlankString private constructor(

/** Returns the length of this string. */
public val length: StrictlyPositiveInt
get() = value.length.asStrictlyPositiveInt.getOrThrow()
get() = value.length.toStrictlyPositiveInt()
.getOrThrow()

/**
* Compares this string lexicographically with the [other] one for order.
Expand Down
9 changes: 5 additions & 4 deletions src/commonTest/kotlin/kotools/types/number/NegativeIntTest.kt
Expand Up @@ -33,18 +33,19 @@ class NegativeIntCompanionTest {

class NegativeIntTest {
@Test
fun int_asNegativeInt_should_pass_with_a_negative_Int() {
fun int_toNegativeInt_should_pass_with_a_negative_Int() {
val value: Int = NegativeInt.random().toInt()
val result: Int = value.asNegativeInt.getOrThrow()
val result: Int = value.toNegativeInt()
.getOrThrow()
.toInt()
result shouldEqual value
}

@Test
fun int_asNegativeInt_should_fail_with_a_strictly_positive_Int() {
fun int_toNegativeInt_should_fail_with_a_strictly_positive_Int() {
val result: Result<NegativeInt> = StrictlyPositiveInt.random()
.toInt()
.asNegativeInt
.toNegativeInt()
assertFailsWith<IllegalArgumentException>(block = result::getOrThrow)
.shouldHaveAMessage()
}
Expand Down
9 changes: 5 additions & 4 deletions src/commonTest/kotlin/kotools/types/number/NonZeroIntTest.kt
Expand Up @@ -34,17 +34,18 @@ class NonZeroIntCompanionTest {

class NonZeroIntTest {
@Test
fun int_asNonZeroInt_should_pass_with_an_Int_other_than_zero() {
fun int_toNonZeroInt_should_pass_with_an_Int_other_than_zero() {
val value: Int = NonZeroInt.random()
.toInt()
val result: Result<NonZeroInt> = value.asNonZeroInt
val result: Result<NonZeroInt> = value.toNonZeroInt()
result.getOrThrow()
.toInt() shouldEqual value
}

@Test
fun int_asNonZeroInt_should_fail_with_an_Int_that_equals_zero() {
val result: Result<NonZeroInt> = ZeroInt.toInt().asNonZeroInt
fun int_toNonZeroInt_should_fail_with_an_Int_that_equals_zero() {
val result: Result<NonZeroInt> = ZeroInt.toInt()
.toNonZeroInt()
val exception: IllegalArgumentException =
assertFailsWith(block = result::getOrThrow)
exception.shouldHaveAMessage()
Expand Down

0 comments on commit 84c1607

Please sign in to comment.