Skip to content

Commit

Permalink
refactor(#12)!: rename AnyInt.value to AnyInt.asInt
Browse files Browse the repository at this point in the history
  • Loading branch information
LVMVRQUXL committed Dec 28, 2022
1 parent 9430823 commit c7a6585
Show file tree
Hide file tree
Showing 18 changed files with 54 additions and 54 deletions.
22 changes: 11 additions & 11 deletions src/commonMain/kotlin/kotools/types/number/AnyInt.kt
Expand Up @@ -16,18 +16,18 @@ import kotools.types.text.asNotBlankString
@Serializable(AnyIntSerializerImplementation::class)
@SinceKotoolsTypes("4.0")
public sealed interface AnyInt : Comparable<AnyInt> {
/** 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
}

Expand All @@ -40,7 +40,7 @@ internal sealed interface AnyIntSerializer<I : AnyInt> : KSerializer<I> {
.getOrThrow()

override fun serialize(encoder: Encoder, value: I): Unit =
encoder.encodeInt(value.value)
encoder.encodeInt(value.asInt)

fun deserialize(value: Int): I

Expand All @@ -55,8 +55,8 @@ internal object AnyIntSerializerImplementation : AnyIntSerializer<AnyInt> {
)

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()
}
4 changes: 2 additions & 2 deletions src/commonMain/kotlin/kotools/types/number/NegativeInt.kt
Expand Up @@ -23,8 +23,8 @@ public sealed interface NegativeInt : AnyInt
*/
@SinceKotoolsTypes("1.1")
public fun Int.toNegativeInt(): Result<NegativeInt> = 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)
}

Expand Down
4 changes: 2 additions & 2 deletions src/commonMain/kotlin/kotools/types/number/NonZeroInt.kt
Expand Up @@ -22,8 +22,8 @@ public sealed interface NonZeroInt : AnyInt
*/
@SinceKotoolsTypes("1.1")
public fun Int.toNonZeroInt(): Result<NonZeroInt> = when {
this > ZeroInt.value -> toStrictlyPositiveInt()
this < ZeroInt.value -> toStrictlyNegativeInt()
this > ZeroInt.asInt -> toStrictlyPositiveInt()
this < ZeroInt.asInt -> toStrictlyNegativeInt()
else -> Result.failure(this shouldBe otherThanZero)
}

Expand Down
4 changes: 2 additions & 2 deletions src/commonMain/kotlin/kotools/types/number/PositiveInt.kt
Expand Up @@ -23,8 +23,8 @@ public sealed interface PositiveInt : AnyInt
*/
@SinceKotoolsTypes("1.1")
public fun Int.toPositiveInt(): Result<PositiveInt> = 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)
}

Expand Down
Expand Up @@ -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<StrictlyNegativeInt> = 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"
}

/**
Expand Down
Expand Up @@ -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<StrictlyPositiveInt> = 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"
}

/**
Expand Down
8 changes: 4 additions & 4 deletions src/commonMain/kotlin/kotools/types/number/ZeroInt.kt
Expand Up @@ -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<ZeroInt> {
Expand Down
Expand Up @@ -42,7 +42,7 @@ class NotEmptyListTest {
val elements: List<Int> = 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
Expand Down
Expand Up @@ -56,7 +56,7 @@ class NotEmptyMapTest {
"c" to Random.nextInt()
)
assertEquals(
actual = notEmptyMap.size.value,
actual = notEmptyMap.size.asInt,
expected = notEmptyMap.asMap.size
)
}
Expand Down
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/commonTest/kotlin/kotools/types/number/AnyIntTest.kt
Expand Up @@ -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)
}

Expand All @@ -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)
}
}
8 changes: 4 additions & 4 deletions src/commonTest/kotlin/kotools/types/number/NegativeIntTest.kt
Expand Up @@ -10,15 +10,15 @@ 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
fun int_toNegativeInt_should_pass_with_a_negative_Int() {
val value: Int = negativeIntRange.random()
val result: Int = value.toNegativeInt()
.getOrThrow()
.value
.asInt
assertEquals(value, result)
}

Expand All @@ -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)
}

Expand All @@ -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
Expand Down
10 changes: 5 additions & 5 deletions src/commonTest/kotlin/kotools/types/number/NonZeroIntTest.kt
Expand Up @@ -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<NonZeroInt> = ZeroInt.value.toNonZeroInt()
val result: Result<NonZeroInt> = ZeroInt.asInt.toNonZeroInt()
assertFailsWith<IllegalArgumentException>(block = result::getOrThrow)
.assertHasAMessage()
}
Expand All @@ -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)
}

Expand All @@ -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)
}
Expand Down
8 changes: 4 additions & 4 deletions src/commonTest/kotlin/kotools/types/number/PositiveIntTest.kt
Expand Up @@ -10,15 +10,15 @@ 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
fun int_toPositiveInt_should_pass_with_a_positive_Int() {
val expected: Int = positiveIntRange.random()
val actual: Int = expected.toPositiveInt()
.getOrThrow()
.value
.asInt
assertEquals(expected, actual)
}

Expand Down Expand Up @@ -46,7 +46,7 @@ class PositiveIntSerializerTest {
.toPositiveInt()
.getOrThrow()
assertEquals(
expected = Json.encodeToString(x.value),
expected = Json.encodeToString(x.asInt),
actual = Json.encodeToString(PositiveIntSerializer, x)
)
}
Expand All @@ -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
Expand Down
Expand Up @@ -31,7 +31,7 @@ class StrictlyNegativeIntTest {
assertEquals(
actual = value.toStrictlyNegativeInt()
.getOrThrow()
.value,
.asInt,
expected = value
)
}
Expand Down Expand Up @@ -61,7 +61,7 @@ class StrictlyNegativeIntSerializerTest {
.getOrThrow()
assertEquals(
actual = Json.encodeToString(x),
expected = Json.encodeToString(x.value)
expected = Json.encodeToString(x.asInt)
)
}

Expand All @@ -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
Expand Down
Expand Up @@ -19,15 +19,15 @@ 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() {
val value: Int = strictlyPositiveIntRange.random()
assertEquals(
actual = value.toStrictlyPositiveInt()
.getOrThrow()
.value,
.asInt,
expected = value
)
}
Expand Down Expand Up @@ -57,7 +57,7 @@ class StrictlyPositiveIntSerializerTest {
.getOrThrow()
assertEquals(
actual = Json.encodeToString(x),
expected = Json.encodeToString(x.value)
expected = Json.encodeToString(x.asInt)
)
}

Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/commonTest/kotlin/kotools/types/number/ZeroIntTest.kt
Expand Up @@ -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
Expand Down
Expand Up @@ -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
)
}
Expand Down

0 comments on commit c7a6585

Please sign in to comment.