OpeningHoursParser cleanup.
This commit is contained in:
parent
b0cf3f2f5a
commit
ec5d0287bf
2 changed files with 65 additions and 51 deletions
1
.gitignore
vendored
Executable file → Normal file
1
.gitignore
vendored
Executable file → Normal file
|
@ -6,3 +6,4 @@ h_*.png
|
||||||
g_*.png
|
g_*.png
|
||||||
mm_*.png
|
mm_*.png
|
||||||
mx_*.png
|
mx_*.png
|
||||||
|
*.class
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
package net.osmand.util;
|
package net.osmand.util;
|
||||||
|
/* Has to be commented out in order to run the main function and test the package? */
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -337,35 +338,34 @@ public class OpeningHoursParser {
|
||||||
@Override
|
@Override
|
||||||
public String toRuleString() {
|
public String toRuleString() {
|
||||||
StringBuilder b = new StringBuilder(25);
|
StringBuilder b = new StringBuilder(25);
|
||||||
{ // Month
|
// Month
|
||||||
boolean dash = false;
|
boolean dash = false;
|
||||||
boolean first = true;
|
|
||||||
for (int i = 0; i < 12; i++) {
|
|
||||||
if (months[i]) {
|
|
||||||
if (i > 0 && months[i - 1] && i < 11 && months[i + 1]) {
|
|
||||||
if (!dash) {
|
|
||||||
dash = true;
|
|
||||||
b.append("-"); //$NON-NLS-1$
|
|
||||||
}
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (first) {
|
|
||||||
first = false;
|
|
||||||
} else if (!dash) {
|
|
||||||
b.append(", "); //$NON-NLS-1$
|
|
||||||
}
|
|
||||||
b.append(monthsStr[i]);
|
|
||||||
dash = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (b.length() != 0) {
|
|
||||||
b.append(": ");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Day
|
|
||||||
boolean dash = false;
|
|
||||||
boolean first = true;
|
boolean first = true;
|
||||||
|
for (int i = 0; i < 12; i++) {
|
||||||
|
if (months[i]) {
|
||||||
|
if (i > 0 && months[i - 1] && i < 11 && months[i + 1]) {
|
||||||
|
if (!dash) {
|
||||||
|
dash = true;
|
||||||
|
b.append("-"); //$NON-NLS-1$
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (first) {
|
||||||
|
first = false;
|
||||||
|
} else if (!dash) {
|
||||||
|
b.append(", "); //$NON-NLS-1$
|
||||||
|
}
|
||||||
|
b.append(monthsStr[i]);
|
||||||
|
dash = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (b.length() != 0) {
|
||||||
|
b.append(": ");
|
||||||
|
}
|
||||||
|
// Day
|
||||||
boolean open24_7 = true;
|
boolean open24_7 = true;
|
||||||
|
dash = false;
|
||||||
|
first = true;
|
||||||
for (int i = 0; i < 7; i++) {
|
for (int i = 0; i < 7; i++) {
|
||||||
if (days[i]) {
|
if (days[i]) {
|
||||||
if (i > 0 && days[i - 1] && i < 6 && days[i + 1]) {
|
if (i > 0 && days[i - 1] && i < 6 && days[i + 1]) {
|
||||||
|
@ -423,16 +423,16 @@ public class OpeningHoursParser {
|
||||||
public void addTimeRange(int startTime, int endTime) {
|
public void addTimeRange(int startTime, int endTime) {
|
||||||
int l = startTimes.length;
|
int l = startTimes.length;
|
||||||
int[] newStartTimes = new int[l + 1];
|
int[] newStartTimes = new int[l + 1];
|
||||||
int[] newEndTimes = new int[l + 1];
|
int[] newEndTimes = new int[l + 1];
|
||||||
for (int i = 0; i < l; i++) {
|
for (int i = 0; i < l; i++) {
|
||||||
newStartTimes[i] = startTimes[i];
|
newStartTimes[i] = startTimes[i];
|
||||||
newEndTimes[i] = endTimes[i];
|
newEndTimes[i] = endTimes[i];
|
||||||
}
|
}
|
||||||
newStartTimes[l] = startTime;
|
newStartTimes[l] = startTime;
|
||||||
newEndTimes[l] = endTime;
|
newEndTimes[l] = endTime;
|
||||||
|
|
||||||
startTimes = newStartTimes;
|
startTimes = newStartTimes;
|
||||||
endTimes = newEndTimes;
|
endTimes = newEndTimes;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -449,14 +449,14 @@ public class OpeningHoursParser {
|
||||||
r = r.replaceAll("sunrise", sunrise);
|
r = r.replaceAll("sunrise", sunrise);
|
||||||
// replace the '+' by an arbitrary value
|
// replace the '+' by an arbitrary value
|
||||||
r = r.replaceAll("\\+", "-" + endOfDay);
|
r = r.replaceAll("\\+", "-" + endOfDay);
|
||||||
int startDay = -1;
|
int startDay = -1;
|
||||||
int previousDay = -1;
|
int previousDay = -1;
|
||||||
int startMonth = -1;
|
int startMonth = -1;
|
||||||
int previousMonth = -1;
|
int previousMonth = -1;
|
||||||
int k = 0;
|
int k = 0; // Position in opening_hours string
|
||||||
|
|
||||||
BasicOpeningHourRule basic = new BasicOpeningHourRule();
|
BasicOpeningHourRule basic = new BasicOpeningHourRule();
|
||||||
boolean[] days = basic.getDays();
|
boolean[] days = basic.getDays();
|
||||||
boolean[] months = basic.getMonths();
|
boolean[] months = basic.getMonths();
|
||||||
// check 24/7
|
// check 24/7
|
||||||
if("24/7".equals(r)){
|
if("24/7".equals(r)){
|
||||||
|
@ -601,8 +601,8 @@ public class OpeningHoursParser {
|
||||||
endHour = Integer.parseInt(stEnd[1].substring(0, i2).trim());
|
endHour = Integer.parseInt(stEnd[1].substring(0, i2).trim());
|
||||||
endMin = Integer.parseInt(stEnd[1].substring(i2 + 1).trim());
|
endMin = Integer.parseInt(stEnd[1].substring(i2 + 1).trim());
|
||||||
}
|
}
|
||||||
st = startHour * 60 + startMin;
|
st = startHour * 60 + startMin;
|
||||||
end = endHour * 60 + endMin;
|
end = endHour * 60 + endMin;
|
||||||
} catch (NumberFormatException e) {
|
} catch (NumberFormatException e) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -624,15 +624,16 @@ public class OpeningHoursParser {
|
||||||
public static OpeningHours parseOpenedHours(String format){
|
public static OpeningHours parseOpenedHours(String format){
|
||||||
// split the OSM string in multiple rules
|
// split the OSM string in multiple rules
|
||||||
String[] rules = format.split(";"); //$NON-NLS-1$
|
String[] rules = format.split(";"); //$NON-NLS-1$
|
||||||
|
// FIXME: What if the semicolon is inside a quoted string?
|
||||||
OpeningHours rs = new OpeningHours();
|
OpeningHours rs = new OpeningHours();
|
||||||
for(String r : rules){
|
for(String r : rules){
|
||||||
r = r.trim();
|
r = r.trim();
|
||||||
if(r.length() == 0){
|
if (r.length() == 0) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
// check if valid
|
// check if valid
|
||||||
boolean rule = parseRule(r, rs);
|
boolean rule = parseRule(r, rs);
|
||||||
if(!rule){
|
if (!rule) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -641,50 +642,62 @@ public class OpeningHoursParser {
|
||||||
|
|
||||||
|
|
||||||
private static void formatTime(int h, int t, StringBuilder b){
|
private static void formatTime(int h, int t, StringBuilder b){
|
||||||
if(h < 10){
|
if (h < 10) {
|
||||||
b.append("0"); //$NON-NLS-1$
|
b.append("0"); //$NON-NLS-1$
|
||||||
}
|
}
|
||||||
b.append(h).append(":"); //$NON-NLS-1$
|
b.append(h).append(":"); //$NON-NLS-1$
|
||||||
if(t < 10){
|
if (t < 10) {
|
||||||
b.append("0"); //$NON-NLS-1$
|
b.append("0"); //$NON-NLS-1$
|
||||||
}
|
}
|
||||||
b.append(t);
|
b.append(t);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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 {
|
private static void testOpened(String time, OpeningHours hours, boolean expected) throws ParseException {
|
||||||
Calendar cal = Calendar.getInstance();
|
Calendar cal = Calendar.getInstance();
|
||||||
cal.setTime(new SimpleDateFormat("dd.MM.yyyy HH:mm").parse(time));
|
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 {
|
public static void main(String[] args) throws ParseException {
|
||||||
|
// Test basic case
|
||||||
|
|
||||||
//Test basic case
|
|
||||||
OpeningHours hours = parseOpenedHours("Mo-Fr 08:30-14:40" ); //$NON-NLS-1$
|
OpeningHours hours = parseOpenedHours("Mo-Fr 08:30-14:40" ); //$NON-NLS-1$
|
||||||
System.out.println(hours);
|
System.out.println(hours);
|
||||||
testOpened("09.08.2012 11:00", hours, true);
|
testOpened("09.08.2012 11:00", hours, true);
|
||||||
testOpened("09.08.2012 16:00", hours, false);
|
testOpened("09.08.2012 16:00", hours, false);
|
||||||
|
|
||||||
// two time and date ranges
|
// two time and date ranges
|
||||||
hours = parseOpenedHours("Mo-We, Fr 08:30-14:40,15:00-19:00"); //$NON-NLS-1$
|
hours = parseOpenedHours("Mo-We, Fr 08:30-14:40,15:00-19:00"); //$NON-NLS-1$
|
||||||
System.out.println(hours);
|
System.out.println(hours);
|
||||||
testOpened("08.08.2012 14:00", hours, true);
|
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("08.08.2012 14:50", hours, false);
|
||||||
|
testOpened("10.08.2012 15:00", hours, true);
|
||||||
|
|
||||||
// test exception on general schema
|
// test exception on general schema
|
||||||
hours = parseOpenedHours("Mo-Sa 08:30-14:40; Tu 08:00 - 14:00"); //$NON-NLS-1$
|
hours = parseOpenedHours("Mo-Sa 08:30-14:40; Tu 08:00 - 14:00"); //$NON-NLS-1$
|
||||||
System.out.println(hours);
|
System.out.println(hours);
|
||||||
testOpened("07.08.2012 14:20", hours, false);
|
testOpened("07.08.2012 14:20", hours, false);
|
||||||
|
|
||||||
// test off value
|
// test off value
|
||||||
hours = parseOpenedHours("Mo-Sa 09:00-18:25; Th off"); //$NON-NLS-1$
|
hours = parseOpenedHours("Mo-Sa 09:00-18:25; Th off"); //$NON-NLS-1$
|
||||||
System.out.println(hours);
|
System.out.println(hours);
|
||||||
testOpened("08.08.2012 12:00", hours, true);
|
testOpened("08.08.2012 12:00", hours, true);
|
||||||
testOpened("09.08.2012 12:00", hours, false);
|
testOpened("09.08.2012 12:00", hours, false);
|
||||||
|
|
||||||
//test 24/7
|
//test 24/7
|
||||||
hours = parseOpenedHours("24/7"); //$NON-NLS-1$
|
hours = parseOpenedHours("24/7"); //$NON-NLS-1$
|
||||||
System.out.println(hours);
|
System.out.println(hours);
|
||||||
testOpened("08.08.2012 23:59", hours, true);
|
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:
|
// some people seem to use the following syntax:
|
||||||
hours = parseOpenedHours("Sa-Su 24/7");
|
hours = parseOpenedHours("Sa-Su 24/7");
|
||||||
System.out.println(hours);
|
System.out.println(hours);
|
||||||
|
|
Loading…
Reference in a new issue