Fix datatype.Scalar equals() and hashCode() methods

The previously used Number.equals() and hashCode() was just delegated
to Object and hence did not behave as expected.
This commit is contained in:
Florian Schmaus 2019-10-17 12:08:20 +02:00
parent db150a850a
commit 8f371c5381
3 changed files with 52 additions and 5 deletions

View File

@ -54,13 +54,29 @@ public abstract class Scalar extends java.lang.Number {
}
@Override
public final int hashCode() {
return number.hashCode();
}
public abstract int hashCode();
@Override
public final boolean equals(Object other) {
return number.equals(other);
public boolean equals(Object other) {
if (!(other instanceof Scalar)) {
return false;
}
Scalar otherScalar = (Scalar) other;
if (longValue() == otherScalar.longValue()) {
return true;
}
if (doubleValue() == otherScalar.doubleValue()) {
return true;
}
if (floatValue() == otherScalar.floatValue()) {
return true;
}
return false;
}
@Override

View File

@ -39,4 +39,19 @@ public final class UInt16 extends Scalar {
public static UInt16 from(int number) {
return new UInt16(number);
}
@Override
public int hashCode() {
return number;
}
@Override
public boolean equals(Object other) {
if (other instanceof UInt16) {
UInt16 otherUint16 = (UInt16) other;
return number == otherUint16.number;
}
return super.equals(other);
}
}

View File

@ -39,4 +39,20 @@ public final class UInt32 extends Scalar {
public static UInt32 from(long number) {
return new UInt32(number);
}
@Override
public int hashCode() {
// TODO: Use Long.hashCode(number) once Smack's minimum Android SDK level is 24 or higher.
return (int) (number ^ (number >>> 32));
}
@Override
public boolean equals(Object other) {
if (other instanceof UInt32) {
UInt32 otherUint32 = (UInt32) other;
return number == otherUint32.number;
}
return super.equals(other);
}
}