From c7a6585a6eff803113b1a8fda21418192ad7f417 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Lamarque?= Date: Wed, 28 Dec 2022 21:13:49 +0100 Subject: [PATCH] refactor(#12)!: rename AnyInt.value to AnyInt.asInt --- .../kotlin/kotools/types/number/AnyInt.kt | 22 +++++++++---------- .../kotools/types/number/NegativeInt.kt | 4 ++-- .../kotlin/kotools/types/number/NonZeroInt.kt | 4 ++-- .../kotools/types/number/PositiveInt.kt | 4 ++-- .../types/number/StrictlyNegativeInt.kt | 6 ++--- .../types/number/StrictlyPositiveInt.kt | 6 ++--- .../kotlin/kotools/types/number/ZeroInt.kt | 8 +++---- .../types/collection/NotEmptyListTest.kt | 2 +- .../types/collection/NotEmptyMapTest.kt | 2 +- .../types/collection/NotEmptySetTest.kt | 2 +- .../kotlin/kotools/types/number/AnyIntTest.kt | 4 ++-- .../kotools/types/number/NegativeIntTest.kt | 8 +++---- .../kotools/types/number/NonZeroIntTest.kt | 10 ++++----- .../kotools/types/number/PositiveIntTest.kt | 8 +++---- .../types/number/StrictlyNegativeIntTest.kt | 6 ++--- .../types/number/StrictlyPositiveIntTest.kt | 8 +++---- .../kotools/types/number/ZeroIntTest.kt | 2 +- .../kotools/types/text/NotBlankStringTest.kt | 2 +- 18 files changed, 54 insertions(+), 54 deletions(-) diff --git a/src/commonMain/kotlin/kotools/types/number/AnyInt.kt b/src/commonMain/kotlin/kotools/types/number/AnyInt.kt index a03039bb3..0606b6190 100644 --- a/src/commonMain/kotlin/kotools/types/number/AnyInt.kt +++ b/src/commonMain/kotlin/kotools/types/number/AnyInt.kt @@ -16,18 +16,18 @@ import kotools.types.text.asNotBlankString @Serializable(AnyIntSerializerImplementation::class) @SinceKotoolsTypes("4.0") public sealed interface AnyInt : Comparable { - /** The value to hold. */ - public val value: Int + /** Returns this integer as an [Int]. */ + public val asInt: Int /** - * Compares this [value] with the [other] value for order. - * Returns zero if this [value] equals the [other] value, a negative number - * if it's less than the [other] value, or a positive number if it's greater - * than the [other] value. + * Compares this integer with the [other] one for order. + * Returns zero if this integer equals the [other] one, a negative number if + * it's less than the [other] one, or a positive number if it's greater than + * the [other] one. */ - override fun compareTo(other: AnyInt): Int = value.compareTo(other.value) + override fun compareTo(other: AnyInt): Int = asInt.compareTo(other.asInt) - /** Returns this [value] as a [String]. */ + /** Returns the string representation of this integer. */ override fun toString(): String } @@ -40,7 +40,7 @@ internal sealed interface AnyIntSerializer : KSerializer { .getOrThrow() override fun serialize(encoder: Encoder, value: I): Unit = - encoder.encodeInt(value.value) + encoder.encodeInt(value.asInt) fun deserialize(value: Int): I @@ -55,8 +55,8 @@ internal object AnyIntSerializerImplementation : AnyIntSerializer { ) override fun deserialize(value: Int): AnyInt = when { - value == ZeroInt.value -> Result.success(ZeroInt) - value > ZeroInt.value -> value.toStrictlyPositiveInt() + value == ZeroInt.asInt -> Result.success(ZeroInt) + value > ZeroInt.asInt -> value.toStrictlyPositiveInt() else -> value.toStrictlyNegativeInt() }.getOrThrow() } diff --git a/src/commonMain/kotlin/kotools/types/number/NegativeInt.kt b/src/commonMain/kotlin/kotools/types/number/NegativeInt.kt index 441e9beab..f2c6c5a73 100644 --- a/src/commonMain/kotlin/kotools/types/number/NegativeInt.kt +++ b/src/commonMain/kotlin/kotools/types/number/NegativeInt.kt @@ -23,8 +23,8 @@ public sealed interface NegativeInt : AnyInt */ @SinceKotoolsTypes("1.1") public fun Int.toNegativeInt(): Result = when { - this == ZeroInt.value -> Result.success(ZeroInt) - this < ZeroInt.value -> toStrictlyNegativeInt() + this == ZeroInt.asInt -> Result.success(ZeroInt) + this < ZeroInt.asInt -> toStrictlyNegativeInt() else -> Result.failure(this shouldBe aNegativeNumber) } diff --git a/src/commonMain/kotlin/kotools/types/number/NonZeroInt.kt b/src/commonMain/kotlin/kotools/types/number/NonZeroInt.kt index f1369ea72..d83735748 100644 --- a/src/commonMain/kotlin/kotools/types/number/NonZeroInt.kt +++ b/src/commonMain/kotlin/kotools/types/number/NonZeroInt.kt @@ -22,8 +22,8 @@ public sealed interface NonZeroInt : AnyInt */ @SinceKotoolsTypes("1.1") public fun Int.toNonZeroInt(): Result = when { - this > ZeroInt.value -> toStrictlyPositiveInt() - this < ZeroInt.value -> toStrictlyNegativeInt() + this > ZeroInt.asInt -> toStrictlyPositiveInt() + this < ZeroInt.asInt -> toStrictlyNegativeInt() else -> Result.failure(this shouldBe otherThanZero) } diff --git a/src/commonMain/kotlin/kotools/types/number/PositiveInt.kt b/src/commonMain/kotlin/kotools/types/number/PositiveInt.kt index 202849cd5..c22e06416 100644 --- a/src/commonMain/kotlin/kotools/types/number/PositiveInt.kt +++ b/src/commonMain/kotlin/kotools/types/number/PositiveInt.kt @@ -23,8 +23,8 @@ public sealed interface PositiveInt : AnyInt */ @SinceKotoolsTypes("1.1") public fun Int.toPositiveInt(): Result = when { - this == ZeroInt.value -> Result.success(ZeroInt) - this > ZeroInt.value -> toStrictlyPositiveInt() + this == ZeroInt.asInt -> Result.success(ZeroInt) + this > ZeroInt.asInt -> toStrictlyPositiveInt() else -> Result.failure(this shouldBe aPositiveNumber) } diff --git a/src/commonMain/kotlin/kotools/types/number/StrictlyNegativeInt.kt b/src/commonMain/kotlin/kotools/types/number/StrictlyNegativeInt.kt index 7b13bfc74..e9a68f72c 100644 --- a/src/commonMain/kotlin/kotools/types/number/StrictlyNegativeInt.kt +++ b/src/commonMain/kotlin/kotools/types/number/StrictlyNegativeInt.kt @@ -19,15 +19,15 @@ import kotlin.jvm.JvmInline @Serializable(StrictlyNegativeIntSerializer::class) @SinceKotoolsTypes("1.1") public value class StrictlyNegativeInt -private constructor(override val value: Int) : NonZeroInt, NegativeInt { +private constructor(override val asInt: Int) : NonZeroInt, NegativeInt { internal companion object { infix fun of(value: Int): Result = value - .takeIf { it < ZeroInt.value } + .takeIf { it < ZeroInt.asInt } ?.toSuccessfulResult(::StrictlyNegativeInt) ?: Result.failure(value shouldBe aStrictlyNegativeNumber) } - override fun toString(): String = "$value" + override fun toString(): String = "$asInt" } /** diff --git a/src/commonMain/kotlin/kotools/types/number/StrictlyPositiveInt.kt b/src/commonMain/kotlin/kotools/types/number/StrictlyPositiveInt.kt index c9d20d96b..cd9aee878 100644 --- a/src/commonMain/kotlin/kotools/types/number/StrictlyPositiveInt.kt +++ b/src/commonMain/kotlin/kotools/types/number/StrictlyPositiveInt.kt @@ -19,15 +19,15 @@ import kotlin.jvm.JvmInline @Serializable(StrictlyPositiveIntSerializer::class) @SinceKotoolsTypes("1.1") public value class StrictlyPositiveInt -private constructor(override val value: Int) : NonZeroInt, PositiveInt { +private constructor(override val asInt: Int) : NonZeroInt, PositiveInt { internal companion object { infix fun of(value: Int): Result = value - .takeIf { it > ZeroInt.value } + .takeIf { it > ZeroInt.asInt } ?.toSuccessfulResult(::StrictlyPositiveInt) ?: Result.failure(value shouldBe aStrictlyPositiveNumber) } - override fun toString(): String = "$value" + override fun toString(): String = "$asInt" } /** diff --git a/src/commonMain/kotlin/kotools/types/number/ZeroInt.kt b/src/commonMain/kotlin/kotools/types/number/ZeroInt.kt index d47d8e810..30f72e9a2 100644 --- a/src/commonMain/kotlin/kotools/types/number/ZeroInt.kt +++ b/src/commonMain/kotlin/kotools/types/number/ZeroInt.kt @@ -11,17 +11,17 @@ import kotools.types.text.asNotBlankString @Serializable(ZeroIntSerializer::class) @SinceKotoolsTypes("4.0") public object ZeroInt : PositiveInt, NegativeInt { - override val value: Int = 0 + override val asInt: Int = 0 /** * Returns `true` if the [other] value is a [ZeroInt] or `false` otherwise. */ override fun equals(other: Any?): Boolean = other is ZeroInt - /** Returns the hash code of this [value]. */ - override fun hashCode(): Int = value.hashCode() + /** Returns the hash code of this integer. */ + override fun hashCode(): Int = asInt.hashCode() - override fun toString(): String = "$value" + override fun toString(): String = "$asInt" } internal object ZeroIntSerializer : AnyIntSerializer { diff --git a/src/commonTest/kotlin/kotools/types/collection/NotEmptyListTest.kt b/src/commonTest/kotlin/kotools/types/collection/NotEmptyListTest.kt index b1c3b7eae..664e1c9bb 100644 --- a/src/commonTest/kotlin/kotools/types/collection/NotEmptyListTest.kt +++ b/src/commonTest/kotlin/kotools/types/collection/NotEmptyListTest.kt @@ -42,7 +42,7 @@ class NotEmptyListTest { val elements: List = List(3) { Random.nextInt() } val result: StrictlyPositiveInt = elements.asNotEmptyList.getOrThrow().size - assertEquals(actual = result.value, expected = elements.size) + assertEquals(actual = result.asInt, expected = elements.size) } @Test diff --git a/src/commonTest/kotlin/kotools/types/collection/NotEmptyMapTest.kt b/src/commonTest/kotlin/kotools/types/collection/NotEmptyMapTest.kt index 430b68807..fc350d427 100644 --- a/src/commonTest/kotlin/kotools/types/collection/NotEmptyMapTest.kt +++ b/src/commonTest/kotlin/kotools/types/collection/NotEmptyMapTest.kt @@ -56,7 +56,7 @@ class NotEmptyMapTest { "c" to Random.nextInt() ) assertEquals( - actual = notEmptyMap.size.value, + actual = notEmptyMap.size.asInt, expected = notEmptyMap.asMap.size ) } diff --git a/src/commonTest/kotlin/kotools/types/collection/NotEmptySetTest.kt b/src/commonTest/kotlin/kotools/types/collection/NotEmptySetTest.kt index 308ac2e86..4968db5ee 100644 --- a/src/commonTest/kotlin/kotools/types/collection/NotEmptySetTest.kt +++ b/src/commonTest/kotlin/kotools/types/collection/NotEmptySetTest.kt @@ -44,7 +44,7 @@ class NotEmptySetTest { .toSet() val result: StrictlyPositiveInt = elements.asNotEmptySet.getOrThrow().size - assertEquals(actual = result.value, expected = elements.size) + assertEquals(actual = result.asInt, expected = elements.size) } @Test diff --git a/src/commonTest/kotlin/kotools/types/number/AnyIntTest.kt b/src/commonTest/kotlin/kotools/types/number/AnyIntTest.kt index 50dfb8dfe..4a2c6ebab 100644 --- a/src/commonTest/kotlin/kotools/types/number/AnyIntTest.kt +++ b/src/commonTest/kotlin/kotools/types/number/AnyIntTest.kt @@ -59,7 +59,7 @@ class AnyIntSerializerTest { .toStrictlyPositiveInt() .getOrThrow() val result: String = Json.encodeToString(serializer, x) - val expected: String = Json.encodeToString(x.value) + val expected: String = Json.encodeToString(x.asInt) assertEquals(expected, result) } @@ -68,6 +68,6 @@ class AnyIntSerializerTest { val value: Int = Random.nextInt() val encoded: String = Json.encodeToString(value) val result: AnyInt = Json.decodeFromString(serializer, encoded) - assertEquals(value, result.value) + assertEquals(value, result.asInt) } } diff --git a/src/commonTest/kotlin/kotools/types/number/NegativeIntTest.kt b/src/commonTest/kotlin/kotools/types/number/NegativeIntTest.kt index e6a81e96e..e90bcd9d7 100644 --- a/src/commonTest/kotlin/kotools/types/number/NegativeIntTest.kt +++ b/src/commonTest/kotlin/kotools/types/number/NegativeIntTest.kt @@ -10,7 +10,7 @@ import kotlin.test.Test import kotlin.test.assertEquals import kotlin.test.assertFailsWith -internal val negativeIntRange: IntRange = Int.MIN_VALUE..ZeroInt.value +internal val negativeIntRange: IntRange = Int.MIN_VALUE..ZeroInt.asInt class NegativeIntTest { @Test @@ -18,7 +18,7 @@ class NegativeIntTest { val value: Int = negativeIntRange.random() val result: Int = value.toNegativeInt() .getOrThrow() - .value + .asInt assertEquals(value, result) } @@ -45,7 +45,7 @@ class NegativeIntSerializerTest { .toNegativeInt() .getOrThrow() val result: String = Json.encodeToString(NegativeIntSerializer, x) - val expected: String = Json.encodeToString(x.value) + val expected: String = Json.encodeToString(x.asInt) assertEquals(expected, result) } @@ -55,7 +55,7 @@ class NegativeIntSerializerTest { val encoded: String = Json.encodeToString(value) val result: NegativeInt = Json.decodeFromString(NegativeIntSerializer, encoded) - assertEquals(value, result.value) + assertEquals(value, result.asInt) } @Test diff --git a/src/commonTest/kotlin/kotools/types/number/NonZeroIntTest.kt b/src/commonTest/kotlin/kotools/types/number/NonZeroIntTest.kt index bc9479435..0761ba49a 100644 --- a/src/commonTest/kotlin/kotools/types/number/NonZeroIntTest.kt +++ b/src/commonTest/kotlin/kotools/types/number/NonZeroIntTest.kt @@ -22,13 +22,13 @@ class NonZeroIntTest { .random() val result: Int = value.toNonZeroInt() .getOrThrow() - .value + .asInt assertEquals(value, result) } @Test fun int_toNonZeroInt_should_fail_with_an_Int_that_equals_zero() { - val result: Result = ZeroInt.value.toNonZeroInt() + val result: Result = ZeroInt.asInt.toNonZeroInt() assertFailsWith(block = result::getOrThrow) .assertHasAMessage() } @@ -51,7 +51,7 @@ class NonZeroIntSerializerTest { .toNonZeroInt() .getOrThrow() val result: String = Json.encodeToString(NonZeroIntSerializer, x) - val expected: String = Json.encodeToString(x.value) + val expected: String = Json.encodeToString(x.asInt) assertEquals(expected, result) } @@ -62,12 +62,12 @@ class NonZeroIntSerializerTest { val encoded: String = Json.encodeToString(value) val result: NonZeroInt = Json.decodeFromString(NonZeroIntSerializer, encoded) - assertEquals(value, result.value) + assertEquals(value, result.asInt) } @Test fun deserialization_should_fail_with_an_Int_that_equals_zero() { - val encoded: String = Json.encodeToString(ZeroInt.value) + val encoded: String = Json.encodeToString(ZeroInt.asInt) val exception: SerializationException = assertFailsWith { Json.decodeFromString(NonZeroIntSerializer, encoded) } diff --git a/src/commonTest/kotlin/kotools/types/number/PositiveIntTest.kt b/src/commonTest/kotlin/kotools/types/number/PositiveIntTest.kt index 5fe040a06..8816113cf 100644 --- a/src/commonTest/kotlin/kotools/types/number/PositiveIntTest.kt +++ b/src/commonTest/kotlin/kotools/types/number/PositiveIntTest.kt @@ -10,7 +10,7 @@ import kotlin.test.Test import kotlin.test.assertEquals import kotlin.test.assertFailsWith -internal val positiveIntRange: IntRange = ZeroInt.value..Int.MAX_VALUE +internal val positiveIntRange: IntRange = ZeroInt.asInt..Int.MAX_VALUE class PositiveIntTest { @Test @@ -18,7 +18,7 @@ class PositiveIntTest { val expected: Int = positiveIntRange.random() val actual: Int = expected.toPositiveInt() .getOrThrow() - .value + .asInt assertEquals(expected, actual) } @@ -46,7 +46,7 @@ class PositiveIntSerializerTest { .toPositiveInt() .getOrThrow() assertEquals( - expected = Json.encodeToString(x.value), + expected = Json.encodeToString(x.asInt), actual = Json.encodeToString(PositiveIntSerializer, x) ) } @@ -57,7 +57,7 @@ class PositiveIntSerializerTest { val encoded: String = Json.encodeToString(expected) val result: PositiveInt = Json.decodeFromString(PositiveIntSerializer, encoded) - assertEquals(expected, actual = result.value) + assertEquals(expected, actual = result.asInt) } @Test diff --git a/src/commonTest/kotlin/kotools/types/number/StrictlyNegativeIntTest.kt b/src/commonTest/kotlin/kotools/types/number/StrictlyNegativeIntTest.kt index 06433841e..630460475 100644 --- a/src/commonTest/kotlin/kotools/types/number/StrictlyNegativeIntTest.kt +++ b/src/commonTest/kotlin/kotools/types/number/StrictlyNegativeIntTest.kt @@ -31,7 +31,7 @@ class StrictlyNegativeIntTest { assertEquals( actual = value.toStrictlyNegativeInt() .getOrThrow() - .value, + .asInt, expected = value ) } @@ -61,7 +61,7 @@ class StrictlyNegativeIntSerializerTest { .getOrThrow() assertEquals( actual = Json.encodeToString(x), - expected = Json.encodeToString(x.value) + expected = Json.encodeToString(x.asInt) ) } @@ -70,7 +70,7 @@ class StrictlyNegativeIntSerializerTest { val value: Int = strictlyNegativeIntRange.random() val encoded: String = Json.encodeToString(value) val result: StrictlyNegativeInt = Json.decodeFromString(encoded) - assertEquals(actual = result.value, expected = value) + assertEquals(actual = result.asInt, expected = value) } @Test diff --git a/src/commonTest/kotlin/kotools/types/number/StrictlyPositiveIntTest.kt b/src/commonTest/kotlin/kotools/types/number/StrictlyPositiveIntTest.kt index 896a52dcb..4726f9acb 100644 --- a/src/commonTest/kotlin/kotools/types/number/StrictlyPositiveIntTest.kt +++ b/src/commonTest/kotlin/kotools/types/number/StrictlyPositiveIntTest.kt @@ -19,7 +19,7 @@ class StrictlyPositiveIntTest { .random() .toStrictlyPositiveInt() .getOrThrow() - .run { assertEquals(actual = toString(), expected = "$value") } + .run { assertEquals(actual = toString(), expected = "$asInt") } @Test fun int_toStrictlyPositiveInt_should_pass_with_a_strictly_positive_Int() { @@ -27,7 +27,7 @@ class StrictlyPositiveIntTest { assertEquals( actual = value.toStrictlyPositiveInt() .getOrThrow() - .value, + .asInt, expected = value ) } @@ -57,7 +57,7 @@ class StrictlyPositiveIntSerializerTest { .getOrThrow() assertEquals( actual = Json.encodeToString(x), - expected = Json.encodeToString(x.value) + expected = Json.encodeToString(x.asInt) ) } @@ -66,7 +66,7 @@ class StrictlyPositiveIntSerializerTest { val value: Int = strictlyPositiveIntRange.random() val encoded: String = Json.encodeToString(value) val result: StrictlyPositiveInt = Json.decodeFromString(encoded) - assertEquals(actual = result.value, expected = value) + assertEquals(actual = result.asInt, expected = value) } @Test diff --git a/src/commonTest/kotlin/kotools/types/number/ZeroIntTest.kt b/src/commonTest/kotlin/kotools/types/number/ZeroIntTest.kt index 79ac7a3ac..0dd895c96 100644 --- a/src/commonTest/kotlin/kotools/types/number/ZeroIntTest.kt +++ b/src/commonTest/kotlin/kotools/types/number/ZeroIntTest.kt @@ -43,7 +43,7 @@ class ZeroIntSerializerTest { @Test fun serialize_should_behave_like_the_zero_integer(): Unit = assertEquals( actual = Json.encodeToString(ZeroInt), - expected = Json.encodeToString(ZeroInt.value) + expected = Json.encodeToString(ZeroInt.asInt) ) @Test diff --git a/src/commonTest/kotlin/kotools/types/text/NotBlankStringTest.kt b/src/commonTest/kotlin/kotools/types/text/NotBlankStringTest.kt index 759219128..6fe05222e 100644 --- a/src/commonTest/kotlin/kotools/types/text/NotBlankStringTest.kt +++ b/src/commonTest/kotlin/kotools/types/text/NotBlankStringTest.kt @@ -16,7 +16,7 @@ class NotBlankStringTest { fun length_should_pass() { val string = "hello world" assertEquals( - actual = string.asNotBlankString.getOrThrow().length.value, + actual = string.asNotBlankString.getOrThrow().length.asInt, expected = string.length ) }