/* * Copyright (c) 2017. Developed by Hedgecode. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.hedgecode.snooker.cache; import org.hedgecode.snooker.api.APIException; import org.hedgecode.snooker.api.Event; import org.hedgecode.snooker.api.Events; import org.hedgecode.snooker.api.Match; import org.hedgecode.snooker.api.Matches; import org.hedgecode.snooker.api.OngoingMatches; import org.hedgecode.snooker.api.Player; import org.hedgecode.snooker.api.PlayerCategory; import org.hedgecode.snooker.api.Players; import org.hedgecode.snooker.api.RankingType; import org.hedgecode.snooker.api.Rankings; import org.hedgecode.snooker.api.Season; import org.hedgecode.snooker.api.SnookerScoreAPI; import org.hedgecode.snooker.cache.assign.EventAssigner; import org.hedgecode.snooker.cache.assign.EventsAssigner; import org.hedgecode.snooker.cache.assign.MatchAssigner; import org.hedgecode.snooker.cache.assign.MatchesAssigner; import org.hedgecode.snooker.cache.assign.RankingsAssigner; import org.hedgecode.snooker.json.JsonSnookerScore; /** * Implementation of Interface {@link SnookerScoreAPI} * with caching {@link Players} and {@link Events}. * * @author Dmitry Samoshin aka gotty */ public class CacheSnookerScore extends JsonSnookerScore implements SnookerScoreAPI { private Season currentSeason; private Players playersCache; private Events eventsCache; private Player unknownPlayer; private final EventsAssigner eventsAssigner = EventsAssigner.getInstance(); private final MatchesAssigner matchesAssigner = MatchesAssigner.getInstance(); private final RankingsAssigner rankingsAssigner = RankingsAssigner.getInstance(); private static CacheSnookerScore _instance; public static CacheSnookerScore getInstance() { if (_instance == null) _instance= new CacheSnookerScore(); return _instance; } private CacheSnookerScore() { currentSeason = Season.CURRENT_SEASON; } @Override public void setCurrentSeason(Season season) throws APIException { if (season == null) throw new APIException( APIException.Type.INFO, "Current season can not be null" ); currentSeason = season; initPlayersCache(); initEventsCache(); } @Override public Season currentSeason() throws APIException { return currentSeason; } private void initPlayersCache() throws APIException { playersCache = super.getPlayers( currentSeason, PlayerCategory.PRO ); unknownPlayer = playersCache.byId(UNKNOWN_PLAYER_ID); if (unknownPlayer == null) unknownPlayer = super.getPlayer(UNKNOWN_PLAYER_ID); } private void initEventsCache() throws APIException { eventsCache = super.getSeasonEvents( currentSeason ); eventsAssigner.assign(eventsCache); } @Override public Event getEvent(int eventId) throws APIException { Event event = getCachedEvent(eventId); if (event == null) { event = super.getEvent(eventId); EventAssigner.getInstance().assign(event); } return event; } public Event getCachedEvent(int eventId) throws APIException { if (eventsCache == null) initEventsCache(); return eventsCache.byId(eventId); } @Override public Match getMatch(int eventId, int roundId, int matchNumber) throws APIException { Match match = super.getMatch(eventId, roundId, matchNumber); MatchAssigner.getInstance().assign(match); return match; } @Override public Player getPlayer(int playerId) throws APIException { Player player = getCachedPlayer(playerId); return player != null ? player : super.getPlayer(playerId); } public Player getCachedPlayer(int playerId) throws APIException { if (playersCache == null) initPlayersCache(); if (playerId == UNKNOWN_PLAYER_ID) return unknownPlayer; return playersCache.byId(playerId); } @Override public Events getSeasonEvents(Season season) throws APIException { if (eventsCache == null) initEventsCache(); Events events; if (currentSeason.equals(season) || currentSeason.equals(Season.ALL)) { events = eventsCache.bySeason(season); } else { events = super.getSeasonEvents(season); eventsAssigner.assign(events); } return events; } @Override public Matches getEventMatches(int eventId) throws APIException { Matches matches = super.getEventMatches(eventId); matchesAssigner.assign(matches); return matches; } @Override public OngoingMatches getOngoingMatches() throws APIException { OngoingMatches matches = super.getOngoingMatches(); matchesAssigner.assign(matches); return matches; } @Override public Matches getPlayerMatches(int playerId, Season season) throws APIException { Matches matches = super.getPlayerMatches(playerId, season); matchesAssigner.assign(matches); return matches; } @Override public Rankings getRankings(Season season, RankingType type) throws APIException { Rankings rankings = super.getRankings(season, type); rankingsAssigner.assign(rankings); return rankings; } }