2 * Copyright (c) 2017. Developed by Hedgecode.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.hedgecode.snooker.json;
19 import com.google.gson.Gson;
21 import org.hedgecode.snooker.api.APIException;
22 import org.hedgecode.snooker.api.Event;
23 import org.hedgecode.snooker.api.Events;
24 import org.hedgecode.snooker.api.Match;
25 import org.hedgecode.snooker.api.Matches;
26 import org.hedgecode.snooker.api.OngoingMatches;
27 import org.hedgecode.snooker.api.Player;
28 import org.hedgecode.snooker.api.PlayerCategory;
29 import org.hedgecode.snooker.api.Players;
30 import org.hedgecode.snooker.api.RankingType;
31 import org.hedgecode.snooker.api.Rankings;
32 import org.hedgecode.snooker.api.Season;
33 import org.hedgecode.snooker.api.SnookerScoreAPI;
34 import org.hedgecode.snooker.gson.SnookerGsonBuilder;
35 import org.hedgecode.snooker.request.RequestException;
36 import org.hedgecode.snooker.request.RequestParams;
37 import org.hedgecode.snooker.request.RequestType;
40 * Implementation of Interface {@link SnookerScoreAPI}
41 * with deserialize from input JSON strings.
43 * @author Dmitry Samoshin aka gotty
45 public class JsonSnookerScore implements SnookerScoreAPI {
47 public static final int UNKNOWN_PLAYER_ID = 376;
49 private final Gson GSON = SnookerGsonBuilder.build();
51 private static JsonSnookerScore _instance;
53 public static JsonSnookerScore getInstance() {
54 if (_instance == null)
55 _instance = new JsonSnookerScore();
59 protected JsonSnookerScore() {
63 public Event getEvent(int eventId) throws APIException {
66 events = GSON.fromJson(
67 JsonStringToken.token(
68 RequestType.request(RequestType.EVENT, eventId)
72 } catch (RequestException e) {
73 throw new APIException(
74 APIException.Type.REQUEST, e.getMessage()
77 return events.length > 0 ? events[0] : null;
81 public Match getMatch(int eventId, int roundId, int matchNumber) throws APIException {
84 matches = GSON.fromJson(
85 JsonStringToken.token(
89 eventId, roundId, matchNumber
95 } catch (RequestException e) {
96 throw new APIException(
97 APIException.Type.REQUEST, e.getMessage()
100 return matches.length > 0 ? matches[0] : null;
104 public Player getPlayer(int playerId) throws APIException {
107 players = GSON.fromJson(
108 JsonStringToken.token(
109 RequestType.request(RequestType.PLAYER, playerId)
113 } catch (RequestException e) {
114 throw new APIException(
115 APIException.Type.REQUEST, e.getMessage()
118 return players.length > 0 ? players[0] : null;
122 public Events getSeasonEvents(Season season) throws APIException {
125 events = new JsonEvents(
127 JsonStringToken.token(
129 RequestType.SEASON_EVENTS,
131 RequestType.SEASON_EVENTS, 0, season
138 } catch (RequestException e) {
139 throw new APIException(
140 APIException.Type.REQUEST, e.getMessage()
147 public Matches getEventMatches(int eventId) throws APIException {
150 matches = new JsonMatches(
152 JsonStringToken.token(
154 RequestType.EVENT_MATCHES,
161 } catch (RequestException e) {
162 throw new APIException(
163 APIException.Type.REQUEST, e.getMessage()
170 public OngoingMatches getOngoingMatches() throws APIException {
171 OngoingMatches ongoingMatches;
173 ongoingMatches = new JsonOngoingMatches(
175 JsonStringToken.token(
177 RequestType.ONGOING_MATCHES,
181 JsonOngoingMatch[].class
184 } catch (RequestException e) {
185 throw new APIException(
186 APIException.Type.REQUEST, e.getMessage()
189 return ongoingMatches;
193 public Matches getPlayerMatches(int playerId, Season season) throws APIException {
196 matches = new JsonMatches(
198 JsonStringToken.token(
200 RequestType.PLAYER_MATCHES,
202 RequestType.PLAYER_MATCHES, playerId, season
209 } catch (RequestException e) {
210 throw new APIException(
211 APIException.Type.REQUEST, e.getMessage()
218 public Players getEventPlayers(int eventId) throws APIException {
221 players = new JsonPlayers(
223 JsonStringToken.token(
225 RequestType.EVENT_PLAYERS,
232 } catch (RequestException e) {
233 throw new APIException(
234 APIException.Type.REQUEST, e.getMessage()
241 public Players getPlayers(Season season, PlayerCategory category) throws APIException {
244 players = new JsonPlayers(
246 JsonStringToken.token(
248 RequestType.SEASON_PLAYERS,
257 } catch (RequestException e) {
258 throw new APIException(
259 APIException.Type.REQUEST, e.getMessage()
266 public Rankings getRankings(Season season, RankingType type) throws APIException {
269 rankings = new JsonRankings(
271 JsonStringToken.token(
282 } catch (RequestException e) {
283 throw new APIException(
284 APIException.Type.REQUEST, e.getMessage()
291 public void setCurrentSeason(Season season) throws APIException {
292 throw new APIException(
293 APIException.Type.INFO, "This feature is only available in Cached API"
298 public Season currentSeason() throws APIException {
299 throw new APIException(
300 APIException.Type.INFO, "This feature is only available in Cached API"