OpeningHoursParser cleanup.

This commit is contained in:
Robin Schneider 2013-07-30 21:28:19 +02:00
parent b0cf3f2f5a
commit ec5d0287bf
2 changed files with 65 additions and 51 deletions

1
.gitignore vendored Executable file → Normal file
View file

@ -6,3 +6,4 @@ h_*.png
g_*.png
mm_*.png
mx_*.png
*.class

View file

@ -1,4 +1,5 @@
package net.osmand.util;
/* Has to be commented out in order to run the main function and test the package? */
@ -337,7 +338,7 @@ public class OpeningHoursParser {
@Override
public String toRuleString() {
StringBuilder b = new StringBuilder(25);
{ // Month
// Month
boolean dash = false;
boolean first = true;
for (int i = 0; i < 12; i++) {
@ -360,12 +361,11 @@ public class OpeningHoursParser {
}
if (b.length() != 0) {
b.append(": ");
}
}
// Day
boolean dash = false;
boolean first = true;
boolean open24_7 = true;
dash = false;
first = true;
for (int i = 0; i < 7; i++) {
if (days[i]) {
if (i > 0 && days[i - 1] && i < 6 && days[i + 1]) {
@ -453,7 +453,7 @@ public class OpeningHoursParser {
int previousDay = -1;
int startMonth = -1;
int previousMonth = -1;
int k = 0;
int k = 0; // Position in opening_hours string
BasicOpeningHourRule basic = new BasicOpeningHourRule();
boolean[] days = basic.getDays();
@ -624,6 +624,7 @@ public class OpeningHoursParser {
public static OpeningHours parseOpenedHours(String format){
// split the OSM string in multiple rules
String[] rules = format.split(";"); //$NON-NLS-1$
// FIXME: What if the semicolon is inside a quoted string?
OpeningHours rs = new OpeningHours();
for(String r : rules){
r = r.trim();
@ -652,39 +653,51 @@ public class OpeningHoursParser {
}
/**
* test if the calculated opening hours are what you expect
* @param time the time to test in the format "dd.MM.yyyy HH:mm"
* @param hours the OpeningHours object
* @param expected the expected state
*/
private static void testOpened(String time, OpeningHours hours, boolean expected) throws ParseException {
Calendar cal = Calendar.getInstance();
cal.setTime(new SimpleDateFormat("dd.MM.yyyy HH:mm").parse(time));
System.out.println("Expected " + time+": " + expected +" = " + hours.isOpenedForTime(cal));
boolean isOpen = hours.isOpenedForTime(cal);
System.out.println(" " + (isOpen == expected ? "ok" : "not ok") + " - " + "state for " + time+": " + isOpen);
}
public static void main(String[] args) throws ParseException {
// Test basic case
OpeningHours hours = parseOpenedHours("Mo-Fr 08:30-14:40" ); //$NON-NLS-1$
System.out.println(hours);
testOpened("09.08.2012 11:00", hours, true);
testOpened("09.08.2012 16:00", hours, false);
// two time and date ranges
hours = parseOpenedHours("Mo-We, Fr 08:30-14:40,15:00-19:00"); //$NON-NLS-1$
System.out.println(hours);
testOpened("08.08.2012 14:00", hours, true);
testOpened("10.08.2012 15:00", hours, true);
testOpened("08.08.2012 14:50", hours, false);
testOpened("10.08.2012 15:00", hours, true);
// test exception on general schema
hours = parseOpenedHours("Mo-Sa 08:30-14:40; Tu 08:00 - 14:00"); //$NON-NLS-1$
System.out.println(hours);
testOpened("07.08.2012 14:20", hours, false);
// test off value
hours = parseOpenedHours("Mo-Sa 09:00-18:25; Th off"); //$NON-NLS-1$
System.out.println(hours);
testOpened("08.08.2012 12:00", hours, true);
testOpened("09.08.2012 12:00", hours, false);
//test 24/7
hours = parseOpenedHours("24/7"); //$NON-NLS-1$
System.out.println(hours);
testOpened("08.08.2012 23:59", hours, true);
testOpened("08.08.2012 12:23", hours, true);
testOpened("08.08.2012 06:23", hours, true);
// some people seem to use the following syntax:
hours = parseOpenedHours("Sa-Su 24/7");
System.out.println(hours);