Skip to content

Commit

Permalink
Add TransactionPartnerTelegramApi transaction partner, rewrite Transa…
Browse files Browse the repository at this point in the history
…ctionPartnerTypeAdapter to kotlin
  • Loading branch information
anfanik committed Oct 31, 2024
1 parent 1256312 commit c49e109
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 33 deletions.
Original file line number Diff line number Diff line change
@@ -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 +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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<TransactionPartner> {

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)
}

}

0 comments on commit c49e109

Please sign in to comment.