/******************************************************************************* * Copyright (c) 2011 Michel DAVID mimah35-at-gmail.com * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. ******************************************************************************/ package fr.gotorennes.remote; import android.content.Context; import org.json.JSONException; import org.json.JSONObject; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import fr.gotorennes.domain.MetroStation; public class MetroStationTopoService extends RemoteService<MetroStation> { private static MetroStationTopoService instance; private MetroStationTopoService(Context context) { super(); } public static synchronized MetroStationTopoService getInstance(Context context) { if (instance == null) { instance = new MetroStationTopoService(context); } return instance; } private Map<String, MetroStation> cache = null; public MetroStation getMetroStation(String id) { if (cache == null || cache.isEmpty()) { List<MetroStation> stations = loadList(); cache = new HashMap<String, MetroStation>(); if (stations != null && !stations.isEmpty()) { for (MetroStation station : stations) { cache.put(station.id, station); } } } return cache.get(id); } @Override protected MetroStation populate(JSONObject jsonObject) throws JSONException { MetroStation metroStation = new MetroStation(); metroStation.id = jsonObject.getString("id"); metroStation.name = jsonObject.getString("nom"); metroStation.address = jsonObject.getString("adressevoie") + " " + jsonObject.getString("nomcommune"); metroStation.latitude = jsonObject.getJSONArray("coordonnees").getDouble(0); metroStation.longitude = jsonObject.getJSONArray("coordonnees").getDouble(1); return metroStation; } @Override protected String getDataSet() { return "tco-metro-topologie-stations-td"; } }