2010-08-17 00:36:24 +02:00
|
|
|
package net.osmand.osm;
|
2010-07-05 20:19:03 +02:00
|
|
|
|
|
|
|
import java.util.ArrayList;
|
2010-07-06 08:19:15 +02:00
|
|
|
import java.util.Calendar;
|
2010-07-05 20:19:03 +02:00
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
public class OpeningHoursParser {
|
|
|
|
private static final String[] daysStr = new String[] {"Mo", "Tu", "We", "Th", "Fr", "Sa", "Su"}; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$
|
2010-07-06 08:19:15 +02:00
|
|
|
|
|
|
|
public static interface OpeningHoursRule {
|
|
|
|
|
|
|
|
public boolean isOpenedForTime(Calendar cal);
|
|
|
|
|
|
|
|
public String toRuleString();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static class BasicDayOpeningHourRule implements OpeningHoursRule {
|
|
|
|
private boolean[] days = new boolean[7];
|
|
|
|
private int startTime = -1;
|
|
|
|
private int endTime = - 1;
|
|
|
|
|
|
|
|
public boolean[] getDays() {
|
|
|
|
return days;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setStartTime(int startTime) {
|
|
|
|
this.startTime = startTime;
|
|
|
|
}
|
|
|
|
public int getStartTime() {
|
|
|
|
return startTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getEndTime() {
|
|
|
|
return endTime;
|
|
|
|
}
|
|
|
|
public void setEndTime(int endTime) {
|
|
|
|
this.endTime = endTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isOpenedForTime(Calendar cal) {
|
|
|
|
if(startTime == -1){
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
int i = cal.get(Calendar.DAY_OF_WEEK);
|
|
|
|
int d = (i + 5) % 7;
|
|
|
|
int p = d - 1;
|
|
|
|
if(p < 0){
|
|
|
|
p+=7;
|
|
|
|
}
|
2010-07-07 08:42:17 +02:00
|
|
|
int time = cal.get(Calendar.HOUR_OF_DAY) * 60 + cal.get(Calendar.MINUTE);
|
2010-07-06 08:19:15 +02:00
|
|
|
// one day working 10 - 20 (not 20 - 04)
|
|
|
|
if(startTime < endTime || endTime == -1){
|
|
|
|
if(days[d]){
|
|
|
|
if(time >= startTime && (endTime == -1 || time <= endTime)){
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
} else {
|
2010-08-03 15:21:44 +02:00
|
|
|
if (time >= startTime && days[p]) {
|
2010-07-06 08:19:15 +02:00
|
|
|
// check in previous day
|
|
|
|
return true;
|
2010-08-03 15:21:44 +02:00
|
|
|
} else if (time <= endTime && days[d]) {
|
2010-07-06 08:19:15 +02:00
|
|
|
// check in previous day
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@Override
|
|
|
|
public String toRuleString() {
|
|
|
|
StringBuilder b = new StringBuilder(25);
|
|
|
|
boolean dash = false;
|
|
|
|
boolean first = true;
|
|
|
|
for(int i=0; i< 7; i++){
|
|
|
|
if (days[i]) {
|
|
|
|
if (i > 0 && days[i - 1] && i < 6 && days[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(daysStr[i]);
|
|
|
|
dash = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
int stHour = startTime / 60;
|
|
|
|
int stTime = startTime - stHour * 60;
|
|
|
|
int enHour = endTime / 60;
|
|
|
|
int enTime = endTime - enHour * 60;
|
|
|
|
b.append(" "); //$NON-NLS-1$
|
|
|
|
formatTime(stHour, stTime, b);
|
|
|
|
b.append("-"); //$NON-NLS-1$
|
|
|
|
formatTime(enHour, enTime, b);
|
|
|
|
return b.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String toString() {
|
|
|
|
return toRuleString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static OpeningHoursRule parseRule(String r){
|
2010-07-05 20:19:03 +02:00
|
|
|
int startDay = -1;
|
|
|
|
int previousDay = -1;
|
2010-07-06 08:19:15 +02:00
|
|
|
BasicDayOpeningHourRule basic = new BasicDayOpeningHourRule();
|
2010-07-05 20:19:03 +02:00
|
|
|
int k = 0;
|
2010-07-06 08:19:15 +02:00
|
|
|
boolean[] days = basic.getDays();
|
2010-07-05 20:19:03 +02:00
|
|
|
for (; k < r.length(); k++) {
|
|
|
|
char ch = r.charAt(k);
|
|
|
|
if (Character.isDigit(ch)) {
|
|
|
|
// time starts
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if(Character.isWhitespace(ch) || ch == ','){
|
|
|
|
continue;
|
|
|
|
} else if(ch == '-'){
|
|
|
|
if(previousDay != -1){
|
|
|
|
startDay = previousDay;
|
|
|
|
} else {
|
2010-07-06 08:19:15 +02:00
|
|
|
return null;
|
2010-07-05 20:19:03 +02:00
|
|
|
}
|
|
|
|
} else if(k < r.length() - 1){
|
|
|
|
int i = 0;
|
|
|
|
for(String s : daysStr){
|
|
|
|
if(s.charAt(0) == ch && s.charAt(1) == r.charAt(k+1)){
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
if(i < daysStr.length){
|
|
|
|
if(startDay != -1){
|
|
|
|
for (int j = startDay; j <= i; j++) {
|
|
|
|
days[j] = true;
|
|
|
|
}
|
|
|
|
startDay = -1;
|
|
|
|
} else {
|
|
|
|
days[i] = true;
|
|
|
|
}
|
|
|
|
previousDay = i;
|
|
|
|
}
|
|
|
|
} else {
|
2010-07-06 08:19:15 +02:00
|
|
|
return null;
|
2010-07-05 20:19:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if(previousDay == -1){
|
2010-07-06 08:19:15 +02:00
|
|
|
return null;
|
2010-07-05 20:19:03 +02:00
|
|
|
}
|
|
|
|
String time = r.substring(k);
|
|
|
|
String[] stEnd = time.split("-"); //$NON-NLS-1$
|
|
|
|
if(stEnd.length != 2){
|
2010-07-06 08:19:15 +02:00
|
|
|
return null;
|
2010-07-05 20:19:03 +02:00
|
|
|
}
|
|
|
|
int st;
|
|
|
|
int end;
|
|
|
|
try {
|
|
|
|
int i1 = stEnd[0].indexOf(':');
|
|
|
|
int i2 = stEnd[1].indexOf(':');
|
2010-08-04 23:07:15 +02:00
|
|
|
if(i1 == -1 || i2 == -1){
|
|
|
|
return null;
|
|
|
|
}
|
2010-07-05 20:19:03 +02:00
|
|
|
st = Integer.parseInt(stEnd[0].substring(0, i1).trim())* 60 + Integer.parseInt(stEnd[0].substring(i1 + 1).trim());
|
|
|
|
end = Integer.parseInt(stEnd[1].substring(0, i2).trim())* 60 + Integer.parseInt(stEnd[1].substring(i2 + 1).trim());
|
|
|
|
} catch (NumberFormatException e) {
|
2010-07-06 08:19:15 +02:00
|
|
|
return null;
|
2010-07-05 20:19:03 +02:00
|
|
|
}
|
2010-07-06 08:19:15 +02:00
|
|
|
basic.setStartTime(st);
|
|
|
|
basic.setEndTime(end);
|
|
|
|
return basic;
|
2010-07-05 20:19:03 +02:00
|
|
|
}
|
|
|
|
|
2010-07-06 08:19:15 +02:00
|
|
|
|
|
|
|
public static List<OpeningHoursRule> parseOpenedHours(String format){
|
2010-07-05 20:19:03 +02:00
|
|
|
String[] rules = format.split(";"); //$NON-NLS-1$
|
2010-07-06 08:19:15 +02:00
|
|
|
List<OpeningHoursRule> rs = new ArrayList<OpeningHoursRule>();
|
2010-07-05 20:19:03 +02:00
|
|
|
for(String r : rules){
|
|
|
|
r = r.trim();
|
|
|
|
if(r.length() == 0){
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// check if valid
|
2010-07-06 08:19:15 +02:00
|
|
|
OpeningHoursRule rule = parseRule(r);
|
|
|
|
if(rule == null){
|
2010-07-05 20:19:03 +02:00
|
|
|
return null;
|
|
|
|
}
|
2010-07-06 08:19:15 +02:00
|
|
|
rs.add(rule);
|
|
|
|
|
2010-07-05 20:19:03 +02:00
|
|
|
}
|
2010-07-06 08:19:15 +02:00
|
|
|
return rs;
|
2010-07-05 20:19:03 +02:00
|
|
|
}
|
2010-07-06 08:19:15 +02:00
|
|
|
public static String toStringOpenedHours(List<? extends OpeningHoursRule> rules){
|
2010-07-05 20:19:03 +02:00
|
|
|
StringBuilder b = new StringBuilder(100);
|
|
|
|
boolean first = true;
|
2010-07-06 08:19:15 +02:00
|
|
|
for (OpeningHoursRule p : rules) {
|
|
|
|
if(p == null){
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (first) {
|
2010-07-05 20:19:03 +02:00
|
|
|
first = false;
|
|
|
|
} else {
|
|
|
|
b.append("; "); //$NON-NLS-1$
|
|
|
|
}
|
2010-07-06 08:19:15 +02:00
|
|
|
b.append(p.toRuleString());
|
2010-07-05 20:19:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return b.toString();
|
|
|
|
}
|
|
|
|
private static void formatTime(int h, int t, StringBuilder b){
|
|
|
|
if(h < 10){
|
|
|
|
b.append("0"); //$NON-NLS-1$
|
|
|
|
}
|
|
|
|
b.append(h).append(":"); //$NON-NLS-1$
|
|
|
|
if(t < 10){
|
|
|
|
b.append("0"); //$NON-NLS-1$
|
|
|
|
}
|
|
|
|
b.append(t);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void main(String[] args) {
|
2010-07-06 08:19:15 +02:00
|
|
|
List<OpeningHoursRule> hours = parseOpenedHours("Mo-Fr 08:30-14:40; Sa 08:00 - 14:00"); //$NON-NLS-1$
|
|
|
|
System.out.println(hours);
|
2010-07-05 20:19:03 +02:00
|
|
|
System.out.println(toStringOpenedHours(hours));
|
2010-07-06 08:19:15 +02:00
|
|
|
hours = parseOpenedHours("Mo, We-Fr, Th, Sa 08:30-14:40; Sa 08:00 - 14:00"); //$NON-NLS-1$
|
|
|
|
System.out.println(hours);
|
2010-07-05 20:19:03 +02:00
|
|
|
System.out.println(toStringOpenedHours(hours));
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|