Skip to content

Commit

Permalink
🔥 Remove Zero.Companion.orNull(Any) function (#676)
Browse files Browse the repository at this point in the history
  • Loading branch information
LVMVRQUXL committed Jul 15, 2024
1 parent 0878450 commit 01fab1f
Show file tree
Hide file tree
Showing 6 changed files with 6 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@ internal object ZeroAsByteSerializer : KSerializer<Zero> {

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")
}
}
}
2 changes: 0 additions & 2 deletions subprojects/library/src/api/types.api
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}

Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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].
*
* <br>
* <details open>
* <summary>
* <b>Calling from Kotlin</b>
* </summary>
*
* Here's an example of calling this function from Kotlin code:
*
* SAMPLE: [org.kotools.types.ZeroCompanionCommonSample.orNull]
* </details>
*
* <br>
* <details>
* <summary>
* <b>Calling from Java</b>
* </summary>
*
* Here's an example of calling this function from Java code:
*
* SAMPLE: [org.kotools.types.ZeroCompanionJavaSample.orNull]
* </details>
*/
@ExperimentalSince(KotoolsTypesVersion.Unreleased)
@JvmStatic
public fun orNull(number: Any): Zero? {
val regex = Regex(this.PATTERN)
return if ("$number" matches regex) Zero() else null
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -303,38 +301,10 @@ class ZeroTest {

@OptIn(ExperimentalKotoolsTypesApi::class)
class ZeroCompanionTest {
private val validNumbers: List<Any>
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<Any>
get() = listOf<Any>(
".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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit 01fab1f

Please sign in to comment.