NonNegativeInteger

Represents an Integer that is greater than or equal to zero.


Motivations

Motivations

A standalone type instead of a runtime check

Whether an integer is non-negative could be checked at runtime, inline, wherever such a guarantee is needed. Instead, this library models "an Integer that is greater than or equal to zero" as its own type. This is what lets Integer.rem express the non-negative half of the Euclidean division theorem directly in its return type, rather than merely asserting it in documentation prose: any value of type NonNegativeInteger is already known to satisfy >= 0, so callers of Integer.rem get that guarantee statically instead of having to trust and re-verify it themselves.

A closure-driven arithmetic surface

plus and times stay within NonNegativeInteger, because the set of non-negative integers is closed under addition and under multiplication by another non-negative integer.

unaryMinus and times with a NonPositiveInteger operand cross over to NonPositiveInteger instead of staying within NonNegativeInteger: negating a non-negative integer, or multiplying it by a non-positive one, never stays non-negative (except for zero), but its result is always representable as a NonPositiveInteger. Returning NonPositiveInteger keeps both operations total instead of omitting them or widening their result to the unconstrained Integer.

minus only accepts a NonPositiveInteger operand, not a NonNegativeInteger one: subtracting a non-positive integer from a non-negative one always stays non-negative (x - y == x + (-y), and -y >= 0), but subtracting another non-negative integer can produce a negative result (e.g. 1 - 2 = -1), which isn't representable as a NonNegativeInteger. Use toInteger together with Integer.minus for that case.

Division and remainder operations are absent, since they would only duplicate the semantics already covered by Integer.div and Integer.rem.


Key features

Key features

Since

5.2.0

Types

Link copied to clipboard
object Companion

Contains class-level declarations for the NonNegativeInteger type.

Functions

Link copied to clipboard
operator override fun equals(other: Any?): Boolean

Returns true if the other object is a NonNegativeInteger representing the same numeric value as this one, or returns false otherwise.

Link copied to clipboard
override fun hashCode(): Int

Returns a hash code value for this integer.

Link copied to clipboard

Subtracts the other integer from this one.

Link copied to clipboard

Adds the other integer to this one.

Link copied to clipboard

Multiplies this integer by the other one.

Link copied to clipboard

Returns this integer as an Integer.

Link copied to clipboard
override fun toString(): String

Returns the decimal string representation of this integer, delegating to Integer.toString.

Link copied to clipboard

Returns the negative of this integer.