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.cache;
19 import org.hedgecode.snooker.api.APIException;
20 import org.hedgecode.snooker.api.Event;
21 import org.hedgecode.snooker.api.Events;
22 import org.hedgecode.snooker.api.Match;
23 import org.hedgecode.snooker.api.Matches;
24 import org.hedgecode.snooker.api.OngoingMatches;
25 import org.hedgecode.snooker.api.Player;
26 import org.hedgecode.snooker.api.PlayerCategory;
27 import org.hedgecode.snooker.api.Players;
28 import org.hedgecode.snooker.api.RankingType;
29 import org.hedgecode.snooker.api.Rankings;
30 import org.hedgecode.snooker.api.Rounds;
31 import org.hedgecode.snooker.api.Season;
32 import org.hedgecode.snooker.api.Seedings;
33 import org.hedgecode.snooker.api.SnookerScoreAPI;
34 import org.hedgecode.snooker.cache.assign.EventAssigner;
35 import org.hedgecode.snooker.cache.assign.EventsAssigner;
36 import org.hedgecode.snooker.cache.assign.MatchAssigner;
37 import org.hedgecode.snooker.cache.assign.MatchesAssigner;
38 import org.hedgecode.snooker.cache.assign.RankingsAssigner;
39 import org.hedgecode.snooker.cache.assign.RoundsAssigner;
40 import org.hedgecode.snooker.cache.assign.SeedingsAssigner;
41 import org.hedgecode.snooker.json.JsonSnookerScore;
44 * Implementation of Interface {@link SnookerScoreAPI}
45 * with caching {@link Players} and {@link Events}.
47 * @author Dmitry Samoshin aka gotty
49 public class CacheSnookerScore extends JsonSnookerScore implements SnookerScoreAPI {
51 private Season currentSeason;
53 private Players playersCache;
54 private Events eventsCache;
56 private Player unknownPlayer;
58 private final EventsAssigner eventsAssigner = EventsAssigner.getInstance();
59 private final MatchesAssigner matchesAssigner = MatchesAssigner.getInstance();
60 private final RankingsAssigner rankingsAssigner = RankingsAssigner.getInstance();
61 private final RoundsAssigner roundsAssigner = RoundsAssigner.getInstance();
62 private final SeedingsAssigner seedingsAssigner = SeedingsAssigner.getInstance();
64 private static CacheSnookerScore _instance;
66 public static CacheSnookerScore getInstance() {
67 if (_instance == null)
68 _instance= new CacheSnookerScore();
72 private CacheSnookerScore() {
73 currentSeason = Season.CURRENT_SEASON;
77 public void setCurrentSeason(Season season) throws APIException {
79 throw new APIException(
80 APIException.Type.INFO, "Current season can not be null"
82 currentSeason = season;
88 public Season currentSeason() throws APIException {
92 private void initPlayersCache() throws APIException {
93 playersCache = super.getPlayers(
97 unknownPlayer = playersCache.byId(UNKNOWN_PLAYER_ID);
98 if (unknownPlayer == null)
99 unknownPlayer = super.getPlayer(UNKNOWN_PLAYER_ID);
102 private void initEventsCache() throws APIException {
103 eventsCache = super.getSeasonEvents(
106 eventsAssigner.assign(eventsCache);
110 public Event getEvent(int eventId) throws APIException {
111 Event event = getCachedEvent(eventId);
113 event = super.getEvent(eventId);
114 EventAssigner.getInstance().assign(event);
119 public Event getCachedEvent(int eventId) throws APIException {
120 if (eventsCache == null)
122 return eventsCache.byId(eventId);
126 public Match getMatch(int eventId, int roundId, int matchNumber) throws APIException {
127 Match match = super.getMatch(eventId, roundId, matchNumber);
128 MatchAssigner.getInstance().assign(match);
133 public Player getPlayer(int playerId) throws APIException {
134 Player player = getCachedPlayer(playerId);
135 return player != null
137 : super.getPlayer(playerId);
140 public Player getCachedPlayer(int playerId) throws APIException {
141 if (playersCache == null)
143 if (playerId == UNKNOWN_PLAYER_ID)
144 return unknownPlayer;
145 return playersCache.byId(playerId);
149 public Events getSeasonEvents(Season season) throws APIException {
150 if (eventsCache == null)
153 if (currentSeason.equals(season) || currentSeason.equals(Season.ALL)) {
154 events = eventsCache.bySeason(season);
156 events = super.getSeasonEvents(season);
157 eventsAssigner.assign(events);
163 public Matches getEventMatches(int eventId) throws APIException {
164 Matches matches = super.getEventMatches(eventId);
165 matchesAssigner.assign(matches);
170 public OngoingMatches getOngoingMatches() throws APIException {
171 OngoingMatches matches = super.getOngoingMatches();
172 matchesAssigner.assign(matches);
177 public Matches getPlayerMatches(int playerId, Season season) throws APIException {
178 Matches matches = super.getPlayerMatches(playerId, season);
179 matchesAssigner.assign(matches);
184 public Rankings getRankings(Season season, RankingType type) throws APIException {
185 Rankings rankings = super.getRankings(season, type);
186 rankingsAssigner.assign(rankings);
191 public Rounds getEventRounds(int eventId) throws APIException {
192 Rounds rounds = super.getEventRounds(eventId);
193 roundsAssigner.assign(rounds);
198 public Seedings getEventSeedings(int eventId) throws APIException {
199 Seedings seedings = super.getEventSeedings(eventId);
200 seedingsAssigner.assign(seedings);