From 977a08fa2a11412362b42b8a2914e4d83dda241e Mon Sep 17 00:00:00 2001 From: Stas Parshin <2172170+pengrad@users.noreply.github.com> Date: Sun, 7 Apr 2024 10:32:07 +0700 Subject: [PATCH] Fix equals --- .../com/pengrad/telegrambot/model/Birthdate.java | 14 ++++++-------- .../java/com/pengrad/telegrambot/model/User.java | 15 ++------------- .../pengrad/telegrambot/model/UsersShared.java | 14 ++++++-------- .../java/com/pengrad/telegrambot/ModelTest.java | 6 ++++++ 4 files changed, 20 insertions(+), 29 deletions(-) diff --git a/library/src/main/java/com/pengrad/telegrambot/model/Birthdate.java b/library/src/main/java/com/pengrad/telegrambot/model/Birthdate.java index 96d63670..fc43482a 100644 --- a/library/src/main/java/com/pengrad/telegrambot/model/Birthdate.java +++ b/library/src/main/java/com/pengrad/telegrambot/model/Birthdate.java @@ -26,11 +26,9 @@ public Integer year() { @Override public boolean equals(Object o) { if (this == o) return true; - if (!(o instanceof Birthdate)) return false; + if (o == null || getClass() != o.getClass()) return false; Birthdate birthdate = (Birthdate) o; - return Objects.equals(day, birthdate.day) - && Objects.equals(month, birthdate.month) - && Objects.equals(year, birthdate.year); + return Objects.equals(day, birthdate.day) && Objects.equals(month, birthdate.month) && Objects.equals(year, birthdate.year); } @Override @@ -41,9 +39,9 @@ public int hashCode() { @Override public String toString() { return "Birthday{" + - "day=" + day + - ", month=" + month + - ", year=" + year + - '}'; + "day=" + day + + ", month=" + month + + ", year=" + year + + '}'; } } diff --git a/library/src/main/java/com/pengrad/telegrambot/model/User.java b/library/src/main/java/com/pengrad/telegrambot/model/User.java index ae7bfc6c..9aa5cecf 100644 --- a/library/src/main/java/com/pengrad/telegrambot/model/User.java +++ b/library/src/main/java/com/pengrad/telegrambot/model/User.java @@ -81,20 +81,9 @@ public Boolean canConnectToBusiness() { @Override public boolean equals(Object o) { if (this == o) return true; - if (!(o instanceof User)) return false; + if (o == null || getClass() != o.getClass()) return false; User user = (User) o; - return Objects.equals(id, user.id) - && Objects.equals(is_bot, user.is_bot) - && Objects.equals(first_name, user.first_name) - && Objects.equals(last_name, user.last_name) - && Objects.equals(username, user.username) - && Objects.equals(language_code, user.language_code) - && Objects.equals(is_premium, user.is_premium) - && Objects.equals(added_to_attachment_menu, user.added_to_attachment_menu) - && Objects.equals(can_join_groups, user.can_join_groups) - && Objects.equals(can_read_all_group_messages, user.can_read_all_group_messages) - && Objects.equals(supports_inline_queries, user.supports_inline_queries) - && Objects.equals(can_connect_to_business, user.can_connect_to_business); + return Objects.equals(id, user.id) && Objects.equals(is_bot, user.is_bot) && Objects.equals(first_name, user.first_name) && Objects.equals(last_name, user.last_name) && Objects.equals(username, user.username) && Objects.equals(language_code, user.language_code) && Objects.equals(is_premium, user.is_premium) && Objects.equals(added_to_attachment_menu, user.added_to_attachment_menu) && Objects.equals(can_join_groups, user.can_join_groups) && Objects.equals(can_read_all_group_messages, user.can_read_all_group_messages) && Objects.equals(supports_inline_queries, user.supports_inline_queries) && Objects.equals(can_connect_to_business, user.can_connect_to_business); } @Override diff --git a/library/src/main/java/com/pengrad/telegrambot/model/UsersShared.java b/library/src/main/java/com/pengrad/telegrambot/model/UsersShared.java index 241d55d7..6030f3a4 100644 --- a/library/src/main/java/com/pengrad/telegrambot/model/UsersShared.java +++ b/library/src/main/java/com/pengrad/telegrambot/model/UsersShared.java @@ -32,11 +32,9 @@ public Integer[] userIds() { @Override public boolean equals(Object o) { if (this == o) return true; - if (!(o instanceof UsersShared)) return false; + if (o == null || getClass() != o.getClass()) return false; UsersShared that = (UsersShared) o; - return Objects.equals(request_id, that.request_id) - && Arrays.equals(user_ids, that.user_ids) - && Arrays.equals(users, that.users); + return Objects.equals(request_id, that.request_id) && Arrays.equals(user_ids, that.user_ids) && Arrays.equals(users, that.users); } @Override @@ -50,9 +48,9 @@ public int hashCode() { @Override public String toString() { return "UsersShared{" + - "request_id=" + request_id + - ", user_ids=" + Arrays.toString(user_ids) + - ", users=" + Arrays.toString(users) + - '}'; + "request_id=" + request_id + + ", user_ids=" + Arrays.toString(user_ids) + + ", users=" + Arrays.toString(users) + + '}'; } } diff --git a/library/src/test/java/com/pengrad/telegrambot/ModelTest.java b/library/src/test/java/com/pengrad/telegrambot/ModelTest.java index dfb9d71c..13735d50 100644 --- a/library/src/test/java/com/pengrad/telegrambot/ModelTest.java +++ b/library/src/test/java/com/pengrad/telegrambot/ModelTest.java @@ -72,12 +72,18 @@ public void testEquals() throws ReflectiveOperationException { f.setAccessible(true); f.set(update, 1); + Chat chat = new Chat(); + f = Chat.class.getDeclaredField("id"); + f.setAccessible(true); + f.set(chat, 1L); + for (Class c : classes) { EqualsVerifierApi verifierApi = EqualsVerifier.forClass(c) .usingGetClass() .withPrefabValues(Update.class, Update.class.getDeclaredConstructor().newInstance(), update) .withPrefabValues(Message.class, Message.class.getDeclaredConstructor().newInstance(), message) .withPrefabValues(CallbackQuery.class, CallbackQuery.class.getDeclaredConstructor().newInstance(), callbackQuery) + .withPrefabValues(Chat.class, new Chat(), chat) .suppress(Warning.STRICT_HASHCODE) .suppress(Warning.NONFINAL_FIELDS);