Skip to content

Commit b59782f

Browse files
committed
Added the classes PaidMedia, PaidMediaInfo, PaidMediaPreview, PaidMediaPhoto and PaidMediaVideo
1 parent 691b0ee commit b59782f

File tree

6 files changed

+216
-0
lines changed

6 files changed

+216
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.pengrad.telegrambot.model.paidmedia;
2+
3+
import java.io.Serializable;
4+
import java.util.Objects;
5+
6+
public abstract class PaidMedia implements Serializable {
7+
8+
private final static long serialVersionUID = 0L;
9+
10+
private String type;
11+
12+
public PaidMedia(String type) {
13+
this.type = type;
14+
}
15+
16+
public String type() {
17+
return type;
18+
}
19+
20+
@Override
21+
public boolean equals(Object o) {
22+
if (this == o) return true;
23+
if (o == null || getClass() != o.getClass()) return false;
24+
25+
PaidMedia that = (PaidMedia) o;
26+
return Objects.equals(type, that.type);
27+
}
28+
29+
@Override
30+
public int hashCode() {
31+
return Objects.hash(type);
32+
}
33+
34+
@Override
35+
public String toString() {
36+
return "PaidMedia{" +
37+
"type='" + type + "'" +
38+
'}';
39+
}
40+
41+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.pengrad.telegrambot.model.paidmedia;
2+
3+
import com.pengrad.telegrambot.model.PhotoSize;
4+
5+
import java.util.Arrays;
6+
import java.util.Objects;
7+
8+
public class PaidMediaPhoto extends PaidMedia {
9+
10+
public final static String TYPE = "photo";
11+
12+
private PhotoSize[] photo;
13+
14+
public PaidMediaPhoto() {
15+
super(TYPE);
16+
}
17+
18+
public PhotoSize[] getPhoto() {
19+
return photo;
20+
}
21+
22+
@Override
23+
public boolean equals(Object o) {
24+
if (this == o) return true;
25+
if (o == null || getClass() != o.getClass()) return false;
26+
PaidMediaPhoto that = (PaidMediaPhoto) o;
27+
return Objects.equals(type(), that.type()) &&
28+
Arrays.equals(photo, that.photo);
29+
}
30+
31+
@Override
32+
public int hashCode() {
33+
return Objects.hash(type(), photo);
34+
}
35+
36+
@Override
37+
public String toString() {
38+
return "PaidMediaPhoto{" +
39+
"type='" + type() + "\'," +
40+
", photo=" + Arrays.toString(photo) + "\'" +
41+
'}';
42+
}
43+
44+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.pengrad.telegrambot.model.paidmedia;
2+
3+
import java.util.Objects;
4+
5+
public class PaidMediaPreview extends PaidMedia {
6+
7+
public final static String TYPE = "preview";
8+
9+
private Integer width;
10+
11+
private Integer height;
12+
13+
private Integer duration;
14+
15+
public PaidMediaPreview() {
16+
super(TYPE);
17+
}
18+
19+
public Integer getWidth() {
20+
return width;
21+
}
22+
23+
public Integer getHeight() {
24+
return height;
25+
}
26+
27+
public Integer getDuration() {
28+
return duration;
29+
}
30+
31+
@Override
32+
public boolean equals(Object o) {
33+
if (this == o) return true;
34+
if (o == null || getClass() != o.getClass()) return false;
35+
PaidMediaPreview that = (PaidMediaPreview) o;
36+
return Objects.equals(type(), that.type()) &&
37+
Objects.equals(width, that.width) &&
38+
Objects.equals(height, that.height) &&
39+
Objects.equals(duration, that.duration);
40+
}
41+
42+
@Override
43+
public int hashCode() {
44+
return Objects.hash(type(), width, height, duration);
45+
}
46+
47+
@Override
48+
public String toString() {
49+
return "PaidMediaPreview{" +
50+
"type='" + type() + "\'," +
51+
", width=" + width + "\'," +
52+
", height=" + height + "\'," +
53+
", duration=" + duration + "\'" +
54+
'}';
55+
}
56+
57+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.pengrad.telegrambot.model.paidmedia;
2+
3+
import com.pengrad.telegrambot.model.Video;
4+
5+
import java.util.Arrays;
6+
import java.util.Objects;
7+
8+
public class PaidMediaVideo extends PaidMedia {
9+
10+
public final static String TYPE = "video";
11+
12+
private Video video;
13+
14+
public PaidMediaVideo() {
15+
super(TYPE);
16+
}
17+
18+
public Video getVideo() {
19+
return video;
20+
}
21+
22+
@Override
23+
public boolean equals(Object o) {
24+
if (this == o) return true;
25+
if (o == null || getClass() != o.getClass()) return false;
26+
PaidMediaVideo that = (PaidMediaVideo) o;
27+
return Objects.equals(type(), that.type()) &&
28+
Objects.equals(video, that.video);
29+
}
30+
31+
@Override
32+
public int hashCode() {
33+
return Objects.hash(type(), video);
34+
}
35+
36+
@Override
37+
public String toString() {
38+
return "PaidMediaVideo{" +
39+
"type='" + type() + "\'," +
40+
", video=" + video + "\'" +
41+
'}';
42+
}
43+
44+
}

library/src/main/java/com/pengrad/telegrambot/utility/BotUtils.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import com.pengrad.telegrambot.model.chatboost.source.ChatBoostSource;
1010
import com.pengrad.telegrambot.model.message.MaybeInaccessibleMessage;
1111
import com.pengrad.telegrambot.model.message.origin.MessageOrigin;
12+
import com.pengrad.telegrambot.model.paidmedia.PaidMedia;
1213
import com.pengrad.telegrambot.model.reaction.ReactionType;
1314
import com.pengrad.telegrambot.model.stars.partner.TransactionPartner;
1415
import com.pengrad.telegrambot.model.stars.withdrawal.RevenueWithdrawalState;
@@ -36,6 +37,7 @@ private BotUtils() {}
3637
.registerTypeAdapter(BackgroundFill.class, new BackgroundFillAdapter())
3738
.registerTypeAdapter(RevenueWithdrawalState.class, new RevenueWithdrawalStateTypeAdapter())
3839
.registerTypeAdapter(TransactionPartner.class, new TransactionPartnerTypeAdapter())
40+
.registerTypeAdapter(PaidMedia.class, new PaidMediaTypeAdapter())
3941
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
4042
.create();
4143

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.pengrad.telegrambot.utility.gson;
2+
3+
import com.google.gson.*;
4+
import com.pengrad.telegrambot.model.paidmedia.PaidMedia;
5+
import com.pengrad.telegrambot.model.paidmedia.PaidMediaPhoto;
6+
import com.pengrad.telegrambot.model.paidmedia.PaidMediaPreview;
7+
import com.pengrad.telegrambot.model.paidmedia.PaidMediaVideo;
8+
9+
import java.lang.reflect.Type;
10+
11+
public class PaidMediaTypeAdapter implements JsonDeserializer<PaidMedia> {
12+
13+
@Override
14+
public PaidMedia deserialize(JsonElement element, Type type, JsonDeserializationContext context) throws JsonParseException {
15+
JsonObject object = element.getAsJsonObject();
16+
String discriminator = object.getAsJsonPrimitive("type").getAsString();
17+
18+
if (PaidMediaPreview.TYPE.equals(discriminator)) {
19+
return context.deserialize(object, PaidMediaPreview.class);
20+
} else if (PaidMediaPhoto.TYPE.equals(discriminator)) {
21+
return context.deserialize(object, PaidMediaPhoto.class);
22+
} else if (PaidMediaVideo.TYPE.equals(discriminator)) {
23+
return context.deserialize(object, PaidMediaVideo.class);
24+
}
25+
26+
return context.deserialize(object, PaidMedia.class);
27+
}
28+
}

0 commit comments

Comments
 (0)