[LIB-5] Collection empty objects,reporting and serializable
[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     protected JsonRanking() {
54     }
55
56     @Override
57     public int rankingId() {
58         return rankingId;
59     }
60
61     @Override
62     public int position() {
63         return position;
64     }
65
66     @Override
67     public int playerId() {
68         return playerId;
69     }
70
71     @Override
72     public Player player() {
73         return player;
74     }
75
76     public void setPlayer(Player player) {
77         if (player != null && playerId == player.playerId())
78             this.player = player;
79     }
80
81     @Override
82     public Season season() {
83         if (season == null)
84             season = Season.getSeason(seasonYear);
85         return season;
86     }
87
88     @Override
89     public long sum() {
90         return sum;
91     }
92
93     @Override
94     public RankingType rankingType() {
95         if (rankingType == null)
96             rankingType = RankingType.byName(type);
97         return rankingType;
98     }
99
100     @Override
101     public int getId() {
102         return rankingId;
103     }
104
105 }