hashCode

override fun hashCode(): Int

Returns a hash code value for this integer.

This function follows the contract of Any.hashCode, with an additional property: if two instances of NonNegativeInteger are not equal, then calling this function on these objects must produce different hash codes.


Calling from Kotlin

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

val value: Integer = Integer.fromLong(42)
val x: NonNegativeInteger = NonNegativeInteger.fromInteger(value)
val y: NonNegativeInteger = NonNegativeInteger.fromInteger(value)
check(x == y)
check(x.hashCode() == y.hashCode())

val other: Integer = Integer.fromLong(7)
val z: NonNegativeInteger = NonNegativeInteger.fromInteger(other)
check(x != z)
check(x.hashCode() != z.hashCode())

Calling from Java

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

final Integer value = Integer.fromLong(42);
final NonNegativeInteger x = NonNegativeInteger.fromInteger(value);
final NonNegativeInteger y = NonNegativeInteger.fromInteger(value);
final boolean equality = x.equals(y) && x.hashCode() == y.hashCode();
if (!equality) throw new IllegalStateException("Check failed.");

final Integer other = Integer.fromLong(7);
final NonNegativeInteger z = NonNegativeInteger.fromInteger(other);
final boolean inequality =
!x.equals(z) && x.hashCode() != z.hashCode();
if (!inequality) throw new IllegalStateException("Check failed.");