diff --git a/src/it/reyboz/bustorino/backend/ArrivalsFetcher.java b/src/it/reyboz/bustorino/backend/ArrivalsFetcher.java
new file mode 100644
index 0000000..b847570
--- /dev/null
+++ b/src/it/reyboz/bustorino/backend/ArrivalsFetcher.java
@@ -0,0 +1,42 @@
+/*
+ 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;
+
+// "arrivals" è più usato di "transit" o simili, e chi sono io per mettermi a dibattere con gli inglesi?
+
+public interface ArrivalsFetcher {
+ /**
+ * Reads arrival times from a (hopefully) real-time source, e.g. the GTT website.
+ *
+ * @param stopID stop ID, in normalized form.
+ * @param routeID route ID, in normalized form.
+ * @return arrival times
+ * @see FiveTNormalizer
+ */
+ Palina ReadArrivalTimesLine(String stopID, String routeID);
+
+ /**
+ * Reads arrival times from a (hopefully) real-time source, e.g. the GTT website.
+ *
+ * @param routeID route ID, in normalized form.
+ * @return arrival times
+ * @see FiveTNormalizer
+ */
+ Palina ReadArrivalTimesAll(String routeID);
+}
diff --git a/src/it/reyboz/bustorino/backend/FiveTNormalizer.java b/src/it/reyboz/bustorino/backend/FiveTNormalizer.java
new file mode 100644
index 0000000..c07aff6
--- /dev/null
+++ b/src/it/reyboz/bustorino/backend/FiveTNormalizer.java
@@ -0,0 +1,150 @@
+/*
+ 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;
+
+/**
+ * Converts some weird stop IDs found on the 5T website to the form used everywhere else (including GTT website).
+ *
+ * A stop ID in normalized form is:
+ * - a string containing a number without leading zeros
+ * - a string beginning with "ST" and then a number
+ * - whatever the GTT website uses.
+ *
+ * A bus route ID in normalized form is:
+ * - a string containing a number and optionally: "B", "CS", "CD", "N", "S", "C" at the end
+ * - the string "METRO"
+ * - "ST1" or "ST2" (Star 1 and Star 2)
+ * - a string beginning with E, N, S or W, followed by two digits (with a leading zero)
+ * - "RV2", "OB1"
+ * - "CAN" or "FTC" (railway lines)
+ * - ...screw it, let's just hope all the websites and APIs return something sane as route IDs.
+ *
+ * This class exists because Java doesn't support traits.
+ */
+public abstract class FiveTNormalizer {
+ private static String FiveTNormalizeRoute(String RouteID) {
+ while (RouteID.startsWith("0")) {
+ RouteID = RouteID.substring(1);
+ }
+ return RouteID;
+ }
+
+ private static String FiveTNormalizeStop(String StopID) {
+ StopID = FiveTNormalizeRoute(StopID);
+ // is this faster than a regex?
+ if (StopID.length() == 5 && StopID.startsWith("ST") && Character.isLetter(StopID.charAt(2)) && Character.isLetter(StopID.charAt(3)) && Character.isLetter(StopID.charAt(4))) {
+ switch (StopID) {
+ case "STFER":
+ return "8210";
+ case "STPAR":
+ return "8211";
+ case "STMAR":
+ return "8212";
+ case "STMAS":
+ return "8213";
+ case "STPOS":
+ return "8214";
+ case "STMGR":
+ return "8215";
+ case "STRIV":
+ return "8216";
+ case "STRAC":
+ return "8217";
+ case "STBER":
+ return "8218";
+ case "STPDA":
+ return "8219";
+ case "STDOD":
+ return "8220";
+ case "STPSU":
+ return "8221";
+ case "STVIN":
+ return "8222";
+ case "STREU":
+ return "8223";
+ case "STPNU":
+ return "8224";
+ case "STMCI":
+ return "8225";
+ case "STNIZ":
+ return "8226";
+ case "STDAN":
+ return "8227";
+ case "STCAR":
+ return "8228";
+ case "STSPE":
+ return "8229";
+ case "STLGO":
+ return "8230";
+ }
+ }
+ return StopID;
+ }
+
+ private static String NormalizedToFiveT(String StopID) {
+ if (StopID.startsWith("82")) {
+ switch (StopID) {
+ case "8230":
+ return "STLGO";
+ case "8229":
+ return "STSPE";
+ case "8228":
+ return "STCAR";
+ case "8227":
+ return "STDAN";
+ case "8226":
+ return "STNIZ";
+ case "8225":
+ return "STMCI";
+ case "8224":
+ return "STPNU";
+ case "8223":
+ return "STREU";
+ case "8222":
+ return "STVIN";
+ case "8221":
+ return "STPSU";
+ case "8220":
+ return "STDOD";
+ case "8219":
+ return "STPDA";
+ case "8218":
+ return "STBER";
+ case "8217":
+ return "STRAC";
+ case "8216":
+ return "STRIV";
+ case "8215":
+ return "STMGR";
+ case "8214":
+ return "STPOS";
+ case "8213":
+ return "STMAS";
+ case "8212":
+ return "STMAR";
+ case "8211":
+ return "STPAR";
+ case "8210":
+ return "STFER";
+ }
+ }
+
+ return StopID;
+ }
+}
diff --git a/src/it/reyboz/bustorino/backend/Palina.java b/src/it/reyboz/bustorino/backend/Palina.java
new file mode 100644
index 0000000..a49100e
--- /dev/null
+++ b/src/it/reyboz/bustorino/backend/Palina.java
@@ -0,0 +1,217 @@
+/*
+ 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 java.util.List;
+import java.util.ArrayList;
+import java.util.LinkedList;
+import java.util.Iterator;
+
+/**
+ * Timetable for multiple routes.
+ *
+ * Apparently "palina" and a bunch of other terms can't really be translated into English.
+ * Not in a way that makes sense and keeps the code readable, at least.
+ */
+public class Palina {
+ private ArrayList routes;
+
+ public Palina() {
+ this.routes = new ArrayList<>();
+ }
+
+ /**
+ * Adds a route to the timetable.
+ *
+ * @param routeID name
+ * @param destinazione end of line\terminus (underground stations have the same ID for both directions)
+ * @return array index for this route
+ */
+ public int addRoute(String routeID, String destinazione) {
+ this.routes.add(new RouteInternal(routeID, destinazione));
+ return this.routes.size() - 1; // last inserted element and pray that direct access to ArrayList elements really is direct
+ }
+
+ /**
+ * Adds a timetable entry to a route.
+ *
+ * @param TimeGTT time in GTT format (e.g. "11:22*")
+ * @param arrayIndex position in the array for this route (returned by addRoute)
+ */
+ public void addPassaggio(String TimeGTT, int arrayIndex) {
+ this.routes.get(arrayIndex).addPassaggio(TimeGTT);
+ }
+
+ /**
+ * Clears a route timetable (or creates an empty route) and returns its index
+ *
+ * @param routeID name
+ * @param destinazione end of line\terminus
+ * @return array index for this route
+ */
+ public int updateRoute(String routeID, String destinazione) {
+ int s = this.routes.size();
+ RouteInternal r;
+
+ for(int i = 0; i < s; i++) {
+ r = routes.get(i);
+ if(r.name.compareTo(routeID) == 0 && r.destinazione.compareTo(destinazione) == 0) {
+ // TODO: capire se è possibile che ci siano stessa linea e stessa destinazione su 2 righe diverse del sito e qui una sovrascrive l'altra (probabilmente no)
+ r.updateFlag();
+ r.deletePassaggio();
+ return i;
+ }
+ }
+
+ return this.addRoute(routeID, destinazione);
+ }
+
+ /**
+ * Deletes routes marked as "not updated" (= disappeared from the GTT website\API\whatever).
+ * Sets all remaining routes to "not updated" because that's how this contraption works.
+ */
+ public void finishUpdatingRoutes() {
+ RouteInternal r;
+
+ for(Iterator itr = this.routes.iterator(); itr.hasNext(); ) {
+ r = itr.next();
+ if(r.unupdateFlag()) {
+ itr.remove();
+ }
+ }
+ }
+
+ /**
+ * Gets the current timetable for a route. Returns null if the route doesn't exist.
+ * This is slower than queryRouteByIndex.
+ *
+ * @return timetable (passaggi)
+ */
+ public List queryRoute(String routeID) {
+ for(RouteInternal route : this.routes) {
+ if(routeID.equals(route.name)) {
+ return route.getPassaggi();
+ }
+ }
+
+ return null;
+ }
+
+ /**
+ * Gets the current timetable for this route, from its index in the array.
+ *
+ * @return timetable (passaggi)
+ */
+ public List queryRouteByIndex(int index) {
+ return this.routes.get(index).getPassaggi();
+ }
+
+ // TODO: ponder on the usefulness\uselessness of having separate Route and RouteInternal classes just for this method
+ /**
+ * Gets timetables for every route.
+ *
+ * @return routes and timetables.
+ */
+ public List queryAllRoutes() {
+ List list = new LinkedList<>();
+ for(RouteInternal r : this.routes) {
+ list.add(new Route(r.name, r.destinazione, r.passaggi));
+ }
+ return list;
+ }
+
+ /**
+ * Route with terminus (destinazione) and timetables (passaggi), internal implementation.
+ *
+ * Contains mostly the same data as the Route public class, but methods are quite different and extending Route doesn't really work, here.
+ */
+ private final class RouteInternal {
+ public final String name;
+ public final String destinazione;
+ private boolean updated;
+ private List passaggi; // TODO: capire se con "final" si intende che copia la lista ogni volta che la modifico, o che non modifico la reference ma la lista è modificabile
+
+ /**
+ * Creates a new route and marks it as "updated", since it's new.
+ *
+ * @param routeID name
+ * @param destinazione end of line\terminus
+ */
+ public RouteInternal(String routeID, String destinazione) {
+ this.name = routeID;
+ this.destinazione = destinazione;
+ this.passaggi = new LinkedList<>();
+ this.updated = true;
+ }
+
+ /**
+ * 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));
+ }
+
+ /**
+ * Deletes al times (passaggi) from the timetable.
+ */
+ public void deletePassaggio() {
+ this.passaggi = new LinkedList<>();
+ this.updated = true;
+ }
+
+ /**
+ * Sets the "updated" flag to false.
+ *
+ * @return previous state
+ */
+ public boolean unupdateFlag() {
+ if(this.updated) {
+ this.updated = false;
+ return true;
+ } else {
+ return false;
+ }
+ }
+
+ /**
+ * Sets the "updated" flag to true.
+ *
+ * @return previous state
+ */
+ public boolean updateFlag() {
+ if(this.updated) {
+ return true;
+ } else {
+ this.updated = true;
+ return false;
+ }
+ }
+
+ /**
+ * Exactly what it says on the tin.
+ *
+ * @return times from the timetable
+ */
+ public List getPassaggi() {
+ return this.passaggi;
+ }
+ }
+}
\ No newline at end of file
diff --git a/src/it/reyboz/bustorino/backend/Passaggio.java b/src/it/reyboz/bustorino/backend/Passaggio.java
new file mode 100644
index 0000000..ba90b61
--- /dev/null
+++ b/src/it/reyboz/bustorino/backend/Passaggio.java
@@ -0,0 +1,90 @@
+/*
+ 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(String TimeGTT) {
+ String[] parts = TimeGTT.split(":");
+ if(parts.length != 2) {
+ // TODO: messaggio d'errore meno ridicolo
+ throw new IllegalArgumentException("The string " + TimeGTT + " doesn't follow the sacred format of time according to GTT!");
+ }
+ 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);
+
+ // an attempt to correctly sort 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);
+
+ // 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;
+ }
+
+ /**
+ * 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
new file mode 100644
index 0000000..9128a8c
--- /dev/null
+++ b/src/it/reyboz/bustorino/backend/Route.java
@@ -0,0 +1,33 @@
+/*
+ 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 java.util.Collection;
+
+public class Route {
+ public final String name;
+ public final String destinazione;
+ public final Collection passaggi;
+
+ public Route(String name, String destinazione, Collection passaggi) {
+ this.name = name;
+ this.destinazione = destinazione;
+ this.passaggi = passaggi;
+ }
+}
diff --git a/src/it/reyboz/bustorino/backend/StopsFinderByName.java b/src/it/reyboz/bustorino/backend/StopsFinderByName.java
new file mode 100644
index 0000000..f4bf6a4
--- /dev/null
+++ b/src/it/reyboz/bustorino/backend/StopsFinderByName.java
@@ -0,0 +1,32 @@
+/*
+ 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 java.util.List;
+
+public interface StopsFinderByName {
+ /**
+ * Finds stops by name.
+ *
+ * @param name the string to search for
+ * @return list of stops, in normalized form.
+ * @see FiveTNormalizer
+ */
+ List FindByName(String name);
+}
diff --git a/src/it/reyboz/bustorino/backend/StopsFinderByRoute.java b/src/it/reyboz/bustorino/backend/StopsFinderByRoute.java
new file mode 100644
index 0000000..97e66c1
--- /dev/null
+++ b/src/it/reyboz/bustorino/backend/StopsFinderByRoute.java
@@ -0,0 +1,32 @@
+/*
+ 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 java.util.List;
+
+public interface StopsFinderByRoute {
+ /**
+ * Finds every stop in a route.
+ *
+ * @param routeID route ID, in normalized form.
+ * @return list of stops, in normalized form.
+ * @see FiveTNormalizer
+ */
+ List FindByRoute(String routeID);
+}
\ No newline at end of file