fromLong
Returns a NonZeroInteger representing the specified value, or throws an IllegalArgumentException if value is 0.
Calling from Kotlin
Here's an example of calling this function from Kotlin code:
val result: NonZeroInteger = NonZeroInteger.fromLong(42)
check("$result" == "42")
val exception: Throwable? =
runCatching { NonZeroInteger.fromLong(0) }.exceptionOrNull()
check(exception is IllegalArgumentException)
val safeResult: NonZeroInteger? = NonZeroInteger.fromLongOrNull(0)
check(safeResult == null)Content copied to clipboard
Calling from Java
Here's an example of calling this function from Java code:
final NonZeroInteger result = NonZeroInteger.fromLong(42L);
final boolean check = String.valueOf(result).equals("42");
if (!check) throw new IllegalStateException("Check failed.");
try {
NonZeroInteger.fromLong(0L);
throw new IllegalStateException("Check failed.");
} catch (IllegalArgumentException ignored) {
}Content copied to clipboard
See the fromLongOrNull function for returning null instead of throwing an exception when value is 0.