From 5c37e919faf6773c5dbde3171a9f5d1700753b06 Mon Sep 17 00:00:00 2001 From: "andreas.schildbach@gmail.com" Date: Wed, 15 Dec 2010 21:19:11 +0000 Subject: [PATCH] workaround for live departures delivered unsorted git-svn-id: https://public-transport-enabler.googlecode.com/svn/trunk@393 0924bc21-9374-b0fa-ee44-9ff1593b38f0 --- src/de/schildbach/pte/BvgProvider.java | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/src/de/schildbach/pte/BvgProvider.java b/src/de/schildbach/pte/BvgProvider.java index d6d1ffa9..c6eb2c5a 100644 --- a/src/de/schildbach/pte/BvgProvider.java +++ b/src/de/schildbach/pte/BvgProvider.java @@ -23,6 +23,8 @@ import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; +import java.util.Collections; +import java.util.Comparator; import java.util.Date; import java.util.GregorianCalendar; import java.util.HashMap; @@ -85,8 +87,7 @@ public final class BvgProvider implements NetworkProvider { final Matcher mMulti = P_MULTI_NAME.matcher(page); while (mMulti.find()) - results - .add(new Location(LocationType.STATION, Integer.parseInt(mMulti.group(1)), 0, 0, ParserUtils.resolveEntities(mMulti.group(2)))); + results.add(new Location(LocationType.STATION, Integer.parseInt(mMulti.group(1)), 0, 0, ParserUtils.resolveEntities(mMulti.group(2)))); } return results; @@ -398,8 +399,8 @@ public final class BvgProvider implements NetworkProvider { final int arrivalId = mDetFine.group(12) != null ? Integer.parseInt(mDetFine.group(12)) : 0; - final String arrival = ParserUtils.resolveEntities(ParserUtils.selectNotNull(mDetFine.group(13), mDetFine.group(16), mDetFine - .group(17))); + final String arrival = ParserUtils.resolveEntities(ParserUtils.selectNotNull(mDetFine.group(13), mDetFine.group(16), + mDetFine.group(17))); final int arrivalLon = mDetFine.group(14) != null ? Integer.parseInt(mDetFine.group(14)) : 0; @@ -481,6 +482,8 @@ public final class BvgProvider implements NetworkProvider if (P_DEPARTURES_SERVICE_DOWN.matcher(page).find()) return new QueryDeparturesResult(Status.SERVICE_DOWN, Integer.parseInt(stationId)); + final boolean live = stationId.length() == 6; + // parse page final Matcher mHead = P_DEPARTURES_HEAD.matcher(page); if (mHead.matches()) @@ -493,7 +496,6 @@ public final class BvgProvider implements NetworkProvider final Matcher mDepCoarse = P_DEPARTURES_COARSE.matcher(page); while (mDepCoarse.find()) { - final boolean live = stationId.length() == 6; final Matcher mDepFine = (live ? P_DEPARTURES_LIVE_FINE : P_DEPARTURES_PLAN_FINE).matcher(mDepCoarse.group(1)); if (mDepFine.matches()) { @@ -535,6 +537,20 @@ public final class BvgProvider implements NetworkProvider } } + // workaround for live departures delivered unsorted + if (live) + { + Collections.sort(departures, new Comparator() + { + public int compare(final Departure d1, final Departure d2) + { + final Date t1 = d1.predictedTime != null ? d1.predictedTime : d1.plannedTime; + final Date t2 = d2.predictedTime != null ? d2.predictedTime : d2.plannedTime; + return t1.compareTo(t2); + } + }); + } + return new QueryDeparturesResult(new Location(LocationType.STATION, Integer.parseInt(stationId), 0, 0, location), departures, null); } else