From 6d99e63db88e9bb802830dcf6e6615343e95a812 Mon Sep 17 00:00:00 2001 From: Torsten Grote Date: Fri, 27 May 2016 15:39:07 -0300 Subject: [PATCH] Navitia: More efficient product parsing According to CanalTP/navitia#1386, Navitia has finally added the product to departure query results, so all information we need can now be retrieved by one network query only. The AbstractNavitiaProvider was adapted accordingly and all legacy code including the Product Cache was removed. --- .../pte/AbstractNavitiaProvider.java | 65 ++----------------- 1 file changed, 6 insertions(+), 59 deletions(-) diff --git a/enabler/src/de/schildbach/pte/AbstractNavitiaProvider.java b/enabler/src/de/schildbach/pte/AbstractNavitiaProvider.java index 614e7ed0..0916a9c1 100644 --- a/enabler/src/de/schildbach/pte/AbstractNavitiaProvider.java +++ b/enabler/src/de/schildbach/pte/AbstractNavitiaProvider.java @@ -669,15 +669,18 @@ public abstract class AbstractNavitiaProvider extends AbstractNetworkProvider } } - private Line parseLine(final JSONObject jsonLine) throws IOException + private Line parseLine(final JSONObject jsonRoute) throws IOException { try { + final JSONObject jsonLine = jsonRoute.getJSONObject("line"); final String lineId = jsonLine.getString("id"); String network = null; if (jsonLine.has("network")) network = Strings.emptyToNull(jsonLine.getJSONObject("network").optString("name")); - final Product product = parseLineProduct(jsonLine); + final JSONObject mode = jsonRoute.getJSONArray("physical_modes").getJSONObject(0); + final String modeId = mode.getString("id"); + final Product product = parseLineProductFromMode(modeId); final String code = jsonLine.getString("code"); final String name = Strings.emptyToNull(jsonLine.optString("name")); final String color = Strings.emptyToNull(jsonLine.getString("color")); @@ -692,8 +695,6 @@ public abstract class AbstractNavitiaProvider extends AbstractNetworkProvider } } - private Map lineProductCache = new WeakHashMap(); - private Product parseLineProductFromMode(final String modeId) { final String modeType = modeId.replace("physical_mode:", ""); @@ -726,59 +727,6 @@ public abstract class AbstractNavitiaProvider extends AbstractNetworkProvider } } - private Product parseLineProduct(final JSONObject line) throws IOException - { - try - { - final String lineId = line.getString("id"); - final JSONObject mode; - - if (line.has("physical_modes")) - { - mode = line.getJSONArray("physical_modes").getJSONObject(0); - } - else - { - final Product cachedProduct = lineProductCache.get(lineId); - if (cachedProduct != null) - return cachedProduct; - - // this makes a network request and is sometimes necessary - mode = getLinePhysicalMode(lineId); - } - - final String modeId = mode.getString("id"); - final Product product = parseLineProductFromMode(modeId); - - lineProductCache.put(lineId, product); - - return product; - } - catch (final JSONException jsonExc) - { - throw new ParserException(jsonExc); - } - } - - private JSONObject getLinePhysicalMode(final String lineId) throws IOException - { - final String uri = uri() + "lines/" + ParserUtils.urlEncode(lineId) + "/physical_modes"; - final CharSequence page = httpClient.get(uri); - - try - { - final JSONObject head = new JSONObject(page.toString()); - final JSONArray physicalModes = head.getJSONArray("physical_modes"); - final JSONObject physicalMode = physicalModes.getJSONObject(0); - - return physicalMode; - } - catch (final JSONException jsonExc) - { - throw new ParserException(jsonExc); - } - } - private LineDestination getStationLine(final Line line, final JSONObject jsonDeparture) throws IOException { try @@ -971,8 +919,7 @@ public abstract class AbstractNavitiaProvider extends AbstractNetworkProvider // Build line. final JSONObject route = jsonDeparture.getJSONObject("route"); - final JSONObject jsonLine = route.getJSONObject("line"); - final Line line = parseLine(jsonLine); + final Line line = parseLine(route); final JSONObject stopPoint = jsonDeparture.getJSONObject("stop_point"); final Location location = parsePlace(stopPoint, PlaceType.STOP_POINT);