From 01fab1f3d7cb10371e6b0145d4c45bb90209463f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Lamarque?= Date: Mon, 15 Jul 2024 08:29:01 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A5=20Remove=20`Zero.Companion.orNull(?= =?UTF-8?q?Any)`=20function=20(#676)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../internal/ZeroAsByteSerializer.kt | 10 +++--- subprojects/library/src/api/types.api | 2 -- .../kotlin/org/kotools/types/Zero.kt | 35 ------------------- .../types/ZeroCompanionCommonSample.kt | 8 ----- .../kotlin/org/kotools/types/ZeroTest.kt | 30 ---------------- .../types/ZeroCompanionJavaSample.java | 7 ---- 6 files changed, 6 insertions(+), 86 deletions(-) diff --git a/subprojects/kotlinx-serialization/src/commonMain/kotlin/org/kotools/types/kotlinx/serialization/internal/ZeroAsByteSerializer.kt b/subprojects/kotlinx-serialization/src/commonMain/kotlin/org/kotools/types/kotlinx/serialization/internal/ZeroAsByteSerializer.kt index f26dc0ee4..1dc166167 100644 --- a/subprojects/kotlinx-serialization/src/commonMain/kotlin/org/kotools/types/kotlinx/serialization/internal/ZeroAsByteSerializer.kt +++ b/subprojects/kotlinx-serialization/src/commonMain/kotlin/org/kotools/types/kotlinx/serialization/internal/ZeroAsByteSerializer.kt @@ -26,9 +26,11 @@ internal object ZeroAsByteSerializer : KSerializer { override fun deserialize(decoder: Decoder): Zero { val decodedValue: Byte = decoder.decodeByte() - val zero: Zero? = Zero.orNull(decodedValue) - if (zero != null) return zero - val error = InvalidZero(decodedValue) - throw SerializationException("$error") + return try { + Zero(decodedValue) + } catch (exception: IllegalArgumentException) { + val error = InvalidZero(decodedValue) + throw SerializationException("$error") + } } } diff --git a/subprojects/library/src/api/types.api b/subprojects/library/src/api/types.api index cf33d491b..28855bdd2 100644 --- a/subprojects/library/src/api/types.api +++ b/subprojects/library/src/api/types.api @@ -466,7 +466,6 @@ public final class org/kotools/types/Zero { public final fun compareTo (S)I public final fun equals (Ljava/lang/Object;)Z public final fun hashCode ()I - public static final fun orNull (Ljava/lang/Object;)Lorg/kotools/types/Zero; public final fun toByte ()B public final fun toChar ()C public final fun toDouble ()D @@ -478,6 +477,5 @@ public final class org/kotools/types/Zero { } public final class org/kotools/types/Zero$Companion { - public final fun orNull (Ljava/lang/Object;)Lorg/kotools/types/Zero; } diff --git a/subprojects/library/src/commonMain/kotlin/org/kotools/types/Zero.kt b/subprojects/library/src/commonMain/kotlin/org/kotools/types/Zero.kt index 14fad89bb..44bd7b019 100644 --- a/subprojects/library/src/commonMain/kotlin/org/kotools/types/Zero.kt +++ b/subprojects/library/src/commonMain/kotlin/org/kotools/types/Zero.kt @@ -5,7 +5,6 @@ import kotools.types.internal.hashCodeOf import org.kotools.types.internal.ExperimentalSince import org.kotools.types.internal.KotoolsTypesVersion import org.kotools.types.internal.Warning -import kotlin.jvm.JvmStatic /** Represents the [zero](https://en.wikipedia.org/wiki/0) number. */ @ExperimentalKotoolsTypesApi @@ -603,39 +602,5 @@ public class Zero { */ @ExperimentalSince(KotoolsTypesVersion.Unreleased) public const val PATTERN: String = "^[+-]?0+(?:\\.0+)?\$" - - /** - * Creates an instance of [Zero] from the string representation of the - * specified [number], or returns `null` if the string representation of - * [number] doesn't match the [corresponding pattern][Zero.PATTERN]. - * - *
- *
- * - * Calling from Kotlin - * - * - * Here's an example of calling this function from Kotlin code: - * - * SAMPLE: [org.kotools.types.ZeroCompanionCommonSample.orNull] - *
- * - *
- *
- * - * Calling from Java - * - * - * Here's an example of calling this function from Java code: - * - * SAMPLE: [org.kotools.types.ZeroCompanionJavaSample.orNull] - *
- */ - @ExperimentalSince(KotoolsTypesVersion.Unreleased) - @JvmStatic - public fun orNull(number: Any): Zero? { - val regex = Regex(this.PATTERN) - return if ("$number" matches regex) Zero() else null - } } } diff --git a/subprojects/library/src/commonSample/kotlin/org/kotools/types/ZeroCompanionCommonSample.kt b/subprojects/library/src/commonSample/kotlin/org/kotools/types/ZeroCompanionCommonSample.kt index a7169e7ba..4fb34a220 100644 --- a/subprojects/library/src/commonSample/kotlin/org/kotools/types/ZeroCompanionCommonSample.kt +++ b/subprojects/library/src/commonSample/kotlin/org/kotools/types/ZeroCompanionCommonSample.kt @@ -2,7 +2,6 @@ package org.kotools.types import kotools.types.experimental.ExperimentalKotoolsTypesApi import kotlin.test.Test -import kotlin.test.assertNotNull import kotlin.test.assertTrue @OptIn(ExperimentalKotoolsTypesApi::class) @@ -18,11 +17,4 @@ class ZeroCompanionCommonSample { val numbersAreValid: Boolean = numbers.all { "$it" matches regex } assertTrue(numbersAreValid) } - - @Test - fun orNull() { - val number: Any = "-000.000" - val actual: Zero? = Zero.orNull(number) - assertNotNull(actual) - } } diff --git a/subprojects/library/src/commonTest/kotlin/org/kotools/types/ZeroTest.kt b/subprojects/library/src/commonTest/kotlin/org/kotools/types/ZeroTest.kt index f7e016256..db71e3b4a 100644 --- a/subprojects/library/src/commonTest/kotlin/org/kotools/types/ZeroTest.kt +++ b/subprojects/library/src/commonTest/kotlin/org/kotools/types/ZeroTest.kt @@ -6,8 +6,6 @@ import kotlin.test.Test import kotlin.test.assertEquals import kotlin.test.assertFailsWith import kotlin.test.assertFalse -import kotlin.test.assertNotNull -import kotlin.test.assertNull import kotlin.test.assertTrue @OptIn(ExperimentalKotoolsTypesApi::class) @@ -303,38 +301,10 @@ class ZeroTest { @OptIn(ExperimentalKotoolsTypesApi::class) class ZeroCompanionTest { - private val validNumbers: List - get() = listOf( - 0, 0.0, - "+0", "+000", "+0.000", "+000.000", // with unary plus - "-0", "-000", "-0.000", "-000.000" // with unary minus - ) - - private val invalidNumbers: List - get() = listOf( - ".0", "+.0", "-.0", // integer part missing - "0,0", "+0,0", "-0,0", // comma as decimal point - "0.", "+0.", "-0.", // decimal part missing - "hello world", "123456789" // not zero number - ) - @Test fun patternShouldPass() { val actual: String = Zero.PATTERN val expected = """^[+-]?0+(?:\.0+)?$""" assertEquals(expected, actual) } - - @Test - fun orNullShouldPassWithValidNumber(): Unit = this.validNumbers.forEach { - val actual: Zero? = Zero.orNull(it) - assertNotNull(actual) - } - - @Test - fun orNullShouldFailWithInvalidNumber(): Unit = - this.invalidNumbers.forEach { - val actual: Zero? = Zero.orNull(it) - assertNull(actual) - } } diff --git a/subprojects/library/src/jvmSample/java/org/kotools/types/ZeroCompanionJavaSample.java b/subprojects/library/src/jvmSample/java/org/kotools/types/ZeroCompanionJavaSample.java index 3882a65b6..18f598108 100644 --- a/subprojects/library/src/jvmSample/java/org/kotools/types/ZeroCompanionJavaSample.java +++ b/subprojects/library/src/jvmSample/java/org/kotools/types/ZeroCompanionJavaSample.java @@ -19,11 +19,4 @@ void pattern() { .allMatch(number -> number.toString().matches(regex)); Assertions.assertTrue(numbersAreValid); } - - @Test - void orNull() { - final Object number = "-000.000"; - final Zero actual = Zero.orNull(number); - Assertions.assertNotNull(actual); - } }