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 NonPositiveInteger 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: NonPositiveInteger = NonPositiveInteger.fromInteger(value)
val y: NonPositiveInteger = NonPositiveInteger.fromInteger(value)
check(x == y)
check(x.hashCode() == y.hashCode())

val other: Integer = Integer.fromLong(-7)
val z: NonPositiveInteger = NonPositiveInteger.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 NonPositiveInteger x = NonPositiveInteger.fromInteger(value);
final NonPositiveInteger y = NonPositiveInteger.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 NonPositiveInteger z = NonPositiveInteger.fromInteger(other);
final boolean inequality =
!x.equals(z) && x.hashCode() != z.hashCode();
if (!inequality) throw new IllegalStateException("Check failed.");