From 4121a6f8c9522738fdb6e82d24d52b48874e38e8 Mon Sep 17 00:00:00 2001 From: Niels Ulrik Andersen Date: Sun, 19 Nov 2017 10:44:05 +0100 Subject: [PATCH] #325 Implement equals() and hashCode() in SendMessage --- .../api/methods/send/SendMessage.java | 29 +++++++++++++++++++ .../api/methods/send/SendMessageTest.java | 25 ++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 telegrambots-meta/src/test/java/org/telegram/telegrambots/api/methods/send/SendMessageTest.java diff --git a/telegrambots-meta/src/main/java/org/telegram/telegrambots/api/methods/send/SendMessage.java b/telegrambots-meta/src/main/java/org/telegram/telegrambots/api/methods/send/SendMessage.java index d750304c..53238969 100644 --- a/telegrambots-meta/src/main/java/org/telegram/telegrambots/api/methods/send/SendMessage.java +++ b/telegrambots-meta/src/main/java/org/telegram/telegrambots/api/methods/send/SendMessage.java @@ -187,6 +187,35 @@ public class SendMessage extends BotApiMethod { } } + @Override + public boolean equals(Object o) { + if (o == this) return true; + if (!(o instanceof SendMessage)) { + return false; + } + SendMessage sendMessage = (SendMessage) o; + return Objects.equals(chatId, sendMessage.chatId) + && Objects.equals(disableNotification, sendMessage.disableNotification) + && Objects.equals(disableWebPagePreview, sendMessage.disableWebPagePreview) + && Objects.equals(parseMode, sendMessage.parseMode) + && Objects.equals(replyMarkup, sendMessage.replyMarkup) + && Objects.equals(replyToMessageId, sendMessage.replyToMessageId) + && Objects.equals(text, sendMessage.text) + ; + } + + @Override + public int hashCode() { + return Objects.hash( + chatId, + disableNotification, + disableWebPagePreview, + parseMode, + replyMarkup, + replyToMessageId, + text); + } + @Override public String toString() { return "SendMessage{" + diff --git a/telegrambots-meta/src/test/java/org/telegram/telegrambots/api/methods/send/SendMessageTest.java b/telegrambots-meta/src/test/java/org/telegram/telegrambots/api/methods/send/SendMessageTest.java new file mode 100644 index 00000000..d5854ece --- /dev/null +++ b/telegrambots-meta/src/test/java/org/telegram/telegrambots/api/methods/send/SendMessageTest.java @@ -0,0 +1,25 @@ +package org.telegram.telegrambots.api.methods.send; + +import org.junit.Test; + +import static org.junit.Assert.*; + +public class SendMessageTest { + + @Test + public void comparison() throws Exception { + SendMessage sm1 = new SendMessage().setChatId(1L).setText("Hello World"); + SendMessage sm2 = new SendMessage().setChatId(1L).setText("Hello World"); + SendMessage noMessage = new SendMessage().setChatId(1L); + SendMessage disabledNotification = new SendMessage().setChatId(1L).setText("Hello World").disableNotification(); + + assertTrue(sm1.equals(sm2)); + assertFalse(sm1.equals(noMessage)); + assertFalse(sm1.equals(disabledNotification)); + + assertTrue(sm1.hashCode() == sm2.hashCode()); + assertFalse(sm1.hashCode() == noMessage.hashCode()); + assertFalse(sm1.hashCode() == disabledNotification.hashCode()); + } + +} \ No newline at end of file