From 03324ce2e33169d124615956f1bccc235f4e2f74 Mon Sep 17 00:00:00 2001 From: "andreas.schildbach" Date: Sun, 5 Sep 2010 19:36:46 +0000 Subject: [PATCH] live departures for Austria git-svn-id: https://public-transport-enabler.googlecode.com/svn/trunk@136 0924bc21-9374-b0fa-ee44-9ff1593b38f0 --- src/de/schildbach/pte/Departure.java | 58 +++++++++++++++++++------ src/de/schildbach/pte/OebbProvider.java | 32 ++++++++++---- 2 files changed, 69 insertions(+), 21 deletions(-) diff --git a/src/de/schildbach/pte/Departure.java b/src/de/schildbach/pte/Departure.java index 0b5f133b..2f3a1a95 100644 --- a/src/de/schildbach/pte/Departure.java +++ b/src/de/schildbach/pte/Departure.java @@ -24,17 +24,31 @@ import java.util.Date; */ public final class Departure { - final public Date time; + final public Date plannedTime; + final public Date predictedTime; final public String line; final public int[] lineColors; final public String position; final public int destinationId; final public String destination; - public Departure(final Date time, final String line, final int[] lineColors, final String position, final int destinationId, + public Departure(final Date plannedTime, final Date predictedTime, final String line, final int[] lineColors, final String position, + final int destinationId, final String destination) + { + this.plannedTime = plannedTime; + this.predictedTime = predictedTime; + this.line = line; + this.lineColors = lineColors; + this.position = position; + this.destinationId = destinationId; + this.destination = destination; + } + + public Departure(final Date plannedTime, final String line, final int[] lineColors, final String position, final int destinationId, final String destination) { - this.time = time; + this.plannedTime = plannedTime; + this.predictedTime = null; this.line = line; this.lineColors = lineColors; this.position = position; @@ -46,7 +60,9 @@ public final class Departure public String toString() { StringBuilder builder = new StringBuilder("Departure("); - builder.append(time != null ? time : "null"); + builder.append(plannedTime != null ? plannedTime : "null"); + builder.append(","); + builder.append(predictedTime != null ? predictedTime : "null"); builder.append(","); builder.append(line != null ? line : "null"); builder.append(","); @@ -60,20 +76,18 @@ public final class Departure } @Override - public boolean equals(Object o) + public boolean equals(final Object o) { if (o == this) return true; if (!(o instanceof Departure)) return false; final Departure other = (Departure) o; - if (!this.time.equals(other.time)) + if (!nullSafeEquals(this.plannedTime, other.plannedTime)) return false; - if (this.line == null && other.line != null) + if (!nullSafeEquals(this.predictedTime, other.predictedTime)) return false; - if (other.line == null && this.line != null) - return false; - if (this.line != null && !this.line.equals(other.line)) + if (!nullSafeEquals(this.line, other.line)) return false; if (this.destinationId != other.destinationId) return false; @@ -85,14 +99,32 @@ public final class Departure @Override public int hashCode() { - int hashCode = time.hashCode(); + int hashCode = 0; + hashCode += nullSafeHashCode(plannedTime); hashCode *= 29; - if (line != null) - hashCode += line.hashCode(); + hashCode += nullSafeHashCode(predictedTime); + hashCode *= 29; + hashCode += nullSafeHashCode(line); hashCode *= 29; hashCode += destinationId; hashCode *= 29; hashCode += destination.hashCode(); return hashCode; } + + private boolean nullSafeEquals(final Object o1, final Object o2) + { + if (o1 == null && o2 == null) + return true; + if (o1 != null && o1.equals(o2)) + return true; + return false; + } + + private int nullSafeHashCode(final Object o) + { + if (o == null) + return 0; + return o.hashCode(); + } } diff --git a/src/de/schildbach/pte/OebbProvider.java b/src/de/schildbach/pte/OebbProvider.java index 81c813e8..586d9023 100644 --- a/src/de/schildbach/pte/OebbProvider.java +++ b/src/de/schildbach/pte/OebbProvider.java @@ -403,7 +403,10 @@ public class OebbProvider implements NetworkProvider , Pattern.DOTALL); private static final Pattern P_DEPARTURES_COARSE = Pattern.compile("(.*?)", Pattern.DOTALL); private static final Pattern P_DEPARTURES_FINE = Pattern.compile(".*?" // - + "(\\d{1,2}:\\d{2}).*?" // time + + "(\\d{1,2}:\\d{2})\n" // plannedTime + + "(?:\n" // + + "(?: |(pünktlich|\\d{1,2}:\\d{2}))\n\n" // predictedTime + + ")?.*?" // + "\\s*(.*?)\\s*\n" // + "" // destinationId @@ -454,18 +457,31 @@ public class OebbProvider implements NetworkProvider if (ParserUtils.timeDiff(parsed.getTime(), currentTime) < -PARSER_DAY_ROLLOVER_THRESHOLD_MS) parsed.add(Calendar.DAY_OF_MONTH, 1); - final String lineType = mDepFine.group(2); + final Date plannedTime = parsed.getTime(); - final String line = normalizeLine(lineType, ParserUtils.resolveEntities(mDepFine.group(3))); + Date predictedTime = null; + final String prognosis = ParserUtils.resolveEntities(mDepFine.group(2)); + System.out.println("===" + prognosis); + if (prognosis != null) + { + if (prognosis.equals("pünktlich")) + predictedTime = plannedTime; + else + predictedTime = ParserUtils.joinDateTime(currentTime, ParserUtils.parseTime(prognosis)); + } - final int destinationId = mDepFine.group(4) != null ? Integer.parseInt(mDepFine.group(4)) : 0; + final String lineType = mDepFine.group(3); - final String destination = ParserUtils.resolveEntities(mDepFine.group(5)); + final String line = normalizeLine(lineType, ParserUtils.resolveEntities(mDepFine.group(4))); - final String position = mDepFine.group(6) != null ? "Gl. " + ParserUtils.resolveEntities(mDepFine.group(6)) : null; + final int destinationId = mDepFine.group(5) != null ? Integer.parseInt(mDepFine.group(5)) : 0; - final Departure dep = new Departure(parsed.getTime(), line, line != null ? LINES.get(line.charAt(0)) : null, position, - destinationId, destination); + final String destination = ParserUtils.resolveEntities(mDepFine.group(6)); + + final String position = mDepFine.group(7) != null ? "Gl. " + ParserUtils.resolveEntities(mDepFine.group(7)) : null; + + final Departure dep = new Departure(plannedTime, predictedTime, line, line != null ? LINES.get(line.charAt(0)) : null, + position, destinationId, destination); if (!departures.contains(dep)) departures.add(dep);