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.
This commit is contained in:
Torsten Grote 2016-05-27 15:39:07 -03:00 committed by Andreas Schildbach
parent 62a5a54d73
commit 6d99e63db8

View file

@ -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 try
{ {
final JSONObject jsonLine = jsonRoute.getJSONObject("line");
final String lineId = jsonLine.getString("id"); final String lineId = jsonLine.getString("id");
String network = null; String network = null;
if (jsonLine.has("network")) if (jsonLine.has("network"))
network = Strings.emptyToNull(jsonLine.getJSONObject("network").optString("name")); 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 code = jsonLine.getString("code");
final String name = Strings.emptyToNull(jsonLine.optString("name")); final String name = Strings.emptyToNull(jsonLine.optString("name"));
final String color = Strings.emptyToNull(jsonLine.getString("color")); final String color = Strings.emptyToNull(jsonLine.getString("color"));
@ -692,8 +695,6 @@ public abstract class AbstractNavitiaProvider extends AbstractNetworkProvider
} }
} }
private Map<String, Product> lineProductCache = new WeakHashMap<String, Product>();
private Product parseLineProductFromMode(final String modeId) private Product parseLineProductFromMode(final String modeId)
{ {
final String modeType = modeId.replace("physical_mode:", ""); 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 private LineDestination getStationLine(final Line line, final JSONObject jsonDeparture) throws IOException
{ {
try try
@ -971,8 +919,7 @@ public abstract class AbstractNavitiaProvider extends AbstractNetworkProvider
// Build line. // Build line.
final JSONObject route = jsonDeparture.getJSONObject("route"); final JSONObject route = jsonDeparture.getJSONObject("route");
final JSONObject jsonLine = route.getJSONObject("line"); final Line line = parseLine(route);
final Line line = parseLine(jsonLine);
final JSONObject stopPoint = jsonDeparture.getJSONObject("stop_point"); final JSONObject stopPoint = jsonDeparture.getJSONObject("stop_point");
final Location location = parsePlace(stopPoint, PlaceType.STOP_POINT); final Location location = parsePlace(stopPoint, PlaceType.STOP_POINT);