parseOrNull

Returns a NonNegativeInteger representing the number described by value, or returns null if value doesn't represent an integer or represents a negative integer.

See the Integer.Companion.parse function for the grammar of a valid integer representation.


Calling from Kotlin

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

val result: NonNegativeInteger = NonNegativeInteger.parse("+00042")
check("$result" == "42")

val invalid: Throwable? =
runCatching { NonNegativeInteger.parse("3.14") }.exceptionOrNull()
check(invalid is NumberFormatException)

val negative: Throwable? =
runCatching { NonNegativeInteger.parse("-1") }.exceptionOrNull()
check(negative is IllegalArgumentException)

check(NonNegativeInteger.parseOrNull("3.14") == null)
check(NonNegativeInteger.parseOrNull("-1") == null)

This function is hidden from Java, because nullability is not explicit in its type system.

See the parse function for throwing an exception instead of returning null in case of invalid or negative value.