Skip to content

Commit

Permalink
💥 Convert Zero from object to class (#644)
Browse files Browse the repository at this point in the history
Converts this type for providing a better instances management.
  • Loading branch information
LVMVRQUXL committed Apr 17, 2024
1 parent a6def46 commit 93ff5d1
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ internal object ZeroAsByteSerializer : KSerializer<Zero> {

override fun deserialize(decoder: Decoder): Zero {
val decodedValue: Byte = decoder.decodeByte()
val zeroAsByte: Byte = Zero.toByte()
if (decodedValue == zeroAsByte) return Zero
val zero = Zero()
val zeroAsByte: Byte = zero.toByte()
if (decodedValue == zeroAsByte) return zero
val message: String = this.deserializationErrorMessage(decodedValue)
throw SerializationException(message)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import kotlinx.serialization.json.Json
import kotools.types.experimental.ExperimentalKotoolsTypesApi
import org.kotools.types.Zero
import kotlin.random.Random
import kotlin.test.Ignore
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith
import kotlin.test.assertSame

@OptIn(ExperimentalKotoolsTypesApi::class)
class ZeroAsByteSerializerTest {
Expand All @@ -38,20 +38,22 @@ class ZeroAsByteSerializerTest {
@Test
fun serialization_should_behave_like_for_the_Byte_type() {
val serializer: KSerializer<Zero> = ZeroAsByteSerializer
val actual: String = Json.encodeToString(serializer, Zero)
val zero = Zero()
val actual: String = Json.encodeToString(serializer, zero)
val zeroAsByte: Byte = 0.toByte()
val expected: String = Json.encodeToString(zeroAsByte)
assertEquals(expected, actual)
}

@Ignore // Needs `Zero.equals(Any?)` override to work.
@Test
fun deserialization_should_pass_with_the_zero_number() {
val zeroAsByte: Byte = 0.toByte()
val encoded: String = Json.encodeToString(zeroAsByte)
val deserializer: KSerializer<Zero> = ZeroAsByteSerializer
val actual: Zero = Json.decodeFromString(deserializer, encoded)
val expected = Zero
assertSame(expected, actual)
val expected = Zero()
assertEquals(expected, actual)
}

@Test
Expand Down
2 changes: 1 addition & 1 deletion subprojects/library/src/api/types.api
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ public final class org/kotools/types/EmailAddress$Companion {
}

public final class org/kotools/types/Zero {
public static final field INSTANCE Lorg/kotools/types/Zero;
public fun <init> ()V
public final fun toByte ()B
public final fun toString ()Ljava/lang/String;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@ import kotools.types.experimental.ExperimentalKotoolsTypesApi
import org.kotools.types.internal.ExperimentalSince
import org.kotools.types.internal.KotoolsTypesVersion

/** Represents the [zero](https://en.wikipedia.org/wiki/0) number. */
/**
* Represents the [zero](https://en.wikipedia.org/wiki/0) number.
*
* @constructor Creates an instance of [Zero].
*/
@ExperimentalKotoolsTypesApi
@ExperimentalSince(KotoolsTypesVersion.Unreleased)
public object Zero {
private const val VALUE: Byte = 0
public class Zero {
private val valueAsByte: Byte = 0

/**
* Returns this number as [Byte].
Expand All @@ -35,7 +39,7 @@ public object Zero {
* SAMPLE: ZeroJavaSample.toByte.md
* </details>
*/
public fun toByte(): Byte = this.VALUE
public fun toByte(): Byte = this.valueAsByte

/**
* Returns the string representation of this number.
Expand Down Expand Up @@ -63,5 +67,5 @@ public object Zero {
* </details>
*/
@Suppress("RedundantModalityModifier")
final override fun toString(): String = VALUE.toString()
final override fun toString(): String = this.valueAsByte.toString()
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,17 @@ class ZeroTest {
@OptIn(ExperimentalKotoolsTypesApi::class)
@Test
fun toByte_should_pass() {
val actual: Byte = Zero.toByte()
val actual: Byte = Zero()
.toByte()
val expected: Byte = 0.toByte()
assertEquals(expected, actual)
}

@OptIn(ExperimentalKotoolsTypesApi::class)
@Test
fun toString_should_pass() {
val actual: String = Zero.toString()
val actual: String = Zero()
.toString()
val expected = "0"
assertEquals(expected, actual)
}
Expand Down
6 changes: 4 additions & 2 deletions subprojects/samples/src/main/java/ZeroJavaSample.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

class ZeroJavaSample {
void toByte() {
final byte number = Zero.INSTANCE.toByte();
final Zero zero = new Zero();
final byte number = zero.toByte();
System.out.println(number); // 0
} // END

void toStringSample() {
final String message = Zero.INSTANCE.toString();
final Zero zero = new Zero();
final String message = zero.toString();
System.out.println(message); // 0
} // END
}
6 changes: 4 additions & 2 deletions subprojects/samples/src/main/kotlin/ZeroKotlinSample.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import kotools.types.experimental.ExperimentalKotoolsTypesApi
@OptIn(ExperimentalKotoolsTypesApi::class)
internal object ZeroKotlinSample {
fun toByte() {
val number: Byte = Zero.toByte()
val number: Byte = Zero()
.toByte() // TABS: 1
println(number) // 0
} // END

fun toStringSample() {
val message: String = Zero.toString()
val message: String = Zero()
.toString() // TABS: 2
println(message) // 0
} // END
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,20 @@ import org.kotools.types.Zero
internal object KotoolsTypesSerializersKotlinSample {
fun all() {
val format = Json { serializersModule = KotoolsTypesSerializers.all }
val encoded: String = format.encodeToString(Zero)
val zero = Zero()
val encoded: String = format.encodeToString(zero)
println(encoded) // 0
val decoded: Zero = format.decodeFromString(encoded)
println(Zero === decoded) // true
println(zero.toByte() == decoded.toByte()) // true
} // END

fun zero() {
val format = Json { serializersModule = KotoolsTypesSerializers.zero }
val encoded: String = format.encodeToString(Zero)
val zero = Zero()
val encoded: String = format.encodeToString(zero)
println(encoded) // 0
val decoded: Zero = format.decodeFromString(encoded)
println(Zero === decoded) // true
println(zero.toByte() == decoded.toByte()) // true
} // END

@Suppress("FunctionName")
Expand Down

0 comments on commit 93ff5d1

Please sign in to comment.