Permalink
Browse files

Use an enum to provide all possible day values for weekday instances

  • Loading branch information...
benfortuna committed Jun 26, 2016
1 parent 5aa13ce commit f894c0f9e5fdabffccf94fa01a68ed2d863647a3
@@ -166,7 +166,7 @@
private NumberList setPosList;
private String weekStartDay;
private WeekDay.Day weekStartDay;
private int calendarWeekStartDay;
@@ -229,8 +229,8 @@ public Recur(final String aValue) throws ParseException {
} else if (BYSETPOS.equals(token)) {
setPosList = new NumberList(nextToken(t, token), 1, 366, true);
} else if (WKST.equals(token)) {
weekStartDay = nextToken(t, token);
calendarWeekStartDay = WeekDay.getCalendarDay(new WeekDay(weekStartDay));
weekStartDay = WeekDay.Day.valueOf(nextToken(t, token));
calendarWeekStartDay = WeekDay.getCalendarDay(WeekDay.getWeekDay(weekStartDay));
} else {
if (CompatibilityHints.isHintEnabled(CompatibilityHints.KEY_RELAXED_PARSING)) {
// assume experimental value..
@@ -404,17 +404,17 @@ public final Date getUntil() {
/**
* @return Returns the weekStartDay or null if there is none.
*/
public final String getWeekStartDay() {
public final WeekDay.Day getWeekStartDay() {
return weekStartDay;
}
/**
* @param weekStartDay The weekStartDay to set.
*/
public final void setWeekStartDay(final String weekStartDay) {
public final void setWeekStartDay(final WeekDay.Day weekStartDay) {
this.weekStartDay = weekStartDay;
if (weekStartDay != null) {
calendarWeekStartDay = WeekDay.getCalendarDay(new WeekDay(weekStartDay));
calendarWeekStartDay = WeekDay.getCalendarDay(WeekDay.getWeekDay(weekStartDay));
}
}
@@ -1046,9 +1046,8 @@ private DateList getDayVariants(final DateList dates) {
* offsets are from 1 to the size of the list. If an invalid offset is supplied, all elements from <code>list</code>
* are added to <code>sublist</code>.
*
* @param list
* @param dates
* @param offset
* @param sublist
*/
private List<Date> getOffsetDates(final DateList dates, final int offset) {
if (offset == 0) {
@@ -55,39 +55,41 @@
/**
* Sunday.
*/
public static final WeekDay SU = new WeekDay("SU", 0);
public static final WeekDay SU = new WeekDay(Day.SU, 0);
/**
* Monday.
*/
public static final WeekDay MO = new WeekDay("MO", 0);
public static final WeekDay MO = new WeekDay(Day.MO, 0);
/**
* Tuesday.
*/
public static final WeekDay TU = new WeekDay("TU", 0);
public static final WeekDay TU = new WeekDay(Day.TU, 0);
/**
* Wednesday.
*/
public static final WeekDay WE = new WeekDay("WE", 0);
public static final WeekDay WE = new WeekDay(Day.WE, 0);
/**
* Thursday.
*/
public static final WeekDay TH = new WeekDay("TH", 0);
public static final WeekDay TH = new WeekDay(Day.TH, 0);
/**
* Friday.
*/
public static final WeekDay FR = new WeekDay("FR", 0);
public static final WeekDay FR = new WeekDay(Day.FR, 0);
/**
* Saturday.
*/
public static final WeekDay SA = new WeekDay("SA", 0);
public static final WeekDay SA = new WeekDay(Day.SA, 0);
private String day;
public enum Day { SU, MO, TU, WE, TH, FR, SA }
private Day day;
private int offset;
@@ -101,15 +103,15 @@ public WeekDay(final String value) {
else {
offset = 0;
}
day = value.substring(value.length() - 2);
day = Day.valueOf(value.substring(value.length() - 2));
validateDay();
}
/**
* @param day a string representation of a week day
* @param offset a month offset value
*/
private WeekDay(final String day, final int offset) {
private WeekDay(final Day day, final int offset) {
this.day = day;
this.offset = offset;
}
@@ -139,7 +141,7 @@ private void validateDay() {
/**
* @return Returns the day.
*/
public final String getDay() {
public final Day getDay() {
return day;
}
@@ -161,13 +163,26 @@ public final String toString() {
b.append(getDay());
return b.toString();
}
public static WeekDay getWeekDay(Day day) {
switch (day) {
case SU: return SU;
case MO: return MO;
case TU: return TU;
case WE: return WE;
case TH: return TH;
case FR: return FR;
case SA: return SA;
default: return null;
}
}
/**
* Returns a weekday representation of the specified calendar.
* @param cal a calendar (java.util)
* @return a weekday instance representing the specified calendar
*/
public static final WeekDay getWeekDay(final Calendar cal) {
public static WeekDay getWeekDay(final Calendar cal) {
return new WeekDay(getDay(cal.get(Calendar.DAY_OF_WEEK)), 0);
}
@@ -176,7 +191,7 @@ public static final WeekDay getWeekDay(final Calendar cal) {
* @param cal a calendar (java.util)
* @return a weekday instance representing the specified calendar
*/
public static final WeekDay getMonthlyOffset(final Calendar cal) {
public static WeekDay getMonthlyOffset(final Calendar cal) {
return new WeekDay(getDay(cal.get(Calendar.DAY_OF_WEEK)), cal.get(Calendar.DAY_OF_WEEK_IN_MONTH));
}
@@ -185,7 +200,7 @@ public static final WeekDay getMonthlyOffset(final Calendar cal) {
* @param cal a calendar (java.util)
* @return a weekday instance representing the specified calendar
*/
public static final WeekDay getNegativeMonthlyOffset(final Calendar cal) {
public static WeekDay getNegativeMonthlyOffset(final Calendar cal) {
return new WeekDay(getDay(cal.get(Calendar.DAY_OF_WEEK)), cal.get(Calendar.DAY_OF_WEEK_IN_MONTH) - 6);
}

0 comments on commit f894c0f

Please sign in to comment.