edu.columbia.cpl
Class DayAndPosition

java.lang.Object
  extended byedu.columbia.cpl.DayAndPosition
All Implemented Interfaces:
Cloneable

public class DayAndPosition
extends Object
implements Cloneable

DayAndPosition is a utility class for representing byday parameters in iCalendar recurrences and CPL time-switches.

It simply holds two pieces of information: a day of the week, and a day position.

The day of the week is represented by the day-of-week parameters of the class Calendar: one of Calendar.SUNDAY, Calendar.MONDAY, Calendar.TUESDAY, Calendar.WEDNESDAY, Calendar.THURSDAY, Calendar.FRIDAY, or Calendar.SATURDAY. It may also have the value NO_WEEKDAY if it has not been initialized.

The day position is an integer with a value between -53 and 53, representing the nth occurence of the day in the enclosing period (a month or a year). Positive values count from the beginning of the period; negative values count from the end. A zero value means that no position is implied, and that every day of the week with the given day value is intended.

If the position is intended to represent a position within a month, only the positions -5 to 5 are meaningful. However, DayAndPosition has no knowledge of what period its position represents.

Change history:

1.0
Initial version

See Also:
Recurrence, Recurrence.setByDay(edu.columbia.cpl.DayAndPosition[]), Calendar

Field Summary
static int NO_WEEKDAY
          The value of dayOfWeek if it has not been initalized.
 
Constructor Summary
DayAndPosition()
          Construct a DayAndPosition with no weekday and zero recurrence.
DayAndPosition(int d, int p)
          Construct a DayAndPosition with the given weekday and recurrence.
DayAndPosition(String s)
          Construct a DayAndPosition from the given string
 
Method Summary
 Object clone()
          Overrides Cloneable
 boolean equals(Object obj)
          Compare this day-and-position to the specified object.
static String generateDayOfWeek(int day)
          Return a string corresponding to the given day-of-the-week.
 int getDayOfWeek()
          Get the day of the week stored in this object.
 int getDayPosition()
          Get the day position stored in this object.
 String getString()
          Get the standardized string representation of the DayAndPosition
static boolean isValidDayOfWeek(int d)
          Test if an integer is a valid day of the week.
static boolean isValidDayPosition(int p)
          Test if an integer is a valid day position.
static int parseDayOfWeek(String daystr)
          Parse a two-char RFC 2445 day-of-week string.
 void setDayOfWeek(int d)
          Set the day of the week stored in this object.
 void setDayPosition(int p)
          Set the day position stored in this object.
 void setString(String s)
          Set the DayAndPosition to the given string
 String toString()
          Return a string representation of this day-and-position.
 
Methods inherited from class java.lang.Object
finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

NO_WEEKDAY

public static final int NO_WEEKDAY
The value of dayOfWeek if it has not been initalized. Not equal to any of the day-of-week values from the class Calendar, which run from 1 (Sunday) to 7 (Saturday).

See Also:
setDayOfWeek(int), Calendar.SUNDAY, Calendar.MONDAY, Calendar.TUESDAY, Calendar.WEDNESDAY, Calendar.THURSDAY, Calendar.FRIDAY, Calendar.SATURDAY, Constant Field Values
Constructor Detail

DayAndPosition

public DayAndPosition()
Construct a DayAndPosition with no weekday and zero recurrence.


DayAndPosition

public DayAndPosition(int d,
                      int p)
Construct a DayAndPosition with the given weekday and recurrence.

Parameters:
d - The day
p - The period
Throws:
IllegalArgumentException - If either argument is out of range.

DayAndPosition

public DayAndPosition(String s)
Construct a DayAndPosition from the given string

Parameters:
s - String
Throws:
IllegalArgumentException - If the given string does not describe a valid DayAndPosition
Method Detail

getString

public String getString()
Get the standardized string representation of the DayAndPosition

Returns:
The string representation, of the form [[-]num]DY

setString

public void setString(String s)
Set the DayAndPosition to the given string

Parameters:
s - String
Throws:
IllegalArgumentException - If the given string does not describe a valid DayAndPosition

getDayOfWeek

public int getDayOfWeek()
Get the day of the week stored in this object. The value will be a day-of-week constant from Calendar or NO_WEEKDAY.

Returns:
The day of the week, or NO_WEEKDAY.
See Also:
Calendar.SUNDAY, Calendar.MONDAY, Calendar.TUESDAY, Calendar.WEDNESDAY, Calendar.THURSDAY, Calendar.FRIDAY, Calendar.SATURDAY, NO_WEEKDAY

setDayOfWeek

public void setDayOfWeek(int d)
Set the day of the week stored in this object. The value must be a day-of-week constant from Calendar or NO_WEEKDAY.

Parameters:
d - The day of the week, or NO_WEEKDAY.
Throws:
IllegalArgumentException - If an invalid weekday is given.
See Also:
Calendar.SUNDAY, Calendar.MONDAY, Calendar.TUESDAY, Calendar.WEDNESDAY, Calendar.THURSDAY, Calendar.FRIDAY, Calendar.SATURDAY, NO_WEEKDAY

getDayPosition

public int getDayPosition()
Get the day position stored in this object. The value is an integer between -53 and 53, representing the nth occurence of a day within a larger period (a month or year). A value of 0 means every occurence of the day.

Thus, if dayOfWeek were Calendar.THURSDAY, a value of 1 would mean means the first Thursday, 2 would mean the seocnd Thursday, -1 would mean the last Thursday, and so forth, while 0 would mean every Thursday.

Returns:
The day position.

setDayPosition

public void setDayPosition(int p)
Set the day position stored in this object. The value must be an integer between -53 and 53, representing the nth occurence of a day within a larger period (a month or year). A value of 0 means every occurence of the day.

Parameters:
p - The day position.
Throws:
IllegalArgumentException - If an invalid position is given.

equals

public boolean equals(Object obj)
Compare this day-and-position to the specified object. The result is true if and only if the argument is not null and is a DayAndPosition object that represents the same day and position as this object.

Parameters:
obj - the object to compare with.
Returns:
true if the objects are the same; false otherwise.

isValidDayOfWeek

public static boolean isValidDayOfWeek(int d)
Test if an integer is a valid day of the week. The result is true if and only if the argument is a valid weekday constant from Calendar, or NO_WEEKDAY.

Parameters:
d - The value to be tested.
Returns:
Whether the value is a valid weekday.
See Also:
Calendar.SUNDAY, Calendar.MONDAY, Calendar.TUESDAY, Calendar.WEDNESDAY, Calendar.THURSDAY, Calendar.FRIDAY, Calendar.SATURDAY, NO_WEEKDAY

isValidDayPosition

public static boolean isValidDayPosition(int p)
Test if an integer is a valid day position. The result is true if and only if the argument is in the range -53 to 53.

Parameters:
p - The value to be tested.
Returns:
Whether the value is a valid day position.

parseDayOfWeek

public static int parseDayOfWeek(String daystr)
Parse a two-char RFC 2445 day-of-week string.

Parameters:
daystr - The string to parse.
Returns:
An appropriate weekday constant from Calendar, or NO_WEEKDAY if the value does not correspond to any weekday.

generateDayOfWeek

public static String generateDayOfWeek(int day)
Return a string corresponding to the given day-of-the-week.

Parameters:
day - The day of the week
Returns:
A two-letter string, as in RFC 2445.

clone

public Object clone()
Overrides Cloneable

Returns:
A clone of this object.

toString

public String toString()
Return a string representation of this day-and-position. This method is intended to be used only for debugging purposes. The returned string may be empty but may not be null.

Returns:
A string representation of this day-and-position.