Changeset View
Changeset View
Standalone View
Standalone View
src/it/reyboz/bustorino/backend/Palina.java
Show All 20 Lines | |||||
import android.util.Log; | import android.util.Log; | ||||
import androidx.annotation.NonNull; | import androidx.annotation.NonNull; | ||||
import androidx.annotation.Nullable; | import androidx.annotation.Nullable; | ||||
import java.util.ArrayList; | import java.util.ArrayList; | ||||
import java.util.Calendar; | import java.util.Calendar; | ||||
import java.util.Collections; | import java.util.Collections; | ||||
import java.util.Comparator; | |||||
import java.util.List; | import java.util.List; | ||||
import it.reyboz.bustorino.util.LinesNameSorter; | |||||
/** | /** | ||||
* Timetable for multiple routes.<br> | * Timetable for multiple routes.<br> | ||||
* <br> | * <br> | ||||
* Apparently "palina" and a bunch of other terms can't really be translated into English.<br> | * Apparently "palina" and a bunch of other terms can't really be translated into English.<br> | ||||
* Not in a way that makes sense and keeps the code readable, at least. | * Not in a way that makes sense and keeps the code readable, at least. | ||||
*/ | */ | ||||
public class Palina extends Stop { | public class Palina extends Stop { | ||||
private ArrayList<Route> routes = new ArrayList<>(); | private ArrayList<Route> routes = new ArrayList<>(); | ||||
▲ Show 20 Lines • Show All 47 Lines • ▼ Show 20 Lines | public class Palina extends Stop { | ||||
* Adds a route to the timetable. | * Adds a route to the timetable. | ||||
* | * | ||||
* @param routeID name | * @param routeID name | ||||
* @param type bus, underground, railway, ... | * @param type bus, underground, railway, ... | ||||
* @param destinazione end of line\terminus (underground stations have the same ID for both directions) | * @param destinazione end of line\terminus (underground stations have the same ID for both directions) | ||||
* @return array index for this route | * @return array index for this route | ||||
*/ | */ | ||||
public int addRoute(String routeID, String destinazione, Route.Type type) { | public int addRoute(String routeID, String destinazione, Route.Type type) { | ||||
this.routes.add(new Route(routeID, destinazione, type, new ArrayList<>(6))); | return addRoute(new Route(routeID, destinazione, type, new ArrayList<>(6))); | ||||
routesModified = true; | |||||
return this.routes.size() - 1; // last inserted element and pray that direct access to ArrayList elements really is direct | |||||
} | } | ||||
public int addRoute(Route r){ | public int addRoute(Route r){ | ||||
this.routes.add(r); | this.routes.add(r); | ||||
routesModified = true; | routesModified = true; | ||||
return this.routes.size()-1; | buildRoutesString(); | ||||
return this.routes.size()-1; // last inserted element and pray that direct access to ArrayList elements really is direct | |||||
} | } | ||||
public void setRoutes(List<Route> routeList){ | public void setRoutes(List<Route> routeList){ | ||||
routes = new ArrayList<>(routeList); | routes = new ArrayList<>(routeList); | ||||
} | } | ||||
@Nullable | |||||
@Override | |||||
protected String buildRoutesString() { | |||||
// no routes => no string | |||||
if(routes == null || routes.size() == 0) { | |||||
return ""; | |||||
} | |||||
final StringBuilder sb = new StringBuilder(); | |||||
final LinesNameSorter nameSorter = new LinesNameSorter(); | |||||
Collections.sort(routes, (o1, o2) -> nameSorter.compare(o1.getName().trim(), o2.getName().trim())); | |||||
int i, lenMinusOne = routes.size() - 1; | |||||
for (i = 0; i < lenMinusOne; i++) { | |||||
sb.append(routes.get(i).getName().trim()).append(", "); | |||||
} | |||||
// last one: | |||||
sb.append(routes.get(i).getName()); | |||||
setRoutesThatStopHereString(sb.toString()); | |||||
return routesThatStopHereToString(); | |||||
} | |||||
// /** | |||||
// * 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) { | |||||
// // 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<RouteInternal> 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<Passaggio> queryRoute(String routeID) { | |||||
// for(Route r : this.routes) { | |||||
// if(routeID.equals(r.name)) { | |||||
// return r.getPassaggi(); | |||||
// } | |||||
// } | |||||
// | |||||
// return null; | |||||
// } | |||||
// | |||||
// /** | |||||
// * Gets the current timetable for this route, from its index in the array. | |||||
// * | |||||
// * @return timetable (passaggi) | |||||
// */ | |||||
// public List<Passaggio> queryRouteByIndex(int index) { | |||||
// return this.routes.get(index).getPassaggi(); | |||||
// } | |||||
protected void checkPassaggi(){ | protected void checkPassaggi(){ | ||||
Passaggio.Source mSource = null; | Passaggio.Source mSource = null; | ||||
for (Route r: routes){ | for (Route r: routes){ | ||||
for(Passaggio pass: r.passaggi){ | for(Passaggio pass: r.passaggi){ | ||||
if (mSource == null) { | if (mSource == null) { | ||||
mSource = pass.source; | mSource = pass.source; | ||||
} else if (mSource != pass.source){ | } else if (mSource != pass.source){ | ||||
Log.w("BusTO-CheckPassaggi", | Log.w("BusTO-CheckPassaggi", | ||||
Show All 38 Lines | public class Palina extends Stop { | ||||
/** | /** | ||||
* Add info about the routes already found from another source | * Add info about the routes already found from another source | ||||
* @param additionalRoutes ArrayList of routes to get the info from | * @param additionalRoutes ArrayList of routes to get the info from | ||||
* @return the number of routes modified | * @return the number of routes modified | ||||
*/ | */ | ||||
public int addInfoFromRoutes(List<Route> additionalRoutes){ | public int addInfoFromRoutes(List<Route> additionalRoutes){ | ||||
if(routes == null || routes.size()==0) { | if(routes == null || routes.size()==0) { | ||||
this.routes = new ArrayList<>(additionalRoutes); | this.routes = new ArrayList<>(additionalRoutes); | ||||
buildRoutesString(); | |||||
return routes.size(); | return routes.size(); | ||||
} | } | ||||
int count=0; | int count=0; | ||||
final Calendar c = Calendar.getInstance(); | final Calendar c = Calendar.getInstance(); | ||||
final int todaysInt = c.get(Calendar.DAY_OF_WEEK); | final int todaysInt = c.get(Calendar.DAY_OF_WEEK); | ||||
for(Route r:routes) { | for(Route r:routes) { | ||||
int j = 0; | int j = 0; | ||||
boolean correct = false; | boolean correct = false; | ||||
▲ Show 20 Lines • Show All 42 Lines • ▼ Show 20 Lines | public int addInfoFromRoutes(List<Route> additionalRoutes){ | ||||
if (!correct || selected == null) { | if (!correct || selected == null) { | ||||
Log.w("Palina_mergeRoutes","Cannot match the route with name "+r.getName()); | Log.w("Palina_mergeRoutes","Cannot match the route with name "+r.getName()); | ||||
continue; //we didn't find any match | continue; //we didn't find any match | ||||
} | } | ||||
//found the correct correspondance | //found the correct correspondance | ||||
//MERGE INFO | //MERGE INFO | ||||
if(r.mergeRouteWithAnother(selected)) count++; | if(r.mergeRouteWithAnother(selected)) count++; | ||||
} | } | ||||
if (count> 0) buildRoutesString(); | |||||
return count; | return count; | ||||
} | } | ||||
// /** | // /** | ||||
// * Route with terminus (destinazione) and timetables (passaggi), internal implementation. | // * 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. | // * Contains mostly the same data as the Route public class, but methods are quite different and extending Route doesn't really work, here. | ||||
// */ | // */ | ||||
▲ Show 20 Lines • Show All 110 Lines • ▼ Show 20 Lines | public int getMinNumberOfPassages(){ | ||||
if( routes.size() == 0) min = 0; | if( routes.size() == 0) min = 0; | ||||
else for (Route r : routes){ | else for (Route r : routes){ | ||||
min = Math.min(min,r.numPassaggi()); | min = Math.min(min,r.numPassaggi()); | ||||
} | } | ||||
return min; | return min; | ||||
} | } | ||||
//private void mergeRoute | //private void mergeRoute | ||||
} | } | ||||
No newline at end of file | No newline at end of file |
Public contents are in Creative Commons Attribution-ShareAlike 4.0 (CC-BY-SA) or GNU Free Documentation License (at your option) unless otherwise noted. · Contact / Register