KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > date > DayAndMonthRule


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
46 package org.jfree.date;
47
48 /**
49  * An annual date rule where the generated date always falls on the same day
50  * and month each year.
51  * <P>
52  * An example is ANZAC Day in Australia and New Zealand: it is observed on
53  * 25 April of every year.
54  *
55  * @author David Gilbert
56  */

57 public class DayAndMonthRule extends AnnualDateRule {
58
59     /** The day of the month. */
60     private int dayOfMonth;
61
62     /** The month (uses 1 to 12 in the obvious way). */
63     private int month;
64
65     /**
66      * Default constructor: builds a DayAndMonthRule for 1 January.
67      */

68     public DayAndMonthRule() {
69         this(1, MonthConstants.JANUARY);
70     }
71
72     /**
73      * Standard constructor: builds a DayAndMonthRule for the given
74      * day-of-the-month and month.
75      * <P>
76      * For the month parameter, use SerialDate.JANUARY, etc. Note that there
77      * are no checks to prevent you from entering an invalid combination (such
78      * as 31 February).
79      *
80      * @param dayOfMonth the day of the month (in the range 1 to 31).
81      * @param month the month (use SerialDate.JANUARY, SerialDate.FEBRUARY etc.);
82      */

83     public DayAndMonthRule(final int dayOfMonth, final int month) {
84
85         // check arguments delegated to setter methods...
86
setMonth(month);
87         setDayOfMonth(dayOfMonth);
88
89     }
90
91     /**
92      * Returns the day of the month.
93      *
94      * @return the day of the month.
95      */

96     public int getDayOfMonth() {
97         return this.dayOfMonth;
98     }
99
100     /**
101      * Sets the day-of-the-month for this rule.
102      *
103      * @param dayOfMonth the day-of-the-month.
104      */

105     public void setDayOfMonth(final int dayOfMonth) {
106
107         // check arguments...
108
if ((dayOfMonth < 1) || (dayOfMonth > SerialDate.LAST_DAY_OF_MONTH[this.month])) {
109             throw new IllegalArgumentException JavaDoc(
110                 "DayAndMonthRule(): dayOfMonth outside valid range.");
111         }
112
113         // make the change...
114
this.dayOfMonth = dayOfMonth;
115
116     }
117
118     /**
119      * Returns an integer code representing the month.
120      * <P>
121      * The codes JANUARY, FEBRUARY, MARCH, APRIL, MAY, JUNE, JULY, AUGUST,
122      * SEPTEMBER, OCTOBER, NOVEMBER and DECEMBER are defined in the SerialDate
123      * class.
124      *
125      * @return an integer code representing the month.
126      */

127     public int getMonth() {
128         return this.month;
129     }
130
131     /**
132      * Sets the month for this rule.
133      *
134      * @param month the month for this rule.
135      */

136     public void setMonth(final int month) {
137
138         // check arguments...
139
if (!SerialDate.isValidMonthCode(month)) {
140             throw new IllegalArgumentException JavaDoc("DayAndMonthRule(): month code not valid.");
141         }
142
143         // make the change...
144
this.month = month;
145
146     }
147
148     /**
149      * Returns the date, given the year.
150      *
151      * @param yyyy the year.
152      *
153      * @return the date generated by this rule for the specified year (null permitted).
154      */

155     public SerialDate getDate(final int yyyy) {
156         return SerialDate.createInstance(this.dayOfMonth, this.month, yyyy);
157     }
158 }
159
Popular Tags