diff --git a/src/it/reyboz/bustorino/backend/Passaggio.java b/src/it/reyboz/bustorino/backend/Passaggio.java index 90f3518..e4dadd4 100644 --- a/src/it/reyboz/bustorino/backend/Passaggio.java +++ b/src/it/reyboz/bustorino/backend/Passaggio.java @@ -1,103 +1,89 @@ /* BusTO (backend components) Copyright (C) 2016 Ludovico Pavesi This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ package it.reyboz.bustorino.backend; import android.support.annotation.NonNull; public final class Passaggio implements Comparable { public final String hh; public final String mm; public final boolean isInRealTime; /** * Constructs a time (passaggio) for the timetable. * * @param TimeGTT time in GTT format (e.g. "11:22*"), already trimmed from whitespace. * @throws IllegalArgumentException if nothing reasonable can be extracted from the string */ public Passaggio(@NonNull String TimeGTT) { String[] parts = TimeGTT.split(":"); if(parts.length != 2) { //throw new IllegalArgumentException("The string " + TimeGTT + " doesn't follow the sacred format of time according to GTT!"); this.hh = "??"; this.mm = "??"; this.isInRealTime = false; return; } this.hh = parts[0]; if(parts[1].endsWith("*")) { this.mm = parts[1].substring(0, parts[1].length() - 1); this.isInRealTime = true; } else { this.mm = parts[1]; this.isInRealTime = false; } } @Override public int compareTo(@NonNull Passaggio altro) { int diff; - diff = failsafeParseInt(altro.hh) - failsafeParseInt(this.hh); + diff = networkTools.failsafeParseInt(altro.hh) - networkTools.failsafeParseInt(this.hh); // an attempt to correctly sortRoutes times around midnight (e.g. 23.59 should come before 00.01) if(diff > 12) { // TODO: see if this works in practice diff = -(24 - diff); } else if(diff < -12) { diff = -(diff + 24); } diff *= 60; - diff += failsafeParseInt(altro.mm) - failsafeParseInt(this.mm); + diff += networkTools.failsafeParseInt(altro.mm) - networkTools.failsafeParseInt(this.mm); // we should take into account if one is in real time and the other isn't, shouldn't we? if(altro.isInRealTime) { ++diff; } if(this.isInRealTime) { --diff; } return diff; } @Override public String toString() { String resultString = (this.hh).concat(":").concat(this.mm); if(this.isInRealTime) { return resultString.concat("*"); } else { return resultString; } } - - /** - * Parses without blowing up. - * - * @param str the number as a string - * @return the number as an integer - */ - private static int failsafeParseInt(String str) { - try { - return Integer.parseInt(str); - } catch(NumberFormatException e) { - return 0; - } - } } \ No newline at end of file diff --git a/src/it/reyboz/bustorino/backend/Route.java b/src/it/reyboz/bustorino/backend/Route.java index fe4eefd..8289cd8 100644 --- a/src/it/reyboz/bustorino/backend/Route.java +++ b/src/it/reyboz/bustorino/backend/Route.java @@ -1,90 +1,101 @@ /* BusTO (backend components) Copyright (C) 2016 Ludovico Pavesi This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ package it.reyboz.bustorino.backend; import android.support.annotation.NonNull; import java.util.List; public class Route implements Comparable { public final String name; public final String destinazione; public final List passaggi; public final Type type; public enum Type { // "long distance" sono gli extraurbani. BUS, LONG_DISTANCE_BUS, METRO, RAILWAY, TRAM } /** * Constructor. * * @param name route ID * @param destinazione terminus\end of line * @param type bus, long distance bus, underground, and so on * @param passaggi timetable, a good choice is an ArrayList of size 6 * @see Palina Palina.addRoute() method */ public Route(String name, String destinazione, Type type, List passaggi) { this.name = name; this.destinazione = destinazione; this.passaggi = passaggi; this.type = type; } /** * Exactly what it says on the tin. * * @return times from the timetable */ public List getPassaggi() { return this.passaggi; } /** * Adds a time (passaggio) to the timetable for this route * * @param TimeGTT time in GTT format (e.g. "11:22*") */ public void addPassaggio(String TimeGTT) { this.passaggi.add(new Passaggio(TimeGTT)); } @Override public int compareTo(@NonNull Route other) { int res; - - res = this.name.compareTo(other.name); - if(res != 0) { - return res; + int thisAsInt = networkTools.failsafeParseInt(this.name); + int otherAsInt = networkTools.failsafeParseInt(other.name); + + // numeric route IDs + if(thisAsInt != 0 && otherAsInt != 0) { + res = thisAsInt - otherAsInt; + if(res != 0) { + return res; + } + } else { + // non-numeric + res = this.name.compareTo(other.name); + if (res != 0) { + return res; + } } res = this.destinazione.compareTo(other.destinazione); if(res != 0) { return res; } if(this.type != other.type) { // ordinal() is evil or whatever, who cares. return this.type.ordinal() - other.type.ordinal(); } return 0; } }