diff --git a/library/src/main/java/com/pengrad/telegrambot/model/stars/partner/TransactionPartnerTelegramApi.java b/library/src/main/java/com/pengrad/telegrambot/model/stars/partner/TransactionPartnerTelegramApi.java new file mode 100644 index 00000000..fbc6bba4 --- /dev/null +++ b/library/src/main/java/com/pengrad/telegrambot/model/stars/partner/TransactionPartnerTelegramApi.java @@ -0,0 +1,39 @@ +package com.pengrad.telegrambot.model.stars.partner; + +import java.util.Objects; + +public class TransactionPartnerTelegramApi extends TransactionPartner { + + public final static String TYPE = "telegram_api"; + + private Integer request_count; + + public TransactionPartnerTelegramApi() { + super(TYPE); + } + + public Integer requestCount() { + return request_count; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof TransactionPartnerTelegramApi)) return false; + if (!super.equals(o)) return false; + TransactionPartnerTelegramApi that = (TransactionPartnerTelegramApi) o; + return Objects.equals(request_count, that.request_count); + } + + @Override + public int hashCode() { + return Objects.hash(super.hashCode(), request_count); + } + + @Override + public String toString() { + return "TransactionPartnerTelegramApi{" + + "request_count=" + request_count + + '}'; + } +} diff --git a/library/src/main/java/com/pengrad/telegrambot/utility/BotUtils.java b/library/src/main/java/com/pengrad/telegrambot/utility/BotUtils.java index 4f34db74..4df59f51 100644 --- a/library/src/main/java/com/pengrad/telegrambot/utility/BotUtils.java +++ b/library/src/main/java/com/pengrad/telegrambot/utility/BotUtils.java @@ -36,7 +36,7 @@ private BotUtils() {} .registerTypeAdapter(BackgroundType.class, new BackgroundTypeAdapter()) .registerTypeAdapter(BackgroundFill.class, new BackgroundFillAdapter()) .registerTypeAdapter(RevenueWithdrawalState.class, new RevenueWithdrawalStateTypeAdapter()) - .registerTypeAdapter(TransactionPartner.class, new TransactionPartnerTypeAdapter()) + .registerTypeAdapter(TransactionPartner.class, TransactionPartnerTypeAdapter.INSTANCE) .registerTypeAdapter(PaidMedia.class, new PaidMediaTypeAdapter()) .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES) .create(); diff --git a/library/src/main/java/com/pengrad/telegrambot/utility/gson/TransactionPartnerTypeAdapter.java b/library/src/main/java/com/pengrad/telegrambot/utility/gson/TransactionPartnerTypeAdapter.java deleted file mode 100644 index 036aff36..00000000 --- a/library/src/main/java/com/pengrad/telegrambot/utility/gson/TransactionPartnerTypeAdapter.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.pengrad.telegrambot.utility.gson; - -import com.google.gson.*; -import com.pengrad.telegrambot.model.stars.partner.*; -import com.pengrad.telegrambot.model.stars.withdrawal.RevenueWithdrawalState; -import com.pengrad.telegrambot.model.stars.withdrawal.RevenueWithdrawalStateFailed; -import com.pengrad.telegrambot.model.stars.withdrawal.RevenueWithdrawalStatePending; -import com.pengrad.telegrambot.model.stars.withdrawal.RevenueWithdrawalStateSucceeded; - -import java.lang.reflect.Type; - -public class TransactionPartnerTypeAdapter implements JsonDeserializer { - - @Override - public TransactionPartner deserialize(JsonElement element, Type type, JsonDeserializationContext context) throws JsonParseException { - JsonObject object = element.getAsJsonObject(); - JsonPrimitive primitive = object.getAsJsonPrimitive("type"); - String discriminator = primitive != null ? primitive.getAsString() : "unknown"; - - if (TransactionPartnerUser.TYPE.equals(discriminator)) { - return context.deserialize(object, TransactionPartnerUser.class); - } else if (TransactionPartnerFragment.TYPE.equals(discriminator)) { - return context.deserialize(object, TransactionPartnerFragment.class); - } else if (TransactionPartnerTelegramAds.TYPE.equals(discriminator)) { - return context.deserialize(object, TransactionPartnerTelegramAds.class); - } else if (TransactionPartnerOther.TYPE.equals(discriminator)) { - return context.deserialize(object, TransactionPartnerOther.class); - } - - return new TransactionPartner(discriminator); - } -} diff --git a/library/src/main/java/com/pengrad/telegrambot/utility/gson/TransactionPartnerTypeAdapter.kt b/library/src/main/java/com/pengrad/telegrambot/utility/gson/TransactionPartnerTypeAdapter.kt new file mode 100644 index 00000000..5fef300a --- /dev/null +++ b/library/src/main/java/com/pengrad/telegrambot/utility/gson/TransactionPartnerTypeAdapter.kt @@ -0,0 +1,31 @@ +package com.pengrad.telegrambot.utility.gson + +import com.google.gson.* +import com.pengrad.telegrambot.model.stars.partner.* +import java.lang.reflect.Type + +object TransactionPartnerTypeAdapter : JsonDeserializer { + + private val typeMapping = mapOf( + TransactionPartnerUser.TYPE to TransactionPartnerUser::class, + TransactionPartnerFragment.TYPE to TransactionPartnerFragment::class, + TransactionPartnerTelegramAds.TYPE to TransactionPartnerTelegramAds::class, + TransactionPartnerTelegramApi.TYPE to TransactionPartnerTelegramApi::class, + TransactionPartnerOther.TYPE to TransactionPartnerOther::class + ) + + @Throws(JsonParseException::class) + override fun deserialize( + element: JsonElement, + type: Type, + context: JsonDeserializationContext + ): TransactionPartner { + val obj = element.asJsonObject + val discriminator = obj.getAsJsonPrimitive("type")?.asString ?: "unknown" + + return typeMapping[discriminator]?.let { + context.deserialize(obj, it.java) + } ?: TransactionPartner(discriminator) + } + +} \ No newline at end of file