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