[LIB-8] URL and HTML tag processing, new entities fields
[snooker-score-api.git] / src / main / java / org / hedgecode / snooker / json / JsonPlayer.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 java.net.URL;
20 import java.util.Date;
21
22 import com.google.gson.annotations.Expose;
23 import com.google.gson.annotations.SerializedName;
24
25 import org.hedgecode.snooker.SnookerURLUtils;
26 import org.hedgecode.snooker.annotation.IsURL;
27 import org.hedgecode.snooker.api.APIException;
28 import org.hedgecode.snooker.api.Player;
29 import org.hedgecode.snooker.api.PlayerImage;
30
31 /**
32  * Player Entity to JSON deserialize.
33  *
34  * @author Dmitry Samoshin aka gotty
35  */
36 public class JsonPlayer extends JsonIdEntity implements Player {
37
38     @SerializedName("ID")
39     private int playerId;
40     @SerializedName("Type")
41     private int type;
42     @SerializedName("FirstName")
43     private String firstName;
44     @SerializedName("MiddleName")
45     private String middleName;
46     @SerializedName("LastName")
47     private String lastName;
48     @SerializedName("TeamName")
49     private String teamName;
50     @SerializedName("TeamNumber")
51     private int teamNumber;
52     @SerializedName("TeamSeason")
53     private int teamSeason;
54     @SerializedName("ShortName")
55     private String shortName;
56     @Expose
57     private String fullName;
58     @SerializedName("Nationality")
59     private String nationality;
60     @SerializedName("Sex")
61     private String sex;
62     @IsURL
63     @SerializedName("BioPage")
64     private String bioPage;
65     @SerializedName("Born")
66     private Date born;
67     @SerializedName("Twitter")
68     private String twitter;
69     @SerializedName("SurnameFirst")
70     private boolean surnameFirst;
71     @SerializedName("License")
72     private String license;
73     @SerializedName("Club")
74     private String club;
75     @IsURL
76     @SerializedName("URL")
77     private String url;
78     @IsURL
79     @SerializedName("Photo")
80     private String photo;
81     @Expose
82     private PlayerImage image;
83     @IsURL
84     @SerializedName("PhotoSource")
85     private String photoSource;
86     @SerializedName("FirstSeasonAsPro")
87     private int firstSeasonAsPro;
88     @SerializedName("LastSeasonAsPro")
89     private int lastSeasonAsPro;
90     @SerializedName("Info")
91     private String info;
92
93     protected JsonPlayer() {
94     }
95
96     @Override
97     public int playerId() {
98         return playerId;
99     }
100
101     @Override
102     public int type() {
103         return type;
104     }
105
106     @Override
107     public String firstName() {
108         return firstName;
109     }
110
111     @Override
112     public String middleName() {
113         return middleName;
114     }
115
116     @Override
117     public String lastName() {
118         return lastName;
119     }
120
121     @Override
122     public String teamName() {
123         return teamName;
124     }
125
126     @Override
127     public int teamNumber() {
128         return teamNumber;
129     }
130
131     @Override
132     public int teamSeason() {
133         return teamSeason;
134     }
135
136     @Override
137     public String shortName() {
138         return shortName;
139     }
140
141     @Override
142     public String fullName() {
143         if (fullName == null) {
144             fullName = String.format(
145                     "%s %s %s",
146                     surnameFirst ? lastName : firstName,
147                     middleName,
148                     surnameFirst ? firstName : lastName
149             ).replaceAll("\\s+", " ").trim();
150         }
151         return fullName;
152     }
153
154     @Override
155     public String nationality() {
156         return nationality;
157     }
158
159     @Override
160     public String sex() {
161         return sex;
162     }
163
164     @Override
165     public String bioPage() {
166         return bioPage;
167     }
168
169     @Override
170     public Date born() {
171         return born == null
172                 ? null
173                 : new Date(born.getTime());
174     }
175
176     @Override
177     public String twitter() {
178         return twitter;
179     }
180
181     @IsURL
182     @Override
183     public String twitterUrl() {
184         return SnookerURLUtils.twitterUrl(twitter);
185     }
186
187     @Override
188     public boolean surnameFirst() {
189         return surnameFirst;
190     }
191
192     @Override
193     public String license() {
194         return license;
195     }
196
197     @Override
198     public String club() {
199         return club;
200     }
201
202     @Override
203     public String url() {
204         return url;
205     }
206
207     @Override
208     public String photo() {
209         return photo;
210     }
211
212     @Override
213     public PlayerImage image() throws APIException {
214         if (image == null) {
215             URL imageUrl = SnookerURLUtils.assignUrl(photo);
216             if (imageUrl != null) {
217                 image = new PlayerImage(imageUrl, false);
218             }
219         }
220         return image;
221     }
222
223     @Override
224     public String photoSource() {
225         return photoSource;
226     }
227
228     @Override
229     public int firstSeasonAsPro() {
230         return firstSeasonAsPro;
231     }
232
233     @Override
234     public int lastSeasonAsPro() {
235         return lastSeasonAsPro;
236     }
237
238     @Override
239     public String info() {
240         return info;
241     }
242
243     @Override
244     public int getId() {
245         return playerId;
246     }
247
248 }