[LIB-8] Add Event Rounds and Seedings functional
[snooker-score-api.git] / src / main / java / org / hedgecode / snooker / cache / CacheSnookerScore.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.cache;
18
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;
42
43 /**
44  * Implementation of Interface {@link SnookerScoreAPI}
45  * with caching {@link Players} and {@link Events}.
46  *
47  * @author Dmitry Samoshin aka gotty
48  */
49 public class CacheSnookerScore extends JsonSnookerScore implements SnookerScoreAPI {
50
51     private Season currentSeason;
52
53     private Players playersCache;
54     private Events eventsCache;
55
56     private Player unknownPlayer;
57
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();
63
64     private static CacheSnookerScore _instance;
65
66     public static CacheSnookerScore getInstance() {
67         if (_instance == null)
68             _instance= new CacheSnookerScore();
69         return _instance;
70     }
71
72     private CacheSnookerScore() {
73         currentSeason = Season.CURRENT_SEASON;
74     }
75
76     @Override
77     public void setCurrentSeason(Season season) throws APIException {
78         if (season == null)
79             throw new APIException(
80                     APIException.Type.INFO, "Current season can not be null"
81             );
82         currentSeason = season;
83         initPlayersCache();
84         initEventsCache();
85     }
86
87     @Override
88     public Season currentSeason() throws APIException {
89         return currentSeason;
90     }
91
92     private void initPlayersCache() throws APIException {
93         playersCache = super.getPlayers(
94                 currentSeason,
95                 PlayerCategory.PRO
96         );
97         unknownPlayer = playersCache.byId(UNKNOWN_PLAYER_ID);
98         if (unknownPlayer == null)
99             unknownPlayer = super.getPlayer(UNKNOWN_PLAYER_ID);
100     }
101
102     private void initEventsCache() throws APIException {
103         eventsCache = super.getSeasonEvents(
104                 currentSeason
105         );
106         eventsAssigner.assign(eventsCache);
107     }
108
109     @Override
110     public Event getEvent(int eventId) throws APIException {
111         Event event = getCachedEvent(eventId);
112         if (event == null) {
113             event = super.getEvent(eventId);
114             EventAssigner.getInstance().assign(event);
115         }
116         return event;
117     }
118
119     public Event getCachedEvent(int eventId) throws APIException {
120         if (eventsCache == null)
121             initEventsCache();
122         return eventsCache.byId(eventId);
123     }
124
125     @Override
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);
129         return match;
130     }
131
132     @Override
133     public Player getPlayer(int playerId) throws APIException {
134         Player player = getCachedPlayer(playerId);
135         return player != null
136                 ? player
137                 : super.getPlayer(playerId);
138     }
139
140     public Player getCachedPlayer(int playerId) throws APIException {
141         if (playersCache == null)
142             initPlayersCache();
143         if (playerId == UNKNOWN_PLAYER_ID)
144             return unknownPlayer;
145         return playersCache.byId(playerId);
146     }
147
148     @Override
149     public Events getSeasonEvents(Season season) throws APIException {
150         if (eventsCache == null)
151             initEventsCache();
152         Events events;
153         if (currentSeason.equals(season) || currentSeason.equals(Season.ALL)) {
154             events = eventsCache.bySeason(season);
155         } else {
156             events = super.getSeasonEvents(season);
157             eventsAssigner.assign(events);
158         }
159         return events;
160     }
161
162     @Override
163     public Matches getEventMatches(int eventId) throws APIException {
164         Matches matches = super.getEventMatches(eventId);
165         matchesAssigner.assign(matches);
166         return matches;
167     }
168
169     @Override
170     public OngoingMatches getOngoingMatches() throws APIException {
171         OngoingMatches matches = super.getOngoingMatches();
172         matchesAssigner.assign(matches);
173         return matches;
174     }
175
176     @Override
177     public Matches getPlayerMatches(int playerId, Season season) throws APIException {
178         Matches matches = super.getPlayerMatches(playerId, season);
179         matchesAssigner.assign(matches);
180         return matches;
181     }
182
183     @Override
184     public Rankings getRankings(Season season, RankingType type) throws APIException {
185         Rankings rankings = super.getRankings(season, type);
186         rankingsAssigner.assign(rankings);
187         return rankings;
188     }
189
190     @Override
191     public Rounds getEventRounds(int eventId) throws APIException {
192         Rounds rounds = super.getEventRounds(eventId);
193         roundsAssigner.assign(rounds);
194         return rounds;
195     }
196
197     @Override
198     public Seedings getEventSeedings(int eventId) throws APIException {
199         Seedings seedings = super.getEventSeedings(eventId);
200         seedingsAssigner.assign(seedings);
201         return seedings;
202     }
203
204 }