RelativeDayOfWeekRule.java 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /* ========================================================================
  2. * JCommon : a free general purpose class library for the Java(tm) platform
  3. * ========================================================================
  4. *
  5. * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
  6. *
  7. * Project Info: http://www.jfree.org/jcommon/index.html
  8. *
  9. * This library is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU Lesser General Public License as published by
  11. * the Free Software Foundation; either version 2.1 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This library is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  16. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  17. * License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
  22. * USA.
  23. *
  24. * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
  25. * in the United States and other countries.]
  26. *
  27. * --------------------------
  28. * RelativeDayOfWeekRule.java
  29. * --------------------------
  30. * (C) Copyright 2000-2003, by Object Refinery Limited and Contributors.
  31. *
  32. * Original Author: David Gilbert (for Object Refinery Limited);
  33. * Contributor(s): -;
  34. *
  35. * $Id: RelativeDayOfWeekRule.java,v 1.6 2005/11/16 15:58:40 taqua Exp $
  36. *
  37. * Changes (from 26-Oct-2001)
  38. * --------------------------
  39. * 26-Oct-2001 : Changed package to com.jrefinery.date.*;
  40. * 03-Oct-2002 : Fixed errors reported by Checkstyle (DG);
  41. *
  42. */
  43. package org.jfree.date;
  44. /**
  45. * An annual date rule that returns a date for each year based on (a) a
  46. * reference rule; (b) a day of the week; and (c) a selection parameter
  47. * (SerialDate.PRECEDING, SerialDate.NEAREST, SerialDate.FOLLOWING).
  48. * <P>
  49. * For example, Good Friday can be specified as 'the Friday PRECEDING Easter
  50. * Sunday'.
  51. *
  52. * @author David Gilbert
  53. */
  54. public class RelativeDayOfWeekRule extends AnnualDateRule {
  55. /** A reference to the annual date rule on which this rule is based. */
  56. private AnnualDateRule subrule;
  57. /**
  58. * The day of the week (SerialDate.MONDAY, SerialDate.TUESDAY, and so on).
  59. */
  60. private int dayOfWeek;
  61. /** Specifies which day of the week (PRECEDING, NEAREST or FOLLOWING). */
  62. private int relative;
  63. /**
  64. * Default constructor - builds a rule for the Monday following 1 January.
  65. */
  66. public RelativeDayOfWeekRule() {
  67. this(new DayAndMonthRule(), SerialDate.MONDAY, SerialDate.FOLLOWING);
  68. }
  69. /**
  70. * Standard constructor - builds rule based on the supplied sub-rule.
  71. *
  72. * @param subrule the rule that determines the reference date.
  73. * @param dayOfWeek the day-of-the-week relative to the reference date.
  74. * @param relative indicates *which* day-of-the-week (preceding, nearest
  75. * or following).
  76. */
  77. public RelativeDayOfWeekRule(final AnnualDateRule subrule,
  78. final int dayOfWeek, final int relative) {
  79. this.subrule = subrule;
  80. this.dayOfWeek = dayOfWeek;
  81. this.relative = relative;
  82. }
  83. /**
  84. * Returns the sub-rule (also called the reference rule).
  85. *
  86. * @return The annual date rule that determines the reference date for this
  87. * rule.
  88. */
  89. public AnnualDateRule getSubrule() {
  90. return this.subrule;
  91. }
  92. /**
  93. * Sets the sub-rule.
  94. *
  95. * @param subrule the annual date rule that determines the reference date
  96. * for this rule.
  97. */
  98. public void setSubrule(final AnnualDateRule subrule) {
  99. this.subrule = subrule;
  100. }
  101. /**
  102. * Returns the day-of-the-week for this rule.
  103. *
  104. * @return the day-of-the-week for this rule.
  105. */
  106. public int getDayOfWeek() {
  107. return this.dayOfWeek;
  108. }
  109. /**
  110. * Sets the day-of-the-week for this rule.
  111. *
  112. * @param dayOfWeek the day-of-the-week (SerialDate.MONDAY,
  113. * SerialDate.TUESDAY, and so on).
  114. */
  115. public void setDayOfWeek(final int dayOfWeek) {
  116. this.dayOfWeek = dayOfWeek;
  117. }
  118. /**
  119. * Returns the 'relative' attribute, that determines *which*
  120. * day-of-the-week we are interested in (SerialDate.PRECEDING,
  121. * SerialDate.NEAREST or SerialDate.FOLLOWING).
  122. *
  123. * @return The 'relative' attribute.
  124. */
  125. public int getRelative() {
  126. return this.relative;
  127. }
  128. /**
  129. * Sets the 'relative' attribute (SerialDate.PRECEDING, SerialDate.NEAREST,
  130. * SerialDate.FOLLOWING).
  131. *
  132. * @param relative determines *which* day-of-the-week is selected by this
  133. * rule.
  134. */
  135. public void setRelative(final int relative) {
  136. this.relative = relative;
  137. }
  138. /**
  139. * Creates a clone of this rule.
  140. *
  141. * @return a clone of this rule.
  142. *
  143. * @throws CloneNotSupportedException this should never happen.
  144. */
  145. public Object clone() throws CloneNotSupportedException {
  146. final RelativeDayOfWeekRule duplicate
  147. = (RelativeDayOfWeekRule) super.clone();
  148. duplicate.subrule = (AnnualDateRule) duplicate.getSubrule().clone();
  149. return duplicate;
  150. }
  151. /**
  152. * Returns the date generated by this rule, for the specified year.
  153. *
  154. * @param year the year (1900 &lt;= year &lt;= 9999).
  155. *
  156. * @return The date generated by the rule for the given year (possibly
  157. * <code>null</code>).
  158. */
  159. public SerialDate getDate(final int year) {
  160. // check argument...
  161. if ((year < SerialDate.MINIMUM_YEAR_SUPPORTED)
  162. || (year > SerialDate.MAXIMUM_YEAR_SUPPORTED)) {
  163. throw new IllegalArgumentException(
  164. "RelativeDayOfWeekRule.getDate(): year outside valid range.");
  165. }
  166. // calculate the date...
  167. SerialDate result = null;
  168. final SerialDate base = this.subrule.getDate(year);
  169. if (base != null) {
  170. switch (this.relative) {
  171. case(SerialDate.PRECEDING):
  172. result = SerialDate.getPreviousDayOfWeek(this.dayOfWeek,
  173. base);
  174. break;
  175. case(SerialDate.NEAREST):
  176. result = SerialDate.getNearestDayOfWeek(this.dayOfWeek,
  177. base);
  178. break;
  179. case(SerialDate.FOLLOWING):
  180. result = SerialDate.getFollowingDayOfWeek(this.dayOfWeek,
  181. base);
  182. break;
  183. default:
  184. break;
  185. }
  186. }
  187. return result;
  188. }
  189. }