From 8379263d092c716ef2ab03d3d58eadc0dc1bc609 Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Sun, 30 Nov 2014 21:09:39 +0200 Subject: [PATCH] Properly implement hashCode(). --- .../com/nutomic/ensichat/messages/DeviceInfoMessage.scala | 4 ++-- .../main/scala/com/nutomic/ensichat/messages/Message.scala | 4 ++-- .../nutomic/ensichat/messages/ResultAddContactMessage.scala | 5 +++-- .../scala/com/nutomic/ensichat/messages/TextMessage.scala | 4 ++-- 4 files changed, 9 insertions(+), 8 deletions(-) diff --git a/app/src/main/scala/com/nutomic/ensichat/messages/DeviceInfoMessage.scala b/app/src/main/scala/com/nutomic/ensichat/messages/DeviceInfoMessage.scala index 3045255..1e44028 100644 --- a/app/src/main/scala/com/nutomic/ensichat/messages/DeviceInfoMessage.scala +++ b/app/src/main/scala/com/nutomic/ensichat/messages/DeviceInfoMessage.scala @@ -2,7 +2,7 @@ package com.nutomic.ensichat.messages import java.security.spec.X509EncodedKeySpec import java.security.{KeyFactory, PublicKey} -import java.util.Date +import java.util.{Date, Objects} import com.nutomic.ensichat.bluetooth.Device import com.nutomic.ensichat.messages.Message._ @@ -35,7 +35,7 @@ class DeviceInfoMessage(override val sender: Device.ID, override val receiver: D override def equals(a: Any) = super.equals(a) && a.asInstanceOf[DeviceInfoMessage].publicKey.toString == publicKey.toString - override def hashCode = super.hashCode + publicKey.hashCode + override def hashCode = Objects.hash(super.hashCode: java.lang.Integer, publicKey) override def toString = "DeviceInfoMessage(" + sender.toString + ", " + receiver.toString + ", " + date.toString + ", " + publicKey.toString + ")" diff --git a/app/src/main/scala/com/nutomic/ensichat/messages/Message.scala b/app/src/main/scala/com/nutomic/ensichat/messages/Message.scala index 57579af..5156e56 100644 --- a/app/src/main/scala/com/nutomic/ensichat/messages/Message.scala +++ b/app/src/main/scala/com/nutomic/ensichat/messages/Message.scala @@ -1,7 +1,7 @@ package com.nutomic.ensichat.messages import java.io.IOException -import java.util.Date +import java.util.{Date, Objects} import com.nutomic.ensichat.bluetooth.Device import org.msgpack.ScalaMessagePack @@ -116,7 +116,7 @@ abstract class Message(val messageType: Int) { * Implementations must provide their own implementation to check the result of this * function and their own data. */ - override def hashCode: Int = sender.hashCode + receiver.hashCode + date.hashCode + override def hashCode: Int = Objects.hash(sender, receiver, date) override def toString: String diff --git a/app/src/main/scala/com/nutomic/ensichat/messages/ResultAddContactMessage.scala b/app/src/main/scala/com/nutomic/ensichat/messages/ResultAddContactMessage.scala index 1fe2259..016c7bc 100644 --- a/app/src/main/scala/com/nutomic/ensichat/messages/ResultAddContactMessage.scala +++ b/app/src/main/scala/com/nutomic/ensichat/messages/ResultAddContactMessage.scala @@ -1,6 +1,6 @@ package com.nutomic.ensichat.messages -import java.util.Date +import java.util.{Date, Objects} import com.nutomic.ensichat.activities.AddContactsActivity import com.nutomic.ensichat.bluetooth.Device @@ -28,7 +28,8 @@ class ResultAddContactMessage(override val sender: Device.ID, override val recei override def equals(a: Any) = super.equals(a) && a.asInstanceOf[ResultAddContactMessage].Accepted == Accepted - override def hashCode = super.hashCode + Accepted.hashCode + override def hashCode = + Objects.hash(super.hashCode: java.lang.Integer, Accepted: java.lang.Boolean) override def toString = "ResultAddContactMessage(" + sender.toString + ", " + receiver.toString + ", " + date.toString + ", " + Accepted + ")" diff --git a/app/src/main/scala/com/nutomic/ensichat/messages/TextMessage.scala b/app/src/main/scala/com/nutomic/ensichat/messages/TextMessage.scala index 2f5d69e..da35070 100644 --- a/app/src/main/scala/com/nutomic/ensichat/messages/TextMessage.scala +++ b/app/src/main/scala/com/nutomic/ensichat/messages/TextMessage.scala @@ -1,6 +1,6 @@ package com.nutomic.ensichat.messages -import java.util.Date +import java.util.{Date, Objects} import com.nutomic.ensichat.bluetooth.Device import com.nutomic.ensichat.messages.Message._ @@ -24,7 +24,7 @@ class TextMessage(override val sender: Device.ID, override val receiver: Device. override def equals(a: Any) = super.equals(a) && a.asInstanceOf[TextMessage].text == text - override def hashCode = super.hashCode + text.hashCode + override def hashCode = Objects.hash(super.hashCode: java.lang.Integer, text) override def toString = "TextMessage(" + sender.toString + ", " + receiver.toString + ", " + date.toString + ", " + text + ")"