package de.jeyp91.teamidmatcher; import com.google.common.collect.HashBiMap; import de.jeyp91.ResourceProvider; import de.jeyp91.StatusHolder; import de.jeyp91.apifootball.APIFootballMatch; import org.json.simple.JSONArray; import org.json.simple.JSONObject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class TeamIDMatcher { private static HashBiMap ids = null; private static final Logger logger = LoggerFactory.getLogger(TeamIDMatcher.class); private static JSONArray teams; private static boolean init = false; public static final int HOME = 0; public static final int GUEST = 1; private static void initBiMap() { if(!init) { ids = HashBiMap.create(); teams = ResourceProvider.getTeamIDMatcherConfig(); for (Object team : teams) { int tippligaID = ((Long) ((JSONObject) team).get("tippligaID")).intValue(); int apiFootballID = ((Long) ((JSONObject) team).get("apiFootballID")).intValue(); ids.put(tippligaID, apiFootballID); } init = true; } } public static Integer getApiFootballIdFromTippligaId(int id) { initBiMap(); if(ids.containsKey(id)) { return ids.get(id); } else { logger.error("Tippliga ID: " + id + " not in ID Matcher."); StatusHolder.setError(); return null; } } public static Integer getTippligaIdFromApiFootballId(APIFootballMatch match, int homeguest) { Integer apiFootballId = homeguest == HOME ? match.getTeamIdHome() : match.getTeamIdGuest(); Integer tlwId = null; if(apiFootballId == null) return null; initBiMap(); if(ids.inverse().containsKey(apiFootballId)) { tlwId = ids.inverse().get(apiFootballId); } else { String teamname = homeguest == HOME ? match.getTeamNameHome() : match.getTeamNameGuest(); logger.error("Could not find id " + apiFootballId + " for " + teamname); StatusHolder.setError(); } return tlwId; } public static String getTeamNameFromTippligaId(Integer id) { initBiMap(); String name = ""; for(Object team : teams) { int tippligaID = ((Long)((JSONObject) team).get("tippligaID")).intValue(); if(id == tippligaID) { name = (((JSONObject) team).get("teamname")).toString(); break; } } return name; } }