Fix isWeekend() etc. API and implementation. Remove temporary static API from IBMCalendar and create proper API in Calendar.
X-SVN-Rev: 966
This commit is contained in:
parent
14248040ac
commit
5043fda0b0
@ -4,15 +4,14 @@
|
||||
* others. All Rights Reserved.
|
||||
*******************************************************************************
|
||||
* $Source: /xsrl/Nsvn/icu/icu4j/src/com/ibm/icu/dev/test/calendar/IBMCalendarTest.java,v $
|
||||
* $Date: 2000/03/14 18:09:36 $
|
||||
* $Revision: 1.1 $
|
||||
* $Date: 2000/03/21 02:51:43 $
|
||||
* $Revision: 1.2 $
|
||||
*******************************************************************************
|
||||
*/
|
||||
package com.ibm.test.calendar;
|
||||
import com.ibm.test.TestFmwk;
|
||||
import com.ibm.util.*;
|
||||
import java.text.*;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.Locale;
|
||||
|
||||
@ -99,7 +98,7 @@ public class IBMCalendarTest extends TestFmwk {
|
||||
if (data[i+4] != 0) {
|
||||
cal.setTime(new Date(cal.getTime().getTime() + data[i+4]));
|
||||
}
|
||||
boolean isWeekend = IBMCalendar.isWeekend(loc, cal.getTime());
|
||||
boolean isWeekend = cal.isWeekend();
|
||||
boolean ok = isWeekend == (data[i+5] != 0);
|
||||
if (ok) {
|
||||
logln("Ok: " + fmt.format(cal.getTime()) + " isWeekend=" + isWeekend);
|
||||
@ -114,8 +113,9 @@ public class IBMCalendarTest extends TestFmwk {
|
||||
Locale loc = (Locale)DATA2[i2];
|
||||
int[] data = (int[]) DATA2[i2+1];
|
||||
logln("Locale: " + loc);
|
||||
Calendar cal = Calendar.getInstance(loc);
|
||||
for (int i=0; i<data.length; i+=2) {
|
||||
int type = IBMCalendar.getDayOfWeekType(loc, data[i]);
|
||||
int type = cal.getDayOfWeekType(data[i]);
|
||||
int exp = data[i+1];
|
||||
if (type == exp) {
|
||||
logln("Ok: DOW " + data[i] + " type=" + type);
|
||||
|
@ -629,6 +629,32 @@ public abstract class Calendar implements Serializable, Cloneable {
|
||||
*/
|
||||
public final static int PM = 1;
|
||||
|
||||
/**
|
||||
* Value returned by getDayOfWeekType(int dayOfWeek) to indicate a
|
||||
* weekday.
|
||||
*/
|
||||
public static final int WEEKDAY = 0;
|
||||
|
||||
/**
|
||||
* Value returned by getDayOfWeekType(int dayOfWeek) to indicate a
|
||||
* weekend day.
|
||||
*/
|
||||
public static final int WEEKEND = 1;
|
||||
|
||||
/**
|
||||
* Value returned by getDayOfWeekType(int dayOfWeek) to indicate a
|
||||
* day that starts as a weekday and transitions to the weekend.
|
||||
* Call getWeekendTransition() to get the point of transition.
|
||||
*/
|
||||
public static final int WEEKEND_ONSET = 2;
|
||||
|
||||
/**
|
||||
* Value returned by getDayOfWeekType(int dayOfWeek) to indicate a
|
||||
* day that starts as the weekend and transitions to a weekday.
|
||||
* Call getWeekendTransition() to get the point of transition.
|
||||
*/
|
||||
public static final int WEEKEND_CEASE = 3;
|
||||
|
||||
// Internal notes:
|
||||
// Calendar contains two kinds of time representations: current "time" in
|
||||
// milliseconds, and a set of time "fields" representing the current time.
|
||||
@ -725,6 +751,12 @@ public abstract class Calendar implements Serializable, Cloneable {
|
||||
*/
|
||||
private int minimalDaysInFirstWeek;
|
||||
|
||||
// Weekend onset and cease
|
||||
private int weekendOnset;
|
||||
private int weekendOnsetMillis;
|
||||
private int weekendCease;
|
||||
private int weekendCeaseMillis;
|
||||
|
||||
/**
|
||||
* Cache to hold the firstDayOfWeek and minimalDaysInFirstWeek
|
||||
* of a Locale.
|
||||
@ -803,6 +835,7 @@ public abstract class Calendar implements Serializable, Cloneable {
|
||||
|
||||
this.zone = zone;
|
||||
setWeekCountData(aLocale);
|
||||
setWeekendData(aLocale);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -1419,6 +1452,139 @@ public abstract class Calendar implements Serializable, Cloneable {
|
||||
return result;
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
// Weekend support -- determining which days of the week are the weekend
|
||||
// in a given locale
|
||||
//-------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Return whether the given day of the week is a weekday, a
|
||||
* weekend day, or a day that transitions from one to the other,
|
||||
* in this calendar system. If a transition occurs at midnight,
|
||||
* then the days before and after the transition will have the
|
||||
* type WEEKDAY or WEEKEND. If a transition occurs at a time
|
||||
* other than midnight, then the day of the transition will have
|
||||
* the type WEEKEND_ONSET or WEEKEND_CEASE. In this case, the
|
||||
* method getWeekendTransition() will return the point of
|
||||
* transition.
|
||||
* @param dayOfWeek either SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
|
||||
* THURSDAY, FRIDAY, or SATURDAY
|
||||
* @return either WEEKDAY, WEEKEND, WEEKEND_ONSET, or
|
||||
* WEEKEND_CEASE
|
||||
* @exception IllegalArgumentException if dayOfWeek is not
|
||||
* between SUNDAY and SATURDAY, inclusive
|
||||
*/
|
||||
public int getDayOfWeekType(int dayOfWeek) {
|
||||
if (dayOfWeek < SUNDAY || dayOfWeek > SATURDAY) {
|
||||
throw new IllegalArgumentException("Invalid day of week");
|
||||
}
|
||||
if (weekendOnset < weekendCease) {
|
||||
if (dayOfWeek < weekendOnset || dayOfWeek > weekendCease) {
|
||||
return WEEKDAY;
|
||||
}
|
||||
} else {
|
||||
if (dayOfWeek > weekendCease && dayOfWeek < weekendOnset) {
|
||||
return WEEKDAY;
|
||||
}
|
||||
}
|
||||
if (dayOfWeek == weekendOnset) {
|
||||
return (weekendOnsetMillis == 0) ? WEEKEND : WEEKEND_ONSET;
|
||||
}
|
||||
if (dayOfWeek == weekendCease) {
|
||||
return (weekendCeaseMillis == 0) ? WEEKDAY : WEEKEND_CEASE;
|
||||
}
|
||||
return WEEKEND;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the time during the day at which the weekend begins in
|
||||
* this calendar system, if getDayOfWeekType(dayOfWeek) ==
|
||||
* WEEKEND_ONSET, or at which the weekend ends, if
|
||||
* getDayOfWeekType(dayOfWeek) == WEEKEND_CEASE. If
|
||||
* getDayOfWeekType(dayOfWeek) has some other value, then an
|
||||
* exception is thrown.
|
||||
* @param dayOfWeek either SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
|
||||
* THURSDAY, FRIDAY, or SATURDAY
|
||||
* @return the milliseconds after midnight at which the
|
||||
* weekend begins or ends
|
||||
* @exception IllegalArgumentException if dayOfWeek is not
|
||||
* WEEKEND_ONSET or WEEKEND_CEASE
|
||||
*/
|
||||
public int getWeekendTransition(int dayOfWeek) {
|
||||
if (dayOfWeek == weekendOnset) {
|
||||
return weekendOnsetMillis;
|
||||
} else if (dayOfWeek == weekendCease) {
|
||||
return weekendCeaseMillis;
|
||||
}
|
||||
throw new IllegalArgumentException("Not weekend transition day");
|
||||
}
|
||||
|
||||
/**
|
||||
* Return true if the given date and time is in the weekend in
|
||||
* this calendar system. Equivalent to calling setTime() followed
|
||||
* by isWeekend(). Note: This method changes the time this
|
||||
* calendar is set to.
|
||||
* @param date the date and time
|
||||
* @return true if the given date and time is part of the
|
||||
* weekend
|
||||
*/
|
||||
public boolean isWeekend(Date date) {
|
||||
setTime(date);
|
||||
return isWeekend();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return true if this Calendar's current date and time is in the
|
||||
* weekend in this calendar system.
|
||||
* @return true if the given date and time is part of the
|
||||
* weekend
|
||||
*/
|
||||
public boolean isWeekend() {
|
||||
int dow = get(DAY_OF_WEEK);
|
||||
int dowt = getDayOfWeekType(dow);
|
||||
switch (dowt) {
|
||||
case WEEKDAY:
|
||||
return false;
|
||||
case WEEKEND:
|
||||
return true;
|
||||
default: // That is, WEEKEND_ONSET or WEEKEND_CEASE
|
||||
// Use internalGet() because the above call to get() populated
|
||||
// all fields.
|
||||
// [Note: There should be a better way to get millis in day.
|
||||
// For ICU4J, submit request for a MILLIS_IN_DAY field
|
||||
// and a DAY_NUMBER field (could be Julian day #). - aliu]
|
||||
int millisInDay = internalGet(MILLISECOND) + 1000 * (internalGet(SECOND) +
|
||||
60 * (internalGet(MINUTE) + 60 * internalGet(HOUR_OF_DAY)));
|
||||
int transition = getWeekendTransition(dow);
|
||||
return (dowt == WEEKEND_ONSET)
|
||||
? (millisInDay >= transition)
|
||||
: (millisInDay < transition);
|
||||
}
|
||||
// (We can never reach this point.)
|
||||
}
|
||||
|
||||
/**
|
||||
* Read the locale weekend data for the given locale.
|
||||
*
|
||||
* This is the initial placement and format of this data -- it may very
|
||||
* well change in the future. See the locale files themselves for
|
||||
* details.
|
||||
*/
|
||||
private void setWeekendData(Locale loc) {
|
||||
ResourceBundle resource =
|
||||
ResourceBundle.getBundle("com.ibm.util.resources.CalendarData",
|
||||
loc);
|
||||
String[] data = resource.getStringArray("Weekend");
|
||||
weekendOnset = Integer.parseInt(data[0]);
|
||||
weekendOnsetMillis = Integer.parseInt(data[1]);
|
||||
weekendCease = Integer.parseInt(data[2]);
|
||||
weekendCeaseMillis = Integer.parseInt(data[3]);
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
// End of weekend support
|
||||
//-------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Overrides Cloneable
|
||||
*/
|
||||
|
@ -4,15 +4,14 @@
|
||||
* others. All Rights Reserved.
|
||||
*******************************************************************************
|
||||
* $Source: /xsrl/Nsvn/icu/icu4j/src/com/ibm/test/calendar/Attic/IBMCalendarTest.java,v $
|
||||
* $Date: 2000/03/14 18:09:36 $
|
||||
* $Revision: 1.1 $
|
||||
* $Date: 2000/03/21 02:51:43 $
|
||||
* $Revision: 1.2 $
|
||||
*******************************************************************************
|
||||
*/
|
||||
package com.ibm.test.calendar;
|
||||
import com.ibm.test.TestFmwk;
|
||||
import com.ibm.util.*;
|
||||
import java.text.*;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.Locale;
|
||||
|
||||
@ -99,7 +98,7 @@ public class IBMCalendarTest extends TestFmwk {
|
||||
if (data[i+4] != 0) {
|
||||
cal.setTime(new Date(cal.getTime().getTime() + data[i+4]));
|
||||
}
|
||||
boolean isWeekend = IBMCalendar.isWeekend(loc, cal.getTime());
|
||||
boolean isWeekend = cal.isWeekend();
|
||||
boolean ok = isWeekend == (data[i+5] != 0);
|
||||
if (ok) {
|
||||
logln("Ok: " + fmt.format(cal.getTime()) + " isWeekend=" + isWeekend);
|
||||
@ -114,8 +113,9 @@ public class IBMCalendarTest extends TestFmwk {
|
||||
Locale loc = (Locale)DATA2[i2];
|
||||
int[] data = (int[]) DATA2[i2+1];
|
||||
logln("Locale: " + loc);
|
||||
Calendar cal = Calendar.getInstance(loc);
|
||||
for (int i=0; i<data.length; i+=2) {
|
||||
int type = IBMCalendar.getDayOfWeekType(loc, data[i]);
|
||||
int type = cal.getDayOfWeekType(data[i]);
|
||||
int exp = data[i+1];
|
||||
if (type == exp) {
|
||||
logln("Ok: DOW " + data[i] + " type=" + type);
|
||||
|
@ -629,6 +629,32 @@ public abstract class Calendar implements Serializable, Cloneable {
|
||||
*/
|
||||
public final static int PM = 1;
|
||||
|
||||
/**
|
||||
* Value returned by getDayOfWeekType(int dayOfWeek) to indicate a
|
||||
* weekday.
|
||||
*/
|
||||
public static final int WEEKDAY = 0;
|
||||
|
||||
/**
|
||||
* Value returned by getDayOfWeekType(int dayOfWeek) to indicate a
|
||||
* weekend day.
|
||||
*/
|
||||
public static final int WEEKEND = 1;
|
||||
|
||||
/**
|
||||
* Value returned by getDayOfWeekType(int dayOfWeek) to indicate a
|
||||
* day that starts as a weekday and transitions to the weekend.
|
||||
* Call getWeekendTransition() to get the point of transition.
|
||||
*/
|
||||
public static final int WEEKEND_ONSET = 2;
|
||||
|
||||
/**
|
||||
* Value returned by getDayOfWeekType(int dayOfWeek) to indicate a
|
||||
* day that starts as the weekend and transitions to a weekday.
|
||||
* Call getWeekendTransition() to get the point of transition.
|
||||
*/
|
||||
public static final int WEEKEND_CEASE = 3;
|
||||
|
||||
// Internal notes:
|
||||
// Calendar contains two kinds of time representations: current "time" in
|
||||
// milliseconds, and a set of time "fields" representing the current time.
|
||||
@ -725,6 +751,12 @@ public abstract class Calendar implements Serializable, Cloneable {
|
||||
*/
|
||||
private int minimalDaysInFirstWeek;
|
||||
|
||||
// Weekend onset and cease
|
||||
private int weekendOnset;
|
||||
private int weekendOnsetMillis;
|
||||
private int weekendCease;
|
||||
private int weekendCeaseMillis;
|
||||
|
||||
/**
|
||||
* Cache to hold the firstDayOfWeek and minimalDaysInFirstWeek
|
||||
* of a Locale.
|
||||
@ -803,6 +835,7 @@ public abstract class Calendar implements Serializable, Cloneable {
|
||||
|
||||
this.zone = zone;
|
||||
setWeekCountData(aLocale);
|
||||
setWeekendData(aLocale);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -1419,6 +1452,139 @@ public abstract class Calendar implements Serializable, Cloneable {
|
||||
return result;
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
// Weekend support -- determining which days of the week are the weekend
|
||||
// in a given locale
|
||||
//-------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Return whether the given day of the week is a weekday, a
|
||||
* weekend day, or a day that transitions from one to the other,
|
||||
* in this calendar system. If a transition occurs at midnight,
|
||||
* then the days before and after the transition will have the
|
||||
* type WEEKDAY or WEEKEND. If a transition occurs at a time
|
||||
* other than midnight, then the day of the transition will have
|
||||
* the type WEEKEND_ONSET or WEEKEND_CEASE. In this case, the
|
||||
* method getWeekendTransition() will return the point of
|
||||
* transition.
|
||||
* @param dayOfWeek either SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
|
||||
* THURSDAY, FRIDAY, or SATURDAY
|
||||
* @return either WEEKDAY, WEEKEND, WEEKEND_ONSET, or
|
||||
* WEEKEND_CEASE
|
||||
* @exception IllegalArgumentException if dayOfWeek is not
|
||||
* between SUNDAY and SATURDAY, inclusive
|
||||
*/
|
||||
public int getDayOfWeekType(int dayOfWeek) {
|
||||
if (dayOfWeek < SUNDAY || dayOfWeek > SATURDAY) {
|
||||
throw new IllegalArgumentException("Invalid day of week");
|
||||
}
|
||||
if (weekendOnset < weekendCease) {
|
||||
if (dayOfWeek < weekendOnset || dayOfWeek > weekendCease) {
|
||||
return WEEKDAY;
|
||||
}
|
||||
} else {
|
||||
if (dayOfWeek > weekendCease && dayOfWeek < weekendOnset) {
|
||||
return WEEKDAY;
|
||||
}
|
||||
}
|
||||
if (dayOfWeek == weekendOnset) {
|
||||
return (weekendOnsetMillis == 0) ? WEEKEND : WEEKEND_ONSET;
|
||||
}
|
||||
if (dayOfWeek == weekendCease) {
|
||||
return (weekendCeaseMillis == 0) ? WEEKDAY : WEEKEND_CEASE;
|
||||
}
|
||||
return WEEKEND;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the time during the day at which the weekend begins in
|
||||
* this calendar system, if getDayOfWeekType(dayOfWeek) ==
|
||||
* WEEKEND_ONSET, or at which the weekend ends, if
|
||||
* getDayOfWeekType(dayOfWeek) == WEEKEND_CEASE. If
|
||||
* getDayOfWeekType(dayOfWeek) has some other value, then an
|
||||
* exception is thrown.
|
||||
* @param dayOfWeek either SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
|
||||
* THURSDAY, FRIDAY, or SATURDAY
|
||||
* @return the milliseconds after midnight at which the
|
||||
* weekend begins or ends
|
||||
* @exception IllegalArgumentException if dayOfWeek is not
|
||||
* WEEKEND_ONSET or WEEKEND_CEASE
|
||||
*/
|
||||
public int getWeekendTransition(int dayOfWeek) {
|
||||
if (dayOfWeek == weekendOnset) {
|
||||
return weekendOnsetMillis;
|
||||
} else if (dayOfWeek == weekendCease) {
|
||||
return weekendCeaseMillis;
|
||||
}
|
||||
throw new IllegalArgumentException("Not weekend transition day");
|
||||
}
|
||||
|
||||
/**
|
||||
* Return true if the given date and time is in the weekend in
|
||||
* this calendar system. Equivalent to calling setTime() followed
|
||||
* by isWeekend(). Note: This method changes the time this
|
||||
* calendar is set to.
|
||||
* @param date the date and time
|
||||
* @return true if the given date and time is part of the
|
||||
* weekend
|
||||
*/
|
||||
public boolean isWeekend(Date date) {
|
||||
setTime(date);
|
||||
return isWeekend();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return true if this Calendar's current date and time is in the
|
||||
* weekend in this calendar system.
|
||||
* @return true if the given date and time is part of the
|
||||
* weekend
|
||||
*/
|
||||
public boolean isWeekend() {
|
||||
int dow = get(DAY_OF_WEEK);
|
||||
int dowt = getDayOfWeekType(dow);
|
||||
switch (dowt) {
|
||||
case WEEKDAY:
|
||||
return false;
|
||||
case WEEKEND:
|
||||
return true;
|
||||
default: // That is, WEEKEND_ONSET or WEEKEND_CEASE
|
||||
// Use internalGet() because the above call to get() populated
|
||||
// all fields.
|
||||
// [Note: There should be a better way to get millis in day.
|
||||
// For ICU4J, submit request for a MILLIS_IN_DAY field
|
||||
// and a DAY_NUMBER field (could be Julian day #). - aliu]
|
||||
int millisInDay = internalGet(MILLISECOND) + 1000 * (internalGet(SECOND) +
|
||||
60 * (internalGet(MINUTE) + 60 * internalGet(HOUR_OF_DAY)));
|
||||
int transition = getWeekendTransition(dow);
|
||||
return (dowt == WEEKEND_ONSET)
|
||||
? (millisInDay >= transition)
|
||||
: (millisInDay < transition);
|
||||
}
|
||||
// (We can never reach this point.)
|
||||
}
|
||||
|
||||
/**
|
||||
* Read the locale weekend data for the given locale.
|
||||
*
|
||||
* This is the initial placement and format of this data -- it may very
|
||||
* well change in the future. See the locale files themselves for
|
||||
* details.
|
||||
*/
|
||||
private void setWeekendData(Locale loc) {
|
||||
ResourceBundle resource =
|
||||
ResourceBundle.getBundle("com.ibm.util.resources.CalendarData",
|
||||
loc);
|
||||
String[] data = resource.getStringArray("Weekend");
|
||||
weekendOnset = Integer.parseInt(data[0]);
|
||||
weekendOnsetMillis = Integer.parseInt(data[1]);
|
||||
weekendCease = Integer.parseInt(data[2]);
|
||||
weekendCeaseMillis = Integer.parseInt(data[3]);
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
// End of weekend support
|
||||
//-------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Overrides Cloneable
|
||||
*/
|
||||
|
@ -5,8 +5,8 @@
|
||||
*******************************************************************************
|
||||
*
|
||||
* $Source: /xsrl/Nsvn/icu/icu4j/src/com/ibm/util/Attic/IBMCalendar.java,v $
|
||||
* $Date: 2000/03/21 02:19:32 $
|
||||
* $Revision: 1.7 $
|
||||
* $Date: 2000/03/21 02:51:35 $
|
||||
* $Revision: 1.8 $
|
||||
*
|
||||
*****************************************************************************************
|
||||
*/
|
||||
@ -99,32 +99,6 @@ import com.ibm.text.SimpleDateFormat;
|
||||
*/
|
||||
public abstract class IBMCalendar extends Calendar {
|
||||
|
||||
/**
|
||||
* Value returned by getDayOfWeekType(int dayOfWeek) to indicate a
|
||||
* weekday.
|
||||
*/
|
||||
public static final int WEEKDAY = 0;
|
||||
|
||||
/**
|
||||
* Value returned by getDayOfWeekType(int dayOfWeek) to indicate a
|
||||
* weekend day.
|
||||
*/
|
||||
public static final int WEEKEND = 1;
|
||||
|
||||
/**
|
||||
* Value returned by getDayOfWeekType(int dayOfWeek) to indicate a
|
||||
* day that starts as a weekday and transitions to the weekend.
|
||||
* Call getWeekendTransition() to get the point of transition.
|
||||
*/
|
||||
public static final int WEEKEND_ONSET = 2;
|
||||
|
||||
/**
|
||||
* Value returned by getDayOfWeekType(int dayOfWeek) to indicate a
|
||||
* day that starts as the weekend and transitions to a weekday.
|
||||
* Call getWeekendTransition() to get the point of transition.
|
||||
*/
|
||||
public static final int WEEKEND_CEASE = 3;
|
||||
|
||||
private static String copyright = "Copyright \u00a9 1997-1998 IBM Corp. All Rights Reserved.";
|
||||
|
||||
protected IBMCalendar(TimeZone zone, Locale aLocale) {
|
||||
@ -1256,256 +1230,6 @@ public abstract class IBMCalendar extends Calendar {
|
||||
return result;
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
// Weekend support -- determining which days of the week are the weekend
|
||||
// in a given locale
|
||||
//
|
||||
// THIS SECTION IS WORK IN PROGRESS -- ANY CODE USING IT WILL HAVE TO BE
|
||||
// CHANGED IN THE FUTURE.
|
||||
//-------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Single cached Weekend object; improves performance.
|
||||
*/
|
||||
private static Weekend cachedWeekend = null;
|
||||
|
||||
/**
|
||||
* TEMPORARY API AND IMPLEMENTATION
|
||||
*
|
||||
* Return whether the given day of the week is a weekday, a
|
||||
* weekend day, or a day that transitions from one to the other,
|
||||
* in this calendar system. If a transition occurs at midnight,
|
||||
* then the days before and after the transition will have the
|
||||
* type WEEKDAY or WEEKEND. If a transition occurs at a time
|
||||
* other than midnight, then the day of the transition will have
|
||||
* the type WEEKEND_ONSET or WEEKEND_CEASE. In this case, the
|
||||
* method getWeekendTransition() will return the point of
|
||||
* transition.
|
||||
* @param dayOfWeek either SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
|
||||
* THURSDAY, FRIDAY, or SATURDAY
|
||||
* @return either WEEKDAY, WEEKEND, WEEKEND_ONSET, or
|
||||
* WEEKEND_CEASE
|
||||
* @exception IllegalArgumentException if dayOfWeek is not
|
||||
* between SUNDAY and SATURDAY, inclusive
|
||||
*/
|
||||
public static int getDayOfWeekType(Locale loc, int dayOfWeek) {
|
||||
if (cachedWeekend == null ||
|
||||
cachedWeekend.getLocale() != loc) {
|
||||
cachedWeekend = new Weekend(loc);
|
||||
}
|
||||
return cachedWeekend.getDayOfWeekType(dayOfWeek);
|
||||
}
|
||||
|
||||
/**
|
||||
* TEMPORARY API AND IMPLEMENTATION
|
||||
*
|
||||
* Return the time during the day at which the weekend begins in
|
||||
* this calendar system, if getDayOfWeekType(dayOfWeek) ==
|
||||
* WEEKEND_ONSET, or at which the weekend ends, if
|
||||
* getDayOfWeekType(dayOfWeek) == WEEKEND_CEASE. If
|
||||
* getDayOfWeekType(dayOfWeek) has some other value, then an
|
||||
* exception is thrown.
|
||||
* @param dayOfWeek either SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
|
||||
* THURSDAY, FRIDAY, or SATURDAY
|
||||
* @return the milliseconds after midnight at which the
|
||||
* weekend begins or ends
|
||||
* @exception IllegalArgumentException if dayOfWeek is not
|
||||
* WEEKEND_ONSET or WEEKEND_CEASE
|
||||
*/
|
||||
public static int getWeekendTransition(Locale loc, int dayOfWeek) {
|
||||
if (cachedWeekend == null ||
|
||||
cachedWeekend.getLocale() != loc) {
|
||||
cachedWeekend = new Weekend(loc);
|
||||
}
|
||||
return cachedWeekend.getWeekendTransition(dayOfWeek);
|
||||
}
|
||||
|
||||
/**
|
||||
* TEMPORARY API AND IMPLEMENTATION
|
||||
*
|
||||
* Return true if the given date and time is in the weekend in
|
||||
* this calendar system. Equivalent to calling setTime() followed
|
||||
* by isWeekend(). Note: This method changes the time this
|
||||
* calendar is set to.
|
||||
* @param date the date and time
|
||||
* @return true if the given date and time is part of the
|
||||
* weekend
|
||||
*/
|
||||
public static boolean isWeekend(Locale loc, Date date) {
|
||||
if (cachedWeekend == null ||
|
||||
cachedWeekend.getLocale() != loc) {
|
||||
cachedWeekend = new Weekend(loc);
|
||||
}
|
||||
return cachedWeekend.isWeekend(date);
|
||||
}
|
||||
|
||||
/**
|
||||
* ********************** TEMPORARY **************************************
|
||||
*
|
||||
* Wrapper for temporary implementation. In the future, these methods and
|
||||
* fields will migrate to the IBM Calendar class itself. This is not
|
||||
* done at this point because the hierarchy is currently in flux.
|
||||
*
|
||||
* ********************** TEMPORARY **************************************
|
||||
*/
|
||||
static class Weekend {
|
||||
|
||||
Locale loc;
|
||||
Calendar cal;
|
||||
int weekendOnset;
|
||||
int weekendOnsetMillis;
|
||||
int weekendCease;
|
||||
int weekendCeaseMillis;
|
||||
|
||||
public Weekend(Locale loc, Calendar cal) {
|
||||
loadWeekendData(loc);
|
||||
this.loc = loc;
|
||||
this.cal = cal;
|
||||
}
|
||||
|
||||
public Weekend(Locale loc) {
|
||||
this(loc, null);
|
||||
}
|
||||
|
||||
public Weekend() {
|
||||
this(Locale.getDefault(), null);
|
||||
}
|
||||
|
||||
public Locale getLocale() {
|
||||
return loc;
|
||||
}
|
||||
|
||||
/**
|
||||
* Read the locale weekend data for the given locale.
|
||||
*
|
||||
* This is the initial placement and format of this data -- it may very
|
||||
* well change in the future. See the locale files themselves for
|
||||
* details.
|
||||
*/
|
||||
private void loadWeekendData(Locale loc) {
|
||||
ResourceBundle resource =
|
||||
ResourceBundle.getBundle("com.ibm.util.resources.CalendarData",
|
||||
loc);
|
||||
String[] data = resource.getStringArray("Weekend");
|
||||
weekendOnset = Integer.parseInt(data[0]);
|
||||
weekendOnsetMillis = Integer.parseInt(data[1]);
|
||||
weekendCease = Integer.parseInt(data[2]);
|
||||
weekendCeaseMillis = Integer.parseInt(data[3]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return whether the given day of the week is a weekday, a
|
||||
* weekend day, or a day that transitions from one to the other,
|
||||
* in this calendar system. If a transition occurs at midnight,
|
||||
* then the days before and after the transition will have the
|
||||
* type WEEKDAY or WEEKEND. If a transition occurs at a time
|
||||
* other than midnight, then the day of the transition will have
|
||||
* the type WEEKEND_ONSET or WEEKEND_CEASE. In this case, the
|
||||
* method getWeekendTransition() will return the point of
|
||||
* transition.
|
||||
* @param dayOfWeek either SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
|
||||
* THURSDAY, FRIDAY, or SATURDAY
|
||||
* @return either WEEKDAY, WEEKEND, WEEKEND_ONSET, or
|
||||
* WEEKEND_CEASE
|
||||
* @exception IllegalArgumentException if dayOfWeek is not
|
||||
* between SUNDAY and SATURDAY, inclusive
|
||||
*/
|
||||
public int getDayOfWeekType(int dayOfWeek) {
|
||||
if (dayOfWeek < SUNDAY || dayOfWeek > SATURDAY) {
|
||||
throw new IllegalArgumentException("Invalid day of week");
|
||||
}
|
||||
if (weekendOnset < weekendCease) {
|
||||
if (dayOfWeek < weekendOnset || dayOfWeek > weekendCease) {
|
||||
return WEEKDAY;
|
||||
}
|
||||
} else {
|
||||
if (dayOfWeek > weekendCease && dayOfWeek < weekendOnset) {
|
||||
return WEEKDAY;
|
||||
}
|
||||
}
|
||||
if (dayOfWeek == weekendOnset) {
|
||||
return (weekendOnsetMillis == 0) ? WEEKEND : WEEKEND_ONSET;
|
||||
}
|
||||
if (dayOfWeek == weekendCease) {
|
||||
return (weekendCeaseMillis == 0) ? WEEKDAY : WEEKEND_CEASE;
|
||||
}
|
||||
return WEEKEND;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the time during the day at which the weekend begins in
|
||||
* this calendar system, if getDayOfWeekType(dayOfWeek) ==
|
||||
* WEEKEND_ONSET, or at which the weekend ends, if
|
||||
* getDayOfWeekType(dayOfWeek) == WEEKEND_CEASE. If
|
||||
* getDayOfWeekType(dayOfWeek) has some other value, then an
|
||||
* exception is thrown.
|
||||
* @param dayOfWeek either SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
|
||||
* THURSDAY, FRIDAY, or SATURDAY
|
||||
* @return the milliseconds after midnight at which the
|
||||
* weekend begins or ends
|
||||
* @exception IllegalArgumentException if dayOfWeek is not
|
||||
* WEEKEND_ONSET or WEEKEND_CEASE
|
||||
*/
|
||||
public int getWeekendTransition(int dayOfWeek) {
|
||||
if (dayOfWeek == weekendOnset) {
|
||||
return weekendOnsetMillis;
|
||||
} else if (dayOfWeek == weekendCease) {
|
||||
return weekendCeaseMillis;
|
||||
}
|
||||
throw new IllegalArgumentException("Not weekend transition day");
|
||||
}
|
||||
|
||||
/**
|
||||
* Return true if the given date and time is in the weekend in
|
||||
* this calendar system. Equivalent to calling setTime() followed
|
||||
* by isWeekend(). Note: This method changes the time this
|
||||
* calendar is set to.
|
||||
* @param date the date and time
|
||||
* @return true if the given date and time is part of the
|
||||
* weekend
|
||||
*/
|
||||
public boolean isWeekend(Date date) {
|
||||
if (cal == null) {
|
||||
cal = Calendar.getInstance(loc);
|
||||
}
|
||||
cal.setTime(date);
|
||||
return isWeekend();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return true if this Calendar's current date and time is in the
|
||||
* weekend in this calendar system.
|
||||
* @return true if the given date and time is part of the
|
||||
* weekend
|
||||
*/
|
||||
public boolean isWeekend() {
|
||||
if (cal == null) {
|
||||
cal = Calendar.getInstance(loc);
|
||||
}
|
||||
int dow = cal.get(DAY_OF_WEEK);
|
||||
int dowt = getDayOfWeekType(dow);
|
||||
switch (dowt) {
|
||||
case WEEKDAY:
|
||||
return false;
|
||||
case WEEKEND:
|
||||
return true;
|
||||
default: // That is, WEEKEND_ONSET or WEEKEND_CEASE
|
||||
// Use internalGet() because the above call to get() populated
|
||||
// all fields.
|
||||
// [Note: There should be a better way to get millis in day.
|
||||
// For ICU4J, submit request for a MILLIS_IN_DAY field
|
||||
// and a DAY_NUMBER field (could be Julian day #). - aliu]
|
||||
int millisInDay = cal.get(MILLISECOND) + 1000 * (cal.get(SECOND) +
|
||||
60 * (cal.get(MINUTE) + 60 * cal.get(HOUR_OF_DAY)));
|
||||
int transition = getWeekendTransition(dow);
|
||||
return (dowt == WEEKEND_ONSET)
|
||||
? (millisInDay >= transition)
|
||||
: (millisInDay < transition);
|
||||
}
|
||||
// (We can never reach this point.)
|
||||
}
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
// Protected utility methods for use by subclasses. These are very handy
|
||||
// for implementing add, roll, and computeFields.
|
||||
|
Loading…
Reference in New Issue
Block a user