Replace anonymous nested classes with lambdas.

This commit is contained in:
Andreas Schildbach 2019-10-15 23:39:11 +02:00
parent dd96d33afa
commit 0c10251957
4 changed files with 1495 additions and 1564 deletions

View file

@ -381,68 +381,65 @@ public abstract class AbstractEfaProvider extends AbstractNetworkProvider {
appendStopfinderRequestParameters(url, constraint, "XML", types, maxLocations); appendStopfinderRequestParameters(url, constraint, "XML", types, maxLocations);
final AtomicReference<SuggestLocationsResult> result = new AtomicReference<>(); final AtomicReference<SuggestLocationsResult> result = new AtomicReference<>();
final HttpClient.Callback callback = new HttpClient.Callback() { final HttpClient.Callback callback = (bodyPeek, body) -> {
@Override try {
public void onSuccessful(final CharSequence bodyPeek, final ResponseBody body) throws IOException { final XmlPullParser pp = parserFactory.newPullParser();
try { pp.setInput(body.charStream());
final XmlPullParser pp = parserFactory.newPullParser(); final ResultHeader header = enterEfa(pp);
pp.setInput(body.charStream()); XmlPullUtil.optSkip(pp, "ers");
final ResultHeader header = enterEfa(pp);
XmlPullUtil.optSkip(pp, "ers");
final List<SuggestedLocation> locations = new ArrayList<>(); final List<SuggestedLocation> locations = new ArrayList<>();
XmlPullUtil.require(pp, "sf"); XmlPullUtil.require(pp, "sf");
if (XmlPullUtil.optEnter(pp, "sf")) { if (XmlPullUtil.optEnter(pp, "sf")) {
while (XmlPullUtil.optEnter(pp, "p")) { while (XmlPullUtil.optEnter(pp, "p")) {
final String name = normalizeLocationName(XmlPullUtil.valueTag(pp, "n")); final String name = normalizeLocationName(XmlPullUtil.valueTag(pp, "n"));
final String u = XmlPullUtil.valueTag(pp, "u"); final String u = XmlPullUtil.valueTag(pp, "u");
if (!"sf".equals(u)) if (!"sf".equals(u))
throw new RuntimeException("unknown usage: " + u); throw new RuntimeException("unknown usage: " + u);
final String ty = XmlPullUtil.valueTag(pp, "ty"); final String ty = XmlPullUtil.valueTag(pp, "ty");
final LocationType type; final LocationType type;
if ("stop".equals(ty)) if ("stop".equals(ty))
type = LocationType.STATION; type = LocationType.STATION;
else if ("poi".equals(ty)) else if ("poi".equals(ty))
type = LocationType.POI; type = LocationType.POI;
else if ("loc".equals(ty)) else if ("loc".equals(ty))
type = LocationType.COORD; type = LocationType.COORD;
else if ("street".equals(ty)) else if ("street".equals(ty))
type = LocationType.ADDRESS; type = LocationType.ADDRESS;
else if ("singlehouse".equals(ty)) else if ("singlehouse".equals(ty))
type = LocationType.ADDRESS; type = LocationType.ADDRESS;
else else
throw new RuntimeException("unknown type: " + ty); throw new RuntimeException("unknown type: " + ty);
XmlPullUtil.enter(pp, "r"); XmlPullUtil.enter(pp, "r");
final String id = XmlPullUtil.valueTag(pp, "id"); final String id = XmlPullUtil.valueTag(pp, "id");
XmlPullUtil.optValueTag(pp, "gid", null); XmlPullUtil.optValueTag(pp, "gid", null);
final String stateless = XmlPullUtil.valueTag(pp, "stateless"); final String stateless = XmlPullUtil.valueTag(pp, "stateless");
XmlPullUtil.valueTag(pp, "omc"); XmlPullUtil.valueTag(pp, "omc");
final String place = normalizeLocationName(XmlPullUtil.optValueTag(pp, "pc", null)); final String place = normalizeLocationName(XmlPullUtil.optValueTag(pp, "pc", null));
XmlPullUtil.valueTag(pp, "pid"); XmlPullUtil.valueTag(pp, "pid");
final Point coord = parseCoord(XmlPullUtil.optValueTag(pp, "c", null)); final Point coord = parseCoord(XmlPullUtil.optValueTag(pp, "c", null));
XmlPullUtil.skipExit(pp, "r"); XmlPullUtil.skipExit(pp, "r");
final String qal = XmlPullUtil.optValueTag(pp, "qal", null); final String qal = XmlPullUtil.optValueTag(pp, "qal", null);
final int quality = qal != null ? Integer.parseInt(qal) : 0; final int quality = qal != null ? Integer.parseInt(qal) : 0;
XmlPullUtil.skipExit(pp, "p"); XmlPullUtil.skipExit(pp, "p");
final Location location = new Location(type, type == LocationType.STATION ? id : stateless, final Location location = new Location(type, type == LocationType.STATION ? id : stateless,
coord, place, name); coord, place, name);
final SuggestedLocation locationAndQuality = new SuggestedLocation(location, quality); final SuggestedLocation locationAndQuality = new SuggestedLocation(location, quality);
locations.add(locationAndQuality); locations.add(locationAndQuality);
}
XmlPullUtil.skipExit(pp, "sf");
} }
result.set(new SuggestLocationsResult(header, locations)); XmlPullUtil.skipExit(pp, "sf");
} catch (final XmlPullParserException | ParserException x) {
throw new ParserException("cannot parse xml: " + bodyPeek, x);
} }
result.set(new SuggestLocationsResult(header, locations));
} catch (final XmlPullParserException | ParserException x) {
throw new ParserException("cannot parse xml: " + bodyPeek, x);
} }
}; };
@ -478,60 +475,57 @@ public abstract class AbstractEfaProvider extends AbstractNetworkProvider {
appendCoordRequestParameters(url, types, coord, maxDistance, maxStations); appendCoordRequestParameters(url, types, coord, maxDistance, maxStations);
final AtomicReference<NearbyLocationsResult> result = new AtomicReference<>(); final AtomicReference<NearbyLocationsResult> result = new AtomicReference<>();
final HttpClient.Callback callback = new HttpClient.Callback() { final HttpClient.Callback callback = (bodyPeek, body) -> {
@Override try {
public void onSuccessful(final CharSequence bodyPeek, final ResponseBody body) throws IOException { final XmlPullParser pp = parserFactory.newPullParser();
try { pp.setInput(body.charStream());
final XmlPullParser pp = parserFactory.newPullParser(); final ResultHeader header = enterItdRequest(pp);
pp.setInput(body.charStream());
final ResultHeader header = enterItdRequest(pp);
XmlPullUtil.enter(pp, "itdCoordInfoRequest"); XmlPullUtil.enter(pp, "itdCoordInfoRequest");
XmlPullUtil.enter(pp, "itdCoordInfo"); XmlPullUtil.enter(pp, "itdCoordInfo");
XmlPullUtil.enter(pp, "coordInfoRequest"); XmlPullUtil.enter(pp, "coordInfoRequest");
XmlPullUtil.skipExit(pp, "coordInfoRequest"); XmlPullUtil.skipExit(pp, "coordInfoRequest");
final List<Location> locations = new ArrayList<>(); final List<Location> locations = new ArrayList<>();
if (XmlPullUtil.optEnter(pp, "coordInfoItemList")) { if (XmlPullUtil.optEnter(pp, "coordInfoItemList")) {
while (XmlPullUtil.test(pp, "coordInfoItem")) { while (XmlPullUtil.test(pp, "coordInfoItem")) {
final String type = XmlPullUtil.attr(pp, "type"); final String type = XmlPullUtil.attr(pp, "type");
final LocationType locationType; final LocationType locationType;
if ("STOP".equals(type)) if ("STOP".equals(type))
locationType = LocationType.STATION; locationType = LocationType.STATION;
else if ("POI_POINT".equals(type)) else if ("POI_POINT".equals(type))
locationType = LocationType.POI; locationType = LocationType.POI;
else else
throw new IllegalStateException("unknown type: " + type); throw new IllegalStateException("unknown type: " + type);
String id = XmlPullUtil.optAttr(pp, "stateless", null); String id = XmlPullUtil.optAttr(pp, "stateless", null);
if (id == null) if (id == null)
id = XmlPullUtil.attr(pp, "id"); id = XmlPullUtil.attr(pp, "id");
final String name = normalizeLocationName(XmlPullUtil.optAttr(pp, "name", null)); final String name = normalizeLocationName(XmlPullUtil.optAttr(pp, "name", null));
final String place = normalizeLocationName(XmlPullUtil.optAttr(pp, "locality", null)); final String place = normalizeLocationName(XmlPullUtil.optAttr(pp, "locality", null));
XmlPullUtil.enter(pp, "coordInfoItem"); XmlPullUtil.enter(pp, "coordInfoItem");
// FIXME this is always only one coordinate // FIXME this is always only one coordinate
final List<Point> path = processItdPathCoordinates(pp); final List<Point> path = processItdPathCoordinates(pp);
final Point coord = path != null ? path.get(0) : null; final Point coord1 = path != null ? path.get(0) : null;
XmlPullUtil.skipExit(pp, "coordInfoItem"); XmlPullUtil.skipExit(pp, "coordInfoItem");
if (name != null) if (name != null)
locations.add(new Location(locationType, id, coord, place, name)); locations.add(new Location(locationType, id, coord1, place, name));
}
XmlPullUtil.skipExit(pp, "coordInfoItemList");
} }
result.set(new NearbyLocationsResult(header, locations)); XmlPullUtil.skipExit(pp, "coordInfoItemList");
} catch (final XmlPullParserException | ParserException x) {
throw new ParserException("cannot parse xml: " + bodyPeek, x);
} }
result.set(new NearbyLocationsResult(header, locations));
} catch (final XmlPullParserException | ParserException x) {
throw new ParserException("cannot parse xml: " + bodyPeek, x);
} }
}; };
@ -546,63 +540,60 @@ public abstract class AbstractEfaProvider extends AbstractNetworkProvider {
appendCoordRequestParameters(url, types, coord, maxDistance, maxStations); appendCoordRequestParameters(url, types, coord, maxDistance, maxStations);
final AtomicReference<NearbyLocationsResult> result = new AtomicReference<>(); final AtomicReference<NearbyLocationsResult> result = new AtomicReference<>();
final HttpClient.Callback callback = new HttpClient.Callback() { final HttpClient.Callback callback = (bodyPeek, body) -> {
@Override try {
public void onSuccessful(final CharSequence bodyPeek, final ResponseBody body) throws IOException { final XmlPullParser pp = parserFactory.newPullParser();
try { pp.setInput(body.charStream());
final XmlPullParser pp = parserFactory.newPullParser(); final ResultHeader header = enterEfa(pp);
pp.setInput(body.charStream());
final ResultHeader header = enterEfa(pp);
XmlPullUtil.enter(pp, "ci"); XmlPullUtil.enter(pp, "ci");
XmlPullUtil.enter(pp, "request"); XmlPullUtil.enter(pp, "request");
XmlPullUtil.skipExit(pp, "request"); XmlPullUtil.skipExit(pp, "request");
final List<Location> stations = new ArrayList<>(); final List<Location> stations = new ArrayList<>();
if (XmlPullUtil.optEnter(pp, "pis")) { if (XmlPullUtil.optEnter(pp, "pis")) {
while (XmlPullUtil.optEnter(pp, "pi")) { while (XmlPullUtil.optEnter(pp, "pi")) {
final String name = normalizeLocationName(XmlPullUtil.optValueTag(pp, "de", null)); final String name = normalizeLocationName(XmlPullUtil.optValueTag(pp, "de", null));
final String type = XmlPullUtil.valueTag(pp, "ty"); final String type = XmlPullUtil.valueTag(pp, "ty");
final LocationType locationType; final LocationType locationType;
if ("STOP".equals(type)) if ("STOP".equals(type))
locationType = LocationType.STATION; locationType = LocationType.STATION;
else if ("POI_POINT".equals(type)) else if ("POI_POINT".equals(type))
locationType = LocationType.POI; locationType = LocationType.POI;
else else
throw new IllegalStateException("unknown type: " + type); throw new IllegalStateException("unknown type: " + type);
final String id = XmlPullUtil.valueTag(pp, "id"); final String id = XmlPullUtil.valueTag(pp, "id");
XmlPullUtil.valueTag(pp, "omc"); XmlPullUtil.valueTag(pp, "omc");
XmlPullUtil.optValueTag(pp, "pid", null); XmlPullUtil.optValueTag(pp, "pid", null);
final String place = normalizeLocationName(XmlPullUtil.optValueTag(pp, "locality", null)); final String place = normalizeLocationName(XmlPullUtil.optValueTag(pp, "locality", null));
XmlPullUtil.valueTag(pp, "layer"); XmlPullUtil.valueTag(pp, "layer");
XmlPullUtil.valueTag(pp, "gisID"); XmlPullUtil.valueTag(pp, "gisID");
XmlPullUtil.valueTag(pp, "ds"); XmlPullUtil.valueTag(pp, "ds");
final String stateless = XmlPullUtil.valueTag(pp, "stateless"); final String stateless = XmlPullUtil.valueTag(pp, "stateless");
final String locationId = locationType == LocationType.STATION ? id : stateless; final String locationId = locationType == LocationType.STATION ? id : stateless;
final Point coord = parseCoord(XmlPullUtil.valueTag(pp, "c")); final Point coord1 = parseCoord(XmlPullUtil.valueTag(pp, "c"));
final Location location; final Location location;
if (name != null) if (name != null)
location = new Location(locationType, locationId, coord, place, name); location = new Location(locationType, locationId, coord1, place, name);
else else
location = new Location(locationType, locationId, coord, null, place); location = new Location(locationType, locationId, coord1, null, place);
stations.add(location); stations.add(location);
XmlPullUtil.skipExit(pp, "pi"); XmlPullUtil.skipExit(pp, "pi");
}
XmlPullUtil.skipExit(pp, "pis");
} }
XmlPullUtil.skipExit(pp, "ci"); XmlPullUtil.skipExit(pp, "pis");
result.set(new NearbyLocationsResult(header, stations));
} catch (final XmlPullParserException | ParserException x) {
throw new ParserException("cannot parse xml: " + bodyPeek, x);
} }
XmlPullUtil.skipExit(pp, "ci");
result.set(new NearbyLocationsResult(header, stations));
} catch (final XmlPullParserException | ParserException x) {
throw new ParserException("cannot parse xml: " + bodyPeek, x);
} }
}; };
@ -813,48 +804,40 @@ public abstract class AbstractEfaProvider extends AbstractNetworkProvider {
url.addEncodedQueryParameter("mode", "direct"); url.addEncodedQueryParameter("mode", "direct");
final AtomicReference<NearbyLocationsResult> result = new AtomicReference<>(); final AtomicReference<NearbyLocationsResult> result = new AtomicReference<>();
final HttpClient.Callback callback = new HttpClient.Callback() { final HttpClient.Callback callback = (bodyPeek, body) -> {
@Override try {
public void onSuccessful(final CharSequence bodyPeek, final ResponseBody body) throws IOException { final XmlPullParser pp = parserFactory.newPullParser();
try { pp.setInput(body.charStream());
final XmlPullParser pp = parserFactory.newPullParser(); final ResultHeader header = enterItdRequest(pp);
pp.setInput(body.charStream());
final ResultHeader header = enterItdRequest(pp);
XmlPullUtil.enter(pp, "itdDepartureMonitorRequest"); XmlPullUtil.enter(pp, "itdDepartureMonitorRequest");
final AtomicReference<Location> ownStation = new AtomicReference<>(); final AtomicReference<Location> ownStation = new AtomicReference<>();
final List<Location> stations = new ArrayList<>(); final List<Location> stations = new ArrayList<>();
final String nameState = processItdOdv(pp, "dm", new ProcessItdOdvCallback() { final String nameState = processItdOdv(pp, "dm", (nameState1, location, matchQuality) -> {
@Override if (location.type == LocationType.STATION) {
public void location(final String nameState, final Location location, final int matchQuality) { if ("identified".equals(nameState1)) ownStation.set(location);
if (location.type == LocationType.STATION) { else if ("assigned".equals(nameState1)) stations.add(location);
if ("identified".equals(nameState)) } else {
ownStation.set(location); throw new IllegalStateException("cannot handle: " + location.type);
else if ("assigned".equals(nameState))
stations.add(location);
} else {
throw new IllegalStateException("cannot handle: " + location.type);
}
}
});
if ("notidentified".equals(nameState)) {
result.set(new NearbyLocationsResult(header, NearbyLocationsResult.Status.INVALID_ID));
return;
} }
});
if (ownStation.get() != null && !stations.contains(ownStation.get())) if ("notidentified".equals(nameState)) {
stations.add(ownStation.get()); result.set(new NearbyLocationsResult(header, NearbyLocationsResult.Status.INVALID_ID));
return;
if (maxLocations == 0 || maxLocations >= stations.size())
result.set(new NearbyLocationsResult(header, stations));
else
result.set(new NearbyLocationsResult(header, stations.subList(0, maxLocations)));
} catch (final XmlPullParserException | ParserException x) {
throw new ParserException("cannot parse xml: " + bodyPeek, x);
} }
if (ownStation.get() != null && !stations.contains(ownStation.get()))
stations.add(ownStation.get());
if (maxLocations == 0 || maxLocations >= stations.size())
result.set(new NearbyLocationsResult(header, stations));
else
result.set(new NearbyLocationsResult(header, stations.subList(0, maxLocations)));
} catch (final XmlPullParserException | ParserException x) {
throw new ParserException("cannot parse xml: " + bodyPeek, x);
} }
}; };
@ -1433,140 +1416,133 @@ public abstract class AbstractEfaProvider extends AbstractNetworkProvider {
appendDepartureMonitorRequestParameters(url, stationId, time, maxDepartures, equivs); appendDepartureMonitorRequestParameters(url, stationId, time, maxDepartures, equivs);
final AtomicReference<QueryDeparturesResult> result = new AtomicReference<>(); final AtomicReference<QueryDeparturesResult> result = new AtomicReference<>();
final HttpClient.Callback callback = new HttpClient.Callback() { final HttpClient.Callback callback = (bodyPeek, body) -> {
@Override try {
public void onSuccessful(final CharSequence bodyPeek, final ResponseBody body) throws IOException { final XmlPullParser pp = parserFactory.newPullParser();
try { pp.setInput(body.charStream());
final XmlPullParser pp = parserFactory.newPullParser(); final ResultHeader header = enterItdRequest(pp);
pp.setInput(body.charStream());
final ResultHeader header = enterItdRequest(pp);
final QueryDeparturesResult r = new QueryDeparturesResult(header); final QueryDeparturesResult r = new QueryDeparturesResult(header);
XmlPullUtil.enter(pp, "itdDepartureMonitorRequest"); XmlPullUtil.enter(pp, "itdDepartureMonitorRequest");
XmlPullUtil.optSkipMultiple(pp, "itdMessage"); XmlPullUtil.optSkipMultiple(pp, "itdMessage");
final String nameState = processItdOdv(pp, "dm", new ProcessItdOdvCallback() { final String nameState = processItdOdv(pp, "dm", (nameState1, location, matchQuality) -> {
@Override if (location.type == LocationType.STATION)
public void location(final String nameState, final Location location, final int matchQuality) { if (findStationDepartures(r.stationDepartures, location.id) == null)
if (location.type == LocationType.STATION) r.stationDepartures.add(new StationDepartures(location, new LinkedList<Departure>(), new LinkedList<LineDestination>()));
if (findStationDepartures(r.stationDepartures, location.id) == null) });
r.stationDepartures.add(new StationDepartures(location, new LinkedList<Departure>(),
new LinkedList<LineDestination>()));
}
});
if (!"identified".equals(nameState)) { if (!"identified".equals(nameState)) {
result.set(new QueryDeparturesResult(header, QueryDeparturesResult.Status.INVALID_STATION)); result.set(new QueryDeparturesResult(header, QueryDeparturesResult.Status.INVALID_STATION));
return; return;
}
XmlPullUtil.optSkip(pp, "itdDateTime");
XmlPullUtil.optSkip(pp, "itdDMDateTime");
XmlPullUtil.optSkip(pp, "itdDateRange");
XmlPullUtil.optSkip(pp, "itdTripOptions");
XmlPullUtil.optSkip(pp, "itdMessage");
if (XmlPullUtil.test(pp, "itdServingLines")) {
if (!pp.isEmptyElementTag()) {
XmlPullUtil.enter(pp, "itdServingLines");
while (XmlPullUtil.test(pp, "itdServingLine")) {
final String assignedStopId = XmlPullUtil.optAttr(pp, "assignedStopID", null);
final LineDestinationAndCancelled lineDestinationAndCancelled = processItdServingLine(
pp);
final LineDestination lineDestination = new LineDestination(
lineDestinationAndCancelled.line, lineDestinationAndCancelled.destination);
StationDepartures assignedStationDepartures;
if (assignedStopId == null)
assignedStationDepartures = r.stationDepartures.get(0);
else
assignedStationDepartures = findStationDepartures(r.stationDepartures,
assignedStopId);
if (assignedStationDepartures == null)
assignedStationDepartures = new StationDepartures(
new Location(LocationType.STATION, assignedStopId),
new LinkedList<Departure>(), new LinkedList<LineDestination>());
final List<LineDestination> assignedStationDeparturesLines = checkNotNull(
assignedStationDepartures.lines);
if (!assignedStationDeparturesLines.contains(lineDestination))
assignedStationDeparturesLines.add(lineDestination);
}
XmlPullUtil.skipExit(pp, "itdServingLines");
} else {
XmlPullUtil.next(pp);
}
} else {
result.set(new QueryDeparturesResult(header, QueryDeparturesResult.Status.INVALID_STATION));
return;
}
XmlPullUtil.require(pp, "itdDepartureList");
if (XmlPullUtil.optEnter(pp, "itdDepartureList")) {
final Calendar plannedDepartureTime = new GregorianCalendar(timeZone);
final Calendar predictedDepartureTime = new GregorianCalendar(timeZone);
while (XmlPullUtil.test(pp, "itdDeparture")) {
final String assignedStopId = XmlPullUtil.attr(pp, "stopID");
StationDepartures assignedStationDepartures = findStationDepartures(r.stationDepartures,
assignedStopId);
if (assignedStationDepartures == null) {
final Point coord = processCoordAttr(pp);
// final String name = normalizeLocationName(XmlPullUtil.attr(pp, "nameWO"));
assignedStationDepartures = new StationDepartures(
new Location(LocationType.STATION, assignedStopId, coord),
new LinkedList<Departure>(), new LinkedList<LineDestination>());
}
final Position position = parsePosition(XmlPullUtil.optAttr(pp, "platformName", null));
XmlPullUtil.enter(pp, "itdDeparture");
XmlPullUtil.require(pp, "itdDateTime");
plannedDepartureTime.clear();
processItdDateTime(pp, plannedDepartureTime);
predictedDepartureTime.clear();
if (XmlPullUtil.test(pp, "itdRTDateTime"))
processItdDateTime(pp, predictedDepartureTime);
XmlPullUtil.optSkip(pp, "itdFrequencyInfo");
XmlPullUtil.require(pp, "itdServingLine");
final boolean isRealtime = XmlPullUtil.attr(pp, "realtime").equals("1");
final LineDestinationAndCancelled lineDestinationAndCancelled = processItdServingLine(pp);
if (isRealtime && !predictedDepartureTime.isSet(Calendar.HOUR_OF_DAY))
predictedDepartureTime.setTimeInMillis(plannedDepartureTime.getTimeInMillis());
XmlPullUtil.skipExit(pp, "itdDeparture");
if (!lineDestinationAndCancelled.cancelled) {
final Departure departure = new Departure(plannedDepartureTime.getTime(),
predictedDepartureTime.isSet(Calendar.HOUR_OF_DAY)
? predictedDepartureTime.getTime() : null,
lineDestinationAndCancelled.line, position,
lineDestinationAndCancelled.destination, null, null);
assignedStationDepartures.departures.add(departure);
}
}
XmlPullUtil.skipExit(pp, "itdDepartureList");
}
result.set(r);
} catch (final XmlPullParserException | ParserException x) {
throw new ParserException("cannot parse xml: " + bodyPeek, x);
} }
XmlPullUtil.optSkip(pp, "itdDateTime");
XmlPullUtil.optSkip(pp, "itdDMDateTime");
XmlPullUtil.optSkip(pp, "itdDateRange");
XmlPullUtil.optSkip(pp, "itdTripOptions");
XmlPullUtil.optSkip(pp, "itdMessage");
if (XmlPullUtil.test(pp, "itdServingLines")) {
if (!pp.isEmptyElementTag()) {
XmlPullUtil.enter(pp, "itdServingLines");
while (XmlPullUtil.test(pp, "itdServingLine")) {
final String assignedStopId = XmlPullUtil.optAttr(pp, "assignedStopID", null);
final LineDestinationAndCancelled lineDestinationAndCancelled = processItdServingLine(
pp);
final LineDestination lineDestination = new LineDestination(
lineDestinationAndCancelled.line, lineDestinationAndCancelled.destination);
StationDepartures assignedStationDepartures;
if (assignedStopId == null)
assignedStationDepartures = r.stationDepartures.get(0);
else
assignedStationDepartures = findStationDepartures(r.stationDepartures,
assignedStopId);
if (assignedStationDepartures == null)
assignedStationDepartures = new StationDepartures(
new Location(LocationType.STATION, assignedStopId),
new LinkedList<Departure>(), new LinkedList<LineDestination>());
final List<LineDestination> assignedStationDeparturesLines = checkNotNull(
assignedStationDepartures.lines);
if (!assignedStationDeparturesLines.contains(lineDestination))
assignedStationDeparturesLines.add(lineDestination);
}
XmlPullUtil.skipExit(pp, "itdServingLines");
} else {
XmlPullUtil.next(pp);
}
} else {
result.set(new QueryDeparturesResult(header, QueryDeparturesResult.Status.INVALID_STATION));
return;
}
XmlPullUtil.require(pp, "itdDepartureList");
if (XmlPullUtil.optEnter(pp, "itdDepartureList")) {
final Calendar plannedDepartureTime = new GregorianCalendar(timeZone);
final Calendar predictedDepartureTime = new GregorianCalendar(timeZone);
while (XmlPullUtil.test(pp, "itdDeparture")) {
final String assignedStopId = XmlPullUtil.attr(pp, "stopID");
StationDepartures assignedStationDepartures = findStationDepartures(r.stationDepartures,
assignedStopId);
if (assignedStationDepartures == null) {
final Point coord = processCoordAttr(pp);
// final String name = normalizeLocationName(XmlPullUtil.attr(pp, "nameWO"));
assignedStationDepartures = new StationDepartures(
new Location(LocationType.STATION, assignedStopId, coord),
new LinkedList<Departure>(), new LinkedList<LineDestination>());
}
final Position position = parsePosition(XmlPullUtil.optAttr(pp, "platformName", null));
XmlPullUtil.enter(pp, "itdDeparture");
XmlPullUtil.require(pp, "itdDateTime");
plannedDepartureTime.clear();
processItdDateTime(pp, plannedDepartureTime);
predictedDepartureTime.clear();
if (XmlPullUtil.test(pp, "itdRTDateTime"))
processItdDateTime(pp, predictedDepartureTime);
XmlPullUtil.optSkip(pp, "itdFrequencyInfo");
XmlPullUtil.require(pp, "itdServingLine");
final boolean isRealtime = XmlPullUtil.attr(pp, "realtime").equals("1");
final LineDestinationAndCancelled lineDestinationAndCancelled = processItdServingLine(pp);
if (isRealtime && !predictedDepartureTime.isSet(Calendar.HOUR_OF_DAY))
predictedDepartureTime.setTimeInMillis(plannedDepartureTime.getTimeInMillis());
XmlPullUtil.skipExit(pp, "itdDeparture");
if (!lineDestinationAndCancelled.cancelled) {
final Departure departure = new Departure(plannedDepartureTime.getTime(),
predictedDepartureTime.isSet(Calendar.HOUR_OF_DAY)
? predictedDepartureTime.getTime() : null,
lineDestinationAndCancelled.line, position,
lineDestinationAndCancelled.destination, null, null);
assignedStationDepartures.departures.add(departure);
}
}
XmlPullUtil.skipExit(pp, "itdDepartureList");
}
result.set(r);
} catch (final XmlPullParserException | ParserException x) {
throw new ParserException("cannot parse xml: " + bodyPeek, x);
} }
}; };
@ -1581,85 +1557,82 @@ public abstract class AbstractEfaProvider extends AbstractNetworkProvider {
appendDepartureMonitorRequestParameters(url, stationId, time, maxDepartures, equivs); appendDepartureMonitorRequestParameters(url, stationId, time, maxDepartures, equivs);
final AtomicReference<QueryDeparturesResult> result = new AtomicReference<>(); final AtomicReference<QueryDeparturesResult> result = new AtomicReference<>();
final HttpClient.Callback callback = new HttpClient.Callback() { final HttpClient.Callback callback = (bodyPeek, body) -> {
@Override try {
public void onSuccessful(final CharSequence bodyPeek, final ResponseBody body) throws IOException { final XmlPullParser pp = parserFactory.newPullParser();
try { pp.setInput(body.charStream());
final XmlPullParser pp = parserFactory.newPullParser(); final ResultHeader header = enterEfa(pp);
pp.setInput(body.charStream()); final QueryDeparturesResult r = new QueryDeparturesResult(header);
final ResultHeader header = enterEfa(pp);
final QueryDeparturesResult r = new QueryDeparturesResult(header);
if (XmlPullUtil.optEnter(pp, "ers")) {
XmlPullUtil.enter(pp, "err");
final String mod = XmlPullUtil.valueTag(pp, "mod");
final String co = XmlPullUtil.valueTag(pp, "co");
XmlPullUtil.optValueTag(pp, "u", null);
if ("-2000".equals(co)) { // STOP_INVALID
result.set(new QueryDeparturesResult(header, QueryDeparturesResult.Status.INVALID_STATION));
} else if ("-4050".equals(co)) { // NO_SERVINGLINES
result.set(r);
} else {
log.debug("EFA error: {} {}", co, mod);
result.set(new QueryDeparturesResult(header, QueryDeparturesResult.Status.SERVICE_DOWN));
}
XmlPullUtil.exit(pp, "err");
XmlPullUtil.exit(pp, "ers");
} else if (XmlPullUtil.optEnter(pp, "dps")) {
final Calendar plannedDepartureTime = new GregorianCalendar(timeZone);
final Calendar predictedDepartureTime = new GregorianCalendar(timeZone);
while (XmlPullUtil.optEnter(pp, "dp")) {
// misc
/* final String stationName = */normalizeLocationName(XmlPullUtil.valueTag(pp, "n"));
/* final String gid = */XmlPullUtil.optValueTag(pp, "gid", null);
/* final String pgid = */XmlPullUtil.optValueTag(pp, "pgid", null);
/* final boolean isRealtime = */XmlPullUtil.valueTag(pp, "realtime").equals("1");
/* final String rts = */XmlPullUtil.optValueTag(pp, "rts", null);
XmlPullUtil.optSkip(pp, "dt");
// time
parseMobileSt(pp, plannedDepartureTime, predictedDepartureTime);
final LineDestination lineDestination = parseMobileM(pp, true);
XmlPullUtil.enter(pp, "r");
final String assignedId = XmlPullUtil.valueTag(pp, "id");
XmlPullUtil.valueTag(pp, "a");
final Position position = parsePosition(XmlPullUtil.optValueTag(pp, "pl", null));
XmlPullUtil.skipExit(pp, "r");
/* final Point positionCoordinate = */parseCoord(XmlPullUtil.optValueTag(pp, "c", null));
// TODO messages
StationDepartures stationDepartures = findStationDepartures(r.stationDepartures,
assignedId);
if (stationDepartures == null) {
stationDepartures = new StationDepartures(
new Location(LocationType.STATION, assignedId),
new ArrayList<Departure>(maxDepartures), null);
r.stationDepartures.add(stationDepartures);
}
stationDepartures.departures.add(new Departure(plannedDepartureTime.getTime(),
predictedDepartureTime.isSet(Calendar.HOUR_OF_DAY)
? predictedDepartureTime.getTime() : null,
lineDestination.line, position, lineDestination.destination, null, null));
XmlPullUtil.skipExit(pp, "dp");
}
XmlPullUtil.skipExit(pp, "dps");
if (XmlPullUtil.optEnter(pp, "ers")) {
XmlPullUtil.enter(pp, "err");
final String mod = XmlPullUtil.valueTag(pp, "mod");
final String co = XmlPullUtil.valueTag(pp, "co");
XmlPullUtil.optValueTag(pp, "u", null);
if ("-2000".equals(co)) { // STOP_INVALID
result.set(new QueryDeparturesResult(header, QueryDeparturesResult.Status.INVALID_STATION));
} else if ("-4050".equals(co)) { // NO_SERVINGLINES
result.set(r); result.set(r);
} else { } else {
result.set(new QueryDeparturesResult(header, QueryDeparturesResult.Status.INVALID_STATION)); log.debug("EFA error: {} {}", co, mod);
result.set(new QueryDeparturesResult(header, QueryDeparturesResult.Status.SERVICE_DOWN));
} }
} catch (final XmlPullParserException | ParserException x) { XmlPullUtil.exit(pp, "err");
throw new ParserException("cannot parse xml: " + bodyPeek, x); XmlPullUtil.exit(pp, "ers");
} else if (XmlPullUtil.optEnter(pp, "dps")) {
final Calendar plannedDepartureTime = new GregorianCalendar(timeZone);
final Calendar predictedDepartureTime = new GregorianCalendar(timeZone);
while (XmlPullUtil.optEnter(pp, "dp")) {
// misc
/* final String stationName = */normalizeLocationName(XmlPullUtil.valueTag(pp, "n"));
/* final String gid = */XmlPullUtil.optValueTag(pp, "gid", null);
/* final String pgid = */XmlPullUtil.optValueTag(pp, "pgid", null);
/* final boolean isRealtime = */XmlPullUtil.valueTag(pp, "realtime").equals("1");
/* final String rts = */XmlPullUtil.optValueTag(pp, "rts", null);
XmlPullUtil.optSkip(pp, "dt");
// time
parseMobileSt(pp, plannedDepartureTime, predictedDepartureTime);
final LineDestination lineDestination = parseMobileM(pp, true);
XmlPullUtil.enter(pp, "r");
final String assignedId = XmlPullUtil.valueTag(pp, "id");
XmlPullUtil.valueTag(pp, "a");
final Position position = parsePosition(XmlPullUtil.optValueTag(pp, "pl", null));
XmlPullUtil.skipExit(pp, "r");
/* final Point positionCoordinate = */parseCoord(XmlPullUtil.optValueTag(pp, "c", null));
// TODO messages
StationDepartures stationDepartures = findStationDepartures(r.stationDepartures,
assignedId);
if (stationDepartures == null) {
stationDepartures = new StationDepartures(
new Location(LocationType.STATION, assignedId),
new ArrayList<Departure>(maxDepartures), null);
r.stationDepartures.add(stationDepartures);
}
stationDepartures.departures.add(new Departure(plannedDepartureTime.getTime(),
predictedDepartureTime.isSet(Calendar.HOUR_OF_DAY)
? predictedDepartureTime.getTime() : null,
lineDestination.line, position, lineDestination.destination, null, null));
XmlPullUtil.skipExit(pp, "dp");
}
XmlPullUtil.skipExit(pp, "dps");
result.set(r);
} else {
result.set(new QueryDeparturesResult(header, QueryDeparturesResult.Status.INVALID_STATION));
} }
} catch (final XmlPullParserException | ParserException x) {
throw new ParserException("cannot parse xml: " + bodyPeek, x);
} }
}; };
@ -2040,16 +2013,13 @@ public abstract class AbstractEfaProvider extends AbstractNetworkProvider {
appendTripRequestParameters(url, from, via, to, date, dep, options); appendTripRequestParameters(url, from, via, to, date, dep, options);
final AtomicReference<QueryTripsResult> result = new AtomicReference<>(); final AtomicReference<QueryTripsResult> result = new AtomicReference<>();
final HttpClient.Callback callback = new HttpClient.Callback() { final HttpClient.Callback callback = (bodyPeek, body) -> {
@Override try {
public void onSuccessful(final CharSequence bodyPeek, final ResponseBody body) throws IOException { result.set(queryTrips(url.build(), body.charStream()));
try { } catch (final XmlPullParserException | ParserException x) {
result.set(queryTrips(url.build(), body.charStream())); throw new ParserException("cannot parse xml: " + bodyPeek, x);
} catch (final XmlPullParserException | ParserException x) { } catch (final RuntimeException x) {
throw new ParserException("cannot parse xml: " + bodyPeek, x); throw new RuntimeException("uncategorized problem while processing " + url, x);
} catch (final RuntimeException x) {
throw new RuntimeException("uncategorized problem while processing " + url, x);
}
} }
}; };
@ -2064,16 +2034,13 @@ public abstract class AbstractEfaProvider extends AbstractNetworkProvider {
appendTripRequestParameters(url, from, via, to, date, dep, options); appendTripRequestParameters(url, from, via, to, date, dep, options);
final AtomicReference<QueryTripsResult> result = new AtomicReference<>(); final AtomicReference<QueryTripsResult> result = new AtomicReference<>();
final HttpClient.Callback callback = new HttpClient.Callback() { final HttpClient.Callback callback = (bodyPeek, body) -> {
@Override try {
public void onSuccessful(final CharSequence bodyPeek, final ResponseBody body) throws IOException { result.set(queryTripsMobile(url.build(), from, via, to, body.charStream()));
try { } catch (final XmlPullParserException | ParserException x) {
result.set(queryTripsMobile(url.build(), from, via, to, body.charStream())); throw new ParserException("cannot parse xml: " + bodyPeek, x);
} catch (final XmlPullParserException | ParserException x) { } catch (final RuntimeException x) {
throw new ParserException("cannot parse xml: " + bodyPeek, x); throw new RuntimeException("uncategorized problem while processing " + url, x);
} catch (final RuntimeException x) {
throw new RuntimeException("uncategorized problem while processing " + url, x);
}
} }
}; };
@ -2091,16 +2058,13 @@ public abstract class AbstractEfaProvider extends AbstractNetworkProvider {
url.addEncodedQueryParameter("command", later ? "tripNext" : "tripPrev"); url.addEncodedQueryParameter("command", later ? "tripNext" : "tripPrev");
final AtomicReference<QueryTripsResult> result = new AtomicReference<>(); final AtomicReference<QueryTripsResult> result = new AtomicReference<>();
final HttpClient.Callback callback = new HttpClient.Callback() { final HttpClient.Callback callback = (bodyPeek, body) -> {
@Override try {
public void onSuccessful(final CharSequence bodyPeek, final ResponseBody body) throws IOException { result.set(queryTrips(url.build(), body.charStream()));
try { } catch (final XmlPullParserException | ParserException x) {
result.set(queryTrips(url.build(), body.charStream())); throw new ParserException("cannot parse xml: " + bodyPeek, x);
} catch (final XmlPullParserException | ParserException x) { } catch (final RuntimeException x) {
throw new ParserException("cannot parse xml: " + bodyPeek, x); throw new RuntimeException("uncategorized problem while processing " + url, x);
} catch (final RuntimeException x) {
throw new RuntimeException("uncategorized problem while processing " + url, x);
}
} }
}; };
@ -2118,16 +2082,13 @@ public abstract class AbstractEfaProvider extends AbstractNetworkProvider {
url.addEncodedQueryParameter("command", later ? "tripNext" : "tripPrev"); url.addEncodedQueryParameter("command", later ? "tripNext" : "tripPrev");
final AtomicReference<QueryTripsResult> result = new AtomicReference<>(); final AtomicReference<QueryTripsResult> result = new AtomicReference<>();
final HttpClient.Callback callback = new HttpClient.Callback() { final HttpClient.Callback callback = (bodyPeek, body) -> {
@Override try {
public void onSuccessful(final CharSequence bodyPeek, final ResponseBody body) throws IOException { result.set(queryTripsMobile(url.build(), null, null, null, body.charStream()));
try { } catch (final XmlPullParserException | ParserException x) {
result.set(queryTripsMobile(url.build(), null, null, null, body.charStream())); throw new ParserException("cannot parse xml: " + bodyPeek, x);
} catch (final XmlPullParserException | ParserException x) { } catch (final RuntimeException x) {
throw new ParserException("cannot parse xml: " + bodyPeek, x); throw new RuntimeException("uncategorized problem while processing " + url, x);
} catch (final RuntimeException x) {
throw new RuntimeException("uncategorized problem while processing " + url, x);
}
} }
}; };
@ -2163,12 +2124,7 @@ public abstract class AbstractEfaProvider extends AbstractNetworkProvider {
final String usage = XmlPullUtil.attr(pp, "usage"); final String usage = XmlPullUtil.attr(pp, "usage");
final List<Location> locations = new ArrayList<>(); final List<Location> locations = new ArrayList<>();
final String nameState = processItdOdv(pp, usage, new ProcessItdOdvCallback() { final String nameState = processItdOdv(pp, usage, (nameState1, location, matchQuality) -> locations.add(location));
@Override
public void location(final String nameState, final Location location, final int matchQuality) {
locations.add(location);
}
});
if ("list".equals(nameState)) { if ("list".equals(nameState)) {
if ("origin".equals(usage)) if ("origin".equals(usage))

File diff suppressed because it is too large Load diff

View file

@ -96,10 +96,5 @@ public final class Departure implements Serializable {
return Objects.hashCode(plannedTime, predictedTime, line, destination); return Objects.hashCode(plannedTime, predictedTime, line, destination);
} }
public static final Comparator<Departure> TIME_COMPARATOR = new Comparator<Departure>() { public static final Comparator<Departure> TIME_COMPARATOR = (departure0, departure1) -> departure0.getTime().compareTo(departure1.getTime());
@Override
public int compare(final Departure departure0, final Departure departure1) {
return departure0.getTime().compareTo(departure1.getTime());
}
};
} }

View file

@ -208,12 +208,7 @@ public final class HttpClient {
public CharSequence get(final HttpUrl url, final String postRequest, final String requestContentType) public CharSequence get(final HttpUrl url, final String postRequest, final String requestContentType)
throws IOException { throws IOException {
final StringBuilder buffer = new StringBuilder(); final StringBuilder buffer = new StringBuilder();
final Callback callback = new Callback() { final Callback callback = (bodyPeek, body) -> buffer.append(body.string());
@Override
public void onSuccessful(final CharSequence bodyPeek, final ResponseBody body) throws IOException {
buffer.append(body.string());
}
};
getInputStream(callback, url, postRequest, requestContentType, null); getInputStream(callback, url, postRequest, requestContentType, null);
return buffer; return buffer;
} }
@ -382,10 +377,5 @@ public final class HttpClient {
} }
}; };
private static final HostnameVerifier SSL_ACCEPT_ALL_HOSTNAMES = new HostnameVerifier() { private static final HostnameVerifier SSL_ACCEPT_ALL_HOSTNAMES = (hostname, session) -> true;
@Override
public boolean verify(final String hostname, final SSLSession session) {
return true;
}
};
} }