#325 Implement equals() and hashCode() in SendMessage

This commit is contained in:
Niels Ulrik Andersen 2017-11-19 10:44:05 +01:00
parent 49c0fef3c5
commit 4121a6f8c9
2 changed files with 54 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

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