fromInteger

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 value: Integer = Integer.fromLong(-42)
val result: NonPositiveInteger = NonPositiveInteger.fromInteger(value)
check(result.toInteger() == value)

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

val safeResult: NonPositiveInteger? =
NonPositiveInteger.fromIntegerOrNull(positive)
check(safeResult == null)

Calling from Java

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

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

final Integer positive = Integer.fromLong(1);
try {
NonPositiveInteger.fromInteger(positive);
throw new IllegalStateException("Check failed.");
} catch (IllegalArgumentException ignored) {
}

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