Merge pull request #340 from myplacedk/SendMessageEqualsHashcode

#325 Implement equals() and hashCode() in SendMessage
This commit is contained in:
Ruben Bermudez 2018-02-13 21:56:49 +01:00 committed by GitHub
commit 014f17818b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 190 additions and 0 deletions

View File

@ -187,6 +187,35 @@ public class SendMessage extends BotApiMethod<Message> {
}
}
@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{" +

View File

@ -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{" +

View File

@ -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{" +

View File

@ -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{" +

View File

@ -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{" +

View File

@ -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{" +

View File

@ -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{" +

View File

@ -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());
}
}