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.Season;
31 import org.hedgecode.snooker.api.SnookerScoreAPI;
32 import org.hedgecode.snooker.cache.assign.EventAssigner;
33 import org.hedgecode.snooker.cache.assign.EventsAssigner;
34 import org.hedgecode.snooker.cache.assign.MatchAssigner;
35 import org.hedgecode.snooker.cache.assign.MatchesAssigner;
36 import org.hedgecode.snooker.cache.assign.RankingsAssigner;
37 import org.hedgecode.snooker.json.JsonSnookerScore;
40 * Implementation of Interface {@link SnookerScoreAPI}
41 * with caching {@link Players} and {@link Events}.
43 * @author Dmitry Samoshin aka gotty
45 public class CacheSnookerScore extends JsonSnookerScore implements SnookerScoreAPI {
47 private Season currentSeason;
49 private Players playersCache;
50 private Events eventsCache;
52 private Player unknownPlayer;
54 private final EventsAssigner eventsAssigner = EventsAssigner.getInstance();
55 private final MatchesAssigner matchesAssigner = MatchesAssigner.getInstance();
56 private final RankingsAssigner rankingsAssigner = RankingsAssigner.getInstance();
58 private static CacheSnookerScore _instance;
60 public static CacheSnookerScore getInstance() {
61 if (_instance == null)
62 _instance= new CacheSnookerScore();
66 private CacheSnookerScore() {
67 currentSeason = Season.CURRENT_SEASON;
71 public void setCurrentSeason(Season season) throws APIException {
73 throw new APIException(
74 APIException.Type.INFO, "Current season can not be null"
76 currentSeason = season;
82 public Season currentSeason() throws APIException {
86 private void initPlayersCache() throws APIException {
87 playersCache = super.getPlayers(
91 unknownPlayer = playersCache.byId(UNKNOWN_PLAYER_ID);
92 if (unknownPlayer == null)
93 unknownPlayer = super.getPlayer(UNKNOWN_PLAYER_ID);
96 private void initEventsCache() throws APIException {
97 eventsCache = super.getSeasonEvents(
100 eventsAssigner.assign(eventsCache);
104 public Event getEvent(int eventId) throws APIException {
105 Event event = getCachedEvent(eventId);
107 event = super.getEvent(eventId);
108 EventAssigner.getInstance().assign(event);
113 public Event getCachedEvent(int eventId) throws APIException {
114 if (eventsCache == null)
116 return eventsCache.byId(eventId);
120 public Match getMatch(int eventId, int roundId, int matchNumber) throws APIException {
121 Match match = super.getMatch(eventId, roundId, matchNumber);
122 MatchAssigner.getInstance().assign(match);
127 public Player getPlayer(int playerId) throws APIException {
128 Player player = getCachedPlayer(playerId);
129 return player != null
131 : super.getPlayer(playerId);
134 public Player getCachedPlayer(int playerId) throws APIException {
135 if (playersCache == null)
137 if (playerId == UNKNOWN_PLAYER_ID)
138 return unknownPlayer;
139 return playersCache.byId(playerId);
143 public Events getSeasonEvents(Season season) throws APIException {
144 if (eventsCache == null)
147 if (currentSeason.equals(season) || currentSeason.equals(Season.ALL)) {
148 events = eventsCache.bySeason(season);
150 events = super.getSeasonEvents(season);
151 eventsAssigner.assign(events);
157 public Matches getEventMatches(int eventId) throws APIException {
158 Matches matches = super.getEventMatches(eventId);
159 matchesAssigner.assign(matches);
164 public OngoingMatches getOngoingMatches() throws APIException {
165 OngoingMatches matches = super.getOngoingMatches();
166 matchesAssigner.assign(matches);
171 public Matches getPlayerMatches(int playerId, Season season) throws APIException {
172 Matches matches = super.getPlayerMatches(playerId, season);
173 matchesAssigner.assign(matches);
178 public Rankings getRankings(Season season, RankingType type) throws APIException {
179 Rankings rankings = super.getRankings(season, type);
180 rankingsAssigner.assign(rankings);