Skip to content

Commit

Permalink
✨ Add Zero.Companion.orThrow(Float) method (#719)
Browse files Browse the repository at this point in the history
  • Loading branch information
LVMVRQUXL committed Jan 9, 2025
1 parent d9fd679 commit 2c5dc6b
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 0 deletions.
2 changes: 2 additions & 0 deletions subprojects/library/src/api/types.api
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,7 @@ public final class org/kotools/types/Zero {
public final fun equals (Ljava/lang/Object;)Z
public final fun hashCode ()I
public static final fun orThrow (B)Lorg/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 (S)Lorg/kotools/types/Zero;
Expand All @@ -459,6 +460,7 @@ public final class org/kotools/types/Zero$Companion {
public final synthetic fun orNull (J)Lorg/kotools/types/Zero;
public final synthetic fun orNull (S)Lorg/kotools/types/Zero;
public final fun orThrow (B)Lorg/kotools/types/Zero;
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 (S)Lorg/kotools/types/Zero;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -794,5 +794,42 @@ public class Zero {
require(number == 0L) { InvalidZero(number) }
return Zero()
}

/**
* Creates an instance of [Zero] from the specified [number], or throws
* an [IllegalArgumentException] if the [number] is other than zero.
*
* <br>
* <details>
* <summary>
* <b>Calling from Kotlin</b>
* </summary>
*
* Here's an example of calling this method from Kotlin code:
*
* SAMPLE: [org.kotools.types.ZeroCompanionCommonSample.orThrowWithFloat]
* </details>
*
* <br>
* <details>
* <summary>
* <b>Calling from Java</b>
* </summary>
*
* Here's an example of calling this method from Java code:
*
* SAMPLE: [org.kotools.types.ZeroCompanionJavaSample.orThrowWithFloat]
* </details>
* <br>
*
* See the [orNull] method for returning `null` instead of throwing an
* exception in case of invalid [number].
*/
@ExperimentalSince(KotoolsTypesVersion.V5_0_0)
@JvmStatic
public fun orThrow(number: Float): Zero {
require(number == 0f) { InvalidZero(number) }
return Zero()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,15 @@ class ZeroCompanionCommonSample {
}
assertTrue(isSuccess)
}

@Test
fun orThrowWithFloat() {
val isSuccess: Boolean = try {
Zero.orThrow(0f)
true
} catch (exception: IllegalArgumentException) {
false
}
assertTrue(isSuccess)
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.kotools.types

import org.kotools.types.internal.InvalidZero
import kotlin.random.Random
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith
Expand Down Expand Up @@ -83,8 +84,28 @@ class ZeroCompanionTest {
assertFailsWith<IllegalArgumentException> { Zero.orThrow(number) }
.assertIsInvalidZero(number)
}

@Test
fun orThrowShouldFailWithFloatOtherThanZero() {
val number: Float = Float.randomNonZero()
assertFailsWith<IllegalArgumentException> { Zero.orThrow(number) }
.assertIsInvalidZero(number)
}
}

// ----------------------------- Number extensions -----------------------------

private fun Float.Companion.randomNonZero(): Float {
val negativeRange: IntRange = Byte.MIN_VALUE..-1
val positiveRange: IntRange = 1..Byte.MAX_VALUE
val integer: Int = negativeRange.plus(positiveRange)
.random()
val decimal: Float = Random.nextFloat()
return integer + decimal
}

// -------------------------------- Assertions ---------------------------------

private fun IllegalArgumentException.assertIsInvalidZero(number: Number) {
val actual: String? = this.message
val expected: String = InvalidZero(number)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,17 @@ void orThrowWithLong() {
}
Assertions.assertTrue(isSuccess);
}

@Test
void orThrowWithFloat() {
final float number = 0;
boolean isSuccess;
try {
Zero.orThrow(number);
isSuccess = true;
} catch (final IllegalArgumentException exception) {
isSuccess = false;
}
Assertions.assertTrue(isSuccess);
}
}

0 comments on commit 2c5dc6b

Please sign in to comment.