From 8f371c5381e3654083b657e9594c69efc33484ca Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Thu, 17 Oct 2019 12:08:20 +0200 Subject: [PATCH] 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. --- .../jivesoftware/smack/datatypes/Scalar.java | 26 +++++++++++++++---- .../jivesoftware/smack/datatypes/UInt16.java | 15 +++++++++++ .../jivesoftware/smack/datatypes/UInt32.java | 16 ++++++++++++ 3 files changed, 52 insertions(+), 5 deletions(-) diff --git a/smack-core/src/main/java/org/jivesoftware/smack/datatypes/Scalar.java b/smack-core/src/main/java/org/jivesoftware/smack/datatypes/Scalar.java index fe2bca23a..9891c2e42 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/datatypes/Scalar.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/datatypes/Scalar.java @@ -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 diff --git a/smack-core/src/main/java/org/jivesoftware/smack/datatypes/UInt16.java b/smack-core/src/main/java/org/jivesoftware/smack/datatypes/UInt16.java index 999597401..22dd8cd8d 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/datatypes/UInt16.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/datatypes/UInt16.java @@ -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); + } } diff --git a/smack-core/src/main/java/org/jivesoftware/smack/datatypes/UInt32.java b/smack-core/src/main/java/org/jivesoftware/smack/datatypes/UInt32.java index cb6c9aa28..e3e6bc670 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/datatypes/UInt32.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/datatypes/UInt32.java @@ -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); + } }