fromLong

Returns a NonPositiveInteger representing the specified value, or throws an IllegalArgumentException if value is positive.


Calling from Kotlin

Here's an example of calling this function from Kotlin code:

val result: NonPositiveInteger = NonPositiveInteger.fromLong(-42)
check(result.toInteger() == Integer.fromLong(-42))

val exception: Throwable? =
runCatching { NonPositiveInteger.fromLong(1) }.exceptionOrNull()
check(exception is IllegalArgumentException)

val safeResult: NonPositiveInteger? =
NonPositiveInteger.fromLongOrNull(1)
check(safeResult == null)

Calling from Java

Here's an example of calling this function from Java code:

final NonPositiveInteger result = NonPositiveInteger.fromLong(-42L);
final boolean check = result.toInteger().equals(Integer.fromLong(-42));
if (!check) throw new IllegalStateException("Check failed.");

try {
NonPositiveInteger.fromLong(1L);
throw new IllegalStateException("Check failed.");
} catch (IllegalArgumentException ignored) {
}

See the fromLongOrNull function for returning null instead of throwing an exception when value is positive.