DayAndMonthRule.java 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. * DayAndMonthRule.java
  29. * --------------------
  30. * (C) Copyright 2000-2003, by Object Refinery Limited.
  31. *
  32. * Original Author: David Gilbert (for Object Refinery Limited);
  33. * Contributor(s): -;
  34. *
  35. * $Id: DayAndMonthRule.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.* (DG);
  40. * 12-Nov-2001 : Added some argument checks (DG);
  41. * 03-Oct-2002 : Fixed errors reported by Checkstyle (DG);
  42. * 01-Jun-2005 : Removed the explicit clonable declaration, it is declared
  43. * in the super class.
  44. */
  45. package org.jfree.date;
  46. /**
  47. * An annual date rule where the generated date always falls on the same day
  48. * and month each year.
  49. * <P>
  50. * An example is ANZAC Day in Australia and New Zealand: it is observed on
  51. * 25 April of every year.
  52. *
  53. * @author David Gilbert
  54. */
  55. public class DayAndMonthRule extends AnnualDateRule {
  56. /** The day of the month. */
  57. private int dayOfMonth;
  58. /** The month (uses 1 to 12 in the obvious way). */
  59. private int month;
  60. /**
  61. * Default constructor: builds a DayAndMonthRule for 1 January.
  62. */
  63. public DayAndMonthRule() {
  64. this(1, MonthConstants.JANUARY);
  65. }
  66. /**
  67. * Standard constructor: builds a DayAndMonthRule for the given
  68. * day-of-the-month and month.
  69. * <P>
  70. * For the month parameter, use SerialDate.JANUARY, etc. Note that there
  71. * are no checks to prevent you from entering an invalid combination (such
  72. * as 31 February).
  73. *
  74. * @param dayOfMonth the day of the month (in the range 1 to 31).
  75. * @param month the month (use SerialDate.JANUARY, SerialDate.FEBRUARY etc.);
  76. */
  77. public DayAndMonthRule(final int dayOfMonth, final int month) {
  78. // check arguments delegated to setter methods...
  79. setMonth(month);
  80. setDayOfMonth(dayOfMonth);
  81. }
  82. /**
  83. * Returns the day of the month.
  84. *
  85. * @return the day of the month.
  86. */
  87. public int getDayOfMonth() {
  88. return this.dayOfMonth;
  89. }
  90. /**
  91. * Sets the day-of-the-month for this rule.
  92. *
  93. * @param dayOfMonth the day-of-the-month.
  94. */
  95. public void setDayOfMonth(final int dayOfMonth) {
  96. // check arguments...
  97. if ((dayOfMonth < 1) || (dayOfMonth > SerialDate.LAST_DAY_OF_MONTH[this.month])) {
  98. throw new IllegalArgumentException(
  99. "DayAndMonthRule(): dayOfMonth outside valid range.");
  100. }
  101. // make the change...
  102. this.dayOfMonth = dayOfMonth;
  103. }
  104. /**
  105. * Returns an integer code representing the month.
  106. * <P>
  107. * The codes JANUARY, FEBRUARY, MARCH, APRIL, MAY, JUNE, JULY, AUGUST,
  108. * SEPTEMBER, OCTOBER, NOVEMBER and DECEMBER are defined in the SerialDate
  109. * class.
  110. *
  111. * @return an integer code representing the month.
  112. */
  113. public int getMonth() {
  114. return this.month;
  115. }
  116. /**
  117. * Sets the month for this rule.
  118. *
  119. * @param month the month for this rule.
  120. */
  121. public void setMonth(final int month) {
  122. // check arguments...
  123. if (!SerialDate.isValidMonthCode(month)) {
  124. throw new IllegalArgumentException("DayAndMonthRule(): month code not valid.");
  125. }
  126. // make the change...
  127. this.month = month;
  128. }
  129. /**
  130. * Returns the date, given the year.
  131. *
  132. * @param yyyy the year.
  133. *
  134. * @return the date generated by this rule for the specified year (null permitted).
  135. */
  136. public SerialDate getDate(final int yyyy) {
  137. return SerialDate.createInstance(this.dayOfMonth, this.month, yyyy);
  138. }
  139. }