mirror of
https://github.com/vanitasvitae/Smack.git
synced 2024-11-25 13:32:07 +01:00
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:
parent
db150a850a
commit
8f371c5381
3 changed files with 52 additions and 5 deletions
|
@ -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
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue