From 4121a6f8c9522738fdb6e82d24d52b48874e38e8 Mon Sep 17 00:00:00 2001 From: Niels Ulrik Andersen Date: Sun, 19 Nov 2017 10:44:05 +0100 Subject: [PATCH 1/2] #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 From 19b05823224d2b10a59911ffa7c2178c4d190dc2 Mon Sep 17 00:00:00 2001 From: Niels Ulrik Andersen Date: Mon, 20 Nov 2017 13:29:17 +0100 Subject: [PATCH 2/2] #325 Implement equals() and hashCode() in reply keyboards --- .../replykeyboard/ForceReplyKeyboard.java | 21 +++++++++++++ .../replykeyboard/InlineKeyboardMarkup.java | 16 ++++++++++ .../replykeyboard/ReplyKeyboardMarkup.java | 24 ++++++++++++++ .../replykeyboard/ReplyKeyboardRemove.java | 21 +++++++++++++ .../buttons/InlineKeyboardButton.java | 31 +++++++++++++++++++ .../replykeyboard/buttons/KeyboardButton.java | 23 ++++++++++++++ 6 files changed, 136 insertions(+) diff --git a/telegrambots-meta/src/main/java/org/telegram/telegrambots/api/objects/replykeyboard/ForceReplyKeyboard.java b/telegrambots-meta/src/main/java/org/telegram/telegrambots/api/objects/replykeyboard/ForceReplyKeyboard.java index d34b5543..a68c9542 100644 --- a/telegrambots-meta/src/main/java/org/telegram/telegrambots/api/objects/replykeyboard/ForceReplyKeyboard.java +++ b/telegrambots-meta/src/main/java/org/telegram/telegrambots/api/objects/replykeyboard/ForceReplyKeyboard.java @@ -4,6 +4,8 @@ import com.fasterxml.jackson.annotation.JsonProperty; import org.telegram.telegrambots.exceptions.TelegramApiValidationException; +import java.util.Objects; + /** * @author Ruben Bermudez * @version 1.0 @@ -56,6 +58,25 @@ public class ForceReplyKeyboard implements ReplyKeyboard { } } + @Override + public boolean equals(Object o) { + if (o == this) return true; + if (!(o instanceof ForceReplyKeyboard)) { + return false; + } + ForceReplyKeyboard forceReplyKeyboard = (ForceReplyKeyboard) o; + return Objects.equals(forceReply, forceReplyKeyboard.forceReply) + && Objects.equals(selective, forceReplyKeyboard.selective) + ; + } + + @Override + public int hashCode() { + return Objects.hash( + forceReply, + selective); + } + @Override public String toString() { return "ForceReplyKeyboard{" + diff --git a/telegrambots-meta/src/main/java/org/telegram/telegrambots/api/objects/replykeyboard/InlineKeyboardMarkup.java b/telegrambots-meta/src/main/java/org/telegram/telegrambots/api/objects/replykeyboard/InlineKeyboardMarkup.java index b3ae10bc..c5bf9d62 100644 --- a/telegrambots-meta/src/main/java/org/telegram/telegrambots/api/objects/replykeyboard/InlineKeyboardMarkup.java +++ b/telegrambots-meta/src/main/java/org/telegram/telegrambots/api/objects/replykeyboard/InlineKeyboardMarkup.java @@ -7,6 +7,7 @@ import org.telegram.telegrambots.exceptions.TelegramApiValidationException; import java.util.ArrayList; import java.util.List; +import java.util.Objects; /** * @author Ruben Bermudez @@ -50,6 +51,21 @@ public class InlineKeyboardMarkup implements ReplyKeyboard { } } + @Override + public boolean equals(Object o) { + if (o == this) return true; + if (!(o instanceof InlineKeyboardMarkup)) { + return false; + } + InlineKeyboardMarkup inlineKeyboardMarkup = (InlineKeyboardMarkup) o; + return Objects.equals(keyboard, inlineKeyboardMarkup.keyboard); + } + + @Override + public int hashCode() { + return Objects.hash(keyboard); + } + @Override public String toString() { return "InlineKeyboardMarkup{" + diff --git a/telegrambots-meta/src/main/java/org/telegram/telegrambots/api/objects/replykeyboard/ReplyKeyboardMarkup.java b/telegrambots-meta/src/main/java/org/telegram/telegrambots/api/objects/replykeyboard/ReplyKeyboardMarkup.java index 6486eb88..c410907c 100644 --- a/telegrambots-meta/src/main/java/org/telegram/telegrambots/api/objects/replykeyboard/ReplyKeyboardMarkup.java +++ b/telegrambots-meta/src/main/java/org/telegram/telegrambots/api/objects/replykeyboard/ReplyKeyboardMarkup.java @@ -7,6 +7,7 @@ import org.telegram.telegrambots.exceptions.TelegramApiValidationException; import java.util.ArrayList; import java.util.List; +import java.util.Objects; /** * @author Ruben Bermudez @@ -87,6 +88,29 @@ public class ReplyKeyboardMarkup implements ReplyKeyboard { } } + @Override + public boolean equals(Object o) { + if (o == this) return true; + if (!(o instanceof ReplyKeyboardMarkup)) { + return false; + } + ReplyKeyboardMarkup replyKeyboardMarkup = (ReplyKeyboardMarkup) o; + return Objects.equals(keyboard, replyKeyboardMarkup.keyboard) + && Objects.equals(oneTimeKeyboard, replyKeyboardMarkup.oneTimeKeyboard) + && Objects.equals(resizeKeyboard, replyKeyboardMarkup.resizeKeyboard) + && Objects.equals(selective, replyKeyboardMarkup.selective) + ; + } + + @Override + public int hashCode() { + return Objects.hash( + keyboard, + oneTimeKeyboard, + resizeKeyboard, + selective); + } + @Override public String toString() { return "ReplyKeyboardMarkup{" + diff --git a/telegrambots-meta/src/main/java/org/telegram/telegrambots/api/objects/replykeyboard/ReplyKeyboardRemove.java b/telegrambots-meta/src/main/java/org/telegram/telegrambots/api/objects/replykeyboard/ReplyKeyboardRemove.java index 250106fa..279101ea 100644 --- a/telegrambots-meta/src/main/java/org/telegram/telegrambots/api/objects/replykeyboard/ReplyKeyboardRemove.java +++ b/telegrambots-meta/src/main/java/org/telegram/telegrambots/api/objects/replykeyboard/ReplyKeyboardRemove.java @@ -4,6 +4,8 @@ import com.fasterxml.jackson.annotation.JsonProperty; import org.telegram.telegrambots.exceptions.TelegramApiValidationException; +import java.util.Objects; + /** * @author Ruben Bermudez * @version 1.0 @@ -52,6 +54,25 @@ public class ReplyKeyboardRemove implements ReplyKeyboard { } } + @Override + public boolean equals(Object o) { + if (o == this) return true; + if (!(o instanceof ReplyKeyboardRemove)) { + return false; + } + ReplyKeyboardRemove replyKeyboardRemove = (ReplyKeyboardRemove) o; + return Objects.equals(removeKeyboard, replyKeyboardRemove.removeKeyboard) + && Objects.equals(selective, replyKeyboardRemove.selective) + ; + } + + @Override + public int hashCode() { + return Objects.hash( + removeKeyboard, + selective); + } + @Override public String toString() { return "ReplyKeyboardRemove{" + diff --git a/telegrambots-meta/src/main/java/org/telegram/telegrambots/api/objects/replykeyboard/buttons/InlineKeyboardButton.java b/telegrambots-meta/src/main/java/org/telegram/telegrambots/api/objects/replykeyboard/buttons/InlineKeyboardButton.java index 419d0175..d40cdfff 100644 --- a/telegrambots-meta/src/main/java/org/telegram/telegrambots/api/objects/replykeyboard/buttons/InlineKeyboardButton.java +++ b/telegrambots-meta/src/main/java/org/telegram/telegrambots/api/objects/replykeyboard/buttons/InlineKeyboardButton.java @@ -6,6 +6,8 @@ import org.telegram.telegrambots.api.interfaces.Validable; import org.telegram.telegrambots.api.objects.games.CallbackGame; import org.telegram.telegrambots.exceptions.TelegramApiValidationException; +import java.util.Objects; + import static com.google.common.base.Preconditions.checkNotNull; /** @@ -146,6 +148,35 @@ public class InlineKeyboardButton implements InputBotApiObject, Validable { } } + @Override + public boolean equals(Object o) { + if (o == this) return true; + if (!(o instanceof InlineKeyboardButton)) { + return false; + } + InlineKeyboardButton inlineKeyboardButton = (InlineKeyboardButton) o; + return Objects.equals(callbackData, inlineKeyboardButton.callbackData) + && Objects.equals(callbackGame, inlineKeyboardButton.callbackGame) + && Objects.equals(pay, inlineKeyboardButton.pay) + && Objects.equals(switchInlineQuery, inlineKeyboardButton.switchInlineQuery) + && Objects.equals(switchInlineQueryCurrentChat, inlineKeyboardButton.switchInlineQueryCurrentChat) + && Objects.equals(text, inlineKeyboardButton.text) + && Objects.equals(url, inlineKeyboardButton.url) + ; + } + + @Override + public int hashCode() { + return Objects.hash( + callbackData, + callbackGame, + pay, + switchInlineQuery, + switchInlineQueryCurrentChat, + text, + url); + } + @Override public String toString() { return "InlineKeyboardButton{" + diff --git a/telegrambots-meta/src/main/java/org/telegram/telegrambots/api/objects/replykeyboard/buttons/KeyboardButton.java b/telegrambots-meta/src/main/java/org/telegram/telegrambots/api/objects/replykeyboard/buttons/KeyboardButton.java index db313c59..42575dcd 100644 --- a/telegrambots-meta/src/main/java/org/telegram/telegrambots/api/objects/replykeyboard/buttons/KeyboardButton.java +++ b/telegrambots-meta/src/main/java/org/telegram/telegrambots/api/objects/replykeyboard/buttons/KeyboardButton.java @@ -6,6 +6,8 @@ import org.telegram.telegrambots.api.interfaces.InputBotApiObject; import org.telegram.telegrambots.api.interfaces.Validable; import org.telegram.telegrambots.exceptions.TelegramApiValidationException; +import java.util.Objects; + /** * @author Ruben Bermudez * @version 1.0 @@ -88,6 +90,27 @@ public class KeyboardButton implements InputBotApiObject, Validable { } } + @Override + public boolean equals(Object o) { + if (o == this) return true; + if (!(o instanceof KeyboardButton)) { + return false; + } + KeyboardButton keyboardButton = (KeyboardButton) o; + return Objects.equals(requestContact, keyboardButton.requestContact) + && Objects.equals(requestLocation, keyboardButton.requestLocation) + && Objects.equals(text, keyboardButton.text) + ; + } + + @Override + public int hashCode() { + return Objects.hash( + requestContact, + requestLocation, + text); + } + @Override public String toString() { return "KeyboardButton{" +