Skip to content
This repository has been archived by the owner on Dec 4, 2022. It is now read-only.

Commit

Permalink
Update examples/tests. Fix load database errors.
Browse files Browse the repository at this point in the history
  • Loading branch information
a1aw committed May 5, 2016
1 parent 64ea997 commit 38010da
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 5 deletions.
46 changes: 44 additions & 2 deletions src/main/java/com/mob41/kmbeta/api/ArrivalManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,17 +96,22 @@ public class ArrivalManager {
private int lang = -1;

//Init

public ArrivalManager(String busno, String stop_code, int bound, int language) throws InvalidArrivalTargetException, CouldNotLoadDatabaseException{
this(busno, stop_code, bound, language, false);
}

/***
* Creates a new <code>ArrivalManager</code> instance.
* @param busno Bus No.
* @param stop_code Bus Stop Code (e.g. WO04N12500), probably it is specified from a BUS DB source.
* @param bound Bus Direction/Bound (1 or 2)
* @param language Language to be selected <code>ArrivalManager.ENGLISH_LANG</code> or <code>ArrivalManager.CHINESE_LANG</code>
* @param loadFromClassPath Load the database file from class-path
* @throws InvalidArrivalTargetException If the specified target arrival was invalid
* @throws CouldNotLoadDatabaseException If the API could not load the database
*/
public ArrivalManager(String busno, String stop_code, int bound, int language) throws InvalidArrivalTargetException, CouldNotLoadDatabaseException{
public ArrivalManager(String busno, String stop_code, int bound, int language, boolean loadFromClassPath) throws InvalidArrivalTargetException, CouldNotLoadDatabaseException{
//Check whether the database in the memory is null or not
if (busstop_pair == null){
//If null, load the database in the directory
Expand All @@ -120,7 +125,7 @@ public ArrivalManager(String busno, String stop_code, int bound, int language) t
long startTime = System.currentTimeMillis();

//Load Database
boolean loaded = loadDatabase(false);
boolean loaded = loadDatabase(loadFromClassPath);

//Save end time in ms (for calculating estimated time)
long endTime = System.currentTimeMillis();
Expand Down Expand Up @@ -463,6 +468,43 @@ public static boolean loadDatabase(boolean fromClassResources){
return loadDatabase(null, fromClassResources);
}

public static String getStopCodeViaStopName(String route, int bound, String stopname){
int routeindex = getBusNoIndex(route);

if (routeindex == -1){
return null;
}

for (int i = 0; i < busstop_pair.get(routeindex).get(bound).size(); i++){
if (busstop_pair.get(routeindex).get(bound).get(i)[4].equals(stopname)){
return busstop_pair.get(routeindex).get(bound).get(i)[2];
}
}
return null;
}

public static String getStopNameViaStopCode(String route, int bound, String stopcode){
int routeindex = getBusNoIndex(route.toUpperCase());
System.out.println("Finding index: " + routeindex + " of " + route);
if (routeindex == -1){
System.out.println("Cannot find route");
return null;
}

bound--;
System.out.println("Need to find the index of: [" + stopcode + "]");
for (int i = 0; i < busstop_pair.get(routeindex).get(bound).size(); i++){
System.out.println("Finding... " + "[" + busstop_pair.get(routeindex).get(bound).get(i)[2] + "] route: " + busstop_pair.get(routeindex).get(bound).get(i)[0] + " bound: " + busstop_pair.get(routeindex).get(bound).get(i)[1]);
if (busstop_pair.get(routeindex).get(bound).get(i)[2].equals(stopcode)){
System.out.println("Found! " + busstop_pair.get(routeindex).get(bound).get(i)[4]);
return busstop_pair.get(routeindex).get(bound).get(i)[4];
}
System.out.println("Nope.");
}
System.out.println("Can't find anything! <->");
return null;
}

private static int getBusNoIndex(String bus_no){
for (int i = 0; i < BUS_NO.length; i++){
if (BUS_NO[i].equals(bus_no)){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public void test() throws InvalidArrivalTargetException, CouldNotLoadDatabaseExc
final String stopcode = "LO02T10000";
final String stopname = "LOK WAH BUS TERMINUS";
final int bound = 1;
ArrivalManager arr = new ArrivalManager(busno, stopcode, bound, ArrivalManager.ENGLISH_LANG);
ArrivalManager arr = new ArrivalManager(busno, stopcode, bound, ArrivalManager.ENGLISH_LANG, false);
arr.fetchNewData(); //This also run getServerTime()
System.out.println("Arrival status at " + stopname + " is " + arr.getArrivalTimeRemaining_Formatted());
System.out.println("Stopname: " + stopname);
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/com/mob41/kmbeta/api/tests/MultiArrivals.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ public class MultiArrivals {
@Test
public void test() throws InvalidArrivalTargetException, CouldNotLoadDatabaseException{
MultiArrivalManager mularr = new MultiArrivalManager(50);
ArrivalManager arr0 = new ArrivalManager("1A", "SA06T10000", 1, ArrivalManager.ENGLISH_LANG); //SAU MAU PING (CENTRAL)
ArrivalManager arr1 = new ArrivalManager("1", "CH15T11000", 1, ArrivalManager.ENGLISH_LANG); //CHUK YUEN EST. BUS TERMINUS
ArrivalManager arr0 = new ArrivalManager("1A", "SA06T10000", 1, ArrivalManager.ENGLISH_LANG, false); //SAU MAU PING (CENTRAL)
ArrivalManager arr1 = new ArrivalManager("1", "CH15T11000", 1, ArrivalManager.ENGLISH_LANG, false); //CHUK YUEN EST. BUS TERMINUS
mularr.addArrivalManager(arr0);
mularr.addArrivalManager(arr1);

Expand Down

0 comments on commit 38010da

Please sign in to comment.