From fc5f74bcd010e7267a6509253b95535fb7792057 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Lamarque?= Date: Thu, 9 Jan 2025 18:44:22 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Add=20`Zero.Companion.orThrow(Strin?= =?UTF-8?q?g)`=20method=20(#761)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- subprojects/library/src/api/types.api | 2 + .../kotlin/org/kotools/types/Zero.kt | 40 +++++++++++++++++++ .../types/ZeroCompanionCommonSample.kt | 12 ++++++ .../org/kotools/types/ZeroCompanionTest.kt | 16 ++++++++ .../types/ZeroCompanionJavaSample.java | 15 +++++++ 5 files changed, 85 insertions(+) diff --git a/subprojects/library/src/api/types.api b/subprojects/library/src/api/types.api index 022c748e2..bc795e65c 100644 --- a/subprojects/library/src/api/types.api +++ b/subprojects/library/src/api/types.api @@ -444,6 +444,7 @@ public final class org/kotools/types/Zero { public static final fun orThrow (F)Lorg/kotools/types/Zero; public static final fun orThrow (I)Lorg/kotools/types/Zero; public static final fun orThrow (J)Lorg/kotools/types/Zero; + public static final fun orThrow (Ljava/lang/String;)Lorg/kotools/types/Zero; public static final fun orThrow (S)Lorg/kotools/types/Zero; public final fun toByte ()B public final fun toChar ()C @@ -467,6 +468,7 @@ public final class org/kotools/types/Zero$Companion { public final fun orThrow (F)Lorg/kotools/types/Zero; public final fun orThrow (I)Lorg/kotools/types/Zero; public final fun orThrow (J)Lorg/kotools/types/Zero; + public final fun orThrow (Ljava/lang/String;)Lorg/kotools/types/Zero; public final fun orThrow (S)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 b90ff4a33..69acd1ba8 100644 --- a/subprojects/library/src/commonMain/kotlin/org/kotools/types/Zero.kt +++ b/subprojects/library/src/commonMain/kotlin/org/kotools/types/Zero.kt @@ -928,5 +928,45 @@ public class Zero { require(number == 0.0) { InvalidZero(number) } return Zero() } + + /** + * Creates an instance of [Zero] from the specified [text], or throws an + * [IllegalArgumentException] if the [text] is an invalid representation + * of zero. + * + * The [text] is a valid representation if it matches the following + * regular expression: [`^0+(?:\.0+)?$`](https://regexr.com/8arpu). + * + *
+ *
+ * + * Calling from Kotlin + * + * + * Here's an example of calling this method from Kotlin code: + * + * SAMPLE: [org.kotools.types.ZeroCompanionCommonSample.orThrowWithString] + *
+ * + *
+ *
+ * + * Calling from Java + * + * + * Here's an example of calling this method from Java code: + * + * SAMPLE: [org.kotools.types.ZeroCompanionJavaSample.orThrowWithString] + *
+ */ + @ExperimentalSince(KotoolsTypesVersion.V5_0_0) + @JvmStatic + public fun orThrow(text: String): Zero { + val regex = Regex("""^0+(?:\.0+)?$""") + require(text matches regex) { + "'$text' is not a valid representation of zero." + } + return Zero() + } } } diff --git a/subprojects/library/src/commonTest/kotlin/org/kotools/types/ZeroCompanionCommonSample.kt b/subprojects/library/src/commonTest/kotlin/org/kotools/types/ZeroCompanionCommonSample.kt index 87310b571..db33d005f 100644 --- a/subprojects/library/src/commonTest/kotlin/org/kotools/types/ZeroCompanionCommonSample.kt +++ b/subprojects/library/src/commonTest/kotlin/org/kotools/types/ZeroCompanionCommonSample.kt @@ -111,4 +111,16 @@ class ZeroCompanionCommonSample { } assertTrue(isSuccess) } + + @Test + fun orThrowWithString() { + val isSuccess: Boolean = try { + listOf("0", "000", "0.0", "0.000", "000.0", "000.000") + .forEach(Zero.Companion::orThrow) + true + } catch (exception: IllegalArgumentException) { + false + } + assertTrue(isSuccess) + } } diff --git a/subprojects/library/src/commonTest/kotlin/org/kotools/types/ZeroCompanionTest.kt b/subprojects/library/src/commonTest/kotlin/org/kotools/types/ZeroCompanionTest.kt index d4050a1b6..e18ef03a8 100644 --- a/subprojects/library/src/commonTest/kotlin/org/kotools/types/ZeroCompanionTest.kt +++ b/subprojects/library/src/commonTest/kotlin/org/kotools/types/ZeroCompanionTest.kt @@ -114,6 +114,22 @@ class ZeroCompanionTest { assertFailsWith { Zero.orThrow(number) } .assertIsInvalidZero(number) } + + @Test + fun orThrowShouldPassWithValidText(): Unit = + listOf("0", "000", "0.0", "0.000", "000.0", "000.000") + .forEach(Zero.Companion::orThrow) + + @Test + fun orThrowShouldFailWithInvalidText(): Unit = + listOf("", " ", ".", "0.", ".0", "abc") + .forEach { + val exception: IllegalArgumentException = + assertFailsWith { Zero.orThrow(it) } + val actual: String? = exception.message + val expected = "'$it' is not a valid representation of zero." + assertEquals(expected, actual) + } } // ----------------------------- Number extensions ----------------------------- diff --git a/subprojects/library/src/jvmTest/java/org/kotools/types/ZeroCompanionJavaSample.java b/subprojects/library/src/jvmTest/java/org/kotools/types/ZeroCompanionJavaSample.java index 5dccd4554..e4b3025a8 100644 --- a/subprojects/library/src/jvmTest/java/org/kotools/types/ZeroCompanionJavaSample.java +++ b/subprojects/library/src/jvmTest/java/org/kotools/types/ZeroCompanionJavaSample.java @@ -3,6 +3,8 @@ import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; +import java.util.Arrays; + @SuppressWarnings("NewClassNamingConvention") class ZeroCompanionJavaSample { @Test @@ -82,4 +84,17 @@ void orThrowWithDouble() { } Assertions.assertTrue(isSuccess); } + + @Test + void orThrowWithString() { + boolean isSuccess; + try { + Arrays.asList("0", "000", "0.0", "0.000", "000.0", "000.000") + .forEach(Zero::orThrow); + isSuccess = true; + } catch (final IllegalArgumentException exception) { + isSuccess = false; + } + Assertions.assertTrue(isSuccess); + } }