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