[LIB-2] Add snooker-score-api source files
[snooker-score-api.git] / src / main / java / org / hedgecode / snooker / json / JsonRanking.java
1 /*
2  * Copyright (c) 2017. Developed by Hedgecode.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.hedgecode.snooker.json;
18
19 import com.google.gson.annotations.Expose;
20 import com.google.gson.annotations.SerializedName;
21
22 import org.hedgecode.snooker.api.Player;
23 import org.hedgecode.snooker.api.Ranking;
24 import org.hedgecode.snooker.api.RankingType;
25 import org.hedgecode.snooker.api.Season;
26
27 /**
28  * Ranking Entity to JSON deserialize.
29  *
30  * @author Dmitry Samoshin aka gotty
31  */
32 public class JsonRanking extends JsonIdEntity implements Ranking {
33
34     @SerializedName("ID")
35     private int rankingId;
36     @SerializedName("Position")
37     private int position;
38     @SerializedName("PlayerID")
39     private int playerId;
40     @Expose
41     private Player player;
42     @SerializedName("Season")
43     private int seasonYear;
44     @Expose
45     private Season season;
46     @SerializedName("Sum")
47     private long sum;
48     @SerializedName("Type")
49     private String type;
50     @Expose
51     private RankingType rankingType;
52
53     @Override
54     public int rankingId() {
55         return rankingId;
56     }
57
58     @Override
59     public int position() {
60         return position;
61     }
62
63     @Override
64     public int playerId() {
65         return playerId;
66     }
67
68     @Override
69     public Player player() {
70         return player;
71     }
72
73     public void setPlayer(Player player) {
74         if (player != null && playerId == player.playerId())
75             this.player = player;
76     }
77
78     @Override
79     public Season season() {
80         if (season == null)
81             season = Season.getSeason(seasonYear);
82         return season;
83     }
84
85     @Override
86     public long sum() {
87         return sum;
88     }
89
90     @Override
91     public RankingType rankingType() {
92         if (rankingType == null)
93             rankingType = RankingType.byName(type);
94         return rankingType;
95     }
96
97     @Override
98     public int getId() {
99         return rankingId;
100     }
101
102 }