public static synchronized Database getInstance(Context context) { if (sDatabaseInstance == null) { sDatabaseInstance = new Database(context); sDatabaseInstance.setWriteAheadLoggingEnabled(true); } return sDatabaseInstance; }
@Override public void parseData() throws BasicModel.FieldNotDefinedException { mDataToBind.clear(); if (mAPIData == null || !mAPIData.isJsonArray()) { return; } JsonArray rankingsData = mAPIData.getAsJsonArray(); if (rankingsData.size() == 0) return; JsonArray headerRow = rankingsData.get(0).getAsJsonArray(); for (int i = 1; i < rankingsData.size(); i++) { JsonArray row = rankingsData.get(i).getAsJsonArray(); /* Assume that the list of lists has rank first and team # second, always */ String teamKey = "frc" + row.get(1).getAsString(); String rankingString; // use a CaseInsensitiveMap in order to find wins, losses, and ties below CaseInsensitiveMap<String> rankingElements = new CaseInsensitiveMap<>(); for (int j = 2; j < row.size(); j++) { rankingElements.put(headerRow.get(j).getAsString(), row.get(j).getAsString()); } String record = EventHelper.extractRankingString(rankingElements); if (record == null) { Set<String> keys = rankingElements.keySet(); if (keys.contains("wins") && keys.contains("losses") && keys.contains("ties")) { record = String.format( "(%1$s-%2$s-%3$s", rankingElements.get("wins"), rankingElements.get("losses"), rankingElements.get("ties")); rankingElements.remove("wins"); rankingElements.remove("losses"); rankingElements.remove("ties"); } } if (record == null) { record = ""; } rankingString = EventHelper.createRankingBreakdown(rankingElements); Team team = mDb.getTeamsTable().get(teamKey); String nickname; if (team != null) { nickname = team.getNickname(); } else { nickname = "Team " + teamKey.substring(3); } mDataToBind.add( new RankingListElement( teamKey, row.get(1).getAsString(), // team number nickname, row.get(0).getAsInt(), // rank record, rankingString)); } mEventBus.post(new EventRankingsEvent(generateTopRanksString(rankingsData))); }